summaryrefslogtreecommitdiff
path: root/Runtime/Utilities/UtilityTests.cpp
blob: daef426e9c553b7898595e33a2c8cf8181e95e1d (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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include "UnityPrefix.h"
#include "Configuration/UnityConfigure.h"

#if ENABLE_UNIT_TESTS

#include "External/UnitTest++/src/UnitTest++.h"

#include "Runtime/Utilities/LinkedList.h"
#include "Runtime/Utilities/MemoryPool.h"
#include "Runtime/Utilities/File.h"
#include <string.h>
#include "Runtime/Utilities/dynamic_bitset.h"
#include "Runtime/Utilities/dynamic_array.h"
#include "Runtime/Utilities/vector_set.h"
#include "Runtime/Utilities/ArrayUtility.h"
#include "Runtime/Allocator/FixedSizeAllocator.h"


#if !GAMERELEASE
#include "Runtime/Utilities/PathNameUtility.h"
#include "Runtime/Utilities/FileUtilities.h"
#include "Runtime/Utilities/GUID.h"
UnityGUID StringToGUID (const std::string& pathName);
static const char* kTempFolder = "Temp";
#endif


SUITE (UtilityTests)
{


TEST (TestMemoryPool)
{
	const int kBubble = 256;
	const int kBubbleSize = 64;

	// memory pool with 128 elements per bubble
	MemoryPool pool (true, "TestPool", kBubbleSize, kBubble * kBubbleSize);

	std::vector<void*> ptrs;

	// allocate 128, should be one bubble and 128 objects
	for (int i = 0; i < kBubble; ++i)
	{
		ptrs.push_back (pool.Allocate ());
	}
	CHECK_EQUAL (kBubble, pool.GetAllocCount ());
	CHECK_EQUAL (1, pool.GetBubbleCount ());

	// Allocate 1 more, should be 2 bubbles and kBubble+1 objects
	ptrs.push_back (pool.Allocate ());
	CHECK_EQUAL (kBubble + 1, pool.GetAllocCount ());
	CHECK_EQUAL (2, pool.GetBubbleCount ());

	// deallocate first bubble
	void* last = ptrs[kBubble];
	for (int i = 0; i < kBubble; ++i)
	{
		pool.Deallocate (ptrs[i]);
	}
	ptrs.clear ();
	ptrs.push_back (last);
	CHECK_EQUAL (1, pool.GetAllocCount());
	CHECK_EQUAL (2, pool.GetBubbleCount());

	// now we have two bubbles: 1st totally free, 2nd one with 1 allocation
	// allocate 255 more objects. Should all fit into existing bubbles!
	for (int i = 0; i < kBubble * 2 - 1; ++i)
	{
		ptrs.push_back (pool.Allocate ());
	}
	CHECK_EQUAL (kBubble * 2, pool.GetAllocCount ());
	CHECK_EQUAL (2, pool.GetBubbleCount ());

	// Allocate one more. Should cause additional bubble
	ptrs.push_back (pool.Allocate ());
	CHECK_EQUAL (kBubble * 2 + 1, pool.GetAllocCount ());
	CHECK_EQUAL (3, pool.GetBubbleCount ());

	// Deallocate all.
	for (int i = 0; i < ptrs.size (); ++i)
	{
		pool.Deallocate (ptrs[i]);
	}
	ptrs.clear ();
	CHECK_EQUAL (0, pool.GetAllocCount ());
	CHECK_EQUAL (3, pool.GetBubbleCount ());

	// now we have three totally free bubbles
	// allocate 3*128 objects. Should fit into existing bubbles!
	for (int i = 0; i < kBubble * 3; ++i)
	{
		ptrs.push_back (pool.Allocate ());
	}
	CHECK_EQUAL (kBubble * 3, pool.GetAllocCount ());
	CHECK_EQUAL (3, pool.GetBubbleCount ());

	// deallocate all from last bubble
	for (int i = kBubble * 2; i < kBubble * 3; ++i)
	{
		pool.Deallocate (ptrs[i]);
	}
	ptrs.resize (kBubble * 2);
	CHECK_EQUAL (kBubble * 2, pool.GetAllocCount ());
	CHECK_EQUAL (3, pool.GetBubbleCount ());

	// allocate one more
	ptrs.push_back (pool.Allocate ());
	CHECK_EQUAL (kBubble * 2 + 1, pool.GetAllocCount ());
	CHECK_EQUAL (3, pool.GetBubbleCount ());

	pool.DeallocateAll ();
	ptrs.clear ();
	CHECK_EQUAL (0, pool.GetAllocCount ());
	CHECK_EQUAL (0, pool.GetBubbleCount ());

	// Test pre-allocation: preallocate 1.5 worth of bubbles
	pool.PreallocateMemory (kBubble * kBubbleSize * 3/2);
	// should result in 2 bubbles
	CHECK_EQUAL (2, pool.GetBubbleCount ());
	for (int i = 0; i < kBubble * 2; ++i)
	{
		ptrs.push_back (pool.Allocate ());
	}
	CHECK_EQUAL (kBubble * 2, pool.GetAllocCount ());
	CHECK_EQUAL (2, pool.GetBubbleCount ());

	// Allocate one more, should create additional bubble
	ptrs.push_back (pool.Allocate ());
	CHECK_EQUAL (kBubble * 2 + 1, pool.GetAllocCount ());
	CHECK_EQUAL (3, pool.GetBubbleCount ());
	pool.DeallocateAll ();
}


#if !GAMERELEASE
TEST (TestPathExist)
{
	using namespace std;

	CreateDirectory (kTempFolder);

	string filePath = AppendPathName (kTempFolder, "TestIsFileCreated");
	DeleteFileOrDirectory (filePath);

	CHECK (!IsPathCreated (filePath));
	CHECK (!IsDirectoryCreated (filePath));
	CHECK (!IsFileCreated (filePath));

	CreateFile (filePath);

	CHECK (IsPathCreated (kTempFolder));
	CHECK (IsDirectoryCreated (kTempFolder));
	CHECK (!IsFileCreated (kTempFolder));

	CHECK (IsPathCreated (filePath));
	CHECK (!IsDirectoryCreated (filePath));
	CHECK (IsFileCreated (filePath));
}
#endif


TEST (DynamicArray)
{
	// no allocation for empty array
	dynamic_array<int> array;
	CHECK_EQUAL (0, array.capacity ());

	// push_back allocates
	int j = 1;
	array.push_back (j);
	CHECK_EQUAL (1, array.size ());
	CHECK (array.capacity () > 0);

	// push_back(void)
	int& i = array.push_back ();
	i = 666;
	CHECK_EQUAL(666, array.back ());

	// clear frees memory?
	array.clear ();
	CHECK_EQUAL (0, array.size ());
	CHECK_EQUAL (0, array.capacity ());

	// 3 item list
	j = 6;
	array.push_back (j);
	j = 7;
	array.push_back (j);
	j = 8;
	array.push_back (j);

	CHECK_EQUAL (3, array.size ());

	// swapping
	dynamic_array<int> ().swap (array);
	CHECK_EQUAL (0, array.capacity ());
	CHECK_EQUAL (0, array.size ());

	// reserve
	array.reserve (1024);
	CHECK_EQUAL (1024, array.capacity ());
	CHECK_EQUAL (0, array.size ());

	// copy assignment
	dynamic_array<int> array1;
	j = 888;
	array1.push_back (j);

	array = array1;

	CHECK_EQUAL (1, array.size ());
	CHECK_EQUAL (888, array.back ());
	CHECK_EQUAL (1, array1.size ());
	CHECK_EQUAL (888, array1.back ());
}

	struct Stuff
	{
		int value;
		int identifier;

		bool operator < (const Stuff& rhs) const
		{
			return value < rhs.value;
		}

		Stuff (int a, int b) { value = a; identifier = b; }
	};


TEST (Test_vector_set_assign_clear_duplicates)
{
	// Make sure that duplicates are removed,
	// but also that only the first same instance is maintained, the following ones are killed
	Stuff input[] = { Stuff (10, 0), Stuff (11, 1), Stuff (3, 2), Stuff (3, 3), Stuff (4, 4), Stuff (10, 5) };
	Stuff output[] = { Stuff (3, 2), Stuff (4, 4), Stuff (10, 0), Stuff (11, 1) };

	vector_set<Stuff> test_set;
	test_set.assign_clear_duplicates(input, input + ARRAY_SIZE(input));

	CHECK_EQUAL(test_set.size(), ARRAY_SIZE(output));
	for (int i=0;i<ARRAY_SIZE(output);i++)
	{
		CHECK_EQUAL(output[i].value, test_set[i].value);
		CHECK_EQUAL(output[i].identifier, test_set[i].identifier);
	}
}

class TestNode : public ListElement
{
};

TEST (TestList)
{
	typedef List<TestNode> ListType;

	struct
	{
		void operator () (ListType& list, TestNode* nodes[], int count)
		{
			CHECK_EQUAL (count, list.size_slow ());
			int c = 0;
			for (ListType::iterator i = list.begin (); i != list.end (); ++i)
			{
				CHECK (nodes[c] == &*i);
				++c;
			}

			CHECK_EQUAL (count, c);
		}
	} CheckNodes;

	ListType list, emptyList, emptyList2;

	CHECK_EQUAL (0, emptyList.size_slow ());
	emptyList.clear ();
	CHECK_EQUAL (0, emptyList.size_slow ());

	TestNode* nodes[] =
	{
		new TestNode (),
		new TestNode (),
		new TestNode (),
		new TestNode (),
		new TestNode (),
		new TestNode ()
	};

	emptyList.swap (emptyList2);
	CHECK_EQUAL (0, emptyList.size_slow ());
	CHECK_EQUAL (0, emptyList2.size_slow ());

	// insertion and pushback
	list.push_back (*nodes[1]);
	list.insert (nodes[1], *nodes[0]);
	list.push_back (*nodes[2]);
	list.push_back (*nodes[3]);
	list.push_back (*nodes[5]);
	list.insert (nodes[5], *nodes[4]);
	CheckNodes (list, nodes, 6);
	// insert before self
	list.insert (list.begin(), *list.begin ());
	CheckNodes (list, nodes, 6);

	list.append (emptyList);
	CHECK_EQUAL (0, emptyList.size_slow ());
	CheckNodes (list, nodes, 6);

	// append remove into something empty
	emptyList.append (list);
	CHECK_EQUAL (0, list.size_slow ());
	CheckNodes (emptyList, nodes, 6);

	// invert operation by doing a swap
	emptyList.swap (list);
	CHECK_EQUAL (0, emptyList.size_slow ());
	CheckNodes (list, nodes, 6);

	// Create another list to test copying
	TestNode* nodes2[] =
	{
		new TestNode (),
		new TestNode (),
		new TestNode ()
	};
	ListType list2;
	list2.push_back (*nodes2[1]);
	list2.push_front (*nodes2[0]);
	list2.push_back (*nodes2[2]);
	CheckNodes (list2, nodes2, 3);

	// swap back and forth
	list2.swap (list);
	CheckNodes (list, nodes2, 3);
	CheckNodes (list2, nodes, 6);
	list.swap (list2);
	CheckNodes (list, nodes, 6);
	CheckNodes (list2, nodes2, 3);

	list.append (list2); // insert before self
	int c = 0;
	for (ListType::iterator i = list.begin (); i != list.end (); ++i)
	{
		if (c >= 6)
			CHECK (nodes2[c - 6] == &*i);
		else
			CHECK (nodes[c] == &*i);
		++c;
	}
	CHECK_EQUAL (9, list.size_slow ());
	CHECK_EQUAL (0, list2.size_slow ());
	CHECK_EQUAL (9, c);

	emptyList.append (emptyList2);
	CHECK_EQUAL (0, emptyList2.size_slow ());
	CHECK_EQUAL (0, emptyList.size_slow ());
}

TEST (DynamicBitSet)
{
	dynamic_bitset set;
	UInt32 block = 1 << 0 | 1 << 3 | 1 << 5;
	set.resize (6);
	from_block_range (&block, &block + 1, set);

	CHECK_EQUAL (true, set.test (0));
	CHECK_EQUAL (false, set.test (1));
	CHECK_EQUAL (false, set.test (2));
	CHECK_EQUAL (true, set.test (3));
	CHECK_EQUAL (false, set.test (4));
	CHECK_EQUAL (true, set.test (5));

	to_block_range (set, &block);
	bool res;

	res = block & (1 << 0);
	CHECK_EQUAL (true, res);
	res = block & (1 << 1);
	CHECK_EQUAL (false, res);
	res = block & (1 << 2);
	CHECK_EQUAL (false, res);
	res = block & (1 << 3);
	CHECK_EQUAL (true, res);
	res = block & (1 << 4);
	CHECK_EQUAL (false, res);
	res = block & (1 << 5);
	CHECK_EQUAL (true, res);
}


TEST (DynamicArrayMisc)
{
	// no allocation for empty array
	dynamic_array<int> array;
	CHECK_EQUAL (0, array.capacity ());
	CHECK (array.owns_data ());
	CHECK (array.begin () == array.end ());
	CHECK (array.empty ());

	// push_back allocates
	int j = 1;
	array.push_back (j);
	CHECK_EQUAL (1, array.size ());
	CHECK (array.capacity () > 0);

	// push_back(void)
	int& i = array.push_back ();
	i = 666;
	CHECK_EQUAL(666, array.back ());

	// clear frees memory?
	array.clear ();
	CHECK_EQUAL (0, array.size ());
	CHECK_EQUAL (0, array.capacity ());

	// 3 item list
	array.push_back (6);
	array.push_back (7);
	array.push_back (8);

	CHECK_EQUAL (3, array.size ());

	// swapping
	dynamic_array<int> ().swap (array);
	CHECK_EQUAL (0, array.capacity ());
	CHECK_EQUAL (0, array.size ());

	// reserve
	array.reserve (1024);
	CHECK_EQUAL (1024, array.capacity ());
	CHECK_EQUAL (0, array.size ());

	// copy assignment
	dynamic_array<int> array1;
	j = 888;
	array1.push_back (j);

	array = array1;

	CHECK_EQUAL (1, array.size ());
	CHECK_EQUAL (888, array.back ());
	CHECK_EQUAL (1, array1.size ());
	CHECK_EQUAL (888, array1.back ());
}


TEST (DynamicArrayErase)
{
	dynamic_array<int> vs;
	vs.resize_uninitialized(5);

	vs[0] = 0;
	vs[1] = 1;
	vs[2] = 2;
	vs[3] = 3;
	vs[4] = 4;

	vs.erase(vs.begin() + 1, vs.begin() + 4);
	CHECK_EQUAL (2, vs.size());
	CHECK_EQUAL (5, vs.capacity());
	CHECK_EQUAL (0, vs[0]);
	CHECK_EQUAL (4, vs[1]);
}

static void VerifyConsecutiveIntArray (dynamic_array<int>& vs, int size, int capacity)
{
	CHECK_EQUAL (capacity, vs.capacity());
	CHECK_EQUAL (size, vs.size());
	for (int i=0;i<vs.size();i++)
		CHECK_EQUAL (i, vs[i]);
}

TEST (DynamicArrayInsertOnEmpty)
{
	dynamic_array<int> vs;
	int vals[] = { 0, 1 };

	vs.insert(vs.begin(), vals, vals + ARRAY_SIZE(vals));

	VerifyConsecutiveIntArray(vs, 2, 2);
}


TEST (DynamicArrayInsert)
{
	dynamic_array<int> vs;
	vs.resize_uninitialized(5);

	vs[0] = 0;
	vs[1] = 1;
	vs[2] = 4;
	vs[3] = 5;
	vs[4] = 6;

	int vals[] = { 2, 3 };

	// inser two values
	vs.insert(vs.begin() + 2, vals, vals + ARRAY_SIZE(vals));
	VerifyConsecutiveIntArray(vs, 7, 7);

	// empty insert
	vs.insert(vs.begin() + 2, vals, vals);

	VerifyConsecutiveIntArray(vs, 7, 7);
}

TEST (DynamicArrayResize)
{
	dynamic_array<int> vs;
	vs.resize_initialized(3, 2);
	CHECK_EQUAL (3, vs.capacity());
	CHECK_EQUAL (3, vs.size());
	CHECK_EQUAL (2, vs[0]);
	CHECK_EQUAL (2, vs[1]);
	CHECK_EQUAL (2, vs[2]);

	vs.resize_initialized(6, 3);
	CHECK_EQUAL (6, vs.capacity());
	CHECK_EQUAL (6, vs.size());
	CHECK_EQUAL (2, vs[0]);
	CHECK_EQUAL (2, vs[1]);
	CHECK_EQUAL (2, vs[2]);
	CHECK_EQUAL (3, vs[3]);
	CHECK_EQUAL (3, vs[4]);
	CHECK_EQUAL (3, vs[5]);

	vs.resize_initialized(5, 3);
	CHECK_EQUAL (6, vs.capacity());
	CHECK_EQUAL (5, vs.size());
	CHECK_EQUAL (2, vs[0]);
	CHECK_EQUAL (2, vs[1]);
	CHECK_EQUAL (2, vs[2]);
	CHECK_EQUAL (3, vs[3]);
	CHECK_EQUAL (3, vs[4]);

	vs.resize_initialized(2, 3);
	CHECK_EQUAL (6, vs.capacity());
	CHECK_EQUAL (2, vs.size());
	CHECK_EQUAL (2, vs[0]);
	CHECK_EQUAL (2, vs[1]);
}


TEST(FixedSizeAllocator)
{
	typedef FixedSizeAllocator<sizeof(int)> TestAllocator;

	TestAllocator testalloc = TestAllocator(MemLabelId(kMemDefaultId, NULL));

	CHECK(testalloc.capacity() == 0);
	CHECK(testalloc.total_free() == 0);
	CHECK(testalloc.total_allocated() == 0);

	int* ptr1 = (int*)testalloc.alloc();
	*ptr1 = 1;

	CHECK(testalloc.capacity() == 255*sizeof(int));
	CHECK(testalloc.total_free() == 254*sizeof(int));
	CHECK(testalloc.total_allocated() == sizeof(int));

	int* ptr2 = (int*)testalloc.alloc();
	*ptr2 = 2;

	CHECK(testalloc.capacity() == 255*sizeof(int));
	CHECK(testalloc.total_free() == 253*sizeof(int));
	CHECK(testalloc.total_allocated() == 2*sizeof(int));
	CHECK(*ptr1==1);
	CHECK(ptr1 + 1 == ptr2);

	testalloc.reset();

	CHECK(testalloc.capacity() == 255*sizeof(int));
	CHECK(testalloc.total_free() == 255*sizeof(int));
	CHECK(testalloc.total_allocated() == 0);

	testalloc.free_memory();

	CHECK(testalloc.capacity() == 0);
	CHECK(testalloc.total_free() == 0);
	CHECK(testalloc.total_allocated() == 0);
}



TEST(StringFormatTest)
{
	CHECK_EQUAL ("Hello world it works", Format ("Hello %s it %s", "world", "works"));
}


#if !GAMERELEASE
TEST(UnityGUIDTest)
{
	UnityGUID identifier[5];
	identifier[0].Init ();
	identifier[1].Init ();
	identifier[2].Init ();
	identifier[3].Init ();
	identifier[4].Init ();

	CHECK (identifier[0] != identifier[1]);
	CHECK (identifier[1] != identifier[2]);
	CHECK (identifier[2] != identifier[3]);
	CHECK (identifier[3] != identifier[4]);
	identifier[0] = identifier[1];
	CHECK (identifier[0] == identifier[1]);
}
#endif


TEST (TestUtility)
{
	using namespace std;

	// Make sure that our stl implementation has consistent clear behaviour.
	// If it doesn't we should probably stop using clear.
	//
	// If this test fails, it means that std::vector clear deallocates memory.
	// Some optimized code this to not be the case!

	vector<int> test;
	test.resize (10);
	test.clear ();
	CHECK (test.capacity () != 0);
}

}
#endif