unique_copy.pass.cpp 5.0 KB

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