unique_copy_pred.pass.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <algorithm>
  9. // template<InputIterator InIter, class OutIter,
  10. // EquivalenceRelation<auto, InIter::value_type> Pred>
  11. // requires OutputIterator<OutIter, RvalueOf<InIter::value_type>::type>
  12. // && HasAssign<InIter::value_type, InIter::reference>
  13. // && Constructible<InIter::value_type, InIter::reference>
  14. // && CopyConstructible<Pred>
  15. // constexpr OutIter // constexpr after C++17
  16. // unique_copy(InIter first, InIter last, OutIter result, Pred pred);
  17. #include <algorithm>
  18. #include <cassert>
  19. #include "test_macros.h"
  20. #include "test_iterators.h"
  21. #if TEST_STD_VER > 17
  22. TEST_CONSTEXPR bool test_constexpr() {
  23. int ia[] = {0, 1, 2, 2, 4};
  24. int ib[] = {0, 0, 0, 0, 0};
  25. const int expected[] = {0, 1, 2, 4};
  26. auto it = std::unique_copy(std::begin(ia), std::end(ia), std::begin(ib),
  27. [](int a, int b) {return a == b; });
  28. return it == (std::begin(ib) + std::size(expected))
  29. && *it == 0 // don't overwrite final value in output
  30. && std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))
  31. ;
  32. }
  33. #endif
  34. struct count_equal
  35. {
  36. static unsigned count;
  37. template <class T>
  38. bool operator()(const T& x, const T& y)
  39. {++count; return x == y;}
  40. };
  41. unsigned count_equal::count = 0;
  42. template <class InIter, class OutIter>
  43. void
  44. test()
  45. {
  46. const int ia[] = {0};
  47. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  48. int ja[sa] = {-1};
  49. count_equal::count = 0;
  50. OutIter r = std::unique_copy(InIter(ia), InIter(ia+sa), OutIter(ja), count_equal());
  51. assert(base(r) == ja + sa);
  52. assert(ja[0] == 0);
  53. assert(count_equal::count == sa-1);
  54. const int ib[] = {0, 1};
  55. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  56. int jb[sb] = {-1};
  57. count_equal::count = 0;
  58. r = std::unique_copy(InIter(ib), InIter(ib+sb), OutIter(jb), count_equal());
  59. assert(base(r) == jb + sb);
  60. assert(jb[0] == 0);
  61. assert(jb[1] == 1);
  62. assert(count_equal::count == sb-1);
  63. const int ic[] = {0, 0};
  64. const unsigned sc = sizeof(ic)/sizeof(ic[0]);
  65. int jc[sc] = {-1};
  66. count_equal::count = 0;
  67. r = std::unique_copy(InIter(ic), InIter(ic+sc), OutIter(jc), count_equal());
  68. assert(base(r) == jc + 1);
  69. assert(jc[0] == 0);
  70. assert(count_equal::count == sc-1);
  71. const int id[] = {0, 0, 1};
  72. const unsigned sd = sizeof(id)/sizeof(id[0]);
  73. int jd[sd] = {-1};
  74. count_equal::count = 0;
  75. r = std::unique_copy(InIter(id), InIter(id+sd), OutIter(jd), count_equal());
  76. assert(base(r) == jd + 2);
  77. assert(jd[0] == 0);
  78. assert(jd[1] == 1);
  79. assert(count_equal::count == sd-1);
  80. const int ie[] = {0, 0, 1, 0};
  81. const unsigned se = sizeof(ie)/sizeof(ie[0]);
  82. int je[se] = {-1};
  83. count_equal::count = 0;
  84. r = std::unique_copy(InIter(ie), InIter(ie+se), OutIter(je), count_equal());
  85. assert(base(r) == je + 3);
  86. assert(je[0] == 0);
  87. assert(je[1] == 1);
  88. assert(je[2] == 0);
  89. assert(count_equal::count == se-1);
  90. const int ig[] = {0, 0, 1, 1};
  91. const unsigned sg = sizeof(ig)/sizeof(ig[0]);
  92. int jg[sg] = {-1};
  93. count_equal::count = 0;
  94. r = std::unique_copy(InIter(ig), InIter(ig+sg), OutIter(jg), count_equal());
  95. assert(base(r) == jg + 2);
  96. assert(jg[0] == 0);
  97. assert(jg[1] == 1);
  98. assert(count_equal::count == sg-1);
  99. const int ih[] = {0, 1, 1};
  100. const unsigned sh = sizeof(ih)/sizeof(ih[0]);
  101. int jh[sh] = {-1};
  102. count_equal::count = 0;
  103. r = std::unique_copy(InIter(ih), InIter(ih+sh), OutIter(jh), count_equal());
  104. assert(base(r) == jh + 2);
  105. assert(jh[0] == 0);
  106. assert(jh[1] == 1);
  107. assert(count_equal::count == sh-1);
  108. const int ii[] = {0, 1, 1, 1, 2, 2, 2};
  109. const unsigned si = sizeof(ii)/sizeof(ii[0]);
  110. int ji[si] = {-1};
  111. count_equal::count = 0;
  112. r = std::unique_copy(InIter(ii), InIter(ii+si), OutIter(ji), count_equal());
  113. assert(base(r) == ji + 3);
  114. assert(ji[0] == 0);
  115. assert(ji[1] == 1);
  116. assert(ji[2] == 2);
  117. assert(count_equal::count == si-1);
  118. }
  119. int main(int, char**)
  120. {
  121. test<input_iterator<const int*>, output_iterator<int*> >();
  122. test<input_iterator<const int*>, forward_iterator<int*> >();
  123. test<input_iterator<const int*>, bidirectional_iterator<int*> >();
  124. test<input_iterator<const int*>, random_access_iterator<int*> >();
  125. test<input_iterator<const int*>, int*>();
  126. test<forward_iterator<const int*>, output_iterator<int*> >();
  127. test<forward_iterator<const int*>, forward_iterator<int*> >();
  128. test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
  129. test<forward_iterator<const int*>, random_access_iterator<int*> >();
  130. test<forward_iterator<const int*>, int*>();
  131. test<bidirectional_iterator<const int*>, output_iterator<int*> >();
  132. test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
  133. test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
  134. test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
  135. test<bidirectional_iterator<const int*>, int*>();
  136. test<random_access_iterator<const int*>, output_iterator<int*> >();
  137. test<random_access_iterator<const int*>, forward_iterator<int*> >();
  138. test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
  139. test<random_access_iterator<const int*>, random_access_iterator<int*> >();
  140. test<random_access_iterator<const int*>, int*>();
  141. test<const int*, output_iterator<int*> >();
  142. test<const int*, forward_iterator<int*> >();
  143. test<const int*, bidirectional_iterator<int*> >();
  144. test<const int*, random_access_iterator<int*> >();
  145. test<const int*, int*>();
  146. #if TEST_STD_VER > 17
  147. static_assert(test_constexpr());
  148. #endif
  149. return 0;
  150. }