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
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
static void test_verify_constructor(struct file *file, const std::string &fpath)
|
2014-04-26 13:31:22 -04:00
|
|
|
{
|
2015-10-08 09:41:51 -04:00
|
|
|
test_equal((void *)file->f_file, NULL);
|
2015-09-10 10:33:24 -04:00
|
|
|
test_equal(file_version(file), 0);
|
|
|
|
test_equal(file->f_mode, NOT_OPEN);
|
|
|
|
test_equal(file_path(file), fpath);
|
|
|
|
}
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
static void test_empty()
|
|
|
|
{
|
|
|
|
struct file fempty;
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
file_init(&fempty, "", 0);
|
|
|
|
test_verify_constructor(&fempty, "");
|
2015-09-10 09:12:48 -04:00
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
test_equal(file_open(&fempty, NOT_OPEN), (bool)false);
|
|
|
|
test_equal(file_open(&fempty, OPEN_READ), (bool)false);
|
2015-10-08 09:41:51 -04:00
|
|
|
test_equal((void *)fempty.f_file, NULL);
|
2015-09-10 10:33:24 -04:00
|
|
|
test_equal(fempty.f_mode, NOT_OPEN);
|
|
|
|
test_equal(file_open(&fempty, OPEN_WRITE), (bool)false);
|
2015-10-08 09:41:51 -04:00
|
|
|
test_equal((void *)fempty.f_file, NULL);
|
2015-09-10 10:33:24 -04:00
|
|
|
test_equal(fempty.f_mode, NOT_OPEN);
|
2015-08-24 08:31:00 -04:00
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
test_equal(file_exists(&fempty), (bool)false);
|
|
|
|
test_equal(test_data_file_exists(NULL), (bool)false);
|
2014-04-26 13:31:22 -04:00
|
|
|
}
|
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
static void test_file()
|
2014-04-26 13:31:22 -04:00
|
|
|
{
|
2015-09-10 10:33:24 -04:00
|
|
|
struct file file;
|
|
|
|
gchar *filepath = test_data_file("file.txt");
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
file_init(&file, "file.txt", 0);
|
|
|
|
test_verify_constructor(&file, filepath);
|
|
|
|
test_equal(file_exists(&file), (bool)false);
|
|
|
|
test_equal(test_data_file_exists(NULL), (bool)false);
|
2014-03-02 14:44:24 -05:00
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
test_equal(file_open(&file, NOT_OPEN), (bool)false);
|
|
|
|
test_equal(file_open(&file, OPEN_READ), (bool)false);
|
|
|
|
test_equal(file_open(&file, OPEN_WRITE), (bool)true);
|
2015-10-08 09:41:51 -04:00
|
|
|
test_not_equal((void *)file.f_file, NULL);
|
2015-09-10 10:33:24 -04:00
|
|
|
test_equal(file.f_mode, OPEN_WRITE);
|
|
|
|
test_equal(file_open(&file, OPEN_WRITE), (bool)false);
|
|
|
|
|
|
|
|
file_close(&file);
|
2015-10-08 09:41:51 -04:00
|
|
|
test_equal((void *)file.f_file, NULL);
|
2015-09-10 10:33:24 -04:00
|
|
|
test_equal(file.f_mode, NOT_OPEN);
|
|
|
|
|
|
|
|
test_equal(file_exists(&file), (bool)true);
|
|
|
|
test_equal(test_data_file_exists("file.txt"), (bool)true);
|
|
|
|
|
|
|
|
g_chmod(file_path(&file).c_str(), 0444);
|
|
|
|
test_equal(file_open(&file, OPEN_WRITE), (bool)false);
|
|
|
|
g_chmod(file_path(&file).c_str(), 0200);
|
|
|
|
test_equal(file_open(&file, OPEN_READ), (bool)false);
|
|
|
|
g_chmod(file_path(&file).c_str(), 0644);
|
|
|
|
|
|
|
|
test_equal(file_open(&file, OPEN_READ), (bool)true);
|
|
|
|
test_equal(file.f_mode, OPEN_READ);
|
|
|
|
test_equal(file_open(&file, OPEN_READ), (bool)false);
|
|
|
|
file_close(&file);
|
|
|
|
|
|
|
|
g_free(filepath);
|
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 10:33:24 -04:00
|
|
|
struct file a, b;
|
|
|
|
std::string res;
|
2014-03-02 14:44:24 -05:00
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
file_init(&a, "file.txt", 1);
|
2015-10-08 09:41:51 -04:00
|
|
|
test_equal(file_open(&a, OPEN_WRITE), (bool)true);
|
|
|
|
file_writef(&a, "ABCDE FGHIJ KLMNO PQRST UVWXYZ\n");
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&a);
|
2015-10-08 09:41:51 -04:00
|
|
|
test_equal(file_exists(&a), (bool)true);
|
2014-04-26 14:06:20 -04:00
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
file_init(&b, "file.txt", 0);
|
2015-10-08 09:41:51 -04:00
|
|
|
test_equal(file_open(&b, OPEN_READ), (bool)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");
|
2015-10-08 15:40:42 -04:00
|
|
|
res = file_readl(&b);
|
2014-04-26 14:06:20 -04:00
|
|
|
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 10:33:24 -04:00
|
|
|
UNIT_TEST("File (Path = \"\")", test_empty),
|
|
|
|
UNIT_TEST("File (Path = \"file.txt\")", test_file),
|
|
|
|
UNIT_TEST("File I/O", test_io),
|
2015-08-24 08:31:00 -04:00
|
|
|
);
|