#include "coco/ADT/DLinkedList.h"
+#include <set>
+
#include <gtest/gtest.h>
namespace
} // namespace coco
-TEST(ADT_DLINKED_LINK, insert_two_elements)
+namespace
+{
+class DLinkedListTest : public ::testing::Test
+{
+public:
+ virtual ~DLinkedListTest()
+ {
+ // NOTE Child SHOULD BE freed before parent
+ for (auto child : _children)
+ {
+ delete child;
+ }
+
+ for (auto parent : _parents)
+ {
+ delete parent;
+ }
+ }
+
+protected:
+ template <typename T> T *create(void);
+
+ void destroy(Child *);
+
+private:
+ std::set<::Parent *> _parents;
+ std::set<::Child *> _children;
+};
+
+template <>::Parent *DLinkedListTest::create(void)
{
auto parent = new ::Parent;
+ _parents.insert(parent);
+ return parent;
+}
+
+template <>::Child *DLinkedListTest::create(void)
+{
+ auto child = new ::Child;
+ _children.insert(child);
+ return child;
+}
+
+void DLinkedListTest::destroy(Child *child)
+{
+ _children.erase(child);
+ delete child;
+}
+
+} // namespace
+
+TEST_F(DLinkedListTest, insert_two_elements)
+{
+ auto parent = create<::Parent>();
ASSERT_EQ(parent->children()->head(), nullptr);
ASSERT_EQ(parent->children()->tail(), nullptr);
- auto child_1 = new ::Child;
+ auto child_1 = create<::Child>();
ASSERT_EQ(child_1->parent(), nullptr);
ASSERT_EQ(child_1->prev(), nullptr);
ASSERT_EQ(parent->children()->head(), child_1);
ASSERT_EQ(parent->children()->tail(), child_1);
- auto child_2 = new ::Child;
+ auto child_2 = create<::Child>();
ASSERT_EQ(child_2->parent(), nullptr);
ASSERT_EQ(child_2->prev(), nullptr);
ASSERT_EQ(parent->children()->head(), child_1);
ASSERT_EQ(parent->children()->tail(), child_2);
-
- delete child_2;
- delete child_1;
- delete parent;
}
-TEST(ADT_DLINKED_LINK, insertBefore)
+TEST_F(DLinkedListTest, insertBefore)
{
- auto parent = new ::Parent;
+ auto parent = create<::Parent>();
- auto child_1 = new ::Child;
- auto child_2 = new ::Child;
+ auto child_1 = create<::Child>();
+ auto child_2 = create<::Child>();
parent->children()->append(child_1);
child_2->insertBefore(child_1);
ASSERT_EQ(parent->children()->head(), child_2);
ASSERT_EQ(parent->children()->tail(), child_1);
-
- delete child_2;
- delete child_1;
- delete parent;
}
-TEST(ADT_DLINKED_LINK, prepend_after_append)
+TEST_F(DLinkedListTest, prepend_after_append)
{
- auto parent = new ::Parent;
+ auto parent = create<::Parent>();
- auto child_1 = new ::Child;
- auto child_2 = new ::Child;
+ auto child_1 = create<::Child>();
+ auto child_2 = create<::Child>();
parent->children()->append(child_1);
parent->children()->prepend(child_2);
ASSERT_EQ(parent->children()->head(), child_2);
ASSERT_EQ(parent->children()->tail(), child_1);
-
- delete child_2;
- delete child_1;
- delete parent;
}
-TEST(ADT_DLINKED_LINK, detach)
+TEST_F(DLinkedListTest, detach)
{
- auto parent = new ::Parent;
+ auto parent = create<::Parent>();
- auto child_1 = new ::Child;
- auto child_2 = new ::Child;
+ auto child_1 = create<::Child>();
+ auto child_2 = create<::Child>();
parent->children()->append(child_1);
parent->children()->append(child_2);
ASSERT_TRUE(parent->children()->empty());
ASSERT_EQ(parent->children()->head(), nullptr);
ASSERT_EQ(parent->children()->tail(), nullptr);
-
- delete child_2;
- delete child_1;
- delete parent;
}
-TEST(ADT_DLINKED_LINK, node_destructor)
+TEST_F(DLinkedListTest, node_destructor)
{
- auto parent = new ::Parent;
+ auto parent = create<::Parent>();
- auto child_1 = new ::Child;
- auto child_2 = new ::Child;
+ auto child_1 = create<::Child>();
+ auto child_2 = create<::Child>();
parent->children()->append(child_1);
parent->children()->append(child_2);
- delete child_2;
+ destroy(child_2);
ASSERT_EQ(parent->children()->head(), child_1);
ASSERT_EQ(parent->children()->tail(), child_1);
ASSERT_EQ(child_1->next(), nullptr);
ASSERT_EQ(child_1->prev(), nullptr);
- delete child_1;
+ destroy(child_1);
ASSERT_EQ(parent->children()->head(), nullptr);
ASSERT_EQ(parent->children()->tail(), nullptr);
-
- delete parent;
}