From b4381aa6463839dc6f21dbc822e14f0571dd42d4 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sun, 30 Aug 2015 23:34:49 -0400 Subject: [PATCH] add unit test for linked_list --- include/libtorrent/linked_list.hpp | 1 - test/Jamfile | 1 + test/Makefile.am | 4 +- test/test_linked_list.cpp | 198 +++++++++++++++++++++++++++++ 4 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 test/test_linked_list.cpp diff --git a/include/libtorrent/linked_list.hpp b/include/libtorrent/linked_list.hpp index 43de017ff..036ee5db1 100644 --- a/include/libtorrent/linked_list.hpp +++ b/include/libtorrent/linked_list.hpp @@ -63,7 +63,6 @@ namespace libtorrent T* m_current; }; - // TOOD: 3 add a unit test // T must derive from list_node. Having an enable_if here would require T // to be a complete type, which is a bit too restrictive. template diff --git a/test/Jamfile b/test/Jamfile index 0379bb67f..4523cd66c 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -145,6 +145,7 @@ test-suite libtorrent : test_dos_blocker.cpp test_stat_cache.cpp test_enum_net.cpp + test_linked_list.cpp test_file_progress.cpp ] [ run test_direct_dht.cpp ] diff --git a/test/Makefile.am b/test/Makefile.am index 4af2d5440..93ba04263 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -30,7 +30,8 @@ test_programs = \ test_url_seed \ test_remap_files \ test_enum_net \ - test_file_progress + test_file_progress \ + test_linked_list if ENABLE_TESTS check_PROGRAMS = $(test_programs) @@ -184,6 +185,7 @@ test_web_seed_SOURCES = test_web_seed.cpp test_url_seed_SOURCES = test_url_seed.cpp test_remap_files_SOURCES = test_remap_files.cpp test_file_progress_SOURCES = test_file_progress.cpp +test_linked_list_SOURCES = test_linked_list.cpp LDADD = libtest.la $(top_builddir)/src/libtorrent-rasterbar.la diff --git a/test/test_linked_list.cpp b/test/test_linked_list.cpp new file mode 100644 index 000000000..cf7efd655 --- /dev/null +++ b/test/test_linked_list.cpp @@ -0,0 +1,198 @@ +/* + +Copyright (c) 2015, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#include "test.hpp" +#include "libtorrent/linked_list.hpp" + +using namespace libtorrent; + +struct test_node : list_node +{ + test_node(int v) : val(v) {} + int val; +}; + +void compare(linked_list const& list, int* array, int size) +{ + TEST_EQUAL(list.size(), size); + + int idx = 0; + for (test_node const* i = list.front(); i != NULL; i = i->next, ++idx) + { + TEST_EQUAL(i->val, array[idx]); + } +} + +TORRENT_TEST(push_back) +{ + test_node n0(0); + test_node n1(1); + + linked_list list; + + list.push_back(&n0); + list.push_back(&n1); + + int expected[] = { 0, 1 }; + compare(list, expected, 2); +} + +TORRENT_TEST(push_front) +{ + test_node n0(0); + test_node n1(1); + + linked_list list; + + list.push_back(&n1); + list.push_front(&n0); + + int expected[] = { 0, 1 }; + compare(list, expected, 2); +} + +TORRENT_TEST(erase_begin) +{ + test_node n0(0); + test_node n1(1); + test_node n2(2); + + linked_list list; + + list.push_back(&n0); + list.push_back(&n1); + list.push_back(&n2); + + list.erase(&n0); + + int expected[] = { 1, 2 }; + compare(list, expected, 2); +} + +TORRENT_TEST(erase_end) +{ + test_node n0(0); + test_node n1(1); + test_node n2(2); + + linked_list list; + + list.push_back(&n0); + list.push_back(&n1); + list.push_back(&n2); + + list.erase(&n2); + + int expected[] = { 0, 1 }; + compare(list, expected, 2); +} + +TORRENT_TEST(erase_middle) +{ + test_node n0(0); + test_node n1(1); + test_node n2(2); + + linked_list list; + + list.push_back(&n0); + list.push_back(&n1); + list.push_back(&n2); + + list.erase(&n1); + + int expected[] = { 0, 2 }; + compare(list, expected, 2); +} + +TORRENT_TEST(erase_last) +{ + test_node n0(0); + + linked_list list; + + list.push_back(&n0); + + list.erase(&n0); + + int expected[] = { }; + compare(list, expected, 0); + + TEST_CHECK(list.empty()); +} + +TORRENT_TEST(iterate_forward) +{ + test_node n0(0); + test_node n1(1); + test_node n2(2); + + linked_list list; + + list.push_back(&n0); + list.push_back(&n1); + list.push_back(&n2); + + list_iterator it = list.iterate(); + TEST_EQUAL(it.get(), &n0); + it.next(); + TEST_EQUAL(it.get(), &n1); + it.next(); + TEST_EQUAL(it.get(), &n2); + it.next(); + TEST_EQUAL(it.get(), static_cast(NULL)); +} + +TORRENT_TEST(iterate_backward) +{ + test_node n0(0); + test_node n1(1); + test_node n2(2); + + linked_list list; + + list.push_back(&n0); + list.push_back(&n1); + list.push_back(&n2); + + list_iterator it = list.iterate(); + it.next(); + it.next(); + TEST_EQUAL(it.get(), &n2); + it.prev(); + TEST_EQUAL(it.get(), &n1); + it.prev(); + TEST_EQUAL(it.get(), &n0); + it.prev(); + TEST_EQUAL(it.get(), static_cast(NULL)); +} +