blob: 70f320f977ba7dfa156ad52592ef38606c5ae2da [file] [log] [blame]
bsalomon@google.combbe52902012-12-03 18:01:45 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon@google.combbe52902012-12-03 18:01:45 +00008#include "SkRandom.h"
9#include "SkTInternalLList.h"
10#include "SkTLList.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "Test.h"
bsalomon@google.combbe52902012-12-03 18:01:45 +000012
13class ListElement {
14public:
15 ListElement(int id) : fID(id) {
16 }
17 bool operator== (const ListElement& other) { return fID == other.fID; }
18
bsalomon@google.combbe52902012-12-03 18:01:45 +000019 int fID;
20
21private:
mtklein2766c002015-06-26 11:45:03 -070022
bsalomon@google.combbe52902012-12-03 18:01:45 +000023 SK_DECLARE_INTERNAL_LLIST_INTERFACE(ListElement);
24};
25
bsalomon@google.combbe52902012-12-03 18:01:45 +000026static void check_list(const SkTInternalLList<ListElement>& list,
27 skiatest::Reporter* reporter,
28 bool empty,
29 int numElements,
30 bool in0, bool in1, bool in2, bool in3,
31 ListElement elements[4]) {
32
33 REPORTER_ASSERT(reporter, empty == list.isEmpty());
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000034#ifdef SK_DEBUG
bsalomon@google.comdd3f7a92012-12-03 19:47:41 +000035 list.validate();
bsalomon@google.combbe52902012-12-03 18:01:45 +000036 REPORTER_ASSERT(reporter, numElements == list.countEntries());
37 REPORTER_ASSERT(reporter, in0 == list.isInList(&elements[0]));
38 REPORTER_ASSERT(reporter, in1 == list.isInList(&elements[1]));
39 REPORTER_ASSERT(reporter, in2 == list.isInList(&elements[2]));
40 REPORTER_ASSERT(reporter, in3 == list.isInList(&elements[3]));
41#endif
42}
43
bsalomonf045d602015-11-18 19:01:12 -080044static void test_tinternallist(skiatest::Reporter* reporter) {
bsalomon@google.combbe52902012-12-03 18:01:45 +000045 SkTInternalLList<ListElement> list;
46 ListElement elements[4] = {
47 ListElement(0),
48 ListElement(1),
49 ListElement(2),
50 ListElement(3),
51 };
52
53 // list should be empty to start with
54 check_list(list, reporter, true, 0, false, false, false, false, elements);
55
56 list.addToHead(&elements[0]);
57
58 check_list(list, reporter, false, 1, true, false, false, false, elements);
59
60 list.addToHead(&elements[1]);
61 list.addToHead(&elements[2]);
62 list.addToHead(&elements[3]);
63
64 check_list(list, reporter, false, 4, true, true, true, true, elements);
65
66 // test out iterators
67 typedef SkTInternalLList<ListElement>::Iter Iter;
68 Iter iter;
69
70 ListElement* cur = iter.init(list, Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -070071 for (int i = 0; cur; ++i, cur = iter.next()) {
bsalomon@google.combbe52902012-12-03 18:01:45 +000072 REPORTER_ASSERT(reporter, cur->fID == 3-i);
73 }
74
75 cur = iter.init(list, Iter::kTail_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -070076 for (int i = 0; cur; ++i, cur = iter.prev()) {
bsalomon@google.combbe52902012-12-03 18:01:45 +000077 REPORTER_ASSERT(reporter, cur->fID == i);
78 }
79
80 // remove middle, frontmost then backmost
81 list.remove(&elements[1]);
82 list.remove(&elements[3]);
83 list.remove(&elements[0]);
84
85 check_list(list, reporter, false, 1, false, false, true, false, elements);
86
87 // remove last element
88 list.remove(&elements[2]);
89
90 // list should be empty again
91 check_list(list, reporter, true, 0, false, false, false, false, elements);
bsalomon@google.comdd3f7a92012-12-03 19:47:41 +000092
93 // test out methods that add to the middle of the list.
halcanary96fcdcc2015-08-27 07:41:13 -070094 list.addAfter(&elements[1], nullptr);
bsalomon@google.comdd3f7a92012-12-03 19:47:41 +000095 check_list(list, reporter, false, 1, false, true, false, false, elements);
96
97 list.remove(&elements[1]);
98
halcanary96fcdcc2015-08-27 07:41:13 -070099 list.addBefore(&elements[1], nullptr);
bsalomon@google.comdd3f7a92012-12-03 19:47:41 +0000100 check_list(list, reporter, false, 1, false, true, false, false, elements);
101
102 list.addBefore(&elements[0], &elements[1]);
103 check_list(list, reporter, false, 2, true, true, false, false, elements);
104
105 list.addAfter(&elements[3], &elements[1]);
106 check_list(list, reporter, false, 3, true, true, false, true, elements);
107
108 list.addBefore(&elements[2], &elements[3]);
109 check_list(list, reporter, false, 4, true, true, true, true, elements);
110
111 cur = iter.init(list, Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700112 for (int i = 0; cur; ++i, cur = iter.next()) {
bsalomon@google.comdd3f7a92012-12-03 19:47:41 +0000113 REPORTER_ASSERT(reporter, cur->fID == i);
114 }
bsalomon@google.combbe52902012-12-03 18:01:45 +0000115}
116
bsalomonf045d602015-11-18 19:01:12 -0800117template <unsigned int N> static void test_tllist(skiatest::Reporter* reporter) {
118 typedef SkTLList<ListElement, N> ElList;
119 typedef typename ElList::Iter Iter;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000120 SkRandom random;
bsalomon@google.combbe52902012-12-03 18:01:45 +0000121
bsalomonf045d602015-11-18 19:01:12 -0800122 ElList list1;
123 ElList list2;
124 Iter iter1;
125 Iter iter2;
126 Iter iter3;
127 Iter iter4;
bsalomon@google.combbe52902012-12-03 18:01:45 +0000128
bsalomonf045d602015-11-18 19:01:12 -0800129 REPORTER_ASSERT(reporter, list1.isEmpty());
130 REPORTER_ASSERT(reporter, nullptr == iter1.init(list1, Iter::kHead_IterStart));
131 REPORTER_ASSERT(reporter, nullptr == iter1.init(list1, Iter::kTail_IterStart));
132 // Try popping an empty list
133 list1.popHead();
134 list1.popTail();
135 REPORTER_ASSERT(reporter, list1.isEmpty());
136 REPORTER_ASSERT(reporter, list1 == list2);
bsalomon@google.combbe52902012-12-03 18:01:45 +0000137
bsalomonf045d602015-11-18 19:01:12 -0800138 // Create two identical lists, one by appending to head and the other to the tail.
139 list1.addToHead(ListElement(1));
140 list2.addToTail(ListElement(1));
141 iter1.init(list1, Iter::kHead_IterStart);
142 iter2.init(list1, Iter::kTail_IterStart);
143 REPORTER_ASSERT(reporter, iter1.get()->fID == iter2.get()->fID);
144 iter3.init(list2, Iter::kHead_IterStart);
145 iter4.init(list2, Iter::kTail_IterStart);
146 REPORTER_ASSERT(reporter, iter3.get()->fID == iter1.get()->fID);
147 REPORTER_ASSERT(reporter, iter4.get()->fID == iter1.get()->fID);
148 REPORTER_ASSERT(reporter, list1 == list2);
bsalomon@google.combbe52902012-12-03 18:01:45 +0000149
bsalomonf045d602015-11-18 19:01:12 -0800150 list2.reset();
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000151
bsalomonf045d602015-11-18 19:01:12 -0800152 // use both before/after in-place construction on an empty list
153 list2.addBefore(list2.headIter(), 1);
154 REPORTER_ASSERT(reporter, list2 == list1);
155 list2.reset();
bsalomon@google.comdd3f7a92012-12-03 19:47:41 +0000156
bsalomonf045d602015-11-18 19:01:12 -0800157 list2.addAfter(list2.tailIter(), 1);
158 REPORTER_ASSERT(reporter, list2 == list1);
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000159
bsalomonf045d602015-11-18 19:01:12 -0800160 // add an element to the second list, check that iters are still valid
161 iter3.init(list2, Iter::kHead_IterStart);
162 iter4.init(list2, Iter::kTail_IterStart);
163 list2.addToHead(ListElement(2));
bsalomon@google.comdd3f7a92012-12-03 19:47:41 +0000164
bsalomonf045d602015-11-18 19:01:12 -0800165 REPORTER_ASSERT(reporter, iter3.get()->fID == iter1.get()->fID);
166 REPORTER_ASSERT(reporter, iter4.get()->fID == iter1.get()->fID);
167 REPORTER_ASSERT(reporter, 1 == Iter(list2, Iter::kTail_IterStart).get()->fID);
168 REPORTER_ASSERT(reporter, 2 == Iter(list2, Iter::kHead_IterStart).get()->fID);
169 REPORTER_ASSERT(reporter, list1 != list2);
170 list1.addToHead(ListElement(2));
171 REPORTER_ASSERT(reporter, list1 == list2);
172 REPORTER_ASSERT(reporter, !list1.isEmpty());
bsalomon@google.comebce0302012-12-04 14:48:57 +0000173
bsalomonf045d602015-11-18 19:01:12 -0800174 list1.reset();
175 list2.reset();
176 REPORTER_ASSERT(reporter, list1.isEmpty() && list2.isEmpty());
bsalomon@google.combbe52902012-12-03 18:01:45 +0000177
bsalomonf045d602015-11-18 19:01:12 -0800178 // randomly perform insertions and deletions on a list and perform tests
179 int count = 0;
180 for (int j = 0; j < 100; ++j) {
181 if (list1.isEmpty() || random.nextBiasedBool(3 * SK_Scalar1 / 4)) {
182 int id = j;
183 // Choose one of three ways to insert a new element: at the head, at the tail,
184 // before a random element, after a random element
185 int numValidMethods = 0 == count ? 2 : 4;
186 int insertionMethod = random.nextULessThan(numValidMethods);
187 switch (insertionMethod) {
188 case 0:
189 list1.addToHead(ListElement(id));
190 break;
191 case 1:
192 list1.addToTail(ListElement(id));
193 break;
194 case 2: // fallthru to share code that picks random element.
195 case 3: {
196 int n = random.nextULessThan(list1.count());
197 Iter iter = list1.headIter();
198 // remember the elements before/after the insertion point.
199 while (n--) {
200 iter.next();
201 }
202 Iter prev(iter);
203 Iter next(iter);
204 next.next();
205 prev.prev();
bsalomon@google.combbe52902012-12-03 18:01:45 +0000206
bsalomonf045d602015-11-18 19:01:12 -0800207 SkASSERT(iter.get());
208 // insert either before or after the iterator, then check that the
209 // surrounding sequence is correct.
210 if (2 == insertionMethod) {
211 list1.addBefore(iter, id);
212 Iter newItem(iter);
213 newItem.prev();
214 REPORTER_ASSERT(reporter, newItem.get()->fID == id);
215
216 if (next.get()) {
217 REPORTER_ASSERT(reporter, next.prev()->fID == iter.get()->fID);
bsalomon@google.comdd3f7a92012-12-03 19:47:41 +0000218 }
bsalomonf045d602015-11-18 19:01:12 -0800219 if (prev.get()) {
220 REPORTER_ASSERT(reporter, prev.next()->fID == id);
221 }
222 } else {
223 list1.addAfter(iter, id);
224 Iter newItem(iter);
225 newItem.next();
226 REPORTER_ASSERT(reporter, newItem.get()->fID == id);
bsalomon@google.comdd3f7a92012-12-03 19:47:41 +0000227
bsalomonf045d602015-11-18 19:01:12 -0800228 if (next.get()) {
229 REPORTER_ASSERT(reporter, next.prev()->fID == id);
230 }
231 if (prev.get()) {
232 REPORTER_ASSERT(reporter, prev.next()->fID == iter.get()->fID);
bsalomon@google.comdd3f7a92012-12-03 19:47:41 +0000233 }
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000234 }
bsalomon@google.combbe52902012-12-03 18:01:45 +0000235 }
bsalomon@google.combbe52902012-12-03 18:01:45 +0000236 }
bsalomonf045d602015-11-18 19:01:12 -0800237 ++count;
238 } else {
239 // walk to a random place either forward or backwards and remove.
240 int n = random.nextULessThan(list1.count());
241 typename Iter::IterStart start;
242 ListElement* (Iter::*incrFunc)();
243
244 if (random.nextBool()) {
245 start = Iter::kHead_IterStart;
246 incrFunc = &Iter::next;
247 } else {
248 start = Iter::kTail_IterStart;
249 incrFunc = &Iter::prev;
250 }
251
252 // find the element
253 Iter iter(list1, start);
254 while (n--) {
255 REPORTER_ASSERT(reporter, iter.get());
256 (iter.*incrFunc)();
257 }
258 REPORTER_ASSERT(reporter, iter.get());
259
260 // remember the prev and next elements from the element to be removed
261 Iter prev = iter;
262 Iter next = iter;
263 prev.prev();
264 next.next();
265 list1.remove(iter.get());
266
267 // make sure the remembered next/prev iters still work
268 Iter pn = prev; pn.next();
269 Iter np = next; np.prev();
270 // pn should match next unless the target node was the head, in which case prev
271 // walked off the list.
272 REPORTER_ASSERT(reporter, pn.get() == next.get() || nullptr == prev.get());
273 // Similarly, np should match prev unless next originally walked off the tail.
274 REPORTER_ASSERT(reporter, np.get() == prev.get() || nullptr == next.get());
275 --count;
bsalomon@google.combbe52902012-12-03 18:01:45 +0000276 }
bsalomonf045d602015-11-18 19:01:12 -0800277 REPORTER_ASSERT(reporter, count == list1.count());
bsalomon@google.combbe52902012-12-03 18:01:45 +0000278 }
279}
280
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000281DEF_TEST(LList, reporter) {
bsalomonf045d602015-11-18 19:01:12 -0800282 test_tinternallist(reporter);
283 test_tllist<1>(reporter);
284 test_tllist<3>(reporter);
285 test_tllist<8>(reporter);
286 test_tllist<10>(reporter);
287 test_tllist<16>(reporter);
bsalomon@google.combbe52902012-12-03 18:01:45 +0000288}