diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-02 15:55:58 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-02 15:55:58 +0100 |
commit | 5e4dd0c0197f6273c61491a5b9a030c93f796a12 (patch) | |
tree | 390fc6d980cc49927523befb43d189c608452276 /test/list.cpp | |
parent | 369e3d71aa79783d95166739cfa93a480defe6ea (diff) |
add some tests
Diffstat (limited to 'test/list.cpp')
-rw-r--r-- | test/list.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/test/list.cpp b/test/list.cpp new file mode 100644 index 0000000..cdb0bc1 --- /dev/null +++ b/test/list.cpp @@ -0,0 +1,87 @@ +#include <gtest/gtest.h> + +#include "backend/ListIterator.h" +#include "backend/List.h" + +static void add_test_items(List<int> & foo) { + foo.push_back(1); + foo.push_back(2); + foo.push_back(3); +} + +TEST(ListTest, PushBack) { + List<int> foo; + ASSERT_EQ(foo.size(), 0); + + add_test_items(foo); + + ASSERT_EQ(foo.size(), 3); +} + +TEST(ListTest, Clear) { + List<int> foo; + foo.clear(); + + add_test_items(foo); + + foo.clear(); + ASSERT_EQ(foo.size(), 0); +} + +TEST(ListTest, Remove) { + List<int> foo; + add_test_items(foo); + + ASSERT_EQ(foo.size(), 3); + foo.remove(2); + ASSERT_EQ(foo.size(), 2); + foo.remove(2); + ASSERT_EQ(foo.size(), 2); +} + +TEST(ListTest, RemoveDuplicates) { + List<int> foo; + + add_test_items(foo); + foo.push_back(2); + + ASSERT_EQ(foo.size(), 4); + foo.remove(2); + ASSERT_EQ(foo.size(), 3); + foo.remove(2); + ASSERT_EQ(foo.size(), 2); +} + +TEST(ListTest, RemoveDuringIterator) { + List<int> foo; + add_test_items(foo); + + for (int & value : foo) { + if (value == 2) + foo.remove(value); + } + + ASSERT_EQ(foo.size(), 2); +} + +TEST(ListTest, UnmanagedPointers) { + char * ptr1 = static_cast<char *>(malloc(3)); + char * ptr2 = static_cast<char *>(malloc(3)); + + { + List<char *> foo; + + foo.push_back(ptr1); + foo.push_back(ptr2); + + // this destructor shouldn't free ptr1 and ptr2 + } + + ASSERT_NE(ptr1, nullptr); + ASSERT_NE(ptr2, nullptr); + + // thus, these shouldn't cause double free() errors: + free(ptr1); + free(ptr2); +} + |