shuffle.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // Copyright 2005 - 2007 Adobe Systems Incorporated
  13. // Distributed under the MIT License(see accompanying file LICENSE_1_0_0.txt
  14. // or a copy at http://stlab.adobe.com/licenses.html)
  15. //===----------------------------------------------------------------------===//
  16. //
  17. // The LLVM Compiler Infrastructure
  18. //
  19. // This file is dual licensed under the MIT and the University of Illinois Open
  20. // Source Licenses. See LICENSE.TXT for details.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #include <array>
  24. #include <range/v3/core.hpp>
  25. #include <range/v3/algorithm/equal.hpp>
  26. #include <range/v3/algorithm/shuffle.hpp>
  27. #include <range/v3/numeric/iota.hpp>
  28. #include "../simple_test.hpp"
  29. #include "../test_utils.hpp"
  30. #include "../test_iterators.hpp"
  31. int main()
  32. {
  33. constexpr unsigned N = 100;
  34. {
  35. std::array<int, N> a, b, c;
  36. for (auto p : {&a, &b, &c})
  37. ranges::iota(*p, 0);
  38. std::minstd_rand g1, g2 = g1;
  39. ranges::shuffle(RandomAccessIterator<int*>(a.data()), Sentinel<int*>(a.data()+N), g1);
  40. CHECK(!ranges::equal(a, b));
  41. CHECK(ranges::shuffle(b.begin(), b.end(), g1) == b.end());
  42. CHECK(!ranges::equal(a, b));
  43. CHECK(ranges::shuffle(c.begin(), c.end(), g2) == c.end());
  44. CHECK(ranges::equal(a, c));
  45. CHECK(!ranges::equal(b, c));
  46. }
  47. {
  48. std::array<int, N> a, b, c;
  49. for (auto p : {&a, &b, &c})
  50. ranges::iota(*p, 0);
  51. std::minstd_rand g1, g2 = g1;
  52. auto rng = ::MakeTestRange(RandomAccessIterator<int*>(a.data()), Sentinel<int*>(a.data() + N));
  53. ranges::shuffle(rng, g1);
  54. CHECK(!ranges::equal(a, b));
  55. CHECK(ranges::shuffle(b, g2) == b.end());
  56. CHECK(ranges::equal(a, b));
  57. CHECK(ranges::shuffle(b, g1) == b.end());
  58. CHECK(!ranges::equal(a, b));
  59. CHECK(!ranges::equal(b, c));
  60. ranges::iota(a, 0);
  61. CHECK(::is_dangling(ranges::shuffle(std::move(rng), g1)));
  62. CHECK(!ranges::equal(a, c));
  63. }
  64. {
  65. std::array<int, N> a, b, c;
  66. for (auto p : {&a, &b, &c})
  67. ranges::iota(*p, 0);
  68. ranges::shuffle(RandomAccessIterator<int*>(a.data()), Sentinel<int*>(a.data() + N));
  69. CHECK(!ranges::equal(a, c));
  70. ranges::shuffle(b);
  71. CHECK(!ranges::equal(b, c));
  72. CHECK(!ranges::equal(a, b));
  73. }
  74. {
  75. std::array<int, N> a, b;
  76. for (auto p : {&a, &b})
  77. ranges::iota(*p, 0);
  78. ranges::shuffle(a);
  79. CHECK(!ranges::equal(a, b));
  80. }
  81. return ::test_result();
  82. }