is_permutation_test1.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright (c) Marshall Clow 2011-2012.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. For more information, see http://www.boost.org
  6. */
  7. #include <iostream>
  8. #include <boost/config.hpp>
  9. #include <boost/algorithm/cxx11/is_permutation.hpp>
  10. #include <boost/algorithm/cxx14/is_permutation.hpp>
  11. #define BOOST_TEST_MAIN
  12. #include <boost/test/unit_test.hpp>
  13. #include <string>
  14. #include <vector>
  15. #include <list>
  16. #include "iterator_test.hpp"
  17. template <typename T>
  18. bool eq ( const T& a, const T& b ) { return a == b; }
  19. template <typename T>
  20. bool never_eq ( const T&, const T& ) { return false; }
  21. namespace ba = boost::algorithm;
  22. void test_sequence1 () {
  23. int num[] = { 1, 1, 2, 3, 5 };
  24. const int sz = sizeof (num)/sizeof(num[0]);
  25. // Empty sequences
  26. BOOST_CHECK (
  27. ba::is_permutation (
  28. forward_iterator<int *>(num), forward_iterator<int *>(num),
  29. forward_iterator<int *>(num)));
  30. BOOST_CHECK (
  31. ba::is_permutation (
  32. forward_iterator<int *>(num), forward_iterator<int *>(num),
  33. forward_iterator<int *>(num), forward_iterator<int *>(num)));
  34. BOOST_CHECK (
  35. ba::is_permutation (
  36. random_access_iterator<int *>(num), random_access_iterator<int *>(num),
  37. random_access_iterator<int *>(num), random_access_iterator<int *>(num)));
  38. BOOST_CHECK (
  39. ba::is_permutation (
  40. forward_iterator<int *>(num), forward_iterator<int *>(num),
  41. forward_iterator<int *>(num),
  42. never_eq<int> )); // Since the sequences are empty, the pred is never called
  43. // Empty vs. non-empty
  44. BOOST_CHECK ( !
  45. ba::is_permutation (
  46. forward_iterator<int *>(num), forward_iterator<int *>(num),
  47. forward_iterator<int *>(num), forward_iterator<int *>(num + 1)));
  48. BOOST_CHECK ( !
  49. ba::is_permutation (
  50. forward_iterator<int *>(num + 1), forward_iterator<int *>(num + 2),
  51. forward_iterator<int *>(num), forward_iterator<int *>(num)));
  52. BOOST_CHECK ( !
  53. ba::is_permutation (
  54. random_access_iterator<int *>(num + 1), random_access_iterator<int *>(num + 2),
  55. random_access_iterator<int *>(num), random_access_iterator<int *>(num)));
  56. BOOST_CHECK ( !
  57. ba::is_permutation (
  58. random_access_iterator<int *>(num), random_access_iterator<int *>(num),
  59. random_access_iterator<int *>(num + 1), random_access_iterator<int *>(num + 2)));
  60. // Something should be a permutation of itself
  61. BOOST_CHECK (
  62. ba::is_permutation (
  63. forward_iterator<int *>(num), forward_iterator<int *>(num + sz),
  64. forward_iterator<int *>(num)));
  65. BOOST_CHECK (
  66. ba::is_permutation (
  67. forward_iterator<int *>(num), forward_iterator<int *>(num + sz),
  68. forward_iterator<int *>(num), eq<int> ));
  69. BOOST_CHECK (
  70. ba::is_permutation (
  71. forward_iterator<int *>(num), forward_iterator<int *>(num + sz),
  72. forward_iterator<int *>(num), forward_iterator<int *>(num + sz )));
  73. BOOST_CHECK (
  74. ba::is_permutation (
  75. forward_iterator<int *>(num), forward_iterator<int *>(num + sz),
  76. forward_iterator<int *>(num), forward_iterator<int *>(num + sz ),
  77. eq<int> ));
  78. BOOST_CHECK (
  79. ba::is_permutation (
  80. random_access_iterator<int *>(num), random_access_iterator<int *>(num + sz),
  81. random_access_iterator<int *>(num), random_access_iterator<int *>(num + sz)));
  82. BOOST_CHECK (
  83. ba::is_permutation (
  84. random_access_iterator<int *>(num), random_access_iterator<int *>(num + sz),
  85. random_access_iterator<int *>(num), random_access_iterator<int *>(num + sz),
  86. eq<int> ));
  87. BOOST_CHECK (
  88. ba::is_permutation (
  89. random_access_iterator<int *>(num), random_access_iterator<int *>(num + sz),
  90. forward_iterator<int *>(num), forward_iterator<int *>(num + sz),
  91. eq<int> ));
  92. std::vector<int> v, v1;
  93. v.clear ();
  94. for ( std::size_t i = 5; i < 15; ++i )
  95. v.push_back ( i );
  96. v1 = v;
  97. BOOST_CHECK ( ba::is_permutation ( v.begin (), v.end (), v.begin ())); // better be a permutation of itself!
  98. BOOST_CHECK ( ba::is_permutation ( v.begin (), v.end (), v1.begin ()));
  99. // With bidirectional iterators.
  100. std::list<int> l;
  101. std::copy ( v.begin (), v.end (), std::back_inserter ( l ));
  102. BOOST_CHECK ( ba::is_permutation ( l.begin (), l.end (), l.begin ())); // better be a permutation of itself!
  103. BOOST_CHECK ( ba::is_permutation ( l.begin (), l.end (), v1.begin ()));
  104. for ( std::size_t i = 0; i < l.size (); ++i ) {
  105. l.push_back ( *l.begin ()); l.pop_front (); // rotation
  106. BOOST_CHECK ( ba::is_permutation ( l.begin (), l.end (), v1.begin ()));
  107. }
  108. }
  109. BOOST_AUTO_TEST_CASE( test_main )
  110. {
  111. test_sequence1 ();
  112. }