]> git.tdb.fi Git - libs/core.git/blob - tests/memory.cpp
Interpret POLLHUP as end-of-file
[libs/core.git] / tests / memory.cpp
1 #include <msp/io/memory.h>
2 #include <msp/test/test.h>
3
4 using namespace std;
5 using namespace Msp;
6
7 class MemoryTests: public Test::RegisteredTest<MemoryTests>
8 {
9 public:
10         MemoryTests();
11
12         static const char *get_name() { return "Memory"; }
13
14 private:
15         void write();
16         void read();
17         void getline();
18         void invalid_access();
19 };
20
21
22 MemoryTests::MemoryTests()
23 {
24         add(&MemoryTests::write,          "write");
25         add(&MemoryTests::read,           "read");
26         add(&MemoryTests::getline,        "getline");
27         add(&MemoryTests::invalid_access, "invalid_access").expect_throw<IO::invalid_access>();
28 }
29
30 void MemoryTests::write()
31 {
32         char buf[64] = { };
33         IO::Memory mem(buf, sizeof(buf));
34         mem.write("foobar");
35         EXPECT(equal(buf, buf+6, "foobar"));
36         for(unsigned i=6; i<sizeof(buf); ++i)
37                 if(buf[i]!=0)
38                         fail("Garbage in buffer");
39 }
40
41 void MemoryTests::read()
42 {
43         static const char buf[] = "foobar";
44
45         IO::Memory mem(buf, 6);
46         char rbuf[16];
47         unsigned len = mem.read(rbuf, sizeof(rbuf));
48         EXPECT_EQUAL(len, 6);
49         EXPECT(equal(rbuf, rbuf+6, buf));
50 }
51
52 void MemoryTests::getline()
53 {
54         static const char buf[] = "foobar\n\nquux\n";
55
56         IO::Memory mem(buf, sizeof(buf)-1);
57         string line;
58         EXPECT(mem.getline(line));
59         EXPECT_EQUAL(line, "foobar");
60         EXPECT(mem.getline(line));
61         EXPECT_EQUAL(line, "");
62         EXPECT(mem.getline(line));
63         EXPECT_EQUAL(line, "quux");
64         EXPECT(!mem.getline(line));
65 }
66
67 void MemoryTests::invalid_access()
68 {
69         static const char buf[] = "foobar";
70
71         IO::Memory mem(buf, sizeof(buf));
72         mem.write("quux", 4);
73 }