From 74b63414259fc55896d046e9f0dc6ccea1e25717 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Fri, 25 Jan 2019 23:19:12 +0100 Subject: [PATCH] Fixed unit tests, added swap function to tl::list --- src/tl/tl/tlList.h | 21 ++++++++++++++++++++- src/tl/unit_tests/tlListTests.cc | 23 ++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/tl/tl/tlList.h b/src/tl/tl/tlList.h index 2d09e868e..607fb27e7 100644 --- a/src/tl/tl/tlList.h +++ b/src/tl/tl/tlList.h @@ -28,6 +28,7 @@ #include "tlTypeTraits.h" #include +#include namespace tl { @@ -141,6 +142,24 @@ public: } } + void swap (list_impl &other) + { + std::swap (m_head.mp_next, other.m_head.mp_next); + if (m_head.mp_next) { + m_head.mp_next->mp_prev = &m_head; + } + if (other.m_head.mp_next) { + other.m_head.mp_next->mp_prev = &other.m_head; + } + std::swap (m_back.mp_prev, other.m_back.mp_prev); + if (m_back.mp_prev) { + m_back.mp_prev->mp_next = &m_back; + } + if (other.m_back.mp_prev) { + other.m_back.mp_prev->mp_next = &other.m_back; + } + } + bool empty () const { return m_head.mp_next == &m_back; @@ -303,7 +322,7 @@ public: list_impl () { } list_impl (const list_impl &other) - : list_impl (other) + : list_impl () { operator= (other); } diff --git a/src/tl/unit_tests/tlListTests.cc b/src/tl/unit_tests/tlListTests.cc index c6831c702..33e229a84 100644 --- a/src/tl/unit_tests/tlListTests.cc +++ b/src/tl/unit_tests/tlListTests.cc @@ -263,9 +263,24 @@ TEST(1_Basic) EXPECT_EQ (l2sr (l3), "1,17"); EXPECT_EQ (l3.size (), size_t (2)); + EXPECT_EQ (l2sr (l2), "17"); + EXPECT_EQ (l2sr (l3), "1,17"); + l3.swap (l2); + EXPECT_EQ (l2sr (l2), "1,17"); + EXPECT_EQ (l2sr (l3), "17"); + + l1.clear (); + l2.swap (l1); + EXPECT_EQ (l2sr (l1), "1,17"); + EXPECT_EQ (l2sr (l2), ""); + l1.clear (); - l2.clear (); l3.clear (); + + l2.swap (l1); + EXPECT_EQ (l2sr (l1), ""); + EXPECT_EQ (l2sr (l2), ""); + EXPECT_EQ (obj_count, size_t (0)); } @@ -383,6 +398,12 @@ TEST(2_BasicNoCopy) EXPECT_EQ (l2sr (l3), "1,17"); EXPECT_EQ (l3.size (), size_t (2)); + EXPECT_EQ (l2sr (l2), "17"); + EXPECT_EQ (l2sr (l3), "1,17"); + l3.swap (l2); + EXPECT_EQ (l2sr (l2), "1,17"); + EXPECT_EQ (l2sr (l3), "17"); + l1.clear (); l2.clear (); l3.clear ();