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[0], 0); |
---|
87 | LOLUNIT_ASSERT_EQUAL(a[1], 2); |
---|
88 | LOLUNIT_ASSERT_EQUAL(a[2], 3); |
---|
89 | } |
---|
90 | #endif |
---|
91 | |
---|
92 | LOLUNIT_TEST(EightElements) |
---|
93 | { |
---|
94 | Array<int, long, float, double, unsigned, char, bool, void *> a; |
---|
95 | a.Push(1, 2, 3.f, 4.0, 5, 'a', true, 0); |
---|
96 | |
---|
97 | LOLUNIT_ASSERT_EQUAL(a[0].m1, 1); |
---|
98 | LOLUNIT_ASSERT_EQUAL(a[0].m2, 2); |
---|
99 | LOLUNIT_ASSERT_EQUAL(a[0].m3, 3.f); |
---|
100 | LOLUNIT_ASSERT_EQUAL(a[0].m4, 4.0); |
---|
101 | LOLUNIT_ASSERT_EQUAL(a[0].m5, 5); |
---|
102 | LOLUNIT_ASSERT_EQUAL(a[0].m6, 'a'); |
---|
103 | LOLUNIT_ASSERT_EQUAL(a[0].m7, true); |
---|
104 | LOLUNIT_ASSERT_EQUAL(a[0].m8, 0); |
---|
105 | } |
---|
106 | |
---|
107 | LOLUNIT_TEST(ArraySwap) |
---|
108 | { |
---|
109 | Array<int, int> a; |
---|
110 | a.Push(10, 20); |
---|
111 | a.Push(30, 40); |
---|
112 | a.Swap(0, 1); |
---|
113 | |
---|
114 | LOLUNIT_ASSERT_EQUAL(30, a[0].m1); |
---|
115 | LOLUNIT_ASSERT_EQUAL(40, a[0].m2); |
---|
116 | LOLUNIT_ASSERT_EQUAL(10, a[1].m1); |
---|
117 | LOLUNIT_ASSERT_EQUAL(20, a[1].m2); |
---|
118 | } |
---|
119 | |
---|
120 | LOLUNIT_TEST(ArrayConcat) |
---|
121 | { |
---|
122 | Array<int> a, b; |
---|
123 | a << 0 << 1; |
---|
124 | b << 2 << 3; |
---|
125 | |
---|
126 | Array<int> c = a + b; |
---|
127 | LOLUNIT_ASSERT_EQUAL(c[0], 0); |
---|
128 | LOLUNIT_ASSERT_EQUAL(c[1], 1); |
---|
129 | LOLUNIT_ASSERT_EQUAL(c[2], 2); |
---|
130 | LOLUNIT_ASSERT_EQUAL(c[3], 3); |
---|
131 | } |
---|
132 | |
---|
133 | LOLUNIT_TEST(ArrayAppend) |
---|
134 | { |
---|
135 | Array<int> a, b; |
---|
136 | a << 0 << 1; |
---|
137 | b << 2 << 3; |
---|
138 | |
---|
139 | a += b; |
---|
140 | LOLUNIT_ASSERT_EQUAL(a[0], 0); |
---|
141 | LOLUNIT_ASSERT_EQUAL(a[1], 1); |
---|
142 | LOLUNIT_ASSERT_EQUAL(a[2], 2); |
---|
143 | LOLUNIT_ASSERT_EQUAL(a[3], 3); |
---|
144 | |
---|
145 | b += b; |
---|
146 | LOLUNIT_ASSERT_EQUAL(b[0], 2); |
---|
147 | LOLUNIT_ASSERT_EQUAL(b[1], 3); |
---|
148 | LOLUNIT_ASSERT_EQUAL(b[2], 2); |
---|
149 | LOLUNIT_ASSERT_EQUAL(b[3], 3); |
---|
150 | } |
---|
151 | |
---|
152 | LOLUNIT_TEST(ElementCtorDtor) |
---|
153 | { |
---|
154 | /* Ensure array elements get created and destroyed the proper |
---|
155 | * number of times. */ |
---|
156 | TrackedObj::m_ctor = 0; |
---|
157 | TrackedObj::m_dtor = 0; |
---|
158 | { |
---|
159 | Array<TrackedObj> a; |
---|
160 | |
---|
161 | a.Push(TrackedObj()); |
---|
162 | } |
---|
163 | LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor); |
---|
164 | |
---|
165 | TrackedObj::m_ctor = 0; |
---|
166 | TrackedObj::m_dtor = 0; |
---|
167 | { |
---|
168 | Array<TrackedObj> a; |
---|
169 | |
---|
170 | a.Resize(2); |
---|
171 | a.Resize(4); |
---|
172 | a.Resize(1); |
---|
173 | } |
---|
174 | LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor); |
---|
175 | } |
---|
176 | }; |
---|
177 | |
---|
178 | } /* namespace lol */ |
---|
179 | |
---|