/* * Copyright 2014 (c) Anna Schumaker. */ #include #include "test.h" static void test_verify_constructor(struct file *file, const std::string &fpath) { test_equal(file_version(file), 0); test_equal(file->f_mode, NOT_OPEN); 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, NOT_OPEN), (bool)false); test_equal(file_open(&fempty, OPEN_READ), (bool)false); test_equal(fempty.f_mode, NOT_OPEN); test_equal(file_open(&fempty, OPEN_WRITE), (bool)false); test_equal(fempty.f_mode, NOT_OPEN); 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, NOT_OPEN), (bool)false); test_equal(file_open(&file, OPEN_READ), (bool)false); test_equal(file_open(&file, OPEN_WRITE), (bool)true); test_equal(file.f_mode, OPEN_WRITE); test_equal(file_open(&file, OPEN_WRITE), (bool)false); file_close(&file); 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); } static void test_io() { struct file a, b; std::string res; file_init(&a, "file.txt", 1); test_equal(file_open(&a, OPEN_WRITE), true); a << "ABCDE FGHIJ KLMNO PQRST UVWXYZ" << std::endl; file_close(&a); test_equal(file_exists(&a), true); file_init(&b, "file.txt", 0); test_equal(file_open(&b, OPEN_READ), true); test_equal(file_version(&b), (unsigned)1); b >> res; test_equal(res, (std::string)"ABCDE"); b >> res; test_equal(res, (std::string)"FGHIJ"); res = file_readl(&b); test_equal(res, (std::string)"KLMNO PQRST UVWXYZ"); file_close(&b); } DECLARE_UNIT_TESTS( UNIT_TEST("File (Path = \"\")", test_empty), UNIT_TEST("File (Path = \"file.txt\")", test_file), UNIT_TEST("File I/O", test_io), );