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