diff --git a/src/tests/kernel/core/util/SinglyLinkedListTest.cpp b/src/tests/kernel/core/util/SinglyLinkedListTest.cpp new file mode 100644 index 0000000000..c2c7b2df90 --- /dev/null +++ b/src/tests/kernel/core/util/SinglyLinkedListTest.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include + +#include "SinglyLinkedList.h" + +SinglyLinkedListTest::SinglyLinkedListTest(std::string name) + : BTestCase(name) +{ +} + +CppUnit::Test* +SinglyLinkedListTest::Suite() { + CppUnit::TestSuite *suite = new CppUnit::TestSuite("SLL"); + + suite->addTest(new CppUnit::TestCaller("SinglyLinkedList::User Strategy Test (default next parameter)", &SinglyLinkedListTest::UserDefaultTest)); + suite->addTest(new CppUnit::TestCaller("SinglyLinkedList::User Strategy Test (custom next parameter)", &SinglyLinkedListTest::UserCustomTest)); + suite->addTest(new CppUnit::TestCaller("SinglyLinkedList::Auto Strategy Test (MallocFreeAllocator)", &SinglyLinkedListTest::AutoTest)); + + return suite; +} + +// Class used for testing default User strategy +class Link { +public: + Link* next; + long data; + + bool operator==(const Link &ref) { + return data == ref.data; + } +}; + +// Class used for testing custom User strategy +class MyLink { +public: + MyLink* mynext; + long data; + + bool operator==(const MyLink &ref) { + return data == ref.data; + } +}; + +using Strategy::SinglyLinkedList::User; +using Strategy::SinglyLinkedList::Auto; + +//! Tests the given list +template +void +SinglyLinkedListTest::TestList(List &list, typename List::ValueType *values, int valueCount) +{ + list.MakeEmpty(); + + // PushFront + for (int i = 0; i < valueCount; i++) { + NextSubTest(); + CHK(list.Count() == i); + CHK(list.PushFront(values[i]) == B_OK); + CHK(list.Count() == i+1); + } + + // Prefix increment + int preIndex = valueCount-1; + for (typename List::Iterator iterator = list.Begin(); iterator != list.End(); --preIndex) { + NextSubTest(); +// printf("(%p, %ld) %s (%p, %ld)\n", iterator->next, iterator->data, ((*iterator == values[preIndex]) ? "==" : "!="), values[preIndex].next, values[preIndex].data); + CHK(*iterator == values[preIndex]); + typename List::Iterator copy = iterator; + CHK(copy == iterator); + CHK(copy != ++iterator); + } + CHK(preIndex == -1); + + list.MakeEmpty(); + + // PushBack + for (int i = 0; i < valueCount; i++) { + NextSubTest(); + CHK(list.Count() == i); + CHK(list.PushBack(values[i]) == B_OK); + CHK(list.Count() == i+1); + } + + // Postfix increment + int postIndex = 0; + for (typename List::Iterator iterator = list.Begin(); iterator != list.End(); ++postIndex) { + NextSubTest(); +// printf("(%p, %ld) %s (%p, %ld)\n", iterator->next, iterator->data, ((*iterator == values[postIndex]) ? "==" : "!="), values[postIndex].next, values[postIndex].data); + CHK(*iterator == values[postIndex]); + typename List::Iterator copy = iterator; + CHK(copy == iterator); + CHK(copy == iterator++); + } + CHK(postIndex == valueCount); + +} + +//! Test using the User strategy with the default NextMember. +void +SinglyLinkedListTest::UserDefaultTest() { + SinglyLinkedList > list; + const int valueCount = 10; + Link values[valueCount]; + for (int i = 0; i < valueCount; i++) { + values[i].data = i; + if (i % 2) + values[i].next = NULL; // Leave some next pointers invalid just for fun + } + + TestList(list, values, valueCount); +} + +//! Test using the User strategy with a custom NextMember. +void +SinglyLinkedListTest::UserCustomTest() { + SinglyLinkedList > list; + const int valueCount = 10; + MyLink values[valueCount]; + for (int i = 0; i < valueCount; i++) { + values[i].data = i*2; + if (!(i % 2)) + values[i].mynext = NULL; // Leave some next pointers invalid just for fun + } + + TestList(list, values, valueCount); +} + +//! Test using the Auto strategy. +void +SinglyLinkedListTest::AutoTest() { + SinglyLinkedList > list; + const int valueCount = 10; + long values[valueCount]; + for (int i = 0; i < valueCount; i++) { + values[i] = i*3; + } + + TestList(list, values, valueCount); +} diff --git a/src/tests/kernel/core/util/SinglyLinkedListTest.h b/src/tests/kernel/core/util/SinglyLinkedListTest.h new file mode 100644 index 0000000000..76747876ee --- /dev/null +++ b/src/tests/kernel/core/util/SinglyLinkedListTest.h @@ -0,0 +1,20 @@ +#ifndef _single_linked_list_test_h_ +#define _single_linked_list_test_h_ + +#include + +class SinglyLinkedListTest : public BTestCase { +public: + SinglyLinkedListTest(std::string name = ""); + + static CppUnit::Test* Suite(); + + void UserDefaultTest(); + void UserCustomTest(); + void AutoTest(); +private: + template + void TestList(List &list, typename List::ValueType *values, int valueCount); +}; + +#endif // _single_linked_list_test_h_