Files
boost_intrusive/test/list_test.cpp

772 lines
22 KiB
C++
Raw Normal View History

2007-05-04 21:30:54 +00:00
/////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Olaf Krzikalla 2004-2006.
// (C) Copyright Ion Gaztanaga 2006-2007.
2007-05-04 21:30:54 +00:00
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/intrusive for documentation.
//
/////////////////////////////////////////////////////////////////////////////
2007-06-23 13:09:46 +00:00
#include <boost/intrusive/detail/config_begin.hpp>
2007-05-04 21:30:54 +00:00
#include <boost/intrusive/list.hpp>
2007-05-04 21:30:54 +00:00
#include <boost/intrusive/detail/pointer_to_other.hpp>
#include "itestvalue.hpp"
#include "smart_ptr.hpp"
#include "common_functors.hpp"
#include <vector>
#include <boost/detail/lightweight_test.hpp>
#include "test_macros.hpp"
2007-07-22 14:19:19 +00:00
#include "test_container.hpp"
2007-05-04 21:30:54 +00:00
using namespace boost::intrusive;
template<class ValueTraits>
struct test_list
{
typedef typename ValueTraits::value_type value_type;
static void test_all(std::vector<value_type>& values);
static void test_front_back(std::vector<value_type>& values);
static void test_sort(std::vector<value_type>& values);
static void test_merge(std::vector<value_type>& values);
static void test_remove_unique(std::vector<value_type>& values);
2007-05-04 21:30:54 +00:00
static void test_insert(std::vector<value_type>& values);
static void test_shift(std::vector<value_type>& values);
static void test_swap(std::vector<value_type>& values);
static void test_clone(std::vector<value_type>& values);
static void test_container_from_end(std::vector<value_type>& values);
2007-05-04 21:30:54 +00:00
};
template<class ValueTraits>
void test_list<ValueTraits>::test_all(std::vector<typename ValueTraits::value_type>& values)
{
typedef typename ValueTraits::value_type value_type;
typedef list
< value_type
, value_traits<ValueTraits>
, size_type<std::size_t>
, constant_time_size<value_type::constant_time_size>
2007-07-22 14:19:19 +00:00
> list_type;
{
list_type list(values.begin(), values.end());
test::test_container(list);
list.clear();
list.insert(list.end(), values.begin(), values.end());
test::test_sequence_container(list, values);
}
2007-05-04 21:30:54 +00:00
test_front_back(values);
test_sort(values);
test_merge(values);
test_remove_unique(values);
2007-05-04 21:30:54 +00:00
test_insert(values);
test_shift(values);
test_swap(values);
test_clone(values);
test_container_from_end(values);
2007-05-04 21:30:54 +00:00
}
//test: push_front, pop_front, push_back, pop_back, front, back, size, empty:
template<class ValueTraits>
void test_list<ValueTraits>
::test_front_back(std::vector<typename ValueTraits::value_type>& values)
{
typedef typename ValueTraits::value_type value_type;
typedef list
< value_type
, value_traits<ValueTraits>
, size_type<std::size_t>
, constant_time_size<value_type::constant_time_size>
2007-05-04 21:30:54 +00:00
> list_type;
list_type testlist;
BOOST_TEST (testlist.empty());
2007-05-04 21:30:54 +00:00
testlist.push_back (values[0]);
BOOST_TEST (testlist.size() == 1);
BOOST_TEST (&testlist.front() == &values[0]);
BOOST_TEST (&testlist.back() == &values[0]);
2007-05-04 21:30:54 +00:00
testlist.push_front (values[1]);
BOOST_TEST (testlist.size() == 2);
BOOST_TEST (&testlist.front() == &values[1]);
BOOST_TEST (&testlist.back() == &values[0]);
2007-05-04 21:30:54 +00:00
testlist.pop_back();
BOOST_TEST (testlist.size() == 1);
2007-05-04 21:30:54 +00:00
const list_type &const_testlist = testlist;
BOOST_TEST (&const_testlist.front() == &values[1]);
BOOST_TEST (&const_testlist.back() == &values[1]);
2007-05-04 21:30:54 +00:00
testlist.pop_front();
BOOST_TEST (testlist.empty());
2007-05-04 21:30:54 +00:00
}
//test: constructor, iterator, reverse_iterator, sort, reverse:
template<class ValueTraits>
void test_list<ValueTraits>
::test_sort(std::vector<typename ValueTraits::value_type>& values)
{
typedef typename ValueTraits::value_type value_type;
typedef list
< value_type
, value_traits<ValueTraits>
, size_type<std::size_t>
, constant_time_size<value_type::constant_time_size>
2007-05-04 21:30:54 +00:00
> list_type;
list_type testlist(values.begin(), values.end());
{ int init_values [] = { 1, 2, 3, 4, 5 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist.begin() ); }
2007-05-04 21:30:54 +00:00
testlist.sort (even_odd());
{ int init_values [] = { 5, 3, 1, 4, 2 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist.rbegin() ); }
2007-05-04 21:30:54 +00:00
testlist.reverse();
{ int init_values [] = { 5, 3, 1, 4, 2 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist.begin() ); }
}
//test: merge due to error in merge implementation:
template<class ValueTraits>
void test_list<ValueTraits>
::test_remove_unique (std::vector<typename ValueTraits::value_type>& values)
{
typedef typename ValueTraits::value_type value_type;
typedef list
< value_type
, value_traits<ValueTraits>
, size_type<std::size_t>
, constant_time_size<value_type::constant_time_size>
> list_type;
{
list_type list(values.begin(), values.end());
list.remove_if(is_even());
int init_values [] = { 1, 3, 5 };
TEST_INTRUSIVE_SEQUENCE( init_values, list.begin() );
}
{
std::vector<typename ValueTraits::value_type> values2(values);
list_type list(values.begin(), values.end());
list.insert(list.end(), values2.begin(), values2.end());
list.sort();
int init_values [] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
TEST_INTRUSIVE_SEQUENCE( init_values, list.begin() );
list.unique();
int init_values2 [] = { 1, 2, 3, 4, 5 };
TEST_INTRUSIVE_SEQUENCE( init_values2, list.begin() );
}
}
//test: merge due to error in merge implementation:
template<class ValueTraits>
void test_list<ValueTraits>
::test_merge (std::vector<typename ValueTraits::value_type>& values)
{
typedef typename ValueTraits::value_type value_type;
typedef list
< value_type
, value_traits<ValueTraits>
, size_type<std::size_t>
, constant_time_size<value_type::constant_time_size>
> list_type;
list_type testlist1, testlist2;
testlist1.push_front (values[0]);
testlist2.push_front (values[4]);
testlist2.push_front (values[3]);
testlist2.push_front (values[2]);
testlist1.merge (testlist2);
int init_values [] = { 1, 3, 4, 5 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() );
}
2007-05-04 21:30:54 +00:00
//test: assign, insert, const_iterator, const_reverse_iterator, erase, s_iterator_to:
2007-05-04 21:30:54 +00:00
template<class ValueTraits>
void test_list<ValueTraits>
::test_insert(std::vector<typename ValueTraits::value_type>& values)
{
typedef typename ValueTraits::value_type value_type;
typedef list
< value_type
, value_traits<ValueTraits>
, size_type<std::size_t>
, constant_time_size<value_type::constant_time_size>
2007-05-04 21:30:54 +00:00
> list_type;
list_type testlist;
testlist.assign (&values[0] + 2, &values[0] + 5);
const list_type& const_testlist = testlist;
{ int init_values [] = { 3, 4, 5 };
TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); }
2007-05-04 21:30:54 +00:00
typename list_type::iterator i = ++testlist.begin();
BOOST_TEST (i->value_ == 4);
{
typename list_type::const_iterator ci = typename list_type::iterator();
//typename list_type::iterator i = typename list_type::const_iterator();
}
2007-05-04 21:30:54 +00:00
testlist.insert (i, values[0]);
{ int init_values [] = { 5, 4, 1, 3 };
TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.rbegin() ); }
2007-05-04 21:30:54 +00:00
i = testlist.iterator_to (values[4]);
BOOST_TEST (&*i == &values[4]);
2007-05-04 21:30:54 +00:00
i = list_type::s_iterator_to (values[4]);
BOOST_TEST (&*i == &values[4]);
2007-05-04 21:30:54 +00:00
i = testlist.erase (i);
BOOST_TEST (i == testlist.end());
2007-05-04 21:30:54 +00:00
{ int init_values [] = { 3, 1, 4 };
TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); }
2007-05-04 21:30:54 +00:00
}
2007-05-04 21:30:54 +00:00
template<class ValueTraits>
void test_list<ValueTraits>
::test_shift(std::vector<typename ValueTraits::value_type>& values)
{
typedef typename ValueTraits::value_type value_type;
typedef list
< value_type
, value_traits<ValueTraits>
, size_type<std::size_t>
, constant_time_size<value_type::constant_time_size>
2007-05-04 21:30:54 +00:00
> list_type;
list_type testlist;
const int num_values = (int)values.size();
std::vector<int> expected_values(num_values);
for(int s = 1; s <= num_values; ++s){
expected_values.resize(s);
//Shift forward all possible positions 3 times
for(int i = 0; i < s*3; ++i){
testlist.insert(testlist.begin(), &values[0], &values[0] + s);
testlist.shift_forward(i);
for(int j = 0; j < s; ++j){
expected_values[(j + s - i%s) % s] = (j + 1);
}
TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin());
testlist.clear();
2007-05-04 21:30:54 +00:00
}
//Shift backwards all possible positions
for(int i = 0; i < s*3; ++i){
testlist.insert(testlist.begin(), &values[0], &values[0] + s);
testlist.shift_backwards(i);
for(int j = 0; j < s; ++j){
expected_values[(j + i) % s] = (j + 1);
}
TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin());
testlist.clear();
2007-05-04 21:30:54 +00:00
}
}
}
//test: insert (seq-version), swap, splice, erase (seq-version):
template<class ValueTraits>
void test_list<ValueTraits>
::test_swap(std::vector<typename ValueTraits::value_type>& values)
{
typedef typename ValueTraits::value_type value_type;
typedef list
< value_type
, value_traits<ValueTraits>
, size_type<std::size_t>
, constant_time_size<value_type::constant_time_size>
2007-05-04 21:30:54 +00:00
> list_type;
{
list_type testlist1 (&values[0], &values[0] + 2);
list_type testlist2;
testlist2.insert (testlist2.end(), &values[0] + 2, &values[0] + 5);
testlist1.swap (testlist2);
{ int init_values [] = { 3, 4, 5 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
{ int init_values [] = { 1, 2 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); }
2007-05-04 21:30:54 +00:00
testlist2.splice (++testlist2.begin(), testlist1);
{ int init_values [] = { 1, 3, 4, 5, 2 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); }
BOOST_TEST (testlist1.empty());
2007-05-04 21:30:54 +00:00
testlist1.splice (testlist1.end(), testlist2, ++(++testlist2.begin()));
{ int init_values [] = { 4 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
{ int init_values [] = { 1, 3, 5, 2 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); }
2007-05-04 21:30:54 +00:00
testlist1.splice (testlist1.end(), testlist2,
testlist2.begin(), ----testlist2.end());
{ int init_values [] = { 4, 1, 3 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
{ int init_values [] = { 5, 2 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); }
2007-05-04 21:30:54 +00:00
testlist1.erase (testlist1.iterator_to(values[0]), testlist1.end());
BOOST_TEST (testlist1.size() == 1);
BOOST_TEST (&testlist1.front() == &values[3]);
2007-05-04 21:30:54 +00:00
}
{
list_type testlist1 (&values[0], &values[0] + 2);
list_type testlist2 (&values[0] + 3, &values[0] + 5);
values[0].swap_nodes(values[2]);
{ int init_values [] = { 3, 2 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
2007-05-04 21:30:54 +00:00
values[2].swap_nodes(values[4]);
{ int init_values [] = { 5, 2 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
{ int init_values [] = { 4, 3 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); }
2007-05-04 21:30:54 +00:00
}
{
list_type testlist1 (&values[0], &values[1]);
{ int init_values [] = { 1 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
values[1].swap_nodes(values[2]);
{ int init_values [] = { 1 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
values[0].swap_nodes(values[2]);
{ int init_values [] = { 3 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
values[0].swap_nodes(values[2]);
{ int init_values [] = { 1 };
TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); }
}
2007-05-04 21:30:54 +00:00
}
template<class ValueTraits>
void test_list<ValueTraits>
::test_container_from_end(std::vector<typename ValueTraits::value_type>& values)
2007-05-04 21:30:54 +00:00
{
typedef typename ValueTraits::value_type value_type;
typedef list
< value_type
, value_traits<ValueTraits>
, size_type<std::size_t>
, constant_time_size<value_type::constant_time_size>
2007-05-04 21:30:54 +00:00
> list_type;
list_type testlist1 (&values[0], &values[0] + values.size());
BOOST_TEST (testlist1 == list_type::container_from_end_iterator(testlist1.end()));
BOOST_TEST (testlist1 == list_type::container_from_end_iterator(testlist1.cend()));
}
2007-05-04 21:30:54 +00:00
template<class ValueTraits>
void test_list<ValueTraits>
::test_clone(std::vector<typename ValueTraits::value_type>& values)
{
typedef typename ValueTraits::value_type value_type;
typedef list
< value_type
, value_traits<ValueTraits>
, size_type<std::size_t>
, constant_time_size<value_type::constant_time_size>
> list_type;
2007-05-04 21:30:54 +00:00
list_type testlist1 (&values[0], &values[0] + values.size());
list_type testlist2;
testlist2.clone_from(testlist1, test::new_cloner<value_type>(), test::delete_disposer<value_type>());
BOOST_TEST (testlist2 == testlist1);
testlist2.clear_and_dispose(test::delete_disposer<value_type>());
BOOST_TEST (testlist2.empty());
2007-05-04 21:30:54 +00:00
}
template<class VoidPointer, bool constant_time_size>
class test_main_template
{
public:
int operator()()
{
typedef testvalue<VoidPointer, constant_time_size> value_type;
std::vector<value_type> data (5);
2007-05-04 21:30:54 +00:00
for (int i = 0; i < 5; ++i)
data[i].value_ = i + 1;
test_list < typename detail::get_base_value_traits
< value_type
, typename value_type::list_base_hook_t
>::type
>::test_all(data);
test_list < typename detail::get_member_value_traits
< value_type
, member_hook< value_type
, typename value_type::list_member_hook_t
, &value_type::list_node_
>
>::type
>::test_all(data);
2007-05-04 21:30:54 +00:00
return 0;
}
};
template<class VoidPointer>
class test_main_template<VoidPointer, false>
{
public:
int operator()()
{
typedef testvalue<VoidPointer, false> value_type;
std::vector<value_type> data (5);
2007-05-04 21:30:54 +00:00
for (int i = 0; i < 5; ++i)
data[i].value_ = i + 1;
test_list < typename detail::get_base_value_traits
< value_type
, typename value_type::list_base_hook_t
>::type
>::test_all(data);
test_list < typename detail::get_member_value_traits
< value_type
, member_hook< value_type
, typename value_type::list_member_hook_t
, &value_type::list_node_
>
>::type
>::test_all(data);
// test_list<stateful_value_traits
// < value_type
// , list_node_traits<VoidPointer>
// , safe_link>
// >::test_all(data);
test_list < typename detail::get_base_value_traits
< value_type
, typename value_type::list_auto_base_hook_t
>::type
>::test_all(data);
test_list < typename detail::get_member_value_traits
< value_type
, member_hook< value_type
, typename value_type::list_auto_member_hook_t
, &value_type::list_auto_node_
>
>::type
>::test_all(data);
// test_list<stateful_value_traits
// < value_type
// , list_node_traits<VoidPointer>
// , auto_unlink>
// >::test_all(data);
2007-05-04 21:30:54 +00:00
return 0;
}
};
2007-06-23 13:09:46 +00:00
int main( int, char* [] )
2007-05-04 21:30:54 +00:00
{
test_main_template<void*, false>()();
test_main_template<smart_ptr<void>, false>()();
2007-05-04 21:30:54 +00:00
test_main_template<void*, true>()();
test_main_template<smart_ptr<void>, true>()();
return boost::report_errors();
2007-05-04 21:30:54 +00:00
}
2007-06-23 13:09:46 +00:00
#include <boost/intrusive/detail/config_end.hpp>
/*
#include <cstddef>
////////////////////////////////////////////////////
// Builds an index_tuple<0, 1, 2, ..., Num-1>, that will
// be used to "unpack" into comma-separated values
// in a function call.
////////////////////////////////////////////////////
template<int... Indexes>
struct index_tuple{};
template<std::size_t Num, typename Tuple = index_tuple<> >
struct build_number_seq;
template<std::size_t Num, int... Indexes>
struct build_number_seq<Num, index_tuple<Indexes...> >
: build_number_seq<Num - 1, index_tuple<Indexes..., sizeof...(Indexes)> >
{};
template<int... Indexes>
struct build_number_seq<0, index_tuple<Indexes...> >
{ typedef index_tuple<Indexes...> type; };
template<class ...Types>
struct typelist
{};
template<class T>
struct invert_typelist;
template<int I, typename Tuple>
struct typelist_element;
template<int I, typename Head, typename... Tail>
struct typelist_element<I, typelist<Head, Tail...> >
{
typedef typename typelist_element<I-1, typelist<Tail...> >::type type;
};
template<typename Head, typename... Tail>
struct typelist_element<0, typelist<Head, Tail...> >
{
typedef Head type;
};
template<int ...Ints, class ...Types>
typelist<typename typelist_element<(sizeof...(Types) - 1) - Ints, typelist<Types...> >::type...>
inverted_typelist(index_tuple<Ints...>, typelist<Types...>)
{
return typelist<typename typelist_element<(sizeof...(Types) - 1) - Ints, typelist<Types...> >::type...>();
}
template<class Typelist>
struct sizeof_typelist;
template<class ...Types>
struct sizeof_typelist< typelist<Types...> >
{
static const std::size_t value = sizeof...(Types);
};
//invert_typelist_impl
template<class Typelist, class Indexes>
struct invert_typelist_impl;
template<class Typelist, int ...Ints>
struct invert_typelist_impl< Typelist, index_tuple<Ints...> >
{
static const std::size_t last_idx = sizeof_typelist<Typelist>::value - 1;
typedef typelist
<typename typelist_element<last_idx - Ints, Typelist>::type...> type;
};
template<class Typelist, int Int>
struct invert_typelist_impl< Typelist, index_tuple<Int> >
{
typedef Typelist type;
};
template<class Typelist>
struct invert_typelist_impl< Typelist, index_tuple<> >
{
typedef Typelist type;
};
//invert_typelist
template<class Typelist>
struct invert_typelist;
template<class ...Types>
struct invert_typelist< typelist<Types...> >
{
typedef typelist<Types...> typelist_t;
typedef typename build_number_seq<sizeof...(Types)>::type indexes_t;
typedef typename invert_typelist_impl<typelist_t, indexes_t>::type type;
};
struct none
{
template<class Base>
struct pack : Base
{ };
};
//!This option setter specifies the type of
//!a void pointer. This will instruct the hook
//!to use this type of pointer instead of the
//!default one
template<class VoidPointer>
struct void_pointer
{
/// @cond
template<class Base>
struct pack : Base
{
typedef VoidPointer void_pointer;
};
/// @endcond
};
//!This option setter specifies the type of
//!the tag of a base hook. A type cannot have two
//!base hooks of the same type, so a tag can be used
//!to differentiate two base hooks with otherwise same type
template<class Tag>
struct tag
{
/// @cond
template<class Base>
struct pack : Base
{
typedef Tag tag;
};
/// @endcond
};
//!This option setter specifies if the hook
//!should be optimized for size instead of for speed.
template<bool Enabled>
struct optimize_size
{
/// @cond
template<class Base>
struct pack : Base
{
static const bool optimize_size = Enabled;
};
/// @endcond
};
//!This option setter specifies if the list container should
//!use a linear implementation instead of a circular one.
template<bool Enabled>
struct linear
{
/// @cond
template<class Base>
struct pack : Base
{
static const bool linear = Enabled;
};
/// @endcond
};
//!This option setter specifies if the list container should
//!use a linear implementation instead of a circular one.
template<bool Enabled>
struct cache_last
{
/// @cond
template<class Base>
struct pack : Base
{
static const bool cache_last = Enabled;
};
/// @endcond
};
template<class Typelist>
struct do_pack;
template<>
struct do_pack<typelist<> >;
template<class Prev>
struct do_pack<typelist<Prev> >
{
typedef Prev type;
};
template<class Prev, class Last>
struct do_pack<typelist<Prev, Last> >
{
typedef typename Prev::template pack<Last> type;
};
template<class Prev, class ...Others>
struct do_pack<typelist<Prev, Others...> >
{
typedef typename Prev::template pack
<typename do_pack<typelist<Others...>>::type> type;
};
template<class ...Options>
struct pack_options
{
typedef typelist<Options...> typelist_t;
typedef typename invert_typelist<typelist_t>::type inverted_typelist;
typedef typename do_pack<inverted_typelist>::type type;
};
struct hook_defaults
: public pack_options
< none
, void_pointer<void*>
, tag<int>
, optimize_size<false>
, linear<false>
>::type
{};
#include <iostream>
#include <typeinfo>
struct S
{};
int main()
{
{
typedef typelist<int, float, double> typelist_t;
typedef invert_typelist<typelist_t>::type inverted_typelist;
std::cout << "original: " << typeid(typelist_t).name() << std::endl;
std::cout << "inverted: " << typeid(inverted_typelist).name() << std::endl;
}
{
typedef typelist<int> typelist_t;
typedef invert_typelist<typelist_t>::type inverted_typelist;
std::cout << "original: " << typeid(typelist_t).name() << std::endl;
std::cout << "inverted: " << typeid(inverted_typelist).name() << std::endl;
}
{
typedef typelist<> typelist_t;
typedef invert_typelist<typelist_t>::type inverted_typelist;
std::cout << "original: " << typeid(typelist_t).name() << std::endl;
std::cout << "inverted: " << typeid(inverted_typelist).name() << std::endl;
}
{
typedef pack_options<S, none>::type options_t;
std::cout << "options_t " << typeid(options_t).name() << std::endl;
}
{
typedef pack_options<S, none, none>::type options_t;
std::cout << "options_t " << typeid(options_t).name() << std::endl;
}
hook_defaults h;
return 1;
}
*/