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 | /* HACK: we disable these tests because they fail with the |
---|
40 | * Xcode iPhone compiler. */ |
---|
41 | #if !defined __clang__ || !defined __arm__ |
---|
42 | LOLUNIT_TEST(ArrayPush) |
---|
43 | { |
---|
44 | Array<int> a; |
---|
45 | a.Push(0); |
---|
46 | a.Push(1); |
---|
47 | a.Push(2); |
---|
48 | a.Push(3); |
---|
49 | |
---|
50 | LOLUNIT_ASSERT_EQUAL(a[0], 0); |
---|
51 | LOLUNIT_ASSERT_EQUAL(a[1], 1); |
---|
52 | LOLUNIT_ASSERT_EQUAL(a[2], 2); |
---|
53 | LOLUNIT_ASSERT_EQUAL(a[3], 3); |
---|
54 | } |
---|
55 | |
---|
56 | LOLUNIT_TEST(ArrayPushWithShift) |
---|
57 | { |
---|
58 | Array<int> a; |
---|
59 | a << 0 << 1 << 2 << 3; |
---|
60 | |
---|
61 | LOLUNIT_ASSERT_EQUAL(a[0], 0); |
---|
62 | LOLUNIT_ASSERT_EQUAL(a[1], 1); |
---|
63 | LOLUNIT_ASSERT_EQUAL(a[2], 2); |
---|
64 | LOLUNIT_ASSERT_EQUAL(a[3], 3); |
---|
65 | } |
---|
66 | |
---|
67 | LOLUNIT_TEST(ArrayCopy) |
---|
68 | { |
---|
69 | Array<int> a; |
---|
70 | a << 0 << 1 << 2 << 3; |
---|
71 | |
---|
72 | Array<int> b = a; |
---|
73 | |
---|
74 | LOLUNIT_ASSERT_EQUAL(b[0], 0); |
---|
75 | LOLUNIT_ASSERT_EQUAL(b[1], 1); |
---|
76 | LOLUNIT_ASSERT_EQUAL(b[2], 2); |
---|
77 | LOLUNIT_ASSERT_EQUAL(b[3], 3); |
---|
78 | } |
---|
79 | |
---|
80 | LOLUNIT_TEST(ArrayRemove) |
---|
81 | { |
---|
82 | Array<int> a; |
---|
83 | a << 0 << 1 << 2 << 3; |
---|
84 | a.Remove(1); |
---|
85 | |
---|
86 | LOLUNIT_ASSERT_EQUAL(a.Count(), 3); |
---|
87 | LOLUNIT_ASSERT_EQUAL(a[0], 0); |
---|
88 | LOLUNIT_ASSERT_EQUAL(a[1], 2); |
---|
89 | LOLUNIT_ASSERT_EQUAL(a[2], 3); |
---|
90 | |
---|
91 | Array<int> b; |
---|
92 | b << 0 << 1 << 2 << 3; |
---|
93 | b.Remove(-2); |
---|
94 | |
---|
95 | LOLUNIT_ASSERT_EQUAL(b.Count(), 3); |
---|
96 | LOLUNIT_ASSERT_EQUAL(b[0], 0); |
---|
97 | LOLUNIT_ASSERT_EQUAL(b[1], 1); |
---|
98 | LOLUNIT_ASSERT_EQUAL(b[2], 3); |
---|
99 | } |
---|
100 | |
---|
101 | LOLUNIT_TEST(ArrayRemoveSwap) |
---|
102 | { |
---|
103 | Array<int> a; |
---|
104 | a << 0 << 1 << 2 << 3; |
---|
105 | a.RemoveSwap(1); |
---|
106 | |
---|
107 | LOLUNIT_ASSERT_EQUAL(a.Count(), 3); |
---|
108 | LOLUNIT_ASSERT_EQUAL(a[0], 0); |
---|
109 | LOLUNIT_ASSERT_EQUAL(a[1], 3); |
---|
110 | LOLUNIT_ASSERT_EQUAL(a[2], 2); |
---|
111 | |
---|
112 | Array<int> b; |
---|
113 | b << 0 << 1 << 2 << 3; |
---|
114 | b.Remove(1, 2); |
---|
115 | |
---|
116 | LOLUNIT_ASSERT_EQUAL(b.Count(), 2); |
---|
117 | LOLUNIT_ASSERT_EQUAL(b[0], 0); |
---|
118 | LOLUNIT_ASSERT_EQUAL(b[1], 3); |
---|
119 | } |
---|
120 | #endif |
---|
121 | |
---|
122 | LOLUNIT_TEST(EightElements) |
---|
123 | { |
---|
124 | Array<int, long, float, double, unsigned, char, bool, void *> a; |
---|
125 | a.Push(1, 2, 3.f, 4.0, 5, 'a', true, 0); |
---|
126 | |
---|
127 | LOLUNIT_ASSERT_EQUAL(a[0].m1, 1); |
---|
128 | LOLUNIT_ASSERT_EQUAL(a[0].m2, 2); |
---|
129 | LOLUNIT_ASSERT_EQUAL(a[0].m3, 3.f); |
---|
130 | LOLUNIT_ASSERT_EQUAL(a[0].m4, 4.0); |
---|
131 | LOLUNIT_ASSERT_EQUAL(a[0].m5, 5); |
---|
132 | LOLUNIT_ASSERT_EQUAL(a[0].m6, 'a'); |
---|
133 | LOLUNIT_ASSERT_EQUAL(a[0].m7, true); |
---|
134 | LOLUNIT_ASSERT_EQUAL(a[0].m8, 0); |
---|
135 | } |
---|
136 | |
---|
137 | LOLUNIT_TEST(ArraySwap) |
---|
138 | { |
---|
139 | Array<int, int> a; |
---|
140 | a.Push(10, 20); |
---|
141 | a.Push(30, 40); |
---|
142 | a.Swap(0, 1); |
---|
143 | |
---|
144 | LOLUNIT_ASSERT_EQUAL(30, a[0].m1); |
---|
145 | LOLUNIT_ASSERT_EQUAL(40, a[0].m2); |
---|
146 | LOLUNIT_ASSERT_EQUAL(10, a[1].m1); |
---|
147 | LOLUNIT_ASSERT_EQUAL(20, a[1].m2); |
---|
148 | } |
---|
149 | |
---|
150 | LOLUNIT_TEST(ArrayConcat) |
---|
151 | { |
---|
152 | Array<int> a, b; |
---|
153 | a << 0 << 1; |
---|
154 | b << 2 << 3; |
---|
155 | |
---|
156 | Array<int> c = a + b; |
---|
157 | LOLUNIT_ASSERT_EQUAL(c[0], 0); |
---|
158 | LOLUNIT_ASSERT_EQUAL(c[1], 1); |
---|
159 | LOLUNIT_ASSERT_EQUAL(c[2], 2); |
---|
160 | LOLUNIT_ASSERT_EQUAL(c[3], 3); |
---|
161 | } |
---|
162 | |
---|
163 | LOLUNIT_TEST(ArrayAppend) |
---|
164 | { |
---|
165 | Array<int> a, b; |
---|
166 | a << 0 << 1; |
---|
167 | b << 2 << 3; |
---|
168 | |
---|
169 | a += b; |
---|
170 | LOLUNIT_ASSERT_EQUAL(a[0], 0); |
---|
171 | LOLUNIT_ASSERT_EQUAL(a[1], 1); |
---|
172 | LOLUNIT_ASSERT_EQUAL(a[2], 2); |
---|
173 | LOLUNIT_ASSERT_EQUAL(a[3], 3); |
---|
174 | |
---|
175 | b += b; |
---|
176 | LOLUNIT_ASSERT_EQUAL(b[0], 2); |
---|
177 | LOLUNIT_ASSERT_EQUAL(b[1], 3); |
---|
178 | LOLUNIT_ASSERT_EQUAL(b[2], 2); |
---|
179 | LOLUNIT_ASSERT_EQUAL(b[3], 3); |
---|
180 | } |
---|
181 | |
---|
182 | LOLUNIT_TEST(ElementCtorDtor) |
---|
183 | { |
---|
184 | /* Ensure array elements get created and destroyed the proper |
---|
185 | * number of times. */ |
---|
186 | TrackedObj::m_ctor = 0; |
---|
187 | TrackedObj::m_dtor = 0; |
---|
188 | { |
---|
189 | Array<TrackedObj> a; |
---|
190 | |
---|
191 | a.Push(TrackedObj()); |
---|
192 | } |
---|
193 | LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor); |
---|
194 | |
---|
195 | TrackedObj::m_ctor = 0; |
---|
196 | TrackedObj::m_dtor = 0; |
---|
197 | { |
---|
198 | Array<TrackedObj> a; |
---|
199 | |
---|
200 | a.Resize(2); |
---|
201 | a.Resize(4); |
---|
202 | a.Resize(1); |
---|
203 | } |
---|
204 | LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor); |
---|
205 | } |
---|
206 | }; |
---|
207 | |
---|
208 | } /* namespace lol */ |
---|
209 | |
---|