aboutsummaryrefslogtreecommitdiff
path: root/tests/irrArray.cpp
blob: 176ba9b2e907e17590c3b7cde9466868459fcc81 (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
// Copyright (C) 2008-2011 Colin MacDonald
// No rights reserved: this software is in the public domain.

#include "testUtils.h"
#include <irrlicht.h>
#include <assert.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( result );

	return result;
}

// Test the functionality of core::array
bool testIrrArray(void)
{
	bool allExpected = true;

	logTestString("crashTestFastAlloc\n");
	crashTestFastAlloc();
	allExpected &= testSelfAssignment();
	allExpected &= testSwap();
	allExpected &= testErase();

	if(allExpected)
		logTestString("\nAll tests passed\n");
	else
		logTestString("\nFAIL!\n");

	return allExpected;
}