From aaa4d104f7eb4094ec6bd69ef2d44f80edd61aa4 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 29 Jul 2008 17:42:19 +0000 Subject: Added two new classes, BufferedReadStream & BufferedSeekableReadStream, as proposed on scummvm-devel svn-id: r33419 --- test/common/bufferedreadstream.h | 27 +++++++++++++ test/common/bufferedseekablereadstream.h | 65 ++++++++++++++++++++++++++++++++ test/common/seekablesubreadstream.h | 20 ++++------ test/common/subreadstream.h | 15 +++----- 4 files changed, 106 insertions(+), 21 deletions(-) create mode 100644 test/common/bufferedreadstream.h create mode 100644 test/common/bufferedseekablereadstream.h (limited to 'test') diff --git a/test/common/bufferedreadstream.h b/test/common/bufferedreadstream.h new file mode 100644 index 0000000000..7733949d9a --- /dev/null +++ b/test/common/bufferedreadstream.h @@ -0,0 +1,27 @@ +#include + +#include "common/stream.h" + +class BufferedReadStreamTestSuite : public CxxTest::TestSuite { + public: + void test_traverse(void) { + byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + Common::MemoryReadStream ms(contents, 10); + + // Use a buffer size of 4 -- note that 10 % 4 != 0, + // so we test what happens if the cache can't be completly + // refilled. + Common::BufferedReadStream srs(&ms, 4); + + int i; + byte b; + for (i = 0; i < 10; ++i) { + TS_ASSERT( !srs.eos() ); + + b = srs.readByte(); + TS_ASSERT_EQUALS( i, b ); + } + + TS_ASSERT( srs.eos() ); + } +}; diff --git a/test/common/bufferedseekablereadstream.h b/test/common/bufferedseekablereadstream.h new file mode 100644 index 0000000000..63941904cd --- /dev/null +++ b/test/common/bufferedseekablereadstream.h @@ -0,0 +1,65 @@ +#include + +#include "common/stream.h" + +class BufferedSeekableReadStreamTestSuite : public CxxTest::TestSuite { + public: + void test_traverse(void) { + byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + Common::MemoryReadStream ms(contents, 10); + + Common::BufferedSeekableReadStream ssrs(&ms, 4); + + int i; + byte b; + for (i = 0; i < 10; ++i) { + TS_ASSERT( !ssrs.eos() ); + + TS_ASSERT_EQUALS( i, ssrs.pos() ); + + ssrs.read(&b, 1); + TS_ASSERT_EQUALS( i, b ); + } + + TS_ASSERT( ssrs.eos() ); + } + + void test_seek(void) { + byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + Common::MemoryReadStream ms(contents, 10); + + Common::BufferedSeekableReadStream ssrs(&ms, 4); + byte b; + + TS_ASSERT_EQUALS( ssrs.pos(), (uint32)0 ); + + ssrs.seek(1, SEEK_SET); + TS_ASSERT_EQUALS( ssrs.pos(), (uint32)1 ); + b = ssrs.readByte(); + TS_ASSERT_EQUALS( b, 1 ); + + ssrs.seek(5, SEEK_CUR); + TS_ASSERT_EQUALS( ssrs.pos(), (uint32)7 ); + b = ssrs.readByte(); + TS_ASSERT_EQUALS( b, 7 ); + + ssrs.seek(-3, SEEK_CUR); + TS_ASSERT_EQUALS( ssrs.pos(), (uint32)5 ); + b = ssrs.readByte(); + TS_ASSERT_EQUALS( b, 5 ); + + ssrs.seek(0, SEEK_END); + TS_ASSERT_EQUALS( ssrs.pos(), (uint32)10 ); + TS_ASSERT( ssrs.eos() ); + + ssrs.seek(3, SEEK_END); + TS_ASSERT_EQUALS( ssrs.pos(), (uint32)7 ); + b = ssrs.readByte(); + TS_ASSERT_EQUALS( b, 7 ); + + ssrs.seek(8, SEEK_END); + TS_ASSERT_EQUALS( ssrs.pos(), (uint32)2 ); + b = ssrs.readByte(); + TS_ASSERT_EQUALS( b, 2 ); + } +}; diff --git a/test/common/seekablesubreadstream.h b/test/common/seekablesubreadstream.h index c4b21667c7..4e517093a5 100644 --- a/test/common/seekablesubreadstream.h +++ b/test/common/seekablesubreadstream.h @@ -2,22 +2,19 @@ #include "common/stream.h" -class SeekableSubReadStreamTestSuite : public CxxTest::TestSuite -{ +class SeekableSubReadStreamTestSuite : public CxxTest::TestSuite { public: - void test_traverse( void ) - { + void test_traverse(void) { byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; - Common::MemoryReadStream ms = Common::MemoryReadStream(contents, 10); + Common::MemoryReadStream ms(contents, 10); int start = 2, end = 8; - Common::SeekableSubReadStream ssrs = Common::SeekableSubReadStream(&ms, start, end); + Common::SeekableSubReadStream ssrs(&ms, start, end); int i; byte b; - for (i = start; i < end; ++i) - { + for (i = start; i < end; ++i) { TS_ASSERT( !ssrs.eos() ); TS_ASSERT_EQUALS( uint32(i - start), ssrs.pos() ); @@ -29,12 +26,11 @@ class SeekableSubReadStreamTestSuite : public CxxTest::TestSuite TS_ASSERT( ssrs.eos() ); } - void test_seek( void ) - { + void test_seek(void) { byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; - Common::MemoryReadStream ms = Common::MemoryReadStream(contents, 10); + Common::MemoryReadStream ms(contents, 10); - Common::SeekableSubReadStream ssrs = Common::SeekableSubReadStream(&ms, 1, 9); + Common::SeekableSubReadStream ssrs(&ms, 1, 9); byte b; TS_ASSERT_EQUALS( ssrs.pos(), (uint32)0 ); diff --git a/test/common/subreadstream.h b/test/common/subreadstream.h index c48f57c3a8..4e14448c06 100644 --- a/test/common/subreadstream.h +++ b/test/common/subreadstream.h @@ -2,25 +2,22 @@ #include "common/stream.h" -class SubReadStreamTestSuite : public CxxTest::TestSuite -{ +class SubReadStreamTestSuite : public CxxTest::TestSuite { public: - void test_traverse( void ) - { + void test_traverse(void) { byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; - Common::MemoryReadStream ms = Common::MemoryReadStream(contents, 10); + Common::MemoryReadStream ms(contents, 10); int end = 5; - Common::SubReadStream srs = Common::SubReadStream(&ms, end); + Common::SubReadStream srs(&ms, end); int i; byte b; - for (i = 0; i < end; ++i) - { + for (i = 0; i < end; ++i) { TS_ASSERT( !srs.eos() ); - srs.read(&b, 1); + b = srs.readByte(); TS_ASSERT_EQUALS( i, b ); } -- cgit v1.2.3