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>
|
2016-03-30 10:11:02 -04:00
|
|
|
#include <core/version.h>
|
2015-10-09 13:55:34 -04:00
|
|
|
#include <tests/test.h>
|
2015-10-08 11:16:38 -04:00
|
|
|
#include <stdlib.h>
|
2014-03-02 14:44:24 -05:00
|
|
|
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2016-03-30 10:11:02 -04:00
|
|
|
static void test_verify_constructor(struct file *file, gchar *fpath, gchar *ftmp)
|
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);
|
2015-10-09 08:10:18 -04:00
|
|
|
test_equal(file->f_mode, OPEN_READ);
|
2016-03-30 10:11:02 -04:00
|
|
|
test_str_equal(file_path(file), fpath);
|
|
|
|
test_str_equal(file_write_path(file), ftmp);
|
2015-09-10 10:33:24 -04:00
|
|
|
}
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2015-10-09 09:55:23 -04:00
|
|
|
static void test_invalid_file(struct file *file)
|
2015-09-10 10:33:24 -04:00
|
|
|
{
|
2016-03-30 10:11:02 -04:00
|
|
|
test_verify_constructor(file, "", "");
|
2015-09-10 09:12:48 -04:00
|
|
|
|
2015-10-09 09:55:23 -04:00
|
|
|
test_equal(file_open(file, OPEN_READ), (bool)false);
|
|
|
|
test_equal((void *)file->f_file, NULL);
|
|
|
|
test_equal(file_open(file, OPEN_WRITE), (bool)false);
|
|
|
|
test_equal((void *)file->f_file, NULL);
|
|
|
|
test_equal(file->f_mode, OPEN_READ);
|
2015-08-24 08:31:00 -04:00
|
|
|
|
2015-10-09 09:55:23 -04:00
|
|
|
test_equal(file_exists(file), (bool)false);
|
2015-09-10 10:33:24 -04:00
|
|
|
test_equal(test_data_file_exists(NULL), (bool)false);
|
2014-04-26 13:31:22 -04:00
|
|
|
}
|
|
|
|
|
2015-10-09 09:55:23 -04:00
|
|
|
static void test_null()
|
|
|
|
{
|
|
|
|
struct file fnull;
|
2016-03-30 11:25:14 -04:00
|
|
|
file_init(&fnull, NULL, 0, 0);
|
2015-10-09 09:55:23 -04:00
|
|
|
test_invalid_file(&fnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_empty()
|
|
|
|
{
|
|
|
|
struct file fempty;
|
2016-03-30 11:25:14 -04:00
|
|
|
file_init(&fempty, "", 0, 0);
|
2015-10-09 09:55:23 -04:00
|
|
|
test_invalid_file(&fempty);
|
|
|
|
}
|
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
static void test_file()
|
2014-04-26 13:31:22 -04:00
|
|
|
{
|
2016-03-30 11:25:14 -04:00
|
|
|
struct file file = FILE_INIT("file.txt", 0, 0);
|
2016-03-30 10:11:02 -04:00
|
|
|
gchar *basepath, *filepath, *realpath;
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2016-03-30 10:11:02 -04:00
|
|
|
basepath = g_strjoin("/", g_get_user_data_dir(), OCARINA_NAME, NULL);
|
|
|
|
filepath = g_strjoin("/", basepath, "file.txt", NULL);
|
|
|
|
realpath = g_strjoin("/", basepath, ".file.txt.tmp", NULL);
|
|
|
|
|
|
|
|
test_verify_constructor(&file, filepath, realpath);
|
2015-09-10 10:33:24 -04:00
|
|
|
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, 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);
|
|
|
|
|
2016-03-30 10:11:02 -04:00
|
|
|
test_equal(file_exists(&file), (bool)false);
|
2015-09-10 10:33:24 -04:00
|
|
|
file_close(&file);
|
2015-10-08 09:41:51 -04:00
|
|
|
test_equal((void *)file.f_file, NULL);
|
2015-10-09 08:10:18 -04:00
|
|
|
test_equal(file.f_mode, OPEN_WRITE);
|
2015-09-10 10:33:24 -04:00
|
|
|
|
|
|
|
test_equal(file_exists(&file), (bool)true);
|
|
|
|
test_equal(test_data_file_exists("file.txt"), (bool)true);
|
|
|
|
|
2015-10-09 10:07:08 -04:00
|
|
|
g_chmod(filepath, 0444);
|
2015-09-10 10:33:24 -04:00
|
|
|
test_equal(file_open(&file, OPEN_WRITE), (bool)false);
|
2015-10-09 10:07:08 -04:00
|
|
|
g_chmod(filepath, 0200);
|
2015-09-10 10:33:24 -04:00
|
|
|
test_equal(file_open(&file, OPEN_READ), (bool)false);
|
2015-10-09 10:07:08 -04:00
|
|
|
g_chmod(filepath, 0644);
|
2015-09-10 10:33:24 -04:00
|
|
|
|
|
|
|
test_equal(file_open(&file, OPEN_READ), (bool)true);
|
2015-10-08 11:16:38 -04:00
|
|
|
test_not_equal((void *)file.f_file, NULL);
|
2015-09-10 10:33:24 -04:00
|
|
|
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
|
|
|
{
|
2016-03-30 11:25:14 -04:00
|
|
|
struct file fout = FILE_INIT("file.txt", 1, 1);
|
|
|
|
struct file fin = FILE_INIT("file.txt", 0, 0);
|
2015-10-08 11:16:38 -04:00
|
|
|
char *res = NULL;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
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);
|
2015-10-09 13:09:47 -04:00
|
|
|
res = file_readl(&fin);
|
2015-10-08 11:16:38 -04:00
|
|
|
test_equal(i, 3);
|
2015-10-09 13:09:47 -04:00
|
|
|
test_equal(res, "");
|
|
|
|
free(res);
|
2015-10-08 11:16:38 -04:00
|
|
|
|
|
|
|
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);
|
2014-03-02 14:44:24 -05:00
|
|
|
}
|
|
|
|
|
2016-03-30 11:25:14 -04:00
|
|
|
static void test_versioning()
|
|
|
|
{
|
|
|
|
struct file fout = FILE_INIT("file.txt", 0, 0);
|
|
|
|
struct file fin = FILE_INIT("file.txt", 2, 1);
|
|
|
|
|
|
|
|
test_equal(file_open(&fout, OPEN_WRITE), (bool)true);
|
|
|
|
file_writef(&fout, "abcdefghijklmnopqrstuvwxyz");
|
|
|
|
file_close(&fout);
|
|
|
|
test_equal(file_exists(&fout), (bool)true);
|
|
|
|
|
|
|
|
test_equal(file_open(&fin, OPEN_READ), (bool)false);
|
|
|
|
test_equal((void *)fin.f_file, NULL);
|
|
|
|
}
|
|
|
|
|
2016-03-31 08:08:33 -04:00
|
|
|
static void test_cache()
|
|
|
|
{
|
|
|
|
struct cache_file file = CACHE_FILE_INIT("dir", "file.txt");
|
2016-03-31 09:25:49 -04:00
|
|
|
gchar *basepath, *filepath, *writepath;
|
2016-03-31 08:52:59 -04:00
|
|
|
|
2016-03-31 09:25:49 -04:00
|
|
|
basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, NULL);
|
|
|
|
filepath = g_strjoin("/", basepath, "dir", "file.txt", NULL);
|
|
|
|
writepath = g_strjoin("/", basepath, "dir", ".file.txt.tmp", NULL);
|
2016-03-31 08:08:33 -04:00
|
|
|
|
|
|
|
test_equal((void *)file.cf_file, NULL);
|
|
|
|
test_equal(file.cf_name, "file.txt");
|
|
|
|
test_equal(file.cf_subdir, "dir");
|
2016-03-31 08:52:59 -04:00
|
|
|
|
|
|
|
test_str_equal(cache_file_path(&file), filepath);
|
2016-03-31 09:25:49 -04:00
|
|
|
test_str_equal(cache_file_write_path(&file), writepath);
|
2016-03-31 08:08:33 -04:00
|
|
|
}
|
|
|
|
|
2015-08-24 08:31:00 -04:00
|
|
|
DECLARE_UNIT_TESTS(
|
2015-10-09 09:55:23 -04:00
|
|
|
UNIT_TEST("File (Path = NULL)", test_null),
|
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),
|
2016-03-30 11:25:14 -04:00
|
|
|
UNIT_TEST("File Versioning", test_versioning),
|
2016-03-31 08:08:33 -04:00
|
|
|
UNIT_TEST("File Cache", test_cache),
|
2015-08-24 08:31:00 -04:00
|
|
|
);
|