ocarina/tests/core/file.cpp

80 lines
1.8 KiB
C++
Raw Normal View History

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/file.h>
#include "test.h"
static void test_filepath()
{
gchar *filepath = test_data_file("file.txt");
file a("", 0);
file b("file.txt", 0);
test_equal(file_version(&a), (unsigned)0);
test_equal((std::string)file_path(&a), (std::string)"");
test_equal(file_version(&b), (unsigned)0);
test_equal((std::string)file_path(&b), filepath);
test_equal(a.f_mode, NOT_OPEN);
test_equal(b.f_mode, NOT_OPEN);
test_equal(file_exists(&a), false);
test_equal(file_exists(&b), false);
test_equal(test_data_file_exists(NULL), false);
g_free(filepath);
}
static void test_open()
{
file a("", 0);
test_equal(file_open(&a, OPEN_READ), false);
test_equal(file_open(&a, OPEN_WRITE), false);
file_close(&a);
test_equal(a.f_mode, NOT_OPEN);
file b("file.txt", 0);
test_equal(file_open(&b, NOT_OPEN), false);
test_equal(file_open(&b, OPEN_READ), false);
test_equal(file_open(&b, OPEN_WRITE), true);
test_equal(b.f_mode, OPEN_WRITE);
test_equal(file_open(&b, OPEN_WRITE), false);
file_close(&b);
test_equal(b.f_mode, NOT_OPEN);
test_equal(test_data_file_exists("file.txt"), true);
}
static void test_io()
{
file a("file.txt", 1);
test_equal(file_open(&a, OPEN_WRITE), true);
a << "ABCDE FGHIJ KLMNO PQRST UVWXYZ" << std::endl;
file_close(&a);
test_equal(file_exists(&a), true);
file b("file.txt", 0);
std::string res;
test_equal(file_open(&b, OPEN_READ), true);
test_equal(file_version(&b), (unsigned)1);
b >> res;
test_equal(res, (std::string)"ABCDE");
b >> res;
test_equal(res, (std::string)"FGHIJ");
res = b.getline();
test_equal(res, (std::string)"KLMNO PQRST UVWXYZ");
file_close(&b);
}
DECLARE_UNIT_TESTS(
UNIT_TEST("file Constructor", test_filepath),
UNIT_TEST("file Opening", test_open),
UNIT_TEST("file I/O", test_io),
);