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 
9 // <algorithm>
10 
11 // template<RandomAccessIterator Iter>
12 //   requires ShuffleIterator<Iter>
13 //   void
14 //   random_shuffle(Iter first, Iter last);
15 
16 // REQUIRES: c++03 || c++11 || c++14
17 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
18 
19 #include <algorithm>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 #include "test_iterators.h"
24 
25 template <class Iter>
26 void
test_with_iterator()27 test_with_iterator()
28 {
29     int empty[] = {};
30     std::random_shuffle(Iter(empty), Iter(empty));
31 
32     const int all_elements[] = {1, 2, 3, 4};
33     int           shuffled[] = {1, 2, 3, 4};
34     const unsigned size = sizeof(all_elements)/sizeof(all_elements[0]);
35 
36     std::random_shuffle(Iter(shuffled), Iter(shuffled+size));
37     assert(std::is_permutation(shuffled, shuffled+size, all_elements));
38 
39     std::random_shuffle(Iter(shuffled), Iter(shuffled+size));
40     assert(std::is_permutation(shuffled, shuffled+size, all_elements));
41 }
42 
43 
main(int,char **)44 int main(int, char**)
45 {
46     int ia[]  = {1, 2, 3, 4};
47     int ia1[] = {1, 4, 3, 2};
48     int ia2[] = {4, 1, 2, 3};
49     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
50 
51     std::random_shuffle(ia, ia+sa);
52     LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
53     assert(std::is_permutation(ia, ia+sa, ia1));
54 
55     std::random_shuffle(ia, ia+sa);
56     LIBCPP_ASSERT(std::equal(ia, ia+sa, ia2));
57     assert(std::is_permutation(ia, ia+sa, ia2));
58 
59     test_with_iterator<random_access_iterator<int*> >();
60     test_with_iterator<int*>();
61 
62   return 0;
63 }
64