unique_copy.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/algorithm/unique_copy.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <boost/bind.hpp>
  16. #include <algorithm>
  17. #include <functional>
  18. #include <list>
  19. #include <numeric>
  20. #include <deque>
  21. #include <vector>
  22. namespace
  23. {
  24. template<class OutputIterator, class Value>
  25. void test_append(OutputIterator target, Value value)
  26. {
  27. *target++ = value;
  28. }
  29. template<class Container>
  30. void test_unique_copy_impl(Container& c)
  31. {
  32. typedef BOOST_DEDUCED_TYPENAME boost::range_value<Container>::type value_type;
  33. std::vector<value_type> reference;
  34. std::vector<value_type> test;
  35. test_append(
  36. std::unique_copy(c.begin(), c.end(), std::back_inserter(reference)),
  37. value_type()
  38. );
  39. test_append(
  40. boost::unique_copy(c, std::back_inserter(test)),
  41. value_type()
  42. );
  43. BOOST_CHECK_EQUAL_COLLECTIONS(reference.begin(), reference.end(),
  44. test.begin(), test.end());
  45. test.clear();
  46. test_append(
  47. boost::unique_copy(boost::make_iterator_range(c),
  48. std::back_inserter(test)),
  49. value_type()
  50. );
  51. BOOST_CHECK_EQUAL_COLLECTIONS(reference.begin(), reference.end(),
  52. test.begin(), test.end());
  53. }
  54. template<class Container, class Pred>
  55. void test_unique_copy_impl(Container& c, Pred pred)
  56. {
  57. typedef BOOST_DEDUCED_TYPENAME boost::range_value<Container>::type value_type;
  58. std::vector<value_type> reference;
  59. std::vector<value_type> test;
  60. test_append(
  61. std::unique_copy(c.begin(), c.end(), std::back_inserter(reference), pred),
  62. value_type()
  63. );
  64. test_append(
  65. boost::unique_copy(c, std::back_inserter(test), pred),
  66. value_type()
  67. );
  68. BOOST_CHECK_EQUAL_COLLECTIONS(reference.begin(), reference.end(),
  69. test.begin(), test.end());
  70. test.clear();
  71. test_append(
  72. boost::unique_copy(boost::make_iterator_range(c),
  73. std::back_inserter(test), pred),
  74. value_type()
  75. );
  76. BOOST_CHECK_EQUAL_COLLECTIONS(reference.begin(), reference.end(),
  77. test.begin(), test.end());
  78. }
  79. template<class Container, class Pred>
  80. void test_unique_copy_driver(Pred pred)
  81. {
  82. using namespace boost::assign;
  83. typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
  84. Container cont;
  85. test_unique_copy_impl(cont);
  86. test_unique_copy_impl(cont, pred);
  87. cont.clear();
  88. cont += 1;
  89. std::vector<value_t> temp(cont.begin(), cont.end());
  90. std::sort(temp.begin(), temp.end());
  91. cont.assign(temp.begin(), temp.end());
  92. test_unique_copy_impl(cont);
  93. std::sort(temp.begin(), temp.end(), pred);
  94. cont.assign(temp.begin(), temp.end());
  95. test_unique_copy_impl(cont, pred);
  96. cont.clear();
  97. cont += 1,2,2,2,2,3,4,5,6,7,8,9;
  98. temp.assign(cont.begin(), cont.end());
  99. std::sort(temp.begin(), temp.end());
  100. cont.assign(temp.begin(), temp.end());
  101. test_unique_copy_impl(cont);
  102. std::sort(temp.begin(), temp.end(), pred);
  103. cont.assign(temp.begin(), temp.end());
  104. test_unique_copy_impl(cont, pred);
  105. }
  106. template<class Container>
  107. void test_unique_copy_impl()
  108. {
  109. test_unique_copy_driver<Container>(std::less<int>());
  110. test_unique_copy_driver<Container>(std::greater<int>());
  111. }
  112. void test_unique_copy()
  113. {
  114. test_unique_copy_impl< std::vector<int> >();
  115. test_unique_copy_impl< std::list<int> >();
  116. test_unique_copy_impl< std::deque<int> >();
  117. }
  118. }
  119. boost::unit_test::test_suite*
  120. init_unit_test_suite(int argc, char* argv[])
  121. {
  122. boost::unit_test::test_suite* test
  123. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.unique_copy" );
  124. test->add( BOOST_TEST_CASE( &test_unique_copy ) );
  125. return test;
  126. }