]> git.tdb.fi Git - libs/core.git/blob - tests/file.cpp
Update Build for latest changes in Builder
[libs/core.git] / tests / file.cpp
1 #include <msp/core/systemerror.h>
2 #include <msp/io/file.h>
3 #include <msp/test/test.h>
4
5 using namespace std;
6 using namespace Msp;
7
8 class FileTests: public Test::RegisteredTest<FileTests>
9 {
10 public:
11         FileTests();
12
13         static const char *get_name() { return "File"; }
14
15 private:
16         void write();
17         void read();
18         void seek();
19         void file_not_found();
20         void invalid_access();
21 };
22
23
24 FileTests::FileTests()
25 {
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>();
31 }
32
33 void FileTests::write()
34 {
35         IO::File out("test.txt", IO::M_WRITE);
36         out.write("foobar\n");
37         out.write("quux\n");
38 }
39
40 void FileTests::read()
41 {
42         IO::File in("test.txt");
43
44         string line;
45         EXPECT(in.getline(line));
46         EXPECT_EQUAL(line, "foobar");
47
48         char buf[128];
49         unsigned len = in.read(buf, sizeof(buf));
50         EXPECT_EQUAL(len, 5U);
51         EXPECT_EQUAL(string(buf, len), "quux\n");
52
53         len = in.read(buf, sizeof(buf));
54         EXPECT_EQUAL(len, 0U);
55         EXPECT(in.eof());
56 }
57
58 void FileTests::seek()
59 {
60         IO::File in("test.txt");
61
62         IO::SeekOffset pos = in.seek(3, IO::S_BEG);
63         EXPECT_EQUAL(pos, 3);
64         char c = in.get();
65         EXPECT_EQUAL(c, 'b');
66
67         pos = in.seek(1, IO::S_CUR);
68         EXPECT_EQUAL(pos, 5);
69         c = in.get();
70         EXPECT_EQUAL(c, 'r');
71
72         pos = in.seek(-2, IO::S_END);
73         EXPECT_EQUAL(pos, 10);
74         c = in.get();
75         EXPECT_EQUAL(c, 'x');
76 }
77
78 void FileTests::file_not_found()
79 {
80         IO::File in("does.not.exist");
81 }
82
83 void FileTests::invalid_access()
84 {
85         IO::File in("test.txt");
86         in.write("foo\n");
87 }