file: Rewrite file test

For some reason I turned the file test into three separate files. All
other tests were in a single source file, so I updated file tests to
match.

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2013-09-29 20:20:52 -04:00 committed by Anna Schumaker
parent 1482f5bcc8
commit 31ea73b0d9
9 changed files with 291 additions and 231 deletions

View File

@ -3,6 +3,4 @@ Import("Test", "CONFIG")
CONFIG.FILE = True
Test("file", "compile.cpp")
Test("file", "open.cpp")
Test("file", "io.cpp")
Test("file", "file.cpp")

View File

@ -1,21 +0,0 @@
/*
* Copyright 2013 (c) Anna Schumaker.
* Test that a file object can be created and resolves paths correctly.
*/
#include <file.h>
#include <print.h>
void test_filepath(std::string file, FileLocHint hint)
{
File f(file, hint);
print("File path is: %s\n", f.get_filepath());
}
int main(int argc, char **argv)
{
test_filepath("test.file", FILE_TYPE_CONFIG);
test_filepath("test.file", FILE_TYPE_DATA);
test_filepath("test.file", FILE_TYPE_LEGACY);
test_filepath("", FILE_TYPE_DATA);
return 0;
}

View File

@ -1,4 +0,0 @@
File path is: /home/anna/.config/ocarina-test/test.file
File path is: /home/anna/.local/share/ocarina-test/test.file
File path is: /home/anna/.ocarina-test/test.file
File path is: INVALID

202
tests/file/file.cpp Normal file
View File

@ -0,0 +1,202 @@
/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <file.h>
#include <print.h>
#include <sstream>
bool test_result(bool passed, bool print_spaces)
{
if (print_spaces)
print(" ");
if (passed == true)
print("Passed\n");
else
print("Failed\n");
return passed;
}
File *get_file(int testno, char subtest, bool valid, FileLocHint hint)
{
std::stringstream s;
std::string path = "";
if (valid == true) {
path = "file.";
s << testno;
path += s.str() + subtest;
}
print("Test %d%c: ", testno, subtest);
return new File(path, hint);
}
static inline bool inval_or_legacy(bool valid, FileLocHint hint)
{
return (valid == false) || (hint == FILE_TYPE_LEGACY);
}
/*
* Check how filepath is expanded
* An empty filepath == INVALID
*/
void test_a(int testno, bool valid, FileLocHint hint)
{
File *f = get_file(testno, 'a', valid, hint);
print("File path is %s\n", f->get_filepath());
}
/*
* NOT_OPEN is not a valid openmode
*/
void test_b(int testno, bool valid, FileLocHint hint)
{
File *f = get_file(testno, 'b', valid, hint);
test_result(f->open(NOT_OPEN) == false, true);
}
/*
* If a file doesn't exist, it can't be opened for reading
*/
void test_c(int testno, bool valid, FileLocHint hint)
{
File *f = get_file(testno, 'c', valid, hint);
test_result(f->open(OPEN_READ) == false, true);
}
/*
* Writing to FILE_TYPE_LEGACY is not supported
*/
void test_d(int testno, bool valid, FileLocHint hint)
{
File *f = get_file(testno, 'd', valid, hint);
bool ret = f->open(OPEN_WRITE);
if (inval_or_legacy(valid, hint))
ret = !ret;
test_result(ret, inval_or_legacy(valid, hint));
}
/*
* Attempt to close a file that was never opened
*/
void test_e(int testno, bool valid, FileLocHint hint)
{
File *f = get_file(testno, 'e', valid, hint);
test_result(f->close(), false);
}
/*
* Open for writing, then for reading
*/
void test_f(int testno, bool valid, FileLocHint hint)
{
File *f;
if (inval_or_legacy(valid, hint))
return;
f = get_file(testno, 'f', valid, hint);
test_result(f->open(OPEN_WRITE), false);
print(" ");
test_result(f->open(OPEN_READ) == false, true);
}
/*
* Create a file, then open for reading
*/
void test_g(int testno, bool valid, FileLocHint hint)
{
File *f;
if (inval_or_legacy(valid, hint))
return;
f = get_file(testno, 'g', valid, hint);
if (!test_result(f->open(OPEN_WRITE), false))
return;
if (!test_result(f->close(), true))
return;
if (!test_result(f->open(OPEN_READ), true))
return;
test_result(f->close(), true);
}
/*
* Test doing IO to a file
*/
void test_h(int testno, bool valid, FileLocHint hint)
{
File *f;
int a, b, c, d, e;
if (inval_or_legacy(valid, hint))
return;
f = get_file(testno, 'h', valid, hint);
/*
* Write data to file
*/
if (!test_result(f->open(OPEN_WRITE), false))
return;
(*f) << "Hello, World!" << std::endl;
(*f) << "This is a multi-line file =)" << std::endl;
(*f) << "1 2 3 4 5" << std::endl;
if (!test_result(f->close(), true))
return;
/*
* Read data back from disk
*/
if (!test_result(f->open(OPEN_READ), true))
return;
print(" File version is: %u\n", f->get_version());
print(" \"%s\"\n", f->getline().c_str());
if (!test_result(f->good(), true))
return;
print(" \"%s\"\n", f->getline().c_str());
if (!test_result(f->good(), true))
return;
(*f) >> a >> b >> c >> d >> e;
if (!test_result(f->good(), true))
return;
print(" a: %d, b: %d, c: %d, d: %d, e: %d\n", a, b, c, d, e);
/*
* Close file
*/
test_result(f->close(), true);
}
static void(*tests[])(int, bool, FileLocHint) = {
test_a,
test_b,
test_c,
test_d,
test_e,
test_f,
test_g,
test_h,
};
static unsigned int num_tests = sizeof(tests) / sizeof(void (*)(int, bool, FileLocHint));
void run_test(int testno, bool valid, FileLocHint hint)
{
for (unsigned int i = 0; i < num_tests; i++)
tests[i](testno, valid, hint);
print("\n");
}
int main(int argc, char **argv)
{
run_test(1, true, FILE_TYPE_CONFIG);
run_test(2, false, FILE_TYPE_CONFIG);
run_test(3, true, FILE_TYPE_DATA);
run_test(4, false, FILE_TYPE_DATA);
run_test(5, true, FILE_TYPE_LEGACY);
run_test(6, false, FILE_TYPE_LEGACY);
return 0;
}

88
tests/file/file.good Normal file
View File

@ -0,0 +1,88 @@
Test 1a: File path is /home/anna/.config/ocarina-test/file.1a
Test 1b: ERROR: NOT_OPEN is not a legal OpenMode (/home/anna/.config/ocarina-test/file.1b)
Passed
Test 1c: ERROR: File does not exist (/home/anna/.config/ocarina-test/file.1c)
Passed
Test 1d: Passed
Test 1e: Passed
Test 1f: Passed
ERROR: File is already open (/home/anna/.config/ocarina-test/file.1f)
Passed
Test 1g: Passed
Passed
Passed
Passed
Test 1h: Passed
Passed
Passed
File version is: 0
"Hello, World!"
Passed
"This is a multi-line file =)"
Passed
Passed
a: 1, b: 2, c: 3, d: 4, e: 5
Passed
Test 2a: File path is INVALID
Test 2b: ERROR: NOT_OPEN is not a legal OpenMode (INVALID)
Passed
Test 2c: ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened
Passed
Test 2d: ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened
Passed
Test 2e: Passed
Test 3a: File path is /home/anna/.local/share/ocarina-test/file.3a
Test 3b: ERROR: NOT_OPEN is not a legal OpenMode (/home/anna/.local/share/ocarina-test/file.3b)
Passed
Test 3c: ERROR: File does not exist (/home/anna/.local/share/ocarina-test/file.3c)
Passed
Test 3d: Passed
Test 3e: Passed
Test 3f: Passed
ERROR: File is already open (/home/anna/.local/share/ocarina-test/file.3f)
Passed
Test 3g: Passed
Passed
Passed
Passed
Test 3h: Passed
Passed
Passed
File version is: 0
"Hello, World!"
Passed
"This is a multi-line file =)"
Passed
Passed
a: 1, b: 2, c: 3, d: 4, e: 5
Passed
Test 4a: File path is INVALID
Test 4b: ERROR: NOT_OPEN is not a legal OpenMode (INVALID)
Passed
Test 4c: ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened
Passed
Test 4d: ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened
Passed
Test 4e: Passed
Test 5a: File path is /home/anna/.ocarina-test/file.5a
Test 5b: ERROR: NOT_OPEN is not a legal OpenMode (/home/anna/.ocarina-test/file.5b)
Passed
Test 5c: ERROR: File does not exist (/home/anna/.ocarina-test/file.5c)
Passed
Test 5d: ERROR: Cannot write to legacy files (/home/anna/.ocarina-test/file.5d)
Passed
Test 5e: Passed
Test 6a: File path is INVALID
Test 6b: ERROR: NOT_OPEN is not a legal OpenMode (INVALID)
Passed
Test 6c: ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened
Passed
Test 6d: ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened
Passed
Test 6e: Passed

View File

@ -1,55 +0,0 @@
/*
* Copyright 2013 (c) Anna Schumaker.
* Test reading and writing to files
*/
#include <file.h>
#include <test.h>
#include <print.h>
int main(int argc, char **argv)
{
int a, b, c, d, e;
File file("test.io", FILE_TYPE_DATA);
File inval("", FILE_TYPE_DATA);
rm_test_data();
print("Writing data to file: %s\n", file.get_filepath());
if (!file.open(OPEN_WRITE))
return 1;
file << "Hello, World!" << std::endl;
file << "This is a multi-line file =)" << std::endl;
file << "1 2 3 4 5" << std::endl;
print("Closing file\n");
if (!file.close())
return 1;
print("Reading file\n");
if (!file.open(OPEN_READ))
return 1;
print("File version is: %u\n", file.get_version());
print("\t%s\n", file.getline().c_str());
if (!file.good())
return 1;
print("\t%s\n", file.getline().c_str());
if (!file.good())
return 1;
file >> a >> b >> c >> d >> e;
if (!file.good())
return 1;
print("\ta: %d, b: %d, c: %d, d: %d, e: %d\n", a, b, c, d, e);
if (!file.close())
return 1;
if (inval.open(OPEN_READ) == true)
return 1;
return 0;
}

View File

@ -1,8 +0,0 @@
Writing data to file: /home/anna/.local/share/ocarina-test/test.io
Closing file
Reading file
File version is: 0
Hello, World!
This is a multi-line file =)
a: 1, b: 2, c: 3, d: 4, e: 5
ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened

View File

@ -1,118 +0,0 @@
/*
* Copyright 2013 (c) Anna Schumaker.
* Test opening files under various conditions
*/
#include <file.h>
#include <test.h>
#include <print.h>
static int test_result(const char *num, bool res, bool expected)
{
print("Test %s: ", num);
if (res == expected) {
print("Passed\n");
return 0;
} else {
print("Failed\n");
return 1;
}
}
static int test_error(const char *num, FileLocHint hint, OpenMode mode)
{
std::string f = "test.";
f += num;
File file(f.c_str(), hint);
print("\n");
return test_result(num, file.open(mode), false);
}
/*
* Attempt to open a file with mode == NOT_OPEN
*/
int test_0()
{
return test_error("0", FILE_TYPE_DATA, NOT_OPEN);
}
/*
* Attempt to open a file that doesn't exist for reading
*/
int test_1()
{
return test_error("1", FILE_TYPE_DATA, OPEN_READ);
}
/*
* Attempt to open a legacy file for writing
*/
int test_2()
{
return test_error("2", FILE_TYPE_LEGACY, OPEN_WRITE);
}
/*
* Write to a file, then open it for reading WITHOUT closing first
*/
int test_3()
{
File file("test.3", FILE_TYPE_DATA);
printf("\n");
if (test_result("3a", file.open(OPEN_WRITE), true) != 0)
return 1;
return test_result("3b", file.open(OPEN_READ), false);
}
/*
* Write to a file, close it, then open it again for reading
*/
int test_4()
{
File file("test.4", FILE_TYPE_DATA);
printf("\n");
if (test_result("4a", file.open(OPEN_WRITE), true) != 0)
return 1;
if (test_result("4b", file.close(), true) != 0)
return 1;
return test_result("4c", file.open(OPEN_READ), true);
}
/*
* Attempt to close a file that was never opened
*/
int test_5()
{
File file("test.5", FILE_TYPE_DATA);
print("\n");
return test_result("5", file.close(), true);
}
/*
* Attempt to open an invalid file
*/
int test_6()
{
File file("", FILE_TYPE_DATA);
print("\n");
return test_result("6", file.open(OPEN_READ), false);
}
int main(int argc, char **argv)
{
int failed = 0;
rm_test_data();
failed += test_0();
failed += test_1();
failed += test_2();
failed += test_3();
failed += test_4();
failed += test_5();
failed += test_6();
return failed;
}

View File

@ -1,22 +0,0 @@
ERROR: NOT_OPEN is not a legal OpenMode (/home/anna/.local/share/ocarina-test/test.0)
Test 0: Passed
ERROR: File does not exist (/home/anna/.local/share/ocarina-test/test.1)
Test 1: Passed
ERROR: Cannot write to legacy files (/home/anna/.ocarina-test/test.2)
Test 2: Passed
Test 3a: Passed
ERROR: File is already open (/home/anna/.local/share/ocarina-test/test.3)
Test 3b: Passed
Test 4a: Passed
Test 4b: Passed
Test 4c: Passed
Test 5: Passed
ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened
Test 6: Passed