#include #include "backend/ListIterator.h" #include "backend/List.h" static void add_test_items(List & foo) { foo.push_back(1); foo.push_back(2); foo.push_back(3); } TEST(ListTest, PushBack) { List foo; ASSERT_EQ(foo.size(), 0); add_test_items(foo); ASSERT_EQ(foo.size(), 3); } TEST(ListTest, Clear) { List foo; foo.clear(); add_test_items(foo); foo.clear(); ASSERT_EQ(foo.size(), 0); } TEST(ListTest, Remove) { List 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 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 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(malloc(3)); char * ptr2 = static_cast(malloc(3)); { List 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); }