diff --git a/test/linkedlist.cpp b/test/linkedlist.cpp new file mode 100644 index 0000000..092f9c2 --- /dev/null +++ b/test/linkedlist.cpp @@ -0,0 +1,137 @@ +#include "../sequential/linkedlist.hpp" +#include "testutil.hpp" + +bool TestAppending() +{ + unsigned const testSize = 5; + + std::vector truth; + List::List 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 truth; + List::List 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 numbers = { 5, 6, 4, 7, 3, 8, 2, 9, 1, 10 }; + List::List 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 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 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; +} \ No newline at end of file