robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "Test.h" |
| 9 | #include "SkTDLinkedList.h" |
| 10 | |
| 11 | class ListElement { |
| 12 | public: |
| 13 | ListElement(int id) : fID(id) { |
| 14 | } |
| 15 | |
| 16 | int fID; |
| 17 | |
| 18 | private: |
| 19 | SK_DEFINE_DLINKEDLIST_INTERFACE(ListElement); |
| 20 | }; |
| 21 | |
| 22 | static void CheckList(const SkTDLinkedList<ListElement>& list, |
| 23 | skiatest::Reporter* reporter, |
| 24 | bool empty, |
| 25 | int numElements, |
| 26 | bool in0, bool in1, bool in2, bool in3, |
| 27 | ListElement elements[4]) { |
| 28 | |
| 29 | REPORTER_ASSERT(reporter, empty == list.isEmpty()); |
| 30 | #if SK_DEBUG |
| 31 | REPORTER_ASSERT(reporter, numElements == list.countEntries()); |
| 32 | REPORTER_ASSERT(reporter, in0 == list.isInList(&elements[0])); |
| 33 | REPORTER_ASSERT(reporter, in1 == list.isInList(&elements[1])); |
| 34 | REPORTER_ASSERT(reporter, in2 == list.isInList(&elements[2])); |
| 35 | REPORTER_ASSERT(reporter, in3 == list.isInList(&elements[3])); |
| 36 | #endif |
| 37 | } |
| 38 | |
| 39 | static void TestTDLinkedList(skiatest::Reporter* reporter) { |
| 40 | SkTDLinkedList<ListElement> list; |
| 41 | ListElement elements[4] = { |
| 42 | ListElement(0), |
| 43 | ListElement(1), |
| 44 | ListElement(2), |
| 45 | ListElement(3), |
| 46 | }; |
| 47 | |
| 48 | // list should be empty to start with |
| 49 | CheckList(list, reporter, true, 0, false, false, false, false, elements); |
| 50 | |
| 51 | list.addToHead(&elements[0]); |
| 52 | |
| 53 | CheckList(list, reporter, false, 1, true, false, false, false, elements); |
| 54 | |
| 55 | list.addToHead(&elements[1]); |
| 56 | list.addToHead(&elements[2]); |
| 57 | list.addToHead(&elements[3]); |
| 58 | |
| 59 | CheckList(list, reporter, false, 4, true, true, true, true, elements); |
| 60 | |
| 61 | // test out iterators |
| 62 | SkTDLinkedList<ListElement>::Iter iter; |
| 63 | |
| 64 | ListElement* cur = iter.init(list, SkTDLinkedList<ListElement>::Iter::kHead_IterStart); |
| 65 | for (int i = 0; NULL != cur; ++i, cur = iter.next()) { |
| 66 | REPORTER_ASSERT(reporter, cur->fID == 3-i); |
| 67 | } |
| 68 | |
| 69 | cur = iter.init(list, SkTDLinkedList<ListElement>::Iter::kTail_IterStart); |
| 70 | for (int i = 0; NULL != cur; ++i, cur = iter.prev()) { |
| 71 | REPORTER_ASSERT(reporter, cur->fID == i); |
| 72 | } |
| 73 | |
| 74 | // remove middle, frontmost then backmost |
| 75 | list.remove(&elements[1]); |
| 76 | list.remove(&elements[3]); |
| 77 | list.remove(&elements[0]); |
| 78 | |
| 79 | CheckList(list, reporter, false, 1, false, false, true, false, elements); |
| 80 | |
| 81 | // remove last element |
| 82 | list.remove(&elements[2]); |
| 83 | |
| 84 | // list should be empty again |
| 85 | CheckList(list, reporter, true, 0, false, false, false, false, elements); |
| 86 | } |
| 87 | |
| 88 | #include "TestClassDef.h" |
| 89 | DEFINE_TESTCLASS("TDLinkedList", TestTDLinkedListClass, TestTDLinkedList) |