unique_copy.pass.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // requires OutputIterator<OutIter, RvalueOf<InIter::value_type>::type>
  11. // && EqualityComparable<InIter::value_type>
  12. // && HasAssign<InIter::value_type, InIter::reference>
  13. // && Constructible<InIter::value_type, InIter::reference>
  14. // constexpr OutIter // constexpr after C++17
  15. // unique_copy(InIter first, InIter last, OutIter result);
  16. #include <algorithm>
  17. #include <cassert>
  18. #include "test_macros.h"
  19. #include "test_iterators.h"
  20. #if TEST_STD_VER > 17
  21. TEST_CONSTEXPR bool test_constexpr() {
  22. int ia[] = {0, 1, 2, 2, 4};
  23. int ib[] = {0, 0, 0, 0, 0};
  24. const int expected[] = {0, 1, 2, 4};
  25. auto it = std::unique_copy(std::begin(ia), std::end(ia), std::begin(ib));
  26. return it == (std::begin(ib) + std::size(expected))
  27. && *it == 0 // don't overwrite final value in output
  28. && std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))
  29. ;
  30. }
  31. #endif
  32. template <class InIter, class OutIter>
  33. void
  34. test()
  35. {
  36. const int ia[] = {0};
  37. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  38. int ja[sa] = {-1};
  39. OutIter r = std::unique_copy(InIter(ia), InIter(ia+sa), OutIter(ja));
  40. assert(base(r) == ja + sa);
  41. assert(ja[0] == 0);
  42. const int ib[] = {0, 1};
  43. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  44. int jb[sb] = {-1};
  45. r = std::unique_copy(InIter(ib), InIter(ib+sb), OutIter(jb));
  46. assert(base(r) == jb + sb);
  47. assert(jb[0] == 0);
  48. assert(jb[1] == 1);
  49. const int ic[] = {0, 0};
  50. const unsigned sc = sizeof(ic)/sizeof(ic[0]);
  51. int jc[sc] = {-1};
  52. r = std::unique_copy(InIter(ic), InIter(ic+sc), OutIter(jc));
  53. assert(base(r) == jc + 1);
  54. assert(jc[0] == 0);
  55. const int id[] = {0, 0, 1};
  56. const unsigned sd = sizeof(id)/sizeof(id[0]);
  57. int jd[sd] = {-1};
  58. r = std::unique_copy(InIter(id), InIter(id+sd), OutIter(jd));
  59. assert(base(r) == jd + 2);
  60. assert(jd[0] == 0);
  61. assert(jd[1] == 1);
  62. const int ie[] = {0, 0, 1, 0};
  63. const unsigned se = sizeof(ie)/sizeof(ie[0]);
  64. int je[se] = {-1};
  65. r = std::unique_copy(InIter(ie), InIter(ie+se), OutIter(je));
  66. assert(base(r) == je + 3);
  67. assert(je[0] == 0);
  68. assert(je[1] == 1);
  69. assert(je[2] == 0);
  70. const int ig[] = {0, 0, 1, 1};
  71. const unsigned sg = sizeof(ig)/sizeof(ig[0]);
  72. int jg[sg] = {-1};
  73. r = std::unique_copy(InIter(ig), InIter(ig+sg), OutIter(jg));
  74. assert(base(r) == jg + 2);
  75. assert(jg[0] == 0);
  76. assert(jg[1] == 1);
  77. const int ih[] = {0, 1, 1};
  78. const unsigned sh = sizeof(ih)/sizeof(ih[0]);
  79. int jh[sh] = {-1};
  80. r = std::unique_copy(InIter(ih), InIter(ih+sh), OutIter(jh));
  81. assert(base(r) == jh + 2);
  82. assert(jh[0] == 0);
  83. assert(jh[1] == 1);
  84. const int ii[] = {0, 1, 1, 1, 2, 2, 2};
  85. const unsigned si = sizeof(ii)/sizeof(ii[0]);
  86. int ji[si] = {-1};
  87. r = std::unique_copy(InIter(ii), InIter(ii+si), OutIter(ji));
  88. assert(base(r) == ji + 3);
  89. assert(ji[0] == 0);
  90. assert(ji[1] == 1);
  91. assert(ji[2] == 2);
  92. }
  93. int main(int, char**)
  94. {
  95. test<input_iterator<const int*>, output_iterator<int*> >();
  96. test<input_iterator<const int*>, forward_iterator<int*> >();
  97. test<input_iterator<const int*>, bidirectional_iterator<int*> >();
  98. test<input_iterator<const int*>, random_access_iterator<int*> >();
  99. test<input_iterator<const int*>, int*>();
  100. test<forward_iterator<const int*>, output_iterator<int*> >();
  101. test<forward_iterator<const int*>, forward_iterator<int*> >();
  102. test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
  103. test<forward_iterator<const int*>, random_access_iterator<int*> >();
  104. test<forward_iterator<const int*>, int*>();
  105. test<bidirectional_iterator<const int*>, output_iterator<int*> >();
  106. test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
  107. test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
  108. test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
  109. test<bidirectional_iterator<const int*>, int*>();
  110. test<random_access_iterator<const int*>, output_iterator<int*> >();
  111. test<random_access_iterator<const int*>, forward_iterator<int*> >();
  112. test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
  113. test<random_access_iterator<const int*>, random_access_iterator<int*> >();
  114. test<random_access_iterator<const int*>, int*>();
  115. test<const int*, output_iterator<int*> >();
  116. test<const int*, forward_iterator<int*> >();
  117. test<const int*, bidirectional_iterator<int*> >();
  118. test<const int*, random_access_iterator<int*> >();
  119. test<const int*, int*>();
  120. #if TEST_STD_VER > 17
  121. static_assert(test_constexpr());
  122. #endif
  123. return 0;
  124. }