X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=tests%2Ffile.cpp;fp=tests%2Ffile.cpp;h=ccd2eb5b080ab32866fde9db7a8b1e2b4b70ed4b;hp=0000000000000000000000000000000000000000;hb=120023d8da0aabcb803a87111608ce84c94661f8;hpb=79482ba7aea1b79c7a310c940cc0292532ef3bcb diff --git a/tests/file.cpp b/tests/file.cpp new file mode 100644 index 0000000..ccd2eb5 --- /dev/null +++ b/tests/file.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +using namespace std; +using namespace Msp; + +class FileTests: public Test::RegisteredTest +{ +public: + FileTests(); + + static const char *get_name() { return "File"; } + +private: + void write(); + void read(); + void seek(); + void file_not_found(); + void invalid_access(); +}; + + +FileTests::FileTests() +{ + add(&FileTests::write, "Writing"); + add(&FileTests::read, "Reading"); + add(&FileTests::seek, "Seeking"); + add(&FileTests::file_not_found, "file_not_found").expect_throw(); + add(&FileTests::invalid_access, "invalid_access").expect_throw(); +} + +void FileTests::write() +{ + IO::File out("test.txt", IO::M_WRITE); + out.write("foobar\n"); + out.write("quux\n"); +} + +void FileTests::read() +{ + IO::File in("test.txt"); + + string line; + EXPECT(in.getline(line)); + EXPECT_EQUAL(line, "foobar"); + + char buf[128]; + unsigned len = in.read(buf, sizeof(buf)); + EXPECT_EQUAL(len, 5U); + EXPECT_EQUAL(string(buf, len), "quux\n"); + + len = in.read(buf, sizeof(buf)); + EXPECT_EQUAL(len, 0U); + EXPECT(in.eof()); +} + +void FileTests::seek() +{ + IO::File in("test.txt"); + + IO::SeekOffset pos = in.seek(3, IO::S_BEG); + EXPECT_EQUAL(pos, 3); + char c = in.get(); + EXPECT_EQUAL(c, 'b'); + + pos = in.seek(1, IO::S_CUR); + EXPECT_EQUAL(pos, 5); + c = in.get(); + EXPECT_EQUAL(c, 'r'); + + pos = in.seek(-2, IO::S_END); + EXPECT_EQUAL(pos, 10); + c = in.get(); + EXPECT_EQUAL(c, 'x'); +} + +void FileTests::file_not_found() +{ + IO::File in("does.not.exist"); +} + +void FileTests::invalid_access() +{ + IO::File in("test.txt"); + in.write("foo\n"); +}