aboutsummaryrefslogtreecommitdiff
path: root/test/common/seekablesubreadstream.h
blob: 4e517093a58826552c6501611610aecfff1c044d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <cxxtest/TestSuite.h>

#include "common/stream.h"

class SeekableSubReadStreamTestSuite : 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);

		int start = 2, end = 8;

		Common::SeekableSubReadStream ssrs(&ms, start, end);

		int i;
		byte b;
		for (i = start; i < end; ++i) {
			TS_ASSERT( !ssrs.eos() );

			TS_ASSERT_EQUALS( uint32(i - start), 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::SeekableSubReadStream ssrs(&ms, 1, 9);
		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, 2 );

		ssrs.seek(5, SEEK_CUR);
		TS_ASSERT_EQUALS( ssrs.pos(), (uint32)7 );
		b = ssrs.readByte();
		TS_ASSERT_EQUALS( b, 8 );

		ssrs.seek(-3, SEEK_CUR);
		TS_ASSERT_EQUALS( ssrs.pos(), (uint32)5 );
		b = ssrs.readByte();
		TS_ASSERT_EQUALS( b, 6 );

		ssrs.seek(0, SEEK_END);
		TS_ASSERT_EQUALS( ssrs.pos(), (uint32)8 );
		TS_ASSERT( ssrs.eos() );

		ssrs.seek(3, SEEK_END);
		TS_ASSERT_EQUALS( ssrs.pos(), (uint32)5 );
		b = ssrs.readByte();
		TS_ASSERT_EQUALS( b, 6 );

		ssrs.seek(8, SEEK_END);
		TS_ASSERT_EQUALS( ssrs.pos(), (uint32)0 );
		b = ssrs.readByte();
		TS_ASSERT_EQUALS( b, 1 );
	}
};