1 #include <msp/core/systemerror.h>
2 #include <msp/io/file.h>
3 #include <msp/test/test.h>
8 class FileTests: public Test::RegisteredTest<FileTests>
13 static const char *get_name() { return "File"; }
19 void file_not_found();
20 void invalid_access();
24 FileTests::FileTests()
26 add(&FileTests::write, "Writing");
27 add(&FileTests::read, "Reading");
28 add(&FileTests::seek, "Seeking");
29 add(&FileTests::file_not_found, "file_not_found").expect_throw<IO::file_not_found>();
30 add(&FileTests::invalid_access, "invalid_access").expect_throw<IO::invalid_access>();
33 void FileTests::write()
35 IO::File out("test.txt", IO::M_WRITE);
36 out.write("foobar\n");
40 void FileTests::read()
42 IO::File in("test.txt");
45 EXPECT(in.getline(line));
46 EXPECT_EQUAL(line, "foobar");
49 unsigned len = in.read(buf, sizeof(buf));
50 EXPECT_EQUAL(len, 5U);
51 EXPECT_EQUAL(string(buf, len), "quux\n");
53 len = in.read(buf, sizeof(buf));
54 EXPECT_EQUAL(len, 0U);
58 void FileTests::seek()
60 IO::File in("test.txt");
62 IO::SeekOffset pos = in.seek(3, IO::S_BEG);
67 pos = in.seek(1, IO::S_CUR);
72 pos = in.seek(-2, IO::S_END);
73 EXPECT_EQUAL(pos, 10);
78 void FileTests::file_not_found()
80 IO::File in("does.not.exist");
83 void FileTests::invalid_access()
85 IO::File in("test.txt");