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 <array>
21#include <cassert>
22
23#include "test_macros.h"
24#include "test_iterators.h"
25
26template <class Iter>
27void
28test_with_iterator()
29{
30 std::array<int, 0> empty = {};
31 std::random_shuffle(Iter(empty.data()), Iter(empty.data()));
32
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]);
36
37 std::random_shuffle(Iter(shuffled), Iter(shuffled + size));
38 assert(std::is_permutation(shuffled, shuffled + size, all_elements));
39
40 std::random_shuffle(Iter(shuffled), Iter(shuffled + size));
41 assert(std::is_permutation(shuffled, shuffled + size, all_elements));
42}
43
44
45int main(int, char**)
46{
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]);
51
52 std::random_shuffle(first: ia, last: ia+sa);
53 LIBCPP_ASSERT(std::equal(first1: ia, last1: ia+sa, first2: ia1));
54 assert(std::is_permutation(ia, ia+sa, ia1));
55
56 std::random_shuffle(first: ia, last: ia+sa);
57 LIBCPP_ASSERT(std::equal(first1: ia, last1: ia+sa, first2: ia2));
58 assert(std::is_permutation(ia, ia+sa, ia2));
59
60 test_with_iterator<random_access_iterator<int*> >();
61 test_with_iterator<int*>();
62
63 return 0;
64}
65

source code of libcxx/test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle.pass.cpp