File: test_EntityMap.h

package info (click to toggle)
0ad 0.27.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 173,296 kB
  • sloc: cpp: 194,003; javascript: 19,098; ansic: 15,066; python: 6,328; sh: 1,699; perl: 1,575; java: 533; xml: 482; php: 192; makefile: 99
file content (312 lines) | stat: -rw-r--r-- 7,649 bytes parent folder | download | duplicates (3)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* Copyright (C) 2024 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "lib/self_test.h"
#include "lib/timer.h"

#include "simulation2/serialization/ISerializer.h"
#include "simulation2/serialization/IDeserializer.h"

#include "simulation2/system/EntityMap.h"

#include <algorithm>
#include <random>

class TestEntityMap : public CxxTest::TestSuite
{
public:
	void setUp()
	{
	}

	void tearDown()
	{
	}

	void test_insert()
	{
		EntityMap<int> test;

		TS_ASSERT(test.empty());

		test.insert(1,1);
		test.insert(2,2);
		test.insert(3,3);
		test.insert(4,4);
		test.insert(4,5);
		test.insert(4,6);

//		TS_ASSERT(test.m_Data.size() == 5);
//		TS_ASSERT(test.m_Data.back().first != INVALID_ENTITY);
		TS_ASSERT(test.size() == 4);
		TS_ASSERT(test.find(3)->second == 3);
		TS_ASSERT(test.find(4)->second == 6);

		test.insert(10,7);
//		TS_ASSERT(test.m_Data.size() == 11);
//		TS_ASSERT(test.m_Data.back().first != INVALID_ENTITY);
		TS_ASSERT(test.size() == 5);
		TS_ASSERT(test.find(4)->second == 6);
		TS_ASSERT(test.find(5) == test.end());
		TS_ASSERT(test.find(6) == test.end());
		TS_ASSERT(test.find(7) == test.end());
		TS_ASSERT(test.find(8) == test.end());
		TS_ASSERT(test.find(9) == test.end());
		TS_ASSERT(test.find(10)->second == 7);

//		EntityMap<int, 5> test2;

//		test2.insert(8,5);
//		TS_ASSERT(test2.find(8)->second == 5);
//		TS_ASSERT(test2.m_Data.size() == 5);
//		TS_ASSERT(test2.size() == 1);

	}
	void test_iterators()
	{
		EntityMap<int> test;

		test.insert(1,1);
		test.insert(2,2);
		test.insert(3,3);
		test.insert(4,4);

		EntityMap<int>::iterator it = test.begin();
		TS_ASSERT(it->first == 1);
		++it;
		TS_ASSERT(it->first == 2);
		++it; // it++ incorrectly returns a pointer in svn
		TS_ASSERT(it->first == 3);
		it = test.end();
//		TS_ASSERT(it->first == test.m_Data.back().first);

		EntityMap<int>::const_iterator cit = test.begin();
		TS_ASSERT(cit->first == 1);
		cit = test.end();
//		TS_ASSERT(cit->first == test.m_Data.back().first);

		size_t iter = 0;
		for (EntityMap<int>::value_type& v : test)
		{
			++iter;
			TS_ASSERT(test.find(iter)->second == (int)iter);
			TS_ASSERT(test.find(iter)->second == v.second);
		}
		TS_ASSERT(iter == 4);

		test.clear();

		test.insert(10,1);
		test.insert(20,2);
		test.insert(30,3);
		test.insert(40,4);

		it = test.begin();
		TS_ASSERT(it->second == 1);
		++it; // it++ incorrectly returns a pointer in svn
		TS_ASSERT(it->second == 2);
		++it;
		TS_ASSERT(it->second == 3);
		it = test.end();
//		TS_ASSERT(it->first == test.m_Data.back().first);

	}

	void test_erase()
	{
		EntityMap<int> test;
		test.insert(1,1);
		test.insert(2,2);
		test.insert(3,3);
		test.insert(4,4);

		test.erase(2);

//		TS_ASSERT(test.m_Data.size() == 5);
		TS_ASSERT(test.size() == 3);
		TS_ASSERT(test.find(2) == test.end());

		test.erase(1);
		test.erase(3);
		test.erase(4);//		TS_ASSERT(test.erase(4) == 1);

//		TS_ASSERT(test.m_Data.size() == 5);
		TS_ASSERT(test.size() == 0);

		test.erase(5);//		TS_ASSERT(test.erase(5) == 0);

		test.insert(1,1);
		test.insert(2,2);
		test.insert(3,3);
		test.insert(4,4);

		test.erase(test.begin());
//		TS_ASSERT(test.m_Data.size() == 5);
		TS_ASSERT(test.size() == 3);
		TS_ASSERT(test.find(1) == test.end());

//		TS_ASSERT(test.erase(test.end()) == 0);
//		TS_ASSERT(test.m_Data.back().first != INVALID_ENTITY);
	}

	void test_clear()
	{
		EntityMap<int> test;
		test.insert(1,1);
		test.insert(2,2);
		test.insert(3,3);
		test.insert(4,4);

		test.clear();

//		TS_ASSERT(test.m_Data.size() == 1);
		TS_ASSERT(test.size() == 0);
	}

	void test_find()
	{
		EntityMap<int> test;
		test.insert(1,1);
		test.insert(2,2);
		test.insert(3,3);
		test.insert(40,4);

		TS_ASSERT(test.find(1)->second == 1);
		TS_ASSERT(test.find(40)->second == 4);
		TS_ASSERT(test.find(30) == test.end());
	}

	void DISABLE_test_perf()
	{
		EntityMap<int> test;
		printf("Testing performance of EntityMap\n");

		double t = timer_Time();
		for (int i = 1; i <= 200000; ++i)
			test.insert(i,i);
		double tt = timer_Time() - t;
		printf("inserting 200K elements in order: %lfs\n", tt);

		t = timer_Time();
		for (int i = 1; i <= 200000; ++i)
			test.erase(i);
		tt = timer_Time() - t;
		printf("Erasing 200K elements, by key: %lfs\n", tt);

		t = timer_Time();
		for (int i = 200000; i >= 1; --i)
			test.insert(i,i);
		tt = timer_Time() - t;
		printf("inserting 200K elements in reverse order: %lfs\n", tt);

		t = timer_Time();
		for (auto i = test.begin(); i != test.end(); ++i)
			test.erase(i);
		tt = timer_Time() - t;
		printf("Erasing 200K elements, by iterator: %lfs\n", tt);

		t = timer_Time();
		for (int i = 1; i <= 200000; ++i)
			test.erase(i);
		tt = timer_Time() - t;
		printf("Erasing 200K non-existing elements: %lfs\n", tt);

		// prep random vector
		std::vector<int> vec;
		for (int i = 1; i <= 200000; ++i)
			vec.push_back(i);

		std::shuffle(vec.begin(), vec.end(), std::mt19937{});

		for (int i = 1; i <= 200000; ++i)
			test.insert(i,i);

		t = timer_Time();
		for (int i = 1; i <= 200000; ++i)
			test.find(vec[i])->second = 3;
		tt = timer_Time() - t;
		printf("200K random lookups in random order: %lfs\n", tt);

		t = timer_Time();
		for (auto& p : test)
			p.second = 3;
		tt = timer_Time() - t;
		printf("auto iteration on 200K continuous entitymap: %lfs\n", tt);

		test.clear();

		for (int i = 1; i <= 200000; ++i)
			test.insert(i*5,i);

		t = timer_Time();
		for (EntityMap<int>::value_type& p : test)
			p.second = 3;
		tt = timer_Time() - t;
		printf("auto iteration on 200K sparse (holes of 5): %lfs\n", tt);

		test.clear();

		for (int i = 1; i <= 4000; ++i)
			test.insert(i*50,i);

		t = timer_Time();
		for (EntityMap<int>::value_type& p : test)
			p.second = 3;
		tt = timer_Time() - t;
		printf("auto iteration on 4K sparse (holes of 50): %lfs\n", tt);

		test.clear();

		for (int i = 1; i <= 200000; ++i)
			test.insert(i*50,i);

		t = timer_Time();
		for (EntityMap<int>::value_type& p : test)
			p.second = 3;
		tt = timer_Time() - t;
		printf("auto iteration on 200K sparse (holes of 50): %lfs\n", tt);

		test.clear();

		for (int i = 1; i <= 2000000; ++i)
			test.insert(i*50,i);

		t = timer_Time();
		for (EntityMap<int>::iterator i = test.begin(); i != test.end(); ++i)
			i->second = 3;
		tt = timer_Time() - t;
		printf("manual ++iteration on 2000K sparse (holes of 50) (warmup 1): %lfs\n", tt);

		t = timer_Time();
		for (EntityMap<int>::iterator i = test.begin(); i != test.end(); ++i)
			i->second = 3;
		tt = timer_Time() - t;
		printf("manual ++iteration on 2000K sparse (holes of 50) (warmup 2): %lfs\n", tt);

		t = timer_Time();
		for (EntityMap<int>::iterator i = test.begin(); i != test.end(); ++i)
			i->second = 3;
		tt = timer_Time() - t;
		printf("manual ++iteration on 2000K sparse (holes of 50): %lfs\n", tt);

		t = timer_Time();
		for (EntityMap<int>::iterator i = test.begin(); i != test.end(); ++i)
			i->second = 3;
		tt = timer_Time() - t;
		printf("manual iteration++ on 2000K sparse (holes of 50): %lfs\n", tt);
	}
};