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
|
// Copyright (C) 2008-2012 Colin MacDonald
// No rights reserved: this software is in the public domain.
#include "testUtils.h"
#include <irrlicht.h>
using namespace irr;
using namespace core;
core::map<int, int> countReferences;
struct SDummy
{
SDummy(int a) : x(a)
{
countReferences.insert(x,1);
}
SDummy() : x(0)
{
countReferences.insert(x,1);
}
SDummy(const SDummy& other)
{
x = other.x;
countReferences[x] = countReferences[x] + 1;
}
~SDummy()
{
countReferences[x] = countReferences[x] - 1;
}
int x;
};
static bool testErase()
{
{
core::array<SDummy> aaa;
aaa.push_back(SDummy(0));
aaa.push_back(SDummy(1));
aaa.push_back(SDummy(2));
aaa.push_back(SDummy(3));
aaa.push_back(SDummy(4));
aaa.push_back(SDummy(5));
aaa.erase(0,2);
}
for ( core::map<int,int>::Iterator it = countReferences.getIterator(); !it.atEnd(); it++ )
{
if ( it->getValue() != 0 )
{
logTestString("testErase: wrong count for %d, it's: %d\n", it->getKey(), it->getValue());
return false;
}
}
return true;
}
struct VarArray
{
core::array < int, core::irrAllocatorFast<int> > MyArray;
};
static bool testSelfAssignment()
{
core::array<int> myArray;
myArray.push_back(1);
myArray = myArray;
return myArray.size() == 1;
}
// this will (did once) crash when wrong due to deallocating memory twice, so no return value
static void crashTestFastAlloc()
{
core::array < VarArray, core::irrAllocatorFast<VarArray> > ArrayArray;
ArrayArray.setAllocStrategy(core::ALLOC_STRATEGY_SAFE); // force more re-allocations
VarArray var;
var.MyArray.setAllocStrategy(core::ALLOC_STRATEGY_SAFE); // force more re-allocations
var.MyArray.push_back( 0 );
for ( int i=0; i< 100; ++i )
{
ArrayArray.push_back(var);
ArrayArray.push_back(var);
}
}
static bool testSwap()
{
bool result = true;
core::array<int> array1, array2, copy1, copy2;
for ( int i=0; i<99; ++i )
{
array1.push_back(i);
if ( i < 10 ) // we want also different container sizes
array2.push_back(99-i);
}
copy1 = array1;
copy2 = array2;
array1.swap(array2);
result &= (array1 == copy2);
result &= (array2 == copy1);
assert_log( result );
return result;
}
// add numbers to the array going down from size to 1
static void addInvNumbers(irr::core::array<int>& arr, irr::u32 size)
{
for ( irr::u32 i=0; i<size; ++i )
{
arr.push_back(size-i);
}
}
// Ensure numbers are sorted in ascending order
static bool validateSortedAscending(const irr::core::array<int>& arr)
{
for ( irr::u32 i=1; i< arr.size(); ++ i)
{
if ( arr[i-1] > arr[i] )
return false;
}
return true;
}
static bool testSort()
{
irr::core::array<int> arr;
for ( irr::u32 i=0; i<1000; ++i )
{
arr.clear();
addInvNumbers(arr, i);
arr.sort();
if ( !validateSortedAscending(arr) )
{
return false;
}
}
for ( irr::u32 i=0; i<1000; ++i )
{
arr.clear();
addInvNumbers(arr, i);
addInvNumbers(arr, i);
arr.sort();
if ( !validateSortedAscending(arr) )
{
return false;
}
}
return true;
}
// Test the functionality of core::array
bool testIrrArray(void)
{
bool allExpected = true;
logTestString("crashTestFastAlloc\n");
crashTestFastAlloc();
allExpected &= testSelfAssignment();
allExpected &= testSwap();
allExpected &= testErase();
allExpected &= testSort();
if(allExpected)
logTestString("\nAll tests passed\n");
else
logTestString("\nFAIL!\n");
return allExpected;
}
|