1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // template<RandomAccessIterator Iter>
12 // requires ShuffleIterator<Iter>
14 // random_shuffle(Iter first, Iter last);
16 // REQUIRES: c++03 || c++11 || c++14
17 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
23 #include "test_macros.h"
24 #include "test_iterators.h"
30 std::array
<int, 0> empty
= {};
31 std::random_shuffle(Iter(empty
.data()), Iter(empty
.data()));
33 const int all_elements
[] = {1, 2, 3, 4};
34 int shuffled
[] = {1, 2, 3, 4};
35 const unsigned size
= sizeof(all_elements
) / sizeof(all_elements
[0]);
37 std::random_shuffle(Iter(shuffled
), Iter(shuffled
+ size
));
38 assert(std::is_permutation(shuffled
, shuffled
+ size
, all_elements
));
40 std::random_shuffle(Iter(shuffled
), Iter(shuffled
+ size
));
41 assert(std::is_permutation(shuffled
, shuffled
+ size
, all_elements
));
47 int ia
[] = {1, 2, 3, 4};
48 int ia1
[] = {1, 4, 3, 2};
49 int ia2
[] = {4, 1, 2, 3};
50 const unsigned sa
= sizeof(ia
)/sizeof(ia
[0]);
52 std::random_shuffle(ia
, ia
+sa
);
53 LIBCPP_ASSERT(std::equal(ia
, ia
+sa
, ia1
));
54 assert(std::is_permutation(ia
, ia
+sa
, ia1
));
56 std::random_shuffle(ia
, ia
+sa
);
57 LIBCPP_ASSERT(std::equal(ia
, ia
+sa
, ia2
));
58 assert(std::is_permutation(ia
, ia
+sa
, ia2
));
60 test_with_iterator
<random_access_iterator
<int*> >();
61 test_with_iterator
<int*>();