1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://www.wtfpl.net/ for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include "core.h" |
---|
16 | #include "lol/unit.h" |
---|
17 | |
---|
18 | namespace lol |
---|
19 | { |
---|
20 | |
---|
21 | struct TrackedObj |
---|
22 | { |
---|
23 | static int m_ctor, m_dtor; |
---|
24 | |
---|
25 | TrackedObj() { m_ctor++; } |
---|
26 | TrackedObj(TrackedObj const &) { m_ctor++; } |
---|
27 | ~TrackedObj() { m_dtor++; } |
---|
28 | }; |
---|
29 | |
---|
30 | int TrackedObj::m_ctor = 0; |
---|
31 | int TrackedObj::m_dtor = 0; |
---|
32 | |
---|
33 | LOLUNIT_FIXTURE(ArrayTest) |
---|
34 | { |
---|
35 | void SetUp() {} |
---|
36 | |
---|
37 | void TearDown() {} |
---|
38 | |
---|
39 | LOLUNIT_TEST(ArrayPush) |
---|
40 | { |
---|
41 | Array<int> a; |
---|
42 | a.Push(0); |
---|
43 | a.Push(1); |
---|
44 | a.Push(2); |
---|
45 | a.Push(3); |
---|
46 | |
---|
47 | LOLUNIT_ASSERT_EQUAL(a[0], 0); |
---|
48 | LOLUNIT_ASSERT_EQUAL(a[1], 1); |
---|
49 | LOLUNIT_ASSERT_EQUAL(a[2], 2); |
---|
50 | LOLUNIT_ASSERT_EQUAL(a[3], 3); |
---|
51 | } |
---|
52 | |
---|
53 | LOLUNIT_TEST(ArrayPushWithShift) |
---|
54 | { |
---|
55 | Array<int> a; |
---|
56 | a << 0 << 1 << 2 << 3; |
---|
57 | |
---|
58 | LOLUNIT_ASSERT_EQUAL(a[0], 0); |
---|
59 | LOLUNIT_ASSERT_EQUAL(a[1], 1); |
---|
60 | LOLUNIT_ASSERT_EQUAL(a[2], 2); |
---|
61 | LOLUNIT_ASSERT_EQUAL(a[3], 3); |
---|
62 | } |
---|
63 | |
---|
64 | LOLUNIT_TEST(ArrayCopy) |
---|
65 | { |
---|
66 | Array<int> a; |
---|
67 | a << 0 << 1 << 2 << 3; |
---|
68 | |
---|
69 | Array<int> b = a; |
---|
70 | |
---|
71 | LOLUNIT_ASSERT_EQUAL(b[0], 0); |
---|
72 | LOLUNIT_ASSERT_EQUAL(b[1], 1); |
---|
73 | LOLUNIT_ASSERT_EQUAL(b[2], 2); |
---|
74 | LOLUNIT_ASSERT_EQUAL(b[3], 3); |
---|
75 | } |
---|
76 | |
---|
77 | LOLUNIT_TEST(ArrayRemove) |
---|
78 | { |
---|
79 | Array<int> a; |
---|
80 | a << 0 << 1 << 2 << 3; |
---|
81 | a.Remove(1); |
---|
82 | |
---|
83 | LOLUNIT_ASSERT_EQUAL(a[0], 0); |
---|
84 | LOLUNIT_ASSERT_EQUAL(a[1], 2); |
---|
85 | LOLUNIT_ASSERT_EQUAL(a[2], 3); |
---|
86 | } |
---|
87 | |
---|
88 | LOLUNIT_TEST(EightElements) |
---|
89 | { |
---|
90 | Array<int, long, float, double, unsigned, char, bool, void *> a; |
---|
91 | a.Push(1, 2, 3.f, 4.0, 5, 'a', true, 0); |
---|
92 | |
---|
93 | LOLUNIT_ASSERT_EQUAL(a[0].m1, 1); |
---|
94 | LOLUNIT_ASSERT_EQUAL(a[0].m2, 2); |
---|
95 | LOLUNIT_ASSERT_EQUAL(a[0].m3, 3.f); |
---|
96 | LOLUNIT_ASSERT_EQUAL(a[0].m4, 4.0); |
---|
97 | LOLUNIT_ASSERT_EQUAL(a[0].m5, 5); |
---|
98 | LOLUNIT_ASSERT_EQUAL(a[0].m6, 'a'); |
---|
99 | LOLUNIT_ASSERT_EQUAL(a[0].m7, true); |
---|
100 | LOLUNIT_ASSERT_EQUAL(a[0].m8, 0); |
---|
101 | } |
---|
102 | |
---|
103 | LOLUNIT_TEST(ArraySwap) |
---|
104 | { |
---|
105 | Array<int, int> a; |
---|
106 | a.Push(10, 20); |
---|
107 | a.Push(30, 40); |
---|
108 | a.Swap(0, 1); |
---|
109 | |
---|
110 | LOLUNIT_ASSERT_EQUAL(30, a[0].m1); |
---|
111 | LOLUNIT_ASSERT_EQUAL(40, a[0].m2); |
---|
112 | LOLUNIT_ASSERT_EQUAL(10, a[1].m1); |
---|
113 | LOLUNIT_ASSERT_EQUAL(20, a[1].m2); |
---|
114 | } |
---|
115 | |
---|
116 | LOLUNIT_TEST(ArrayConcat) |
---|
117 | { |
---|
118 | Array<int> a, b; |
---|
119 | a << 0 << 1; |
---|
120 | b << 2 << 3; |
---|
121 | |
---|
122 | Array<int> c = a + b; |
---|
123 | LOLUNIT_ASSERT_EQUAL(c[0], 0); |
---|
124 | LOLUNIT_ASSERT_EQUAL(c[1], 1); |
---|
125 | LOLUNIT_ASSERT_EQUAL(c[2], 2); |
---|
126 | LOLUNIT_ASSERT_EQUAL(c[3], 3); |
---|
127 | } |
---|
128 | |
---|
129 | LOLUNIT_TEST(ArrayAppend) |
---|
130 | { |
---|
131 | Array<int> a, b; |
---|
132 | a << 0 << 1; |
---|
133 | b << 2 << 3; |
---|
134 | |
---|
135 | a += b; |
---|
136 | LOLUNIT_ASSERT_EQUAL(a[0], 0); |
---|
137 | LOLUNIT_ASSERT_EQUAL(a[1], 1); |
---|
138 | LOLUNIT_ASSERT_EQUAL(a[2], 2); |
---|
139 | LOLUNIT_ASSERT_EQUAL(a[3], 3); |
---|
140 | |
---|
141 | b += b; |
---|
142 | LOLUNIT_ASSERT_EQUAL(b[0], 2); |
---|
143 | LOLUNIT_ASSERT_EQUAL(b[1], 3); |
---|
144 | LOLUNIT_ASSERT_EQUAL(b[2], 2); |
---|
145 | LOLUNIT_ASSERT_EQUAL(b[3], 3); |
---|
146 | } |
---|
147 | |
---|
148 | LOLUNIT_TEST(ElementCtorDtor) |
---|
149 | { |
---|
150 | /* Ensure array elements get created and destroyed the proper |
---|
151 | * number of times. */ |
---|
152 | TrackedObj::m_ctor = 0; |
---|
153 | TrackedObj::m_dtor = 0; |
---|
154 | { |
---|
155 | Array<TrackedObj> a; |
---|
156 | |
---|
157 | a.Push(TrackedObj()); |
---|
158 | } |
---|
159 | LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor); |
---|
160 | |
---|
161 | TrackedObj::m_ctor = 0; |
---|
162 | TrackedObj::m_dtor = 0; |
---|
163 | { |
---|
164 | Array<TrackedObj> a; |
---|
165 | |
---|
166 | a.Resize(2); |
---|
167 | a.Resize(4); |
---|
168 | a.Resize(1); |
---|
169 | } |
---|
170 | LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor); |
---|
171 | } |
---|
172 | }; |
---|
173 | |
---|
174 | } /* namespace lol */ |
---|
175 | |
---|