15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier
95a83710eSEric Fiselier // <algorithm>
105a83710eSEric Fiselier
115a83710eSEric Fiselier // template<RandomAccessIterator Iter>
125a83710eSEric Fiselier // requires ShuffleIterator<Iter>
135a83710eSEric Fiselier // void
145a83710eSEric Fiselier // random_shuffle(Iter first, Iter last);
155a83710eSEric Fiselier
16*61f2b3edSLouis Dionne // REQUIRES: c++03 || c++11 || c++14
17*61f2b3edSLouis Dionne // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
18a470a13aSLouis Dionne
195a83710eSEric Fiselier #include <algorithm>
205a83710eSEric Fiselier #include <cassert>
215a83710eSEric Fiselier
2216629519SEric Fiselier #include "test_macros.h"
235a8525e0SMarshall Clow #include "test_iterators.h"
245a8525e0SMarshall Clow
255a8525e0SMarshall Clow template <class Iter>
265a8525e0SMarshall Clow void
test_with_iterator()275a8525e0SMarshall Clow test_with_iterator()
285a8525e0SMarshall Clow {
295a8525e0SMarshall Clow int empty[] = {};
305a8525e0SMarshall Clow std::random_shuffle(Iter(empty), Iter(empty));
315a8525e0SMarshall Clow
325a8525e0SMarshall Clow const int all_elements[] = {1, 2, 3, 4};
335a8525e0SMarshall Clow int shuffled[] = {1, 2, 3, 4};
345a8525e0SMarshall Clow const unsigned size = sizeof(all_elements)/sizeof(all_elements[0]);
355a8525e0SMarshall Clow
365a8525e0SMarshall Clow std::random_shuffle(Iter(shuffled), Iter(shuffled+size));
375a8525e0SMarshall Clow assert(std::is_permutation(shuffled, shuffled+size, all_elements));
385a8525e0SMarshall Clow
395a8525e0SMarshall Clow std::random_shuffle(Iter(shuffled), Iter(shuffled+size));
405a8525e0SMarshall Clow assert(std::is_permutation(shuffled, shuffled+size, all_elements));
415a8525e0SMarshall Clow }
425a8525e0SMarshall Clow
4316629519SEric Fiselier
main(int,char **)442df59c50SJF Bastien int main(int, char**)
455a83710eSEric Fiselier {
465a83710eSEric Fiselier int ia[] = {1, 2, 3, 4};
475a83710eSEric Fiselier int ia1[] = {1, 4, 3, 2};
485a83710eSEric Fiselier int ia2[] = {4, 1, 2, 3};
495a83710eSEric Fiselier const unsigned sa = sizeof(ia)/sizeof(ia[0]);
505a8525e0SMarshall Clow
515a83710eSEric Fiselier std::random_shuffle(ia, ia+sa);
5216629519SEric Fiselier LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
5316629519SEric Fiselier assert(std::is_permutation(ia, ia+sa, ia1));
545a8525e0SMarshall Clow
555a83710eSEric Fiselier std::random_shuffle(ia, ia+sa);
5616629519SEric Fiselier LIBCPP_ASSERT(std::equal(ia, ia+sa, ia2));
5716629519SEric Fiselier assert(std::is_permutation(ia, ia+sa, ia2));
585a8525e0SMarshall Clow
595a8525e0SMarshall Clow test_with_iterator<random_access_iterator<int*> >();
605a8525e0SMarshall Clow test_with_iterator<int*>();
615a8525e0SMarshall Clow
622df59c50SJF Bastien return 0;
635a83710eSEric Fiselier }
64