diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 3 | ||||
-rw-r--r-- | test/list.cpp | 87 | ||||
-rw-r--r-- | test/ptrlist.cpp | 27 | ||||
-rw-r--r-- | test/util.cpp | 15 |
4 files changed, 132 insertions, 0 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bba1d07..96c0e95 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,3 +1,6 @@ target_sources(test_main PUBLIC string.cpp + list.cpp + ptrlist.cpp + util.cpp ) 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); +} + diff --git a/test/ptrlist.cpp b/test/ptrlist.cpp new file mode 100644 index 0000000..ba955c5 --- /dev/null +++ b/test/ptrlist.cpp @@ -0,0 +1,27 @@ +#include <gtest/gtest.h> + +#include "backend/PtrList.h" + +class FooBar { + int val = 3; + bool other = 4; +}; + +TEST(PtrListTest, FreePointers) { + // PtrList only works on classes: + FooBar * ptr1 = new FooBar(); + FooBar * ptr2 = new FooBar(); + + { + PtrList<FooBar> foo; + + foo.push_back(ptr1); + foo.push_back(ptr2); + + // this destructor SHOULD free ptr1 and ptr2 + } + + ASSERT_NE(ptr1, nullptr); + ASSERT_NE(ptr2, nullptr); +} + diff --git a/test/util.cpp b/test/util.cpp new file mode 100644 index 0000000..e90882a --- /dev/null +++ b/test/util.cpp @@ -0,0 +1,15 @@ +#include <gtest/gtest.h> + +#include "backend/util.h" + +TEST(Util, SafeFree) { + char * str = static_cast<char *>(malloc(3)); + ASSERT_NE(str, nullptr); + + safe_free(&str); + ASSERT_EQ(str, nullptr); + + // this shouldn't cause double free() error: + safe_free(&str); +} + |