1 #include <cxxtest/TestSuite.h> 2 3 #include "common/memstream.h" 4 5 class MemoryReadStreamEndianTestSuite : public CxxTest::TestSuite { 6 public: test_seek_set()7 void test_seek_set() { 8 byte contents[] = { 'a', 'b', '\n', '\n', 'c', '\n' }; 9 Common::MemoryReadStreamEndian ms(contents, sizeof(contents), false); 10 11 ms.seek(0, SEEK_SET); 12 TS_ASSERT_EQUALS(ms.pos(), 0); 13 TS_ASSERT(!ms.eos()); 14 15 ms.seek(1, SEEK_SET); 16 TS_ASSERT_EQUALS(ms.pos(), 1); 17 TS_ASSERT(!ms.eos()); 18 19 ms.seek(5, SEEK_SET); 20 TS_ASSERT_EQUALS(ms.pos(), 5); 21 TS_ASSERT(!ms.eos()); 22 } 23 test_seek_cur()24 void test_seek_cur() { 25 byte contents[] = { 'a', 'b', '\n', '\n', 'c' }; 26 Common::MemoryReadStreamEndian ms(contents, sizeof(contents), false); 27 28 ms.seek(3, SEEK_CUR); 29 TS_ASSERT_EQUALS(ms.pos(), 3); 30 TS_ASSERT(!ms.eos()); 31 32 ms.seek(-1, SEEK_CUR); 33 TS_ASSERT_EQUALS(ms.pos(), 2); 34 TS_ASSERT(!ms.eos()); 35 36 ms.seek(3, SEEK_CUR); 37 TS_ASSERT_EQUALS(ms.pos(), 5); 38 TS_ASSERT(!ms.eos()); 39 40 ms.seek(-1, SEEK_CUR); 41 TS_ASSERT_EQUALS(ms.pos(), 4); 42 TS_ASSERT(!ms.eos()); 43 } 44 test_seek_end()45 void test_seek_end() { 46 byte contents[] = { 'a', 'b', '\n', '\n', 'c' }; 47 Common::MemoryReadStreamEndian ms(contents, sizeof(contents), false); 48 49 ms.seek(0, SEEK_END); 50 TS_ASSERT_EQUALS(ms.pos(), 5); 51 TS_ASSERT(!ms.eos()); 52 53 ms.seek(-1, SEEK_END); 54 TS_ASSERT_EQUALS(ms.pos(), 4); 55 TS_ASSERT(!ms.eos()); 56 57 ms.seek(-5, SEEK_END); 58 TS_ASSERT_EQUALS(ms.pos(), 0); 59 TS_ASSERT(!ms.eos()); 60 } 61 test_seek_read_le()62 void test_seek_read_le() { 63 byte contents[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; 64 Common::MemoryReadStreamEndian ms(contents, sizeof(contents), false); 65 66 TS_ASSERT_EQUALS(ms.readUint16LE(), 0x0201UL); 67 TS_ASSERT_EQUALS(ms.pos(), 2); 68 TS_ASSERT_EQUALS(ms.readUint32LE(), 0x06050403UL); 69 TS_ASSERT_EQUALS(ms.pos(), 6); 70 TS_ASSERT_EQUALS(ms.readUint64LE(), 0x0E0D0C0B0A090807ULL); 71 TS_ASSERT_EQUALS(ms.pos(), 14); 72 TS_ASSERT_EQUALS(ms.readByte(), 0x0F); 73 TS_ASSERT_EQUALS(ms.pos(), 15); 74 TS_ASSERT(!ms.eos()); 75 } 76 test_seek_read_be()77 void test_seek_read_be() { 78 byte contents[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; 79 Common::MemoryReadStreamEndian ms(contents, sizeof(contents), false); 80 81 TS_ASSERT_EQUALS(ms.readUint16BE(), 0x0102UL); 82 TS_ASSERT_EQUALS(ms.pos(), 2); 83 TS_ASSERT_EQUALS(ms.readUint32BE(), 0x03040506UL); 84 TS_ASSERT_EQUALS(ms.pos(), 6); 85 TS_ASSERT_EQUALS(ms.readUint64BE(), 0x0708090A0B0C0D0EULL); 86 TS_ASSERT_EQUALS(ms.pos(), 14); 87 TS_ASSERT_EQUALS(ms.readByte(), 0x0F); 88 TS_ASSERT_EQUALS(ms.pos(), 15); 89 TS_ASSERT(!ms.eos()); 90 } 91 test_seek_read_le2()92 void test_seek_read_le2() { 93 byte contents[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; 94 Common::MemoryReadStreamEndian ms(contents, sizeof(contents), false); 95 96 TS_ASSERT_EQUALS(ms.readUint16(), 0x0201UL); 97 TS_ASSERT_EQUALS(ms.pos(), 2); 98 TS_ASSERT_EQUALS(ms.readUint32(), 0x06050403UL); 99 TS_ASSERT_EQUALS(ms.pos(), 6); 100 TS_ASSERT_EQUALS(ms.readUint64(), 0x0E0D0C0B0A090807ULL); 101 TS_ASSERT_EQUALS(ms.pos(), 14); 102 TS_ASSERT_EQUALS(ms.readByte(), 0x0F); 103 TS_ASSERT_EQUALS(ms.pos(), 15); 104 TS_ASSERT(!ms.eos()); 105 } 106 test_seek_read_be2()107 void test_seek_read_be2() { 108 byte contents[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; 109 Common::MemoryReadStreamEndian ms(contents, sizeof(contents), true); 110 111 TS_ASSERT_EQUALS(ms.readUint16(), 0x0102UL); 112 TS_ASSERT_EQUALS(ms.pos(), 2); 113 TS_ASSERT_EQUALS(ms.readUint32(), 0x03040506UL); 114 TS_ASSERT_EQUALS(ms.pos(), 6); 115 TS_ASSERT_EQUALS(ms.readUint64(), 0x0708090A0B0C0D0EULL); 116 TS_ASSERT_EQUALS(ms.pos(), 14); 117 TS_ASSERT_EQUALS(ms.readByte(), 0x0F); 118 TS_ASSERT_EQUALS(ms.pos(), 15); 119 TS_ASSERT(!ms.eos()); 120 } 121 }; 122