2015-08-31 05:34:49 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2015-08-31 05:34:49 +02:00
|
|
|
|
|
|
|
struct test_node : list_node<test_node>
|
|
|
|
{
|
2016-07-10 20:27:42 +02:00
|
|
|
explicit test_node(int v) : val(v) {}
|
2015-08-31 05:34:49 +02:00
|
|
|
int val;
|
|
|
|
};
|
|
|
|
|
|
|
|
void compare(linked_list<test_node> const& list, int* array, int size)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(list.size(), size);
|
|
|
|
|
|
|
|
int idx = 0;
|
2016-07-09 22:26:26 +02:00
|
|
|
for (test_node const* i = list.front(); i != nullptr; i = i->next, ++idx)
|
2015-08-31 05:34:49 +02:00
|
|
|
{
|
|
|
|
TEST_EQUAL(i->val, array[idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(push_back)
|
|
|
|
{
|
|
|
|
test_node n0(0);
|
|
|
|
test_node n1(1);
|
|
|
|
|
|
|
|
linked_list<test_node> 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<test_node> 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<test_node> 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<test_node> 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<test_node> 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<test_node> list;
|
|
|
|
|
|
|
|
list.push_back(&n0);
|
|
|
|
|
|
|
|
list.erase(&n0);
|
|
|
|
|
2015-08-31 06:19:58 +02:00
|
|
|
int expected[] = { -1 };
|
2015-08-31 05:34:49 +02:00
|
|
|
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<test_node> list;
|
|
|
|
|
|
|
|
list.push_back(&n0);
|
|
|
|
list.push_back(&n1);
|
|
|
|
list.push_back(&n2);
|
|
|
|
|
|
|
|
list_iterator<test_node> it = list.iterate();
|
|
|
|
TEST_EQUAL(it.get(), &n0);
|
|
|
|
it.next();
|
|
|
|
TEST_EQUAL(it.get(), &n1);
|
|
|
|
it.next();
|
|
|
|
TEST_EQUAL(it.get(), &n2);
|
|
|
|
it.next();
|
2016-07-09 22:26:26 +02:00
|
|
|
TEST_EQUAL(it.get(), static_cast<test_node*>(nullptr));
|
2015-08-31 05:34:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(iterate_backward)
|
|
|
|
{
|
|
|
|
test_node n0(0);
|
|
|
|
test_node n1(1);
|
|
|
|
test_node n2(2);
|
|
|
|
|
|
|
|
linked_list<test_node> list;
|
|
|
|
|
|
|
|
list.push_back(&n0);
|
|
|
|
list.push_back(&n1);
|
|
|
|
list.push_back(&n2);
|
|
|
|
|
|
|
|
list_iterator<test_node> 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();
|
2016-07-09 22:26:26 +02:00
|
|
|
TEST_EQUAL(it.get(), static_cast<test_node*>(nullptr));
|
2015-08-31 05:34:49 +02:00
|
|
|
}
|
|
|
|
|