aboutsummaryrefslogtreecommitdiff
path: root/test/common/list.h
blob: 26709b061b5d52b758aafb1bf9859af2eabf88c6 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <cxxtest/TestSuite.h>

#include "common/stdafx.h"
#include "common/list.h"

class ListTestSuite : public CxxTest::TestSuite
{
	public:
	void test_empty_clear( void )
	{
		Common::List<int> container;
		TS_ASSERT( container.empty() );
		container.push_back(17);
		container.push_back(33);
		TS_ASSERT( !container.empty() );
		container.clear();
		TS_ASSERT( container.empty() );
	}

	void test_iterator_begin_end( void )
	{
		Common::List<int> container;

		// The container is initially empty ...
		TS_ASSERT( container.begin() == container.end() );
		
		// ... then non-empty ...
		container.push_back(33);
		TS_ASSERT( container.begin() != container.end() );

		// ... and again empty.
		container.clear();
		TS_ASSERT( container.begin() == container.end() );
	}

	void test_iterator( void )
	{
		Common::List<int> container;
		Common::List<int>::iterator iter;

		// Fill the container with some random data
		container.push_back(17);
		container.push_back(33);
		container.push_back(-11);

		// Iterate over the container and verify that we encounter the elements in
		// the order we expect them to be.

		iter = container.begin();

		TS_ASSERT( *iter == 17 );
		++iter;
		TS_ASSERT( iter != container.end() );

		TS_ASSERT( *iter == 33 );
		++iter;
		TS_ASSERT( iter != container.end() );

		// Also test the postinc
		TS_ASSERT( *iter == -11 );
		iter++;
		TS_ASSERT( iter == container.end() );
	}

	void test_insert( void )
	{
		Common::List<int> container;
		Common::List<int>::iterator iter;

		// Fill the container with some random data
		container.push_back(17);
		container.push_back(33);
		container.push_back(-11);

		// Iterate to after the second element
		iter = container.begin();
		++iter;
		++iter;

		// Now insert some values here
		container.insert(iter, 42);
		container.insert(iter, 43);

		iter = container.begin();

		TS_ASSERT( *iter == 17 );
		++iter;
		TS_ASSERT( iter != container.end() );

		TS_ASSERT( *iter == 33 );
		++iter;
		TS_ASSERT( iter != container.end() );

		TS_ASSERT( *iter == 42 );
		++iter;
		TS_ASSERT( iter != container.end() );

		TS_ASSERT( *iter == 43 );
		++iter;
		TS_ASSERT( iter != container.end() );

		TS_ASSERT( *iter == -11 );
		++iter;
		TS_ASSERT( iter == container.end() );
	}

	void test_reverse( void )
	{
		Common::List<int> container;
		Common::List<int>::iterator iter;

		// Fill the container with some random data
		container.push_back(17);
		container.push_back(33);
		container.push_back(-11);

		iter = container.reverse_begin();
		TS_ASSERT( iter != container.end() );


		TS_ASSERT( *iter == -11 );
		--iter;
		TS_ASSERT( iter != container.end() );

		TS_ASSERT( *iter == 33 );
		--iter;
		TS_ASSERT( iter != container.end() );

		TS_ASSERT( *iter == 17 );
		--iter;
		TS_ASSERT( iter == container.end() );

		iter = container.reverse_begin();

		iter = container.reverse_erase(iter);
		TS_ASSERT( iter != container.end() );
		TS_ASSERT( *iter == 33 );

		iter = container.reverse_erase(iter);
		TS_ASSERT( iter != container.end() );
		TS_ASSERT( *iter == 17 );

		iter = container.reverse_erase(iter);
		TS_ASSERT( iter == container.end() );

		TS_ASSERT( container.begin() == container.end() );
		TS_ASSERT( container.reverse_begin() == container.end() );
	}
};