adjacent_filtered.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/adjacent_filtered.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <algorithm>
  16. #include <list>
  17. #include <set>
  18. #include <string>
  19. #include <vector>
  20. namespace boost
  21. {
  22. namespace
  23. {
  24. template< class Container >
  25. void adjacent_filtered_test_impl( Container& c )
  26. {
  27. using namespace boost::adaptors;
  28. typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
  29. // This is my preferred syntax using the | operator.
  30. std::vector< value_t > test_result1
  31. = boost::copy_range< std::vector< value_t > >(
  32. c | adjacent_filtered(std::not_equal_to< value_t >()));
  33. // This is an alternative syntax preferred by some.
  34. std::vector< value_t > test_result2
  35. = boost::copy_range< std::vector< value_t > >(
  36. adaptors::adjacent_filter(c, std::not_equal_to< value_t >()));
  37. // Calculate the reference result.
  38. std::vector< value_t > reference_result;
  39. typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
  40. value_t prev_v = value_t();
  41. for (iter_t it = c.begin(); it != c.end(); ++it)
  42. {
  43. if (it == c.begin())
  44. {
  45. reference_result.push_back(*it);
  46. }
  47. else if (*it != prev_v)
  48. {
  49. reference_result.push_back(*it);
  50. }
  51. prev_v = *it;
  52. }
  53. BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
  54. reference_result.end(),
  55. test_result1.begin(),
  56. test_result1.end() );
  57. BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
  58. reference_result.end(),
  59. test_result2.begin(),
  60. test_result2.end() );
  61. }
  62. template< class Collection >
  63. void adjacent_filtered_test_impl()
  64. {
  65. using namespace boost::assign;
  66. Collection c;
  67. // test empty collection
  68. adjacent_filtered_test_impl(c);
  69. // test one element;
  70. c += 1;
  71. adjacent_filtered_test_impl(c);
  72. // test many elements;
  73. c += 1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
  74. adjacent_filtered_test_impl(c);
  75. }
  76. void adjacent_filtered_test()
  77. {
  78. adjacent_filtered_test_impl< std::vector< int > >();
  79. adjacent_filtered_test_impl< std::list< int > >();
  80. adjacent_filtered_test_impl< std::set< int > >();
  81. adjacent_filtered_test_impl< std::multiset< int > >();
  82. }
  83. }
  84. }
  85. boost::unit_test::test_suite*
  86. init_unit_test_suite(int argc, char* argv[])
  87. {
  88. boost::unit_test::test_suite* test
  89. = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.adjacent_filtered" );
  90. test->add( BOOST_TEST_CASE( &boost::adjacent_filtered_test ) );
  91. return test;
  92. }