#include #include #include #include #include #include BOOST_AUTO_TEST_SUITE( first_nd_test ) using namespace more::adaptors; using namespace boost::assign; typedef std::set IntSet; typedef std::vector IntVec; // TODO test associativity BOOST_AUTO_TEST_CASE( IteratorBasic ) { using namespace more::range_detail; // first_n_iterator //IntSet set1, set2, set3 = {1,2,3}, set4 = {1,2,3}; IntSet set1, set2, set3, set4; set3 += 1,2,3; set4 += 1,2,3; BOOST_CHECK(first_n_iterator(set1.begin()) != first_n_iterator(set2.begin())); BOOST_CHECK(first_n_iterator(set3.begin()) != first_n_iterator(set4.begin())); BOOST_CHECK(first_n_iterator(set3.begin(),1) != first_n_iterator(set4.begin(),1)); } BOOST_AUTO_TEST_CASE( Iterator ) { using namespace more::range_detail; // first_n_iterator IntSet set; set += 1,2,3,4,5,6,7,8; first_n_iterator b(set.begin()); first_n_iterator e(set.begin(), 3); int result = boost::accumulate(boost::make_iterator_range(b, e), 0); BOOST_CHECK_EQUAL(result, 1+2+3); } BOOST_AUTO_TEST_CASE( ConstRange ) { IntSet set; set += 1,2,3,4,5,6,7,8; const IntSet cset(set); int result = boost::accumulate(first_n(cset, 3), 0); BOOST_CHECK_EQUAL(result, 1+2+3); } BOOST_AUTO_TEST_CASE( MutableRange ) { IntVec vec; vec += 1,2,3,4,5,6,7,8; BOOST_FOREACH (int& v, first_n(vec, 3)) { v*=2; } BOOST_CHECK_EQUAL(vec[0], 2); BOOST_CHECK_EQUAL(vec[1], 4); BOOST_CHECK_EQUAL(vec[2], 6); BOOST_CHECK_EQUAL(vec[3], 4); int result = boost::accumulate(first_n(vec, 3), 0); BOOST_CHECK_EQUAL(result, 2+4+6); } BOOST_AUTO_TEST_CASE( ConstRangeWithOperator ) { IntSet set; set += 1,2,3,4,5,6,7,8; const IntSet cset(set); int result = boost::accumulate(cset | first_nd(3), 0); BOOST_CHECK_EQUAL(result, 1+2+3); } BOOST_AUTO_TEST_CASE( MutableRangeWithOperator ) { IntVec vec; vec += 1,2,3,4,5,6,7,8; BOOST_FOREACH (int& v, vec | first_nd(3)) { v*=2; } BOOST_CHECK_EQUAL(vec[0], 2); BOOST_CHECK_EQUAL(vec[1], 4); BOOST_CHECK_EQUAL(vec[2], 6); BOOST_CHECK_EQUAL(vec[3], 4); int result = boost::accumulate(vec | first_nd(3), 0); BOOST_CHECK_EQUAL(result, 2+4+6); } BOOST_AUTO_TEST_SUITE_END()