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++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 #define _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 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 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