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