1 #include <msp/io/memory.h>
2 #include <msp/test/test.h>
7 class MemoryTests: public Test::RegisteredTest<MemoryTests>
12 static const char *get_name() { return "Memory"; }
18 void invalid_access();
22 MemoryTests::MemoryTests()
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>();
30 void MemoryTests::write()
33 IO::Memory mem(buf, sizeof(buf));
35 EXPECT(equal(buf, buf+6, "foobar"));
36 for(unsigned i=6; i<sizeof(buf); ++i)
38 fail("Garbage in buffer");
41 void MemoryTests::read()
43 static const char buf[] = "foobar";
45 IO::Memory mem(buf, 6);
47 unsigned len = mem.read(rbuf, sizeof(rbuf));
49 EXPECT(equal(rbuf, rbuf+6, buf));
52 void MemoryTests::getline()
54 static const char buf[] = "foobar\n\nquux\n";
56 IO::Memory mem(buf, sizeof(buf)-1);
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));
67 void MemoryTests::invalid_access()
69 static const char buf[] = "foobar";
71 IO::Memory mem(buf, sizeof(buf));