ocarina/tests/file/open.cpp

107 lines
1.8 KiB
C++

/*
* 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);
}
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();
return failed;
}