diff --git a/src/tests/kernel/core/util/DoublyLinkedListTest.cpp b/src/tests/kernel/core/util/DoublyLinkedListTest.cpp new file mode 100644 index 0000000000..4d05b58d15 --- /dev/null +++ b/src/tests/kernel/core/util/DoublyLinkedListTest.cpp @@ -0,0 +1,191 @@ +/* +** Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the OpenBeOS License. +*/ + + +#include "DoublyLinkedListTest.h" +#include +#include +#include +#include +#include + +#include "DoublyLinkedList.h" + + +// Class used for testing without offset +class ItemWithout { + public: + DoublyLinked::Link fLink; + int32 value; +}; + +// Class used for testing with offset +class ItemWith { + public: + int32 value; + DoublyLinked::Link fLink; +}; + +// Class used for testing without offset +class ItemVirtualWithout { + public: + virtual int32 Value(); + + DoublyLinked::Link fLink; + int32 value; +}; + +// Class used for testing with offset +class ItemVirtualWith { + public: + virtual int32 Value(); + + int32 value; + DoublyLinked::Link fLink; +}; + + +int32 +ItemVirtualWithout::Value() +{ + return value; +} + + +int32 +ItemVirtualWith::Value() +{ + return value; +} + + +// #pragma mark - + + +DoublyLinkedListTest::DoublyLinkedListTest(std::string name) + : BTestCase(name) +{ +} + + +CppUnit::Test* +DoublyLinkedListTest::Suite() { + CppUnit::TestSuite *suite = new CppUnit::TestSuite("DLL"); + + suite->addTest(new CppUnit::TestCaller("DoublyLinkedList::no offset", &DoublyLinkedListTest::WithoutOffsetTest)); + suite->addTest(new CppUnit::TestCaller("DoublyLinkedList::with offset", &DoublyLinkedListTest::WithOffsetTest)); + suite->addTest(new CppUnit::TestCaller("DoublyLinkedList::virtual no offset", &DoublyLinkedListTest::VirtualWithoutOffsetTest)); + suite->addTest(new CppUnit::TestCaller("DoublyLinkedList::virtual with offset", &DoublyLinkedListTest::VirtualWithOffsetTest)); + + return suite; +} + + +//! Tests the given list + +template +void +DoublyLinkedListTest::TestList() +{ + DoublyLinked::List list; + int valueCount = 10; + Item items[valueCount]; + + // initialize + + for (int i = 0; i < valueCount; i++) { + items[i].value = i; + list.Add(&items[i]); + } + + // list must not be empty + + CHK(!list.IsEmpty()); + + // count items in list + + int count = 0; + DoublyLinked::Iterator iterator = list.Iterator(); + while (iterator.Next() != NULL) + count++; + + CHK(count == valueCount); + + // test for equality + + iterator = list.Iterator(); + + int i = 0; + Item *item; + while ((item = iterator.Next()) != NULL) { + CHK(item->value == i); + CHK(item == &items[i]); + i++; + } + + // remove first + + Item *first = list.RemoveHead(); + CHK(first->value == 0); + CHK(first == &items[0]); + + // remove every second + + iterator = list.Iterator(); + i = 0; + while ((item = iterator.Next()) != NULL) { + CHK(item->value == i + 1); + + if (i % 2) + list.Remove(item); + i++; + } + + // re-add first + + list.Add(first); + + // count again + + count = 0; + iterator = list.Iterator(); + while (iterator.Next() != NULL) + count++; + + CHK(count == (valueCount / 2) + 1); +} + + +//! Test using no offset, no virtual + +void +DoublyLinkedListTest::WithoutOffsetTest() { + TestList(); +} + + +//! Test using offset, no virtual + +void +DoublyLinkedListTest::WithOffsetTest() { + TestList(); +} + + +//! Test using no offset, virtual + +void +DoublyLinkedListTest::VirtualWithoutOffsetTest() { + TestList(); +} + + +//! Test using offset, virtual + +void +DoublyLinkedListTest::VirtualWithOffsetTest() { + TestList(); +} + diff --git a/src/tests/kernel/core/util/DoublyLinkedListTest.h b/src/tests/kernel/core/util/DoublyLinkedListTest.h new file mode 100644 index 0000000000..f36c76f840 --- /dev/null +++ b/src/tests/kernel/core/util/DoublyLinkedListTest.h @@ -0,0 +1,21 @@ +#ifndef _DOUBLY_LINKED_LIST_TEST_H_ +#define _DOUBLY_LINKED_LIST_TEST_H_ + +#include + +class DoublyLinkedListTest : public BTestCase { + public: + DoublyLinkedListTest(std::string name = ""); + + static CppUnit::Test *Suite(); + + void WithoutOffsetTest(); + void WithOffsetTest(); + void VirtualWithoutOffsetTest(); + void VirtualWithOffsetTest(); + + private: + template void TestList(); +}; + +#endif /* _DOUBLY_LINKED_LIST_TEST_H_ */ diff --git a/src/tests/kernel/core/util/Jamfile b/src/tests/kernel/core/util/Jamfile index 55effbf92e..6e0b10a169 100644 --- a/src/tests/kernel/core/util/Jamfile +++ b/src/tests/kernel/core/util/Jamfile @@ -9,6 +9,7 @@ CommonTestLib libkernelutilstest.so : KernelUtilsTestAddon.cpp # AVLTreeMapTest.cpp SinglyLinkedListTest.cpp + DoublyLinkedListTest.cpp VectorMapTest.cpp VectorSetTest.cpp VectorTest.cpp diff --git a/src/tests/kernel/core/util/KernelUtilsTestAddon.cpp b/src/tests/kernel/core/util/KernelUtilsTestAddon.cpp index 829cdd1743..0a38b81d6d 100644 --- a/src/tests/kernel/core/util/KernelUtilsTestAddon.cpp +++ b/src/tests/kernel/core/util/KernelUtilsTestAddon.cpp @@ -2,14 +2,17 @@ #include //#include #include +#include #include #include #include + BTestSuite* getTestSuite() { BTestSuite *suite = new BTestSuite("KernelUtils"); // suite->addTest("AVLTreeMap", AVLTreeMapTest::Suite()); suite->addTest("SinglyLinkedList", SinglyLinkedListTest::Suite()); + suite->addTest("DoublyLinkedList", DoublyLinkedListTest::Suite()); suite->addTest("VectorMap", VectorMapTest::Suite()); suite->addTest("VectorSet", VectorSetTest::Suite()); suite->addTest("Vector", VectorTest::Suite());