aboutsummaryrefslogtreecommitdiff
path: root/src/unit/array.cpp
blob: f7dc0ae3e9da991998cfa9567915b69eb507d81b (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
/*
 * Copyright (C) 2007-2008 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/unitsuite.h"
#include "bu/hash.h"
#include "bu/array.h"

class Unit : public Bu::UnitSuite
{
public:
	Unit()
	{
		setName("Array");
		addTest( Unit::general );
		addTest( Unit::iterate1 );
		addTest( Unit::iterate2 );
		addTest( Unit::copy );
	}

	virtual ~Unit()
	{
	}

	void general()
	{
		Bu::Array<int> ai;

		ai.append( 5 );
		ai.append( 10 );
		unitTest( ai.getSize() == 2 );
		unitTest( ai.getCapacity() == 10 );
		unitTest( ai[0] == 5 );
		unitTest( ai[1] == 10 );
	}

	void iterate1()
	{
		Bu::Array<int> ai;
		for( int j = 0; j < 10; j++ )
			ai.append( j );

		int j = 0;
		for( Bu::Array<int>::iterator i = ai.begin(); i != ai.end(); i++ )
			unitTest( (*i) == j++ );
		
		const Bu::Array<int> &ci = ai;
		j = 0;
		for( Bu::Array<int>::const_iterator i = ci.begin(); i != ci.end(); i++ )
			unitTest( (*i) == j++ );
	}

	void iterate2()
	{
		Bu::Array<int> ai;
		for( Bu::Array<int>::iterator i = ai.begin(); i != ai.end(); i++ )
			unitFailed("Empty lists shouldn't be iterated through.");
	}

	void copy()
	{
		typedef Bu::Hash<Bu::FString, Bu::FString> StrHash;
		typedef Bu::Array<StrHash> StrHashArray;

		StrHash h1;
		h1["Hi"] = "Yo";
		h1["Bye"] = "Later";

		StrHash h2;
		h2["Test"] = "Bloop";
		h2["Foo"] = "ooF";

		StrHashArray a1;
		a1.append( h1 );
		a1.append( h2 );

		StrHashArray a2(a1);

		unitTest( a2[0].get("Hi") == "Yo" );
		unitTest( a2[0].get("Bye") == "Later" );
		unitTest( a2[1].get("Test") == "Bloop" );
		unitTest( a2[1].get("Foo") == "ooF" );

		StrHashArray a3;
		a3 = a1;

		unitTest( a3[0].get("Hi") == "Yo" );
		unitTest( a3[0].get("Bye") == "Later" );
		unitTest( a3[1].get("Test") == "Bloop" );
		unitTest( a3[1].get("Foo") == "ooF" );
	}
};

int main( int argc, char *argv[] )
{
	return Unit().run( argc, argv );
}