random_shuffle.pass.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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>
  11. // requires ShuffleIterator<Iter>
  12. // void
  13. // random_shuffle(Iter first, Iter last);
  14. #define _LIBCPP_DISABLE_DEPRECATION_WARNINGS
  15. #include <algorithm>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "test_iterators.h"
  19. template <class Iter>
  20. void
  21. test_with_iterator()
  22. {
  23. int empty[] = {};
  24. std::random_shuffle(Iter(empty), Iter(empty));
  25. const int all_elements[] = {1, 2, 3, 4};
  26. int shuffled[] = {1, 2, 3, 4};
  27. const unsigned size = sizeof(all_elements)/sizeof(all_elements[0]);
  28. std::random_shuffle(Iter(shuffled), Iter(shuffled+size));
  29. assert(std::is_permutation(shuffled, shuffled+size, all_elements));
  30. std::random_shuffle(Iter(shuffled), Iter(shuffled+size));
  31. assert(std::is_permutation(shuffled, shuffled+size, all_elements));
  32. }
  33. int main(int, char**)
  34. {
  35. int ia[] = {1, 2, 3, 4};
  36. int ia1[] = {1, 4, 3, 2};
  37. int ia2[] = {4, 1, 2, 3};
  38. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  39. std::random_shuffle(ia, ia+sa);
  40. LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
  41. assert(std::is_permutation(ia, ia+sa, ia1));
  42. std::random_shuffle(ia, ia+sa);
  43. LIBCPP_ASSERT(std::equal(ia, ia+sa, ia2));
  44. assert(std::is_permutation(ia, ia+sa, ia2));
  45. test_with_iterator<random_access_iterator<int*> >();
  46. test_with_iterator<int*>();
  47. return 0;
  48. }