Tests for the SinglyLinkedList class.
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3626 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
17befbdd60
commit
1fe39d4d37
143
src/tests/kernel/core/util/SinglyLinkedListTest.cpp
Normal file
143
src/tests/kernel/core/util/SinglyLinkedListTest.cpp
Normal file
@ -0,0 +1,143 @@
|
||||
#include <SinglyLinkedListTest.h>
|
||||
#include <cppunit/Test.h>
|
||||
#include <cppunit/TestCaller.h>
|
||||
#include <cppunit/TestSuite.h>
|
||||
#include <stdio.h>
|
||||
#include <TestUtils.h>
|
||||
|
||||
#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<SinglyLinkedListTest>("SinglyLinkedList::User Strategy Test (default next parameter)", &SinglyLinkedListTest::UserDefaultTest));
|
||||
suite->addTest(new CppUnit::TestCaller<SinglyLinkedListTest>("SinglyLinkedList::User Strategy Test (custom next parameter)", &SinglyLinkedListTest::UserCustomTest));
|
||||
suite->addTest(new CppUnit::TestCaller<SinglyLinkedListTest>("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 <class List>
|
||||
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<User<Link> > 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<User<MyLink, &MyLink::mynext> > 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<Auto<long> > list;
|
||||
const int valueCount = 10;
|
||||
long values[valueCount];
|
||||
for (int i = 0; i < valueCount; i++) {
|
||||
values[i] = i*3;
|
||||
}
|
||||
|
||||
TestList(list, values, valueCount);
|
||||
}
|
20
src/tests/kernel/core/util/SinglyLinkedListTest.h
Normal file
20
src/tests/kernel/core/util/SinglyLinkedListTest.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef _single_linked_list_test_h_
|
||||
#define _single_linked_list_test_h_
|
||||
|
||||
#include <TestCase.h>
|
||||
|
||||
class SinglyLinkedListTest : public BTestCase {
|
||||
public:
|
||||
SinglyLinkedListTest(std::string name = "");
|
||||
|
||||
static CppUnit::Test* Suite();
|
||||
|
||||
void UserDefaultTest();
|
||||
void UserCustomTest();
|
||||
void AutoTest();
|
||||
private:
|
||||
template <class List>
|
||||
void TestList(List &list, typename List::ValueType *values, int valueCount);
|
||||
};
|
||||
|
||||
#endif // _single_linked_list_test_h_
|
Loading…
x
Reference in New Issue
Block a user