1 #include <cxxtest/TestSuite.h> 2 3 #include "common/memstream.h" 4 #include "common/bufferedstream.h" 5 6 class BufferedReadStreamTestSuite : public CxxTest::TestSuite { 7 public: test_traverse()8 void test_traverse() { 9 byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 10 Common::MemoryReadStream ms(contents, 10); 11 12 // Use a buffer size of 4 -- note that 10 % 4 != 0, 13 // so we test what happens if the cache can't be completely 14 // refilled. 15 Common::ReadStream &srs = *Common::wrapBufferedReadStream(&ms, 4, DisposeAfterUse::NO); 16 17 byte i, b; 18 for (i = 0; i < 10; ++i) { 19 TS_ASSERT(!srs.eos()); 20 21 b = srs.readByte(); 22 TS_ASSERT_EQUALS(i, b); 23 } 24 25 TS_ASSERT(!srs.eos()); 26 27 b = srs.readByte(); 28 29 TS_ASSERT(srs.eos()); 30 31 delete &srs; 32 } 33 test_traverse2()34 void test_traverse2() { 35 byte contents[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; 36 Common::MemoryReadStream ms(contents, 9); 37 38 Common::ReadStream &brs = *Common::wrapBufferedReadStream(&ms, 4, DisposeAfterUse::NO); 39 40 // Traverse the stream with reads of 2 bytes. The size is not 41 // a multiple of 2, so we can test the final partial read. 42 43 byte i, b[2]; 44 for (i = 0; i < 4; ++i) { 45 TS_ASSERT(!brs.eos()); 46 47 int n = brs.read(b, 2); 48 TS_ASSERT_EQUALS(n, 2); 49 } 50 51 TS_ASSERT(!brs.eos()); 52 53 int n = brs.read(b, 2); 54 TS_ASSERT_EQUALS(n, 1); 55 56 TS_ASSERT(brs.eos()); 57 58 delete &brs; 59 } 60 }; 61