/* * Copyright 2013 (c) Anna Schumaker. */ #include #include #include #include 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) { int status = 0; File *f = get_file(testno, 'b', valid, hint); try { f->open(NOT_OPEN); } catch (int error) { status = error; } test_result(status == -E_INVAL, true); } /* * If a file doesn't exist, it can't be opened for reading */ void test_c(int testno, bool valid, FileLocHint hint) { int status = 0; File *f = get_file(testno, 'c', valid, hint); try { f->open(OPEN_READ); } catch (int error) { status = error; } if (valid == false) test_result(status == -E_INVAL, true); else test_result(status == -E_EXIST, true); } /* * Writing to FILE_TYPE_LEGACY is not supported */ void test_d(int testno, bool valid, FileLocHint hint) { int status = 0; File *f = get_file(testno, 'd', valid, hint); try { f->open(OPEN_WRITE); } catch (int error) { status = error; } if (valid == false) test_result(status == -E_INVAL, true); else if (hint == FILE_TYPE_LEGACY) test_result(status == -E_IO, true); else test_result(status == 0, false); } /* * Attempt to close a file that was never opened */ void test_e(int testno, bool valid, FileLocHint hint) { int status = 0; File *f = get_file(testno, 'e', valid, hint); try { f->close(); } catch (int error) { status = error; } test_result(status == 0, false); } /* * Open for writing, then for reading */ void test_f(int testno, bool valid, FileLocHint hint) { File *f; int status = 0; if (inval_or_legacy(valid, hint)) return; f = get_file(testno, 'f', valid, hint); try { f->open(OPEN_WRITE); } catch (int error) { status = error; } test_result(status == 0, false); print(" "); status = 0; try { f->open(OPEN_READ); } catch (int error) { status = error; } test_result(status == -E_IO, true); } /* * Create a file, then open for reading */ void test_g(int testno, bool valid, FileLocHint hint) { File *f; int status = 0; if (inval_or_legacy(valid, hint)) return; f = get_file(testno, 'g', valid, hint); try { f->open(OPEN_WRITE); } catch (int error) { status = error; } if (!test_result(status == 0, false)) return; try { f->close(); } catch (int error) { status = error; } if (!test_result(status == 0, true)) return; try { f->open(OPEN_READ); } catch (int error) { status = error; } if (!test_result(status == 0, true)) return; f->close(); } /* * Test doing IO to a file */ void test_h(int testno, bool valid, FileLocHint hint) { File *f; int a, b, c, d, e, status = 0; if (inval_or_legacy(valid, hint)) return; f = get_file(testno, 'h', valid, hint); /* * Write data to file */ try { f->open(OPEN_WRITE); } catch (int error) { status = error; } if (!test_result(status == 0, false)) return; (*f) << "Hello, World!" << std::endl; (*f) << "This is a multi-line file =)" << std::endl; (*f) << "1 2 3 4 5" << std::endl; try { f->close(); } catch (int error) { status = error; } if (!test_result(status == 0, true)) return; /* * Read data back from disk */ try { f->open(OPEN_READ); } catch (int error) { status = error; } if (!test_result(status == 0, 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 */ try { f->close(); } catch (int error) { status = error; } test_result(status == 0, 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; }