1 /* 2 ** Copyright 2003-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3 ** Distributed under the terms of the OpenBeOS License. 4 */ 5 6 7 #include "DoublyLinkedListTest.h" 8 9 #include <cppunit/TestCaller.h> 10 #include <cppunit/TestSuite.h> 11 #include <TestUtils.h> 12 13 #include <util/DoublyLinkedList.h> 14 15 16 // Class used for testing without offset 17 class ItemWithout { 18 public: 19 DoublyLinked::Link fLink; 20 int32 value; 21 }; 22 23 // Class used for testing with offset 24 class ItemWith { 25 public: 26 int32 value; 27 DoublyLinked::Link fLink; 28 }; 29 30 // Class used for testing without offset 31 class ItemVirtualWithout { 32 public: 33 virtual int32 Value(); 34 35 DoublyLinked::Link fLink; 36 int32 value; 37 }; 38 39 // Class used for testing with offset 40 class ItemVirtualWith { 41 public: 42 virtual int32 Value(); 43 44 int32 value; 45 DoublyLinked::Link fLink; 46 }; 47 48 49 int32 50 ItemVirtualWithout::Value() 51 { 52 return value; 53 } 54 55 56 int32 57 ItemVirtualWith::Value() 58 { 59 return value; 60 } 61 62 63 // #pragma mark - 64 65 66 DoublyLinkedListTest::DoublyLinkedListTest(std::string name) 67 : BTestCase(name) 68 { 69 } 70 71 72 CppUnit::Test* 73 DoublyLinkedListTest::Suite() { 74 CppUnit::TestSuite *suite = new CppUnit::TestSuite("DLL"); 75 76 suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::no offset", &DoublyLinkedListTest::WithoutOffsetTest)); 77 suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::with offset", &DoublyLinkedListTest::WithOffsetTest)); 78 suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::virtual no offset", &DoublyLinkedListTest::VirtualWithoutOffsetTest)); 79 suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::virtual with offset", &DoublyLinkedListTest::VirtualWithOffsetTest)); 80 81 return suite; 82 } 83 84 85 //! Tests the given list 86 87 template <typename Item> 88 void 89 DoublyLinkedListTest::TestList() 90 { 91 DoublyLinked::List<Item> list; 92 int valueCount = 10; 93 Item items[valueCount]; 94 95 // initialize 96 97 for (int i = 0; i < valueCount; i++) { 98 items[i].value = i; 99 list.Add(&items[i]); 100 } 101 102 // list must not be empty 103 104 CHK(!list.IsEmpty()); 105 106 // count items in list 107 108 int count = 0; 109 DoublyLinked::Iterator<Item> iterator = list.Iterator(); 110 while (iterator.Next() != NULL) 111 count++; 112 113 CHK(count == valueCount); 114 115 // test for equality 116 117 iterator = list.Iterator(); 118 119 int i = 0; 120 Item *item; 121 while ((item = iterator.Next()) != NULL) { 122 CHK(item->value == i); 123 CHK(item == &items[i]); 124 i++; 125 } 126 127 // remove first 128 129 Item *first = list.RemoveHead(); 130 CHK(first->value == 0); 131 CHK(first == &items[0]); 132 133 // remove every second 134 135 iterator = list.Iterator(); 136 i = 0; 137 while ((item = iterator.Next()) != NULL) { 138 CHK(item->value == i + 1); 139 140 if (i % 2) 141 list.Remove(item); 142 i++; 143 } 144 145 // re-add first 146 147 list.Add(first); 148 149 // count again 150 151 count = 0; 152 iterator = list.Iterator(); 153 while (iterator.Next() != NULL) 154 count++; 155 156 CHK(count == (valueCount / 2) + 1); 157 } 158 159 160 //! Test using no offset, no virtual 161 162 void 163 DoublyLinkedListTest::WithoutOffsetTest() { 164 TestList<ItemWithout>(); 165 } 166 167 168 //! Test using offset, no virtual 169 170 void 171 DoublyLinkedListTest::WithOffsetTest() { 172 TestList<ItemWith>(); 173 } 174 175 176 //! Test using no offset, virtual 177 178 void 179 DoublyLinkedListTest::VirtualWithoutOffsetTest() { 180 TestList<ItemVirtualWithout>(); 181 } 182 183 184 //! Test using offset, virtual 185 186 void 187 DoublyLinkedListTest::VirtualWithOffsetTest() { 188 TestList<ItemVirtualWith>(); 189 } 190 191