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