/* Boost.MultiIndex test for range(). * * Copyright 2003-2010 Joaquin M Lopez Munoz. * 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/multi_index for library home page. */ #include "test_range.hpp" #include /* keep it first to prevent nasty warns in MSVC */ #include #include #include "pre_multi_index.hpp" #include #include #include #include #include using namespace boost::multi_index; typedef multi_index_container int_set; typedef int_set::iterator int_set_iterator; #undef CHECK_RANGE #define CHECK_RANGE(p,check_seq) \ {\ int v[]={BOOST_PP_SEQ_ENUM(check_seq)};\ std::size_t size_v=sizeof(v)/sizeof(int);\ BOOST_CHECK(std::size_t(std::distance((p).first,(p).second))==size_v);\ BOOST_CHECK(std::equal((p).first,(p).second,&v[0]));\ } #undef CHECK_VOID_RANGE #define CHECK_VOID_RANGE(p) BOOST_CHECK((p).first==(p).second) void test_range() { int_set is; for(int i=1;i<=10;++i)is.insert(i); std::pair p; p=is.range(unbounded,unbounded); CHECK_RANGE(p,(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)); p=is.range( std::bind1st(std::less(),5), /* 5 < x */ unbounded); CHECK_RANGE(p,(6)(7)(8)(9)(10)); p=is.range( std::bind1st(std::less_equal(),8), /* 8 <= x */ unbounded); CHECK_RANGE(p,(8)(9)(10)); p=is.range( std::bind1st(std::less_equal(),11), /* 11 <= x */ unbounded); CHECK_VOID_RANGE(p); p=is.range( unbounded, std::bind2nd(std::less(),8)); /* x < 8 */ CHECK_RANGE(p,(1)(2)(3)(4)(5)(6)(7)); p=is.range( unbounded, std::bind2nd(std::less_equal(),4)); /* x <= 4 */ CHECK_RANGE(p,(1)(2)(3)(4)); p=is.range( unbounded, std::bind2nd(std::less_equal(),0)); /* x <= 0 */ CHECK_VOID_RANGE(p); p=is.range( std::bind1st(std::less(),6), /* 6 < x */ std::bind2nd(std::less_equal(),9)); /* x <= 9 */ CHECK_RANGE(p,(7)(8)(9)); p=is.range( std::bind1st(std::less_equal(),4), /* 4 <= x */ std::bind2nd(std::less(),5)); /* x < 5 */ CHECK_RANGE(p,(4)); p=is.range( std::bind1st(std::less_equal(),10), /* 10 <= x */ std::bind2nd(std::less_equal(),10)); /* x <= 10 */ CHECK_RANGE(p,(10)); p=is.range( std::bind1st(std::less(),0), /* 0 < x */ std::bind2nd(std::less(),11)); /* x < 11 */ CHECK_RANGE(p,(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)); p=is.range( std::bind1st(std::less(),7), /* 7 < x */ std::bind2nd(std::less_equal(),7)); /* x <= 7 */ CHECK_VOID_RANGE(p); BOOST_CHECK(p.first==is.upper_bound(7)); p=is.range( std::bind1st(std::less_equal(),8), /* 8 <= x */ std::bind2nd(std::less(),2)); /* x < 2 */ CHECK_VOID_RANGE(p); BOOST_CHECK(p.first==is.lower_bound(8)); p=is.range( std::bind1st(std::less(),4), /* 4 < x */ std::bind2nd(std::less(),5)); /* x < 5 */ CHECK_VOID_RANGE(p); BOOST_CHECK(p.first!=is.end()); }