aboutsummaryrefslogtreecommitdiff
path: root/test/common/list.h
blob: 3b6b9a172162f8e10a83bb6f13fdf633468130a5 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <cxxtest/TestSuite.h>

#include "common/list.h"

class ListTestSuite : public CxxTest::TestSuite
{
	public:
	void test_empty_clear() {
		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() );
	}

	public:
	void test_size() {
		Common::List<int> container;
		TS_ASSERT_EQUALS( container.size(), (unsigned int)0 );
		container.push_back(17);
		TS_ASSERT_EQUALS( container.size(), (unsigned int)1 );
		container.push_back(33);
		TS_ASSERT_EQUALS( container.size(), (unsigned int)2 );
		container.clear();
		TS_ASSERT_EQUALS( container.size(), (unsigned int)0 );
	}

	void test_iterator_begin_end() {
		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() {
		Common::List<int> container;
		Common::List<int>::iterator iter;
		Common::List<int>::const_iterator cIter;

		// 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();
		cIter = container.begin();

		TS_ASSERT( iter == cIter );

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

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

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

		cIter = iter;
		TS_ASSERT( iter == cIter );
	}

	void test_insert() {
		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);

		// Verify contents are correct
		iter = container.begin();

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

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

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

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

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

	void test_erase() {
		Common::List<int> container;
		Common::List<int>::iterator first, last;

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

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

		// Iterate to after the fourth element
		last = first;
		++last;
		++last;

		// Now erase that range
		container.erase(first, last);

		// Verify contents are correct
		Common::List<int>::iterator iter = container.begin();

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

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

		TS_ASSERT_EQUALS( *iter, 43 );
		++iter;
		TS_ASSERT( iter == container.end() );
	}

	void test_remove() {
		Common::List<int> container;
		Common::List<int>::iterator first, last;

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

		// Remove some stuff
		container.remove(42);
		container.remove(-11);

		// Now erase that range
		container.erase(first, last);

		// Verify contents are correct
		Common::List<int>::iterator iter = container.begin();

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

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

		TS_ASSERT_EQUALS( *iter, 43 );
		++iter;
		TS_ASSERT( iter == container.end() );
	}

	void test_reverse() {
		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_EQUALS( *iter, -11 );
		--iter;
		TS_ASSERT( iter != container.end() );

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

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

		iter = container.reverse_begin();

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

		iter = container.reverse_erase(iter);
		TS_ASSERT( iter != container.end() );
		TS_ASSERT_EQUALS( *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() );
	}

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

		container.push_back( 42);
		container.push_back(-23);

		TS_ASSERT_EQUALS(container.front(), 42);
		TS_ASSERT_EQUALS(container.back(), -23);

		container.front() = -17;
		container.back() = 163;
		TS_ASSERT_EQUALS(container.front(), -17);
		TS_ASSERT_EQUALS(container.back(),  163);

		container.pop_front();
		TS_ASSERT_EQUALS(container.front(), 163);
		TS_ASSERT_EQUALS(container.back(),  163);

		container.push_front(99);
		TS_ASSERT_EQUALS(container.front(), 99);
		TS_ASSERT_EQUALS(container.back(),  163);

		container.pop_back();
		TS_ASSERT_EQUALS(container.front(), 99);
		TS_ASSERT_EQUALS(container.back(),  99);
	}
};