Sync AMTL.
1 // vim: set sts=8 ts=2 sw=2 tw=99 et:
3 // Copyright (C) 2013, David Anderson and AlliedModders LLC
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright notice, this
10 // list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 // * Neither the name of AlliedModders LLC nor the names of its contributors
15 // may be used to endorse or promote products derived from this software
16 // without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
30 #include <am-vector.h>
36 static size_t sCtors = 0;
37 static size_t sCopyCtors = 0;
38 static size_t sMovingCtors = 0;
39 static size_t sMovedDtors = 0;
40 static size_t sDtors = 0;
51 BasicThing(const BasicThing &other)
69 MovingThing(Moveable<MovingThing> other)
71 assert(!other->moved_);
84 MovingThing(const MovingThing &other) KE_DELETE;
88 } // anonymous namespace
90 class TestVector : public Test
102 if (!check(vector.empty(), "vector should be empty"))
104 if (!check(vector.length() == 0, "vector length should be 0"))
112 for (int i = 0; i < 5; i++) {
113 if (!check(vector[i] == i + 1, "vector[%d] is %d", i, i))
116 if (!check(vector.length() == 5, "vector length is 5"))
118 if (!check(!vector.empty(), "vector is not empty"))
121 if (!check(vector.popCopy() == 5, "popped 5"))
123 if (!check(vector.popCopy() == 4, "popped 4"))
125 if (!check(vector.length() == 3, "vector length is 3"))
128 vector.insert(0, 88);
129 vector.insert(0, 99);
130 vector.insert(4, 111);
131 if (!check(vector[0] == 99, "[0] should be 99"))
133 if (!check(vector[1] == 88, "[1] should be 88"))
135 if (!check(vector[2] == 1, "[2] should be 1"))
137 if (!check(vector[3] == 2, "[3] should be 2"))
139 if (!check(vector[4] == 111, "[4] should be 111"))
141 if (!check(vector[5] == 3, "[5] should be 3"))
143 if (!check(vector.length() == 6, "length should be 6"))
147 if (!check(vector[4] == 111, "[4] should still be 111"))
150 if (!check(vector[0] == 88, "[0] should be 88"))
152 if (!check(vector[3] == 111, "[3] should be 111"))
154 if (!check(vector.length() == 4, "length should be 4"))
157 while (!vector.empty())
160 if (!check(vector.empty(), "vector should be empty"))
162 if (!check(vector.ensure(128), "vector size ensured"))
164 if (!check(vector.empty(), "vector should be empty"))
167 for (int i = 0; i < 128; i++)
169 if (!check(vector.length() == 128, "vector length should be 128"))
173 if (!check(vector.empty(), "vector should be empty"))
179 bool testDestructors()
182 Vector<BasicThing> vector;
183 vector.append(BasicThing());
184 vector.append(BasicThing());
185 vector.append(BasicThing());
187 if (!check(sCtors == 3, "should get 3 normal constructors"))
189 if (!check(sCopyCtors == 3, "should get 3 copy constructors"))
191 if (!check(sDtors == 6, "should get 6 destructors"))
203 Vector<MovingThing> vector;
205 vector.append(Moveable<MovingThing>(a));
206 vector.append(Moveable<MovingThing>(b));
207 vector.append(Moveable<MovingThing>(c));
210 if (!check(sCtors == 3, "should get 3 normal constructors"))
212 if (!check(sMovingCtors == 3, "sould get 3 move constructors"))
214 if (!check(sMovedDtors == 3, "sould get 3 moved destructors"))
216 if (!check(sDtors == 6, "should get 6 destructors"))
226 Vector<int> v2(Move(v1));
227 if (!check(v2.length() == 1, "should have moved vector"))
229 if (!check(v1.length() == 0, "should have gutted vector"))
235 bool testFallibleMalloc()
237 Vector<int, FallibleMalloc> vector;
238 vector.setOutOfMemory(true);
239 if (!check(!vector.append(7), "vector handled out-of-memory"))
241 vector.setOutOfMemory(false);
242 if (!check(vector.append(8), "vector recovered out-of-memory"))
244 if (!check(vector.ooms() == 1, "vector received 1 oom"))
249 bool Run() KE_OVERRIDE
253 if (!testDestructors())
257 if (!testFallibleMalloc())