OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Test.h" | 8 #include "Test.h" |
9 #include "SkRandom.h" | 9 #include "SkRandom.h" |
10 #include "SkTInternalLList.h" | 10 #include "SkTInternalLList.h" |
11 #include "SkTLList.h" | 11 #include "SkTLList.h" |
12 | 12 |
13 class ListElement { | 13 class ListElement { |
14 public: | 14 public: |
15 ListElement(int id) : fID(id) { | 15 ListElement(int id) : fID(id) { |
16 } | 16 } |
17 bool operator== (const ListElement& other) { return fID == other.fID; } | 17 bool operator== (const ListElement& other) { return fID == other.fID; } |
18 | 18 |
19 #if SK_ENABLE_INST_COUNT | 19 #if SK_ENABLE_INST_COUNT |
20 // Make the instance count available publicly. | 20 // Make the instance count available publicly. |
21 static int InstanceCount() { return GetInstanceCount(); } | 21 static int InstanceCount() { return GetInstanceCount(); } |
22 #endif | 22 #endif |
23 | 23 |
24 int fID; | 24 int fID; |
25 | 25 |
| 26 SK_DECLARE_NAMED_INTERNAL_LLIST_INTERFACE(ListElement, NamedList); |
26 private: | 27 private: |
27 SK_DECLARE_INST_COUNT_ROOT(ListElement); | 28 SK_DECLARE_INST_COUNT_ROOT(ListElement); |
28 SK_DECLARE_INTERNAL_LLIST_INTERFACE(ListElement); | 29 SK_DECLARE_INTERNAL_LLIST_INTERFACE(ListElement); |
| 30 |
| 31 SK_DECLARE_NAMED_INTERNAL_LLIST_INTERFACE_DATA(ListElement, NamedList); |
29 }; | 32 }; |
30 | 33 |
31 SK_DEFINE_INST_COUNT(ListElement); | 34 SK_DEFINE_INST_COUNT(ListElement); |
32 | 35 |
33 static void check_list(const SkTInternalLList<ListElement>& list, | 36 static void check_list(const SkTInternalLList<ListElement>& list, |
34 skiatest::Reporter* reporter, | 37 skiatest::Reporter* reporter, |
35 bool empty, | 38 bool empty, |
36 int numElements, | 39 int numElements, |
37 bool in0, bool in1, bool in2, bool in3, | 40 bool in0, bool in1, bool in2, bool in3, |
38 ListElement elements[4]) { | 41 ListElement elements[4]) { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 117 |
115 list.addBefore(&elements[2], &elements[3]); | 118 list.addBefore(&elements[2], &elements[3]); |
116 check_list(list, reporter, false, 4, true, true, true, true, elements); | 119 check_list(list, reporter, false, 4, true, true, true, true, elements); |
117 | 120 |
118 cur = iter.init(list, Iter::kHead_IterStart); | 121 cur = iter.init(list, Iter::kHead_IterStart); |
119 for (int i = 0; NULL != cur; ++i, cur = iter.next()) { | 122 for (int i = 0; NULL != cur; ++i, cur = iter.next()) { |
120 REPORTER_ASSERT(reporter, cur->fID == i); | 123 REPORTER_ASSERT(reporter, cur->fID == i); |
121 } | 124 } |
122 } | 125 } |
123 | 126 |
| 127 static void TestNamedTInternalLList(skiatest::Reporter* reporter) { |
| 128 SK_DECLARE_NAMED_INTERNAL_LLIST(ListElement, NamedList, list); |
| 129 ListElement elements[4] = { |
| 130 ListElement(0), |
| 131 ListElement(1), |
| 132 ListElement(2), |
| 133 ListElement(3), |
| 134 }; |
| 135 list.addToHead(&elements[0]); |
| 136 list.addToHead(&elements[1]); |
| 137 list.addToHead(&elements[2]); |
| 138 list.addToHead(&elements[3]); |
| 139 |
| 140 |
| 141 NamedListInternalLListType::Iter iter; |
| 142 ListElement* e = iter.init(list, NamedListInternalLListType::Iter::kTail_Ite
rStart); |
| 143 REPORTER_ASSERT(reporter, e == &elements[0]); |
| 144 REPORTER_ASSERT(reporter, iter.prev() == &elements[1]); |
| 145 REPORTER_ASSERT(reporter, iter.prev() == &elements[2]); |
| 146 REPORTER_ASSERT(reporter, iter.prev() == &elements[3]); |
| 147 REPORTER_ASSERT(reporter, iter.prev() == NULL); |
| 148 } |
| 149 |
| 150 struct ElementFinder { |
| 151 ElementFinder(int id) |
| 152 : fID(id) { |
| 153 } |
| 154 |
| 155 bool operator()(const ListElement* element) const { |
| 156 return element->fID == fID; |
| 157 } |
| 158 |
| 159 int fID; |
| 160 }; |
| 161 |
| 162 |
| 163 static void TestTInternalLListFind(skiatest::Reporter* reporter) { |
| 164 SK_DECLARE_NAMED_INTERNAL_LLIST(ListElement, NamedList, list); |
| 165 ListElement elements[4] = { |
| 166 ListElement(0), |
| 167 ListElement(1), |
| 168 ListElement(2), |
| 169 ListElement(3), |
| 170 }; |
| 171 list.addToHead(&elements[0]); |
| 172 list.addToHead(&elements[1]); |
| 173 list.addToHead(&elements[2]); |
| 174 list.addToHead(&elements[3]); |
| 175 |
| 176 for (size_t i = 0; i < SK_ARRAY_COUNT(elements); ++i) { |
| 177 REPORTER_ASSERT(reporter, &elements[i] == list.find(ElementFinder(elemen
ts[i].fID))); |
| 178 } |
| 179 } |
| 180 |
124 static void TestTLList(skiatest::Reporter* reporter) { | 181 static void TestTLList(skiatest::Reporter* reporter) { |
125 typedef SkTLList<ListElement> ElList; | 182 typedef SkTLList<ListElement> ElList; |
126 typedef ElList::Iter Iter; | 183 typedef ElList::Iter Iter; |
127 SkRandom random; | 184 SkRandom random; |
128 | 185 |
129 for (int i = 1; i <= 16; i *= 2) { | 186 for (int i = 1; i <= 16; i *= 2) { |
130 | 187 |
131 ElList list1(i); | 188 ElList list1(i); |
132 ElList list2(i); | 189 ElList list2(i); |
133 Iter iter1; | 190 Iter iter1; |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 } | 364 } |
308 list1.reset(); | 365 list1.reset(); |
309 #if SK_ENABLE_INST_COUNT | 366 #if SK_ENABLE_INST_COUNT |
310 SkASSERT(0 == ListElement::InstanceCount()); | 367 SkASSERT(0 == ListElement::InstanceCount()); |
311 #endif | 368 #endif |
312 } | 369 } |
313 } | 370 } |
314 | 371 |
315 static void test_llists(skiatest::Reporter* reporter) { | 372 static void test_llists(skiatest::Reporter* reporter) { |
316 TestTInternalLList(reporter); | 373 TestTInternalLList(reporter); |
| 374 TestNamedTInternalLList(reporter); |
| 375 TestTInternalLListFind(reporter); |
317 TestTLList(reporter); | 376 TestTLList(reporter); |
| 377 |
318 } | 378 } |
319 | 379 |
320 #include "TestClassDef.h" | 380 #include "TestClassDef.h" |
321 DEFINE_TESTCLASS("LList", TestLListClass, test_llists) | 381 DEFINE_TESTCLASS("LList", TestLListClass, test_llists) |
OLD | NEW |