random_shuffle_rand.pass.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // REQUIRES: c++98 || c++03 || c++11 || c++14
  10. // template<RandomAccessIterator Iter, Callable<auto, Iter::difference_type> Rand>
  11. // requires ShuffleIterator<Iter>
  12. // && Convertible<Rand::result_type, Iter::difference_type>
  13. // void
  14. // random_shuffle(Iter first, Iter last, Rand&& rand);
  15. #define _LIBCPP_DISABLE_DEPRECATION_WARNINGS
  16. #include <algorithm>
  17. #include <cassert>
  18. #include <cstddef>
  19. #include "test_macros.h"
  20. #include "test_iterators.h"
  21. struct gen
  22. {
  23. std::ptrdiff_t operator()(std::ptrdiff_t n)
  24. {
  25. return n-1;
  26. }
  27. };
  28. template <class Iter>
  29. void
  30. test_with_iterator()
  31. {
  32. int ia[] = {1, 2, 3, 4};
  33. int ia1[] = {4, 1, 2, 3};
  34. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  35. gen r;
  36. std::random_shuffle(ia, ia+sa, r);
  37. LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
  38. assert(std::is_permutation(ia, ia+sa, ia1));
  39. std::random_shuffle(ia, ia+sa, r);
  40. assert(std::is_permutation(ia, ia+sa, ia1));
  41. }
  42. int main(int, char**)
  43. {
  44. test_with_iterator<random_access_iterator<int*> >();
  45. test_with_iterator<int*>();
  46. return 0;
  47. }