ocarina/tests/core/file.cpp

119 lines
3.1 KiB
C++
Raw Normal View History

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/file.h>
#include <stdlib.h>
#include "test.h"
static void test_verify_constructor(struct file *file, const std::string &fpath)
{
test_equal((void *)file->f_file, NULL);
test_equal(file_version(file), 0);
test_equal(file->f_mode, OPEN_READ);
test_equal(file_path(file), fpath);
}
static void test_empty()
{
struct file fempty;
file_init(&fempty, "", 0);
test_verify_constructor(&fempty, "");
test_equal(file_open(&fempty, OPEN_READ), (bool)false);
test_equal((void *)fempty.f_file, NULL);
test_equal(file_open(&fempty, OPEN_WRITE), (bool)false);
test_equal((void *)fempty.f_file, NULL);
test_equal(file_exists(&fempty), (bool)false);
test_equal(test_data_file_exists(NULL), (bool)false);
}
static void test_file()
{
struct file file;
gchar *filepath = test_data_file("file.txt");
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);
test_equal(file_open(&file, OPEN_READ), (bool)false);
test_equal(file_open(&file, OPEN_WRITE), (bool)true);
test_not_equal((void *)file.f_file, NULL);
test_equal(file.f_mode, OPEN_WRITE);
test_equal(file_open(&file, OPEN_WRITE), (bool)false);
file_close(&file);
test_equal((void *)file.f_file, NULL);
test_equal(file.f_mode, OPEN_WRITE);
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_not_equal((void *)file.f_file, NULL);
test_equal(file.f_mode, OPEN_READ);
test_equal(file_open(&file, OPEN_READ), (bool)false);
file_close(&file);
g_free(filepath);
}
static void test_io()
{
struct file fout, fin;
char *res = NULL;
unsigned int i;
file_init(&fout, "file.txt", 1);
file_init(&fin, "file.txt", 0);
test_equal(file_open(&fout, OPEN_WRITE), (bool)true);
file_writef(&fout, "1 ABCDE\n");
file_writef(&fout, "2 FGHIJ KLMNO\n");
file_writef(&fout, "3 \n");
file_writef(&fout, "4 5 PQRST\n");
file_close(&fout);
test_equal(file_exists(&fout), (bool)true);
test_equal(file_open(&fin, OPEN_READ), (bool)true);
test_equal(file_version(&fin), 1);
test_equal(file_readf(&fin, "%u %ms\n", &i, &res), 2);
test_equal(i, 1);
test_equal(res, "ABCDE");
free(res);
test_equal(file_readf(&fin, "%u %m[^\n]\n", &i, &res), 2);
test_equal(i, 2);
test_equal(res, "FGHIJ KLMNO");
free(res);
test_equal(file_readf(&fin, "%u", &i), 1);
test_equal(i, 3);
test_equal(file_readl(&fin), "");
test_equal(file_readf(&fin, "%u %m[^\n]", &i, &res), 2);
test_equal(i, 4);
test_equal(res, "5 PQRST");
free(res);
file_close(&fin);
test_equal(file_version(&fin), 0);
}
DECLARE_UNIT_TESTS(
UNIT_TEST("File (Path = \"\")", test_empty),
UNIT_TEST("File (Path = \"file.txt\")", test_file),
UNIT_TEST("File I/O", test_io),
);