2014-03-02 14:44:24 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/file.h>
|
2015-08-24 08:31:00 -04:00
|
|
|
#include "test.h"
|
2014-03-02 14:44:24 -05:00
|
|
|
|
2014-04-26 13:31:22 -04:00
|
|
|
|
|
|
|
static void test_filepath()
|
|
|
|
{
|
2015-08-24 08:31:00 -04:00
|
|
|
gchar *filepath = test_data_file("file.txt");
|
|
|
|
|
2015-09-10 08:00:42 -04:00
|
|
|
file a("", 0);
|
|
|
|
file b("file.txt", 0);
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2015-09-10 08:10:38 -04:00
|
|
|
test_equal(file_version(&a), (unsigned)0);
|
2015-09-10 08:17:53 -04:00
|
|
|
test_equal((std::string)file_path(&a), (std::string)"");
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2015-09-10 08:10:38 -04:00
|
|
|
test_equal(file_version(&b), (unsigned)0);
|
2015-09-10 08:17:53 -04:00
|
|
|
test_equal((std::string)file_path(&b), filepath);
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2015-09-10 09:12:48 -04:00
|
|
|
test_equal(a.f_mode, NOT_OPEN);
|
|
|
|
test_equal(b.f_mode, NOT_OPEN);
|
|
|
|
|
2015-09-10 08:23:09 -04:00
|
|
|
test_equal(file_exists(&a), false);
|
|
|
|
test_equal(file_exists(&b), false);
|
2015-08-24 08:31:00 -04:00
|
|
|
test_equal(test_data_file_exists(NULL), false);
|
|
|
|
|
|
|
|
g_free(filepath);
|
2014-04-26 13:31:22 -04:00
|
|
|
}
|
|
|
|
|
2014-04-26 14:06:20 -04:00
|
|
|
static void test_open()
|
2014-04-26 13:31:22 -04:00
|
|
|
{
|
2015-09-10 08:00:42 -04:00
|
|
|
file a("", 0);
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2015-09-10 09:46:33 -04:00
|
|
|
test_equal(file_open(&a, OPEN_READ), false);
|
|
|
|
test_equal(file_open(&a, OPEN_WRITE), false);
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&a);
|
|
|
|
test_equal(a.f_mode, NOT_OPEN);
|
2014-03-02 14:44:24 -05:00
|
|
|
|
2015-09-10 08:00:42 -04:00
|
|
|
file b("file.txt", 0);
|
2015-09-10 09:46:33 -04:00
|
|
|
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);
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&b);
|
|
|
|
test_equal(b.f_mode, NOT_OPEN);
|
2014-03-02 14:44:24 -05:00
|
|
|
|
2015-08-24 08:31:00 -04:00
|
|
|
test_equal(test_data_file_exists("file.txt"), true);
|
2014-03-02 14:44:24 -05:00
|
|
|
}
|
|
|
|
|
2014-04-26 14:06:20 -04:00
|
|
|
static void test_io()
|
2014-03-02 14:44:24 -05:00
|
|
|
{
|
2015-09-10 08:00:42 -04:00
|
|
|
file a("file.txt", 1);
|
2014-03-02 14:44:24 -05:00
|
|
|
|
2015-09-10 09:46:33 -04:00
|
|
|
test_equal(file_open(&a, OPEN_WRITE), true);
|
2014-04-26 14:06:20 -04:00
|
|
|
a << "ABCDE FGHIJ KLMNO PQRST UVWXYZ" << std::endl;
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&a);
|
2015-09-10 08:23:09 -04:00
|
|
|
test_equal(file_exists(&a), true);
|
2014-04-26 14:06:20 -04:00
|
|
|
|
2015-09-10 08:00:42 -04:00
|
|
|
file b("file.txt", 0);
|
2014-04-26 14:06:20 -04:00
|
|
|
std::string res;
|
|
|
|
|
2015-09-10 09:46:33 -04:00
|
|
|
test_equal(file_open(&b, OPEN_READ), true);
|
2015-09-10 08:10:38 -04:00
|
|
|
test_equal(file_version(&b), (unsigned)1);
|
2014-04-26 14:06:20 -04:00
|
|
|
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");
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&b);
|
2014-03-02 14:44:24 -05:00
|
|
|
}
|
|
|
|
|
2015-08-24 08:31:00 -04:00
|
|
|
DECLARE_UNIT_TESTS(
|
2015-09-10 08:00:42 -04:00
|
|
|
UNIT_TEST("file Constructor", test_filepath),
|
|
|
|
UNIT_TEST("file Opening", test_open),
|
|
|
|
UNIT_TEST("file I/O", test_io),
|
2015-08-24 08:31:00 -04:00
|
|
|
);
|