ocarina/tests/core/file.cpp

72 lines
1.5 KiB
C++

/*
* 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(a.get_version(), (unsigned)0);
test_equal((std::string)a.get_filepath(), (std::string)"");
test_equal(b.get_version(), (unsigned)0);
test_equal((std::string)b.get_filepath(), filepath);
test_equal(a.exists(), false);
test_equal(b.exists(), false);
test_equal(test_data_file_exists(NULL), false);
g_free(filepath);
}
static void test_open()
{
file a("", 0);
test_equal(a.open(OPEN_READ), false);
test_equal(a.open(OPEN_WRITE), false);
file b("file.txt", 0);
test_equal(b.open(NOT_OPEN), false);
test_equal(b.open(OPEN_READ), false);
test_equal(b.open(OPEN_WRITE), true);
test_equal(b.open(OPEN_WRITE), false);
b.close();
test_equal(test_data_file_exists("file.txt"), true);
}
static void test_io()
{
file a("file.txt", 1);
test_equal(a.open(OPEN_WRITE), true);
a << "ABCDE FGHIJ KLMNO PQRST UVWXYZ" << std::endl;
a.close();
test_equal(a.exists(), true);
file b("file.txt", 0);
std::string res;
test_equal(b.open(OPEN_READ), true);
test_equal(b.get_version(), (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");
}
DECLARE_UNIT_TESTS(
UNIT_TEST("file Constructor", test_filepath),
UNIT_TEST("file Opening", test_open),
UNIT_TEST("file I/O", test_io),
);