123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #include "../../src/ErrorSimulator.hpp"
- #include "../Tests.hpp"
- #include "core/io/FileReader.hpp"
- static constexpr const char* TEST_FILE = "../test/modules/resources/test";
- static void testReadChar() {
- Core::FileReader r;
- CORE_TEST_ERROR(r.open(TEST_FILE));
- Core::String8<128> s;
- while(true) {
- int c = 0;
- if(r.readChar(c) != Core::Error::NONE) {
- break;
- }
- CORE_TEST_ERROR(s.append(static_cast<c32>(c)));
- }
- CORE_TEST_STRING("abc\nBaum\n", s);
- }
- static void testReadCharPath() {
- Core::FileReader r;
- Core::Path path;
- CORE_TEST_ERROR(path.append(TEST_FILE));
- CORE_TEST_ERROR(r.open(path));
- Core::String8<128> s;
- while(true) {
- int c = 0;
- if(r.readChar(c) != Core::Error::NONE) {
- break;
- }
- CORE_TEST_ERROR(s.append(static_cast<c32>(c)));
- }
- CORE_TEST_STRING("abc\nBaum\n", s);
- }
- static void testReadChars() {
- Core::FileReader r;
- CORE_TEST_ERROR(r.open(TEST_FILE));
- char buffer[128];
- CORE_TEST_EQUAL(Core::Error::END_OF_FILE,
- r.readChars(buffer, sizeof(buffer)));
- CORE_TEST_STRING("abc\nBaum\n", buffer);
- }
- static void testReadCharsOverflow() {
- Core::FileReader r;
- CORE_TEST_ERROR(r.open(TEST_FILE));
- char buffer[6];
- CORE_TEST_ERROR(r.readChars(buffer, sizeof(buffer)));
- CORE_TEST_STRING("abc\nB", buffer);
- }
- static void testMoveConstruct() {
- Core::FileReader r;
- CORE_TEST_ERROR(r.open(TEST_FILE));
- Core::FileReader r2 = Core::move(r);
- CORE_TEST_STRING("", r.getPath());
- CORE_TEST_STRING(TEST_FILE, r2.getPath());
- int c = 0;
- CORE_TEST_EQUAL(Core::Error::INVALID_STATE, r.readChar(c));
- CORE_TEST_ERROR(r2.readChar(c));
- CORE_TEST_EQUAL('a', c);
- }
- static void testMoveAssignment() {
- Core::FileReader r;
- CORE_TEST_ERROR(r.open(TEST_FILE));
- Core::FileReader r2;
- r2 = Core::move(r);
- CORE_TEST_STRING("", r.getPath());
- CORE_TEST_STRING(TEST_FILE, r2.getPath());
- int c = 0;
- CORE_TEST_EQUAL(Core::Error::INVALID_STATE, r.readChar(c));
- CORE_TEST_ERROR(r2.readChar(c));
- CORE_TEST_EQUAL('a', c);
- }
- static void testInvalidAccess() {
- char buffer[4];
- Core::FileReader r;
- CORE_TEST_EQUAL(Core::Error::INVALID_STATE,
- r.readChars(buffer, sizeof(buffer)));
- CORE_TEST_ERROR(r.open(TEST_FILE));
- CORE_TEST_EQUAL(Core::Error::INVALID_ARGUMENT, r.readChars(buffer, -1));
- CORE_TEST_EQUAL(Core::Error::INVALID_STATE, r.open(TEST_FILE));
- }
- static void testInvalidAccessPath() {
- Core::FileReader r;
- Core::Path path;
- CORE_TEST_ERROR(path.append(TEST_FILE));
- CORE_TEST_ERROR(r.open(path));
- CORE_TEST_EQUAL(Core::Error::INVALID_STATE, r.open(path));
- }
- static void testCloseFail() {
- #ifdef ERROR_SIMULATOR
- Core::Fail::fileClose = true;
- {
- Core::FileReader r;
- CORE_TEST_ERROR(r.open(TEST_FILE));
- }
- Core::Fail::fileClose = false;
- #endif
- }
- void Core::testFileReader() {
- testReadChar();
- testReadCharPath();
- testReadChars();
- testReadCharsOverflow();
- testMoveConstruct();
- testMoveAssignment();
- testInvalidAccess();
- testInvalidAccessPath();
- testCloseFail();
- }
|