Files
datastructures/test/linkedlist.cpp

137 lines
2.1 KiB
C++

#include "../sequential/linkedlist.hpp"
#include "testutil.hpp"
bool TestAppending()
{
unsigned const testSize = 5;
std::vector<unsigned> truth;
List::List<unsigned> list;
truth.resize(testSize);
for(unsigned i = 0; i < testSize; ++i)
{
unsigned const value = Util::GetRandomNumber();
truth[i] = value;
list.Append(value);
}
for(unsigned i = 0; i < testSize; ++i)
{
if(truth[i] != list[i])
{
return false;
}
}
return true;
}
bool TestPrepending()
{
unsigned const testSize = 5;
std::vector<unsigned> truth;
List::List<unsigned> list;
truth.resize(testSize);
for(unsigned i = testSize; i > 0; --i)
{
unsigned const value = Util::GetRandomNumber();
truth[i - 1] = value;
list.Prepend(value);
}
for(unsigned i = 0; i < testSize; ++i)
{
if(truth[i] != list[i])
{
return false;
}
}
return true;
}
bool TestPrependAppendMix()
{
std::vector<unsigned> numbers = { 5, 6, 4, 7, 3, 8, 2, 9, 1, 10 };
List::List<unsigned> list;
for(unsigned i = 0; i < numbers.size(); i += 2)
{
list.Prepend(numbers[i]);
list.Append(numbers[i + 1]);
}
for(unsigned i = 0; i < list.GetSize(); ++i)
{
if(list[i] != i + 1)
{
return false;
}
}
return true;
}
bool TestDeletion()
{
List::List<unsigned> list;
for(unsigned i = 1; i < 8; ++i)
{
list.Append(i);
}
for(unsigned i = 0; i < list.GetSize(); ++i)
{
list.Delete(i);
}
for(unsigned i = 0; i < list.GetSize(); ++i)
{
if(list[i] % 2 != 0)
{
return false;
}
}
return true;
}
bool TestInsertion()
{
unsigned const testSize = 11;
List::List<unsigned> list;
for(unsigned i = 1; i < testSize; i += 2)
{
list.Append(i);
}
for(unsigned i = 1; i < testSize - 2; i += 2)
{
list.Insert(i + 1, i);
}
for(unsigned i = 1; i <= list.GetSize(); ++i)
{
if(list[i - 1] != i)
{
return false;
}
}
return true;
}
int main()
{
Test::Execute(TestAppending, "Appending test");
Test::Execute(TestPrepending, "Prepending test");
Test::Execute(TestPrependAppendMix, "Prepend append mix test");
Test::Execute(TestDeletion, "Deletion test");
Test::Execute(TestInsertion, "Insertion test");
return 0;
}