brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · 5a2b58f Raw
62 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <algorithm>10 11// template<RandomAccessIterator Iter, Callable<auto, Iter::difference_type> Rand>12//   requires ShuffleIterator<Iter>13//         && Convertible<Rand::result_type, Iter::difference_type>14//   void15//   random_shuffle(Iter first, Iter last, Rand&& rand);16 17// REQUIRES: c++03 || c++11 || c++1418// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS19 20#include <algorithm>21#include <cassert>22#include <cstddef>23 24#include "test_macros.h"25#include "test_iterators.h"26 27 28struct gen29{30    std::ptrdiff_t operator()(std::ptrdiff_t n)31    {32        return n-1;33    }34};35 36 37template <class Iter>38void39test_with_iterator()40{41 42    int ia[] = {1, 2, 3, 4};43    int ia1[] = {4, 1, 2, 3};44    const unsigned sa = sizeof(ia)/sizeof(ia[0]);45    gen r;46 47    std::random_shuffle(ia, ia+sa, r);48    LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));49    assert(std::is_permutation(ia, ia+sa, ia1));50 51    std::random_shuffle(ia, ia+sa, r);52    assert(std::is_permutation(ia, ia+sa, ia1));53}54 55 56int main(int, char**)57{58    test_with_iterator<random_access_iterator<int*> >();59    test_with_iterator<int*>();60    return 0;61}62