fill.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2014-present
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. //===----------------------------------------------------------------------===//
  12. //
  13. // The LLVM Compiler Infrastructure
  14. //
  15. // This file is dual licensed under the MIT and the University of Illinois Open
  16. // Source Licenses. See LICENSE.TXT for details.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include <cstring>
  20. #include <string>
  21. #include <vector>
  22. #include <range/v3/core.hpp>
  23. #include <range/v3/algorithm/fill.hpp>
  24. #include <range/v3/algorithm/equal.hpp>
  25. #include "../array.hpp"
  26. #include "../simple_test.hpp"
  27. #include "../test_utils.hpp"
  28. #include "../test_iterators.hpp"
  29. template<class Iter, class Sent = Iter>
  30. void
  31. test_char()
  32. {
  33. const unsigned n = 4;
  34. char ca[n] = {0};
  35. auto i = ranges::fill(Iter(ca), Sent(ca+n), char(1));
  36. CHECK(ca[0] == 1);
  37. CHECK(ca[1] == 1);
  38. CHECK(ca[2] == 1);
  39. CHECK(ca[3] == 1);
  40. CHECK(i == Iter(ca + 4));
  41. auto rng = ranges::make_subrange(Iter(ca), Sent(ca+n));
  42. i = ranges::fill(rng, char(2));
  43. CHECK(ca[0] == 2);
  44. CHECK(ca[1] == 2);
  45. CHECK(ca[2] == 2);
  46. CHECK(ca[3] == 2);
  47. CHECK(i == Iter(ca + 4));
  48. auto j = ranges::fill(::MakeTestRange(Iter(ca), Sent(ca+n)), char(3));
  49. CHECK(ca[0] == 3);
  50. CHECK(ca[1] == 3);
  51. CHECK(ca[2] == 3);
  52. CHECK(ca[3] == 3);
  53. CHECK(::is_dangling(j));
  54. }
  55. template<class Iter, class Sent = Iter>
  56. void
  57. test_int()
  58. {
  59. const unsigned n = 4;
  60. int ia[n] = {0};
  61. ranges::fill(Iter(ia), Sent(ia+n), 1);
  62. CHECK(ia[0] == 1);
  63. CHECK(ia[1] == 1);
  64. CHECK(ia[2] == 1);
  65. CHECK(ia[3] == 1);
  66. auto rng = ranges::make_subrange(Iter(ia), Sent(ia+n));
  67. ranges::fill(rng, 2);
  68. CHECK(ia[0] == 2);
  69. CHECK(ia[2] == 2);
  70. CHECK(ia[2] == 2);
  71. CHECK(ia[3] == 2);
  72. }
  73. constexpr auto fives()
  74. {
  75. test::array<int, 4> a{{0}};
  76. ranges::fill(a, 5);
  77. return a;
  78. }
  79. constexpr auto fives(int n)
  80. {
  81. test::array<int, 4> a{{0}};
  82. ranges::fill_n(ranges::begin(a), n, 5);
  83. return a;
  84. }
  85. int main()
  86. {
  87. test_char<ForwardIterator<char*> >();
  88. test_char<BidirectionalIterator<char*> >();
  89. test_char<RandomAccessIterator<char*> >();
  90. test_char<char*>();
  91. test_char<ForwardIterator<char*>, Sentinel<char*> >();
  92. test_char<BidirectionalIterator<char*>, Sentinel<char*> >();
  93. test_char<RandomAccessIterator<char*>, Sentinel<char*> >();
  94. test_int<ForwardIterator<int*> >();
  95. test_int<BidirectionalIterator<int*> >();
  96. test_int<RandomAccessIterator<int*> >();
  97. test_int<int*>();
  98. test_int<ForwardIterator<int*>, Sentinel<int*> >();
  99. test_int<BidirectionalIterator<int*>, Sentinel<int*> >();
  100. test_int<RandomAccessIterator<int*>, Sentinel<int*> >();
  101. {
  102. using IL = std::initializer_list<int>;
  103. STATIC_CHECK(ranges::equal(fives(), IL{5, 5, 5, 5}));
  104. STATIC_CHECK(ranges::equal(fives(2), IL{5, 5, 0, 0}));
  105. STATIC_CHECK(!ranges::equal(fives(2), IL{5, 5, 5, 5}));
  106. }
  107. return ::test_result();
  108. }