| 1 | // Boost.Range library |
| 2 | // |
| 3 | // Copyright Neil Groves 2009. Use, modification and |
| 4 | // distribution is subject to the Boost Software License, Version |
| 5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | // |
| 8 | // |
| 9 | // For more information, see http://www.boost.org/libs/range/ |
| 10 | // |
| 11 | #include <boost/range/adaptor/replaced.hpp> |
| 12 | |
| 13 | #include <boost/test/test_tools.hpp> |
| 14 | #include <boost/test/unit_test.hpp> |
| 15 | |
| 16 | #include <boost/assign.hpp> |
| 17 | #include <boost/range/algorithm_ext.hpp> |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <list> |
| 21 | #include <set> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace boost |
| 25 | { |
| 26 | namespace |
| 27 | { |
| 28 | template< class Container > |
| 29 | void replaced_test_impl( Container& c ) |
| 30 | { |
| 31 | using namespace boost::adaptors; |
| 32 | |
| 33 | const int value_to_replace = 1; |
| 34 | const int replacement_value = 0; |
| 35 | |
| 36 | std::vector< int > test_result1; |
| 37 | boost::push_back(test_result1, c | replaced(value_to_replace, replacement_value)); |
| 38 | |
| 39 | std::vector< int > test_result2; |
| 40 | boost::push_back(test_result2, adaptors::replace(c, value_to_replace, replacement_value)); |
| 41 | |
| 42 | std::vector< int > reference( c.begin(), c.end() ); |
| 43 | std::replace(first: reference.begin(), last: reference.end(), old_value: value_to_replace, new_value: replacement_value); |
| 44 | |
| 45 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
| 46 | test_result1.begin(), test_result1.end() ); |
| 47 | } |
| 48 | |
| 49 | template< class Container > |
| 50 | void replaced_test_impl() |
| 51 | { |
| 52 | using namespace boost::assign; |
| 53 | |
| 54 | Container c; |
| 55 | |
| 56 | // Test empty |
| 57 | replaced_test_impl(c); |
| 58 | |
| 59 | // Test one |
| 60 | c += 1; |
| 61 | replaced_test_impl(c); |
| 62 | |
| 63 | // Test many |
| 64 | c += 1,1,1,2,2,2,3,3,3,3,3,4,5,6,6,6,7,8,9; |
| 65 | replaced_test_impl(c); |
| 66 | } |
| 67 | |
| 68 | void replaced_test() |
| 69 | { |
| 70 | replaced_test_impl< std::vector< int > >(); |
| 71 | replaced_test_impl< std::list< int > >(); |
| 72 | replaced_test_impl< std::set< int > >(); |
| 73 | replaced_test_impl< std::multiset< int > >(); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | boost::unit_test::test_suite* |
| 79 | init_unit_test_suite(int argc, char* argv[]) |
| 80 | { |
| 81 | boost::unit_test::test_suite* test |
| 82 | = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.replaced" ); |
| 83 | |
| 84 | test->add( BOOST_TEST_CASE( &boost::replaced_test ) ); |
| 85 | |
| 86 | return test; |
| 87 | } |
| 88 | |