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-linkedlist.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 TestLinkedList : public Test
100 LinkedList<int> list;
102 if (!check(list.empty(), "list should be empty"))
104 if (!check(list.begin() == list.end(), "list iterator should be empty"))
110 if (!check(!list.empty(), "list should not be empty"))
112 if (!check(list.length() == 3, "list size should be 3"))
115 LinkedList<int>::iterator iter = list.begin();
116 if (!check(iter != list.end(), "list iterator should not have ended"))
118 if (!check(*iter == 5, "item 1 should be 5"))
121 if (!check(iter != list.end(), "list iterator should not have ended"))
123 if (!check(*iter == 7, "item 2 should be 7"))
126 if (!check(iter != list.end(), "list iterator should not have ended"))
128 if (!check(*iter == 9, "item 3 should be 9"))
131 if (!check(iter == list.end(), "list iterator should have ended"))
137 iter = list.erase(iter);
138 if (!check(iter == list.end(), "erasure should have returned ended iterator"))
143 iter = list.erase(iter);
144 if (!check(iter != list.end(), "erasure should not have ended iterator"))
146 if (!check(*iter == 7, "erasure should have returned item 2"))
149 if (!check(*iter == 11, "second item should be 11"))
151 if (!check(list.length() == 2, "list should have two items"))
156 if (!check(*iter == 1, "new item should have been prepended"))
159 if (!check(*iter == 7, "second item should be 7"))
161 if (!check(list.length() == 3, "list should have three items"))
165 if (!check(list.empty(), "cleared list should be empty"))
167 if (!check(list.begin() == list.end(), "cleared list iterator should be empty"))
177 if (!check(iter != list.end(), "should have found a 3"))
179 if (!check(*iter == 3, "should have found 3"))
183 if (!check(iter == list.end(), "should not have found a 3"))
186 if (!check(iter != list.end(), "should have found a 5"))
188 if (!check(*iter == 5, "should have found 5"))
192 if (!check(iter != list.end(), "should have found a second 5"))
194 if (!check(*iter == 5, "should have found a second 5"))
199 if (!check(iter == list.end(), "should not have found a third 5"))
205 bool testDestructors()
208 LinkedList<BasicThing> list;
209 list.append(BasicThing());
210 list.append(BasicThing());
211 list.append(BasicThing());
213 if (!check(sCtors == 3, "should get 3 normal constructors"))
215 if (!check(sCopyCtors == 3, "should get 3 copy constructors"))
217 if (!check(sDtors == 6, "should get 6 destructors"))
229 LinkedList<MovingThing> list;
231 list.append(Moveable<MovingThing>(a));
232 list.append(Moveable<MovingThing>(b));
233 list.append(Moveable<MovingThing>(c));
236 if (!check(sCtors == 3, "should get 3 normal constructors"))
238 if (!check(sMovingCtors == 3, "sould get 3 move constructors"))
240 if (!check(sMovedDtors == 3, "sould get 3 moved destructors"))
242 if (!check(sDtors == 6, "should get 6 destructors"))
252 bool testFallibleMalloc()
254 LinkedList<int, FallibleMalloc> list;
257 list.setOutOfMemory(true);
258 if (!check(!list.append(7), "list handled out-of-memory"))
260 list.setOutOfMemory(false);
261 if (!check(list.append(8), "list recovered out-of-memory"))
263 if (!check(list.ooms() == 1, "list received 1 oom"))
268 bool Run() KE_OVERRIDE
272 if (!testDestructors())
276 if (!testFallibleMalloc())