unique_copy.pass.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // OutIter
  16. // unique_copy(InIter first, InIter last, OutIter result);
  17. #include <algorithm>
  18. #include <cassert>
  19. #include "test_iterators.h"
  20. template <class InIter, class OutIter>
  21. void
  22. test()
  23. {
  24. const int ia[] = {0};
  25. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  26. int ja[sa] = {-1};
  27. OutIter r = std::unique_copy(InIter(ia), InIter(ia+sa), OutIter(ja));
  28. assert(base(r) == ja + sa);
  29. assert(ja[0] == 0);
  30. const int ib[] = {0, 1};
  31. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  32. int jb[sb] = {-1};
  33. r = std::unique_copy(InIter(ib), InIter(ib+sb), OutIter(jb));
  34. assert(base(r) == jb + sb);
  35. assert(jb[0] == 0);
  36. assert(jb[1] == 1);
  37. const int ic[] = {0, 0};
  38. const unsigned sc = sizeof(ic)/sizeof(ic[0]);
  39. int jc[sc] = {-1};
  40. r = std::unique_copy(InIter(ic), InIter(ic+sc), OutIter(jc));
  41. assert(base(r) == jc + 1);
  42. assert(jc[0] == 0);
  43. const int id[] = {0, 0, 1};
  44. const unsigned sd = sizeof(id)/sizeof(id[0]);
  45. int jd[sd] = {-1};
  46. r = std::unique_copy(InIter(id), InIter(id+sd), OutIter(jd));
  47. assert(base(r) == jd + 2);
  48. assert(jd[0] == 0);
  49. assert(jd[1] == 1);
  50. const int ie[] = {0, 0, 1, 0};
  51. const unsigned se = sizeof(ie)/sizeof(ie[0]);
  52. int je[se] = {-1};
  53. r = std::unique_copy(InIter(ie), InIter(ie+se), OutIter(je));
  54. assert(base(r) == je + 3);
  55. assert(je[0] == 0);
  56. assert(je[1] == 1);
  57. assert(je[2] == 0);
  58. const int ig[] = {0, 0, 1, 1};
  59. const unsigned sg = sizeof(ig)/sizeof(ig[0]);
  60. int jg[sg] = {-1};
  61. r = std::unique_copy(InIter(ig), InIter(ig+sg), OutIter(jg));
  62. assert(base(r) == jg + 2);
  63. assert(jg[0] == 0);
  64. assert(jg[1] == 1);
  65. const int ih[] = {0, 1, 1};
  66. const unsigned sh = sizeof(ih)/sizeof(ih[0]);
  67. int jh[sh] = {-1};
  68. r = std::unique_copy(InIter(ih), InIter(ih+sh), OutIter(jh));
  69. assert(base(r) == jh + 2);
  70. assert(jh[0] == 0);
  71. assert(jh[1] == 1);
  72. const int ii[] = {0, 1, 1, 1, 2, 2, 2};
  73. const unsigned si = sizeof(ii)/sizeof(ii[0]);
  74. int ji[si] = {-1};
  75. r = std::unique_copy(InIter(ii), InIter(ii+si), OutIter(ji));
  76. assert(base(r) == ji + 3);
  77. assert(ji[0] == 0);
  78. assert(ji[1] == 1);
  79. assert(ji[2] == 2);
  80. }
  81. int main()
  82. {
  83. test<input_iterator<const int*>, output_iterator<int*> >();
  84. test<input_iterator<const int*>, forward_iterator<int*> >();
  85. test<input_iterator<const int*>, bidirectional_iterator<int*> >();
  86. test<input_iterator<const int*>, random_access_iterator<int*> >();
  87. test<input_iterator<const int*>, int*>();
  88. test<forward_iterator<const int*>, output_iterator<int*> >();
  89. test<forward_iterator<const int*>, forward_iterator<int*> >();
  90. test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
  91. test<forward_iterator<const int*>, random_access_iterator<int*> >();
  92. test<forward_iterator<const int*>, int*>();
  93. test<bidirectional_iterator<const int*>, output_iterator<int*> >();
  94. test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
  95. test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
  96. test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
  97. test<bidirectional_iterator<const int*>, int*>();
  98. test<random_access_iterator<const int*>, output_iterator<int*> >();
  99. test<random_access_iterator<const int*>, forward_iterator<int*> >();
  100. test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
  101. test<random_access_iterator<const int*>, random_access_iterator<int*> >();
  102. test<random_access_iterator<const int*>, int*>();
  103. test<const int*, output_iterator<int*> >();
  104. test<const int*, forward_iterator<int*> >();
  105. test<const int*, bidirectional_iterator<int*> >();
  106. test<const int*, random_access_iterator<int*> >();
  107. test<const int*, int*>();
  108. }