Home | History | Annotate | Download | only in alg.random.shuffle
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <algorithm>
     11 // REQUIRES: c++98 || c++03 || c++11 || c++14
     12 
     13 // template<RandomAccessIterator Iter, Callable<auto, Iter::difference_type> Rand>
     14 //   requires ShuffleIterator<Iter>
     15 //         && Convertible<Rand::result_type, Iter::difference_type>
     16 //   void
     17 //   random_shuffle(Iter first, Iter last, Rand&& rand);
     18 
     19 #include <algorithm>
     20 #include <cassert>
     21 #include <cstddef>
     22 
     23 #include "test_macros.h"
     24 
     25 struct gen
     26 {
     27     std::ptrdiff_t operator()(std::ptrdiff_t n)
     28     {
     29         return n-1;
     30     }
     31 };
     32 
     33 int main()
     34 {
     35     int ia[] = {1, 2, 3, 4};
     36     int ia1[] = {4, 1, 2, 3};
     37     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
     38     gen r;
     39     std::random_shuffle(ia, ia+sa, r);
     40     LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
     41     assert(std::is_permutation(ia, ia+sa, ia1));
     42 }
     43