From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- .../libs/assign/test/multi_index_container.cpp | 168 +++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 src/boost/libs/assign/test/multi_index_container.cpp (limited to 'src/boost/libs/assign/test/multi_index_container.cpp') diff --git a/src/boost/libs/assign/test/multi_index_container.cpp b/src/boost/libs/assign/test/multi_index_container.cpp new file mode 100644 index 00000000..711ba5d4 --- /dev/null +++ b/src/boost/libs/assign/test/multi_index_container.cpp @@ -0,0 +1,168 @@ +// Boost.Assign library +// +// Copyright Thorsten Ottosen 2003-2004. Use, modification and +// distribution is subject to 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) +// +// For more information, see http://www.boost.org/libs/assign/ +// + + +#include + +#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) +# pragma warn -8091 // suppress warning in Boost.Test +# pragma warn -8057 // unused argument argc/argv in Boost.Test +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace boost; +using namespace boost::multi_index; +namespace ba = boost::assign; + +// +// Define a classical multi_index_container for employees +// +struct employee +{ + int id; + std::string name; + int age; + + employee(int id_,std::string name_,int age_):id(id_),name(name_),age(age_){} + + bool operator==(const employee& x)const + { + return id==x.id&&name==x.name&&age==x.age; + } + + bool operator<(const employee& x)const + { + return id (const employee& x)const{return x<*this;} + bool operator>=(const employee& x)const{return !(*this >, + ordered_non_unique< + tag, + BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>, + ordered_non_unique< + tag, + BOOST_MULTI_INDEX_MEMBER(employee,int,age)>, + sequenced< + tag > > > + employee_set; + +#if defined(BOOST_NO_MEMBER_TEMPLATES) +typedef nth_index< + employee_set,1>::type employee_set_by_name; +#else +typedef employee_set::nth_index<1>::type employee_set_by_name; +#endif + +typedef boost::multi_index::index< + employee_set,age>::type employee_set_by_age; +typedef boost::multi_index::index< + employee_set,as_inserted>::type employee_set_as_inserted; + +// +// Define a multi_index_container with a list-like index and an ordered index +// +typedef multi_index_container< + std::string, + indexed_by< + sequenced<>, // list-like index + ordered_non_unique > // words by alphabetical order + > +> text_container; + + + +void test_multi_index_container() +{ + employee_set eset = ba::list_of< employee >(1,"Franz",30)(2,"Hanz",40)(3,"Ilse",50); + BOOST_CHECK( eset.size() == 3u ); + + // + // This container is associative, hence we can use 'insert()' + // + + ba::insert( eset )(4,"Kurt",55)(5,"Bjarne",77)(7,"Thorsten",24); + BOOST_CHECK( eset.size() == 6u ); + + employee_set_by_name& name_index = boost::multi_index::get(eset); + employee_set_by_name::iterator i = name_index.find("Ilse"); + BOOST_CHECK( i->id == 3 ); + BOOST_CHECK( i->age == 50 ); + + text_container text = ba::list_of< std::string >("Have")("you")("ever")("wondered")("how")("much")("Boost")("rocks?!"); + BOOST_CHECK_EQUAL( text.size(), 8u ); + BOOST_CHECK_EQUAL( *text.begin(), "Have" ); + + // + // This container is a sequence, hence we can use 'push_back()' and 'push_font()' + // + + ba::push_back( text )("Well")(",")("A")("LOT")(",")("obviously!"); + BOOST_CHECK_EQUAL( text.size(), 14u ); + BOOST_CHECK_EQUAL( *--text.end(), "obviously!" ); + + ba::push_front( text ) = "question:", "simple", "A"; + BOOST_CHECK_EQUAL( text.size(), 17u ); + BOOST_CHECK_EQUAL( text.front(), "A" ); +} + + + +using boost::unit_test::test_suite; + +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite* test = BOOST_TEST_SUITE( "List Test Suite" ); + + test->add( BOOST_TEST_CASE( &test_multi_index_container ) ); + + return test; +} + + -- cgit v1.2.3