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
|
#include <cxxtest/TestSuite.h>
#include "common/array.h"
#include "common/str.h"
class ArrayTestSuite : public CxxTest::TestSuite
{
public:
void test_empty_clear() {
Common::Array<int> array;
TS_ASSERT(array.empty());
array.push_back(17);
array.push_back(33);
TS_ASSERT(!array.empty());
array.clear();
TS_ASSERT(array.empty());
}
void test_iterator() {
Common::Array<int> array;
Common::Array<int>::iterator iter;
// Fill the array with some random data
array.push_back(17);
array.push_back(33);
array.push_back(-11);
// Iterate over the array and verify that we encounter the elements in
// the order we expect them to be.
iter = array.begin();
TS_ASSERT_EQUALS(*iter, 17);
++iter;
TS_ASSERT_DIFFERS(iter, array.end());
TS_ASSERT_EQUALS(*iter, 33);
++iter;
TS_ASSERT_DIFFERS(iter, array.end());
// Also test the postinc
TS_ASSERT_EQUALS(*iter, -11);
iter++;
TS_ASSERT_EQUALS(iter, array.end());
}
void test_direct_access() {
Common::Array<int> array;
// Fill the array with some random data
array.push_back(17);
array.push_back(33);
array.push_back(-11);
TS_ASSERT_EQUALS(array[0], 17);
TS_ASSERT_EQUALS(array[1], 33);
TS_ASSERT_EQUALS(array[2], -11);
}
void test_insert_at() {
Common::Array<int> array;
// First of all some data
array.push_back(-12);
array.push_back(17);
array.push_back(25);
array.push_back(-11);
// Insert some data
array.insert_at(2, 33);
TS_ASSERT_EQUALS(array[0], -12);
TS_ASSERT_EQUALS(array[1], 17);
TS_ASSERT_EQUALS(array[2], 33);
TS_ASSERT_EQUALS(array[3], 25);
TS_ASSERT_EQUALS(array[4], -11);
TS_ASSERT_EQUALS(array.size(), (unsigned int)5);
}
void test_remove_at() {
Common::Array<int> array;
// First of all some data
array.push_back(-12);
array.push_back(17);
array.push_back(33);
array.push_back(25);
array.push_back(-11);
// Remove some data
array.remove_at(1);
TS_ASSERT_EQUALS(array[0], -12);
TS_ASSERT_EQUALS(array[1], 33);
TS_ASSERT_EQUALS(array[2], 25);
TS_ASSERT_EQUALS(array[3], -11);
TS_ASSERT_EQUALS(array.size(), (unsigned int)4);
}
void test_push_back() {
Common::Array<int> array1, array2;
// Some data for both
array1.push_back(-3);
array1.push_back(5);
array1.push_back(9);
array2.push_back(3);
array2.push_back(-2);
array2.push_back(-131);
array1.push_back(array2);
TS_ASSERT_EQUALS(array1[0], -3);
TS_ASSERT_EQUALS(array1[1], 5);
TS_ASSERT_EQUALS(array1[2], 9);
TS_ASSERT_EQUALS(array1[3], 3);
TS_ASSERT_EQUALS(array1[4], -2);
TS_ASSERT_EQUALS(array1[5], -131);
TS_ASSERT_EQUALS(array1.size(), (unsigned int)6);
TS_ASSERT_EQUALS(array2.size(), (unsigned int)3);
}
struct SafeInt {
int val;
SafeInt() : val(0) {}
SafeInt(int v) : val(v) {}
~SafeInt() { val = -1; }
bool operator==(int v) {
return val == v;
}
};
void test_push_back_ex() {
// This test makes sure that inserting an element invalidates
// references/iterators/pointers to elements in the array itself
// only *after* their value has been copied.
Common::Array<SafeInt> array;
array.push_back(42);
for (int i = 0; i < 40; ++i) {
array.push_back(array[0]);
TS_ASSERT_EQUALS(array[i], 42);
}
}
void test_copy_constructor() {
Common::Array<int> array1;
// Some data for both
array1.push_back(-3);
array1.push_back(5);
array1.push_back(9);
Common::Array<int> array2(array1);
TS_ASSERT_EQUALS(array2[0], -3);
TS_ASSERT_EQUALS(array2[1], 5);
TS_ASSERT_EQUALS(array2[2], 9);
TS_ASSERT_EQUALS(array2.size(), (unsigned int)3);
}
void test_equals() {
Common::Array<int> array1;
// Some data for both
array1.push_back(-3);
array1.push_back(5);
array1.push_back(9);
Common::Array<int> array2(array1);
TS_ASSERT(array1 == array2);
array1.push_back(42);
TS_ASSERT(array1 != array2);
array2.push_back(42);
TS_ASSERT(array1 == array2);
}
void test_array_constructor() {
const int array1[] = { -3, 5, 9 };
Common::Array<int> array2(array1, 3);
TS_ASSERT_EQUALS(array2[0], -3);
TS_ASSERT_EQUALS(array2[1], 5);
TS_ASSERT_EQUALS(array2[2], 9);
TS_ASSERT_EQUALS(array2.size(), (unsigned int)3);
}
void test_array_constructor_str() {
const char *array1[] = { "a", "b", "c" };
Common::StringList array2(array1, 3);
TS_ASSERT_EQUALS(array2[0], "a");
TS_ASSERT_EQUALS(array2[1], "b");
TS_ASSERT_EQUALS(array2[2], "c");
TS_ASSERT_EQUALS(array2.size(), (unsigned int)3);
}
void test_front_back_push_pop() {
Common::Array<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_back();
TS_ASSERT_EQUALS(container.front(), -17);
TS_ASSERT_EQUALS(container.back(), -17);
}
void test_resize() {
Common::Array<int> array;
array.resize(3);
TS_ASSERT_EQUALS(array.size(), (unsigned int)3);
array[0] = -3;
array[1] = 163;
array[2] = 17;
array.resize(100);
TS_ASSERT_EQUALS(array.size(), (unsigned int)100);
TS_ASSERT_EQUALS(array[0], -3);
TS_ASSERT_EQUALS(array[1], 163);
TS_ASSERT_EQUALS(array[2], 17);
TS_ASSERT_EQUALS(array[99], 0);
array.resize(2);
TS_ASSERT_EQUALS(array.size(), (unsigned int)2);
TS_ASSERT_EQUALS(array[0], -3);
TS_ASSERT_EQUALS(array[1], 163);
}
};
|