aboutsummaryrefslogtreecommitdiff
path: root/tests/irrString.cpp
blob: 9503252bb95cae31b8d4328c3f3ea24eb676c25f (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
// 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;

static bool testSelfAssignment()
{
	core::stringw myString(L"foo");
	myString = myString;
	return myString == core::stringw(L"foo");
}

static bool testSplit()
{
	logTestString("Test stringw::split()\n");
	core::stringw teststring(L"[b]this [/b] is a [color=0xff000000]test[/color].");
	core::list<core::stringw> parts1;
	teststring.split<core::list<core::stringw> >(parts1, L"[");
	core::list<core::stringw> parts2;
	teststring.split<core::list<core::stringw> >(parts2, L"[", 1, false, true);
	return (parts1.getSize()==4) && (parts2.getSize()==5);
}

static bool testFastAlloc()
{
	core::string<wchar_t, core::irrAllocatorFast<wchar_t> > FastString(L"abc");
	core::string<wchar_t, core::irrAllocatorFast<wchar_t> > FastStringLong(L"longer");

	FastString  = L"test";

	// cause a reallocation
	FastString = FastStringLong;

	// this test should either not compile or crash when the allocaters are messed up
	return true;
}


bool testAppendStringc()
{
	core::stringc str;
	// Test with character
	if (str != "")
		return false;
	str += 'W';
	if (str != "W")
		return false;
	str += 'i';
	if (str != "Wi")
		return false;
	str="";
	if (str != "")
		return false;

	// Test with C-style string
	str += "Another Test";
	if (str != "Another Test")
		return false;
	str="";
	str += 'A';
	str += "nother Test";
	if (str != "Another Test")
		return false;
	str="";

	// Test with int
	str += 10;
	if (str != "10")
		return false;
	str += 0;
	if (str != "100")
		return false;
	str="";
	str += "-32";
	if (str != "-32")
		return false;
	str="";

	// Test with unsigned int
	str += 21u;
	if (str != "21")
		return false;
	str += 0u;
	if (str != "210")
		return false;
	str="";

	// Test with long int
	str += 456l;
	if (str != "456")
		return false;
	str += 0l;
	if (str != "4560")
		return false;
	str="";
	str += -456l;
	if (str != "-456")
		return false;
	str="";

	// Test with unsigned long
	str += 994ul;
	if (str != "994")
		return false;
	str += 0ul;
	if (str != "9940")
		return false;
	str="";
	return true;
}


// Test the functionality of irrString
/** Validation is done with asserts() against expected results. */
bool testIrrString(void)
{
	bool allExpected = true;

	logTestString("Test stringc\n");
	{
		// Check empty string
		core::stringc empty;
		assert(empty.size()==0);
		assert(empty[0]==0);
		assert(empty.c_str()!=0);
		assert(*(empty.c_str())==0);
		// Assign content
		empty = "Test";
		assert(empty.size()==4);
		assert(empty[0]=='T');
		assert(empty[3]=='t');
		assert(*(empty.c_str())=='T');
		//Assign empty string, should be same as in the beginning
		empty = "";
		assert(empty.size()==0);
		assert(empty[0]==0);
		assert(*(empty.c_str())==0);
	}
	logTestString("Test stringw\n");
	{
		core::stringw empty;
		assert(empty.size()==0);
		assert(empty[0]==0);
		assert(empty.c_str()!=0);
		assert(*(empty.c_str())==0);
		empty = L"Test";
		assert(empty.size()==4);
		assert(empty[0]==L'T');
		assert(empty[3]=='t');
		assert(*(empty.c_str())==L'T');
		empty = L"";
		assert(empty.size()==0);
		assert(empty[0]==0);
		assert(*(empty.c_str())==0);
		assert(allExpected &= testSplit());
	}
	allExpected &= testAppendStringc();

	logTestString("Test io::path\n");
	{
		// Only test that this type exists, it's one from above
		io::path myPath;
		myPath = "Some text"; // Only to avoid wrong optimizations
	}

	logTestString("Test self assignment\n");
	allExpected &= testSelfAssignment();

	logTestString("test fast alloc\n");
	allExpected &= testFastAlloc();

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

	return allExpected;
}