Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / algorithms / alg.modifying.operations / alg.random.sample / sample.stable.pass.cpp
blob99ae6873bf6236a23e267078d2676b1432ae6bf7
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
11 // <algorithm>
13 // template <class PopulationIterator, class SampleIterator, class Distance,
14 // class UniformRandomNumberGenerator>
15 // SampleIterator sample(PopulationIterator first, PopulationIterator last,
16 // SampleIterator out, Distance n,
17 // UniformRandomNumberGenerator &&g);
19 #include <algorithm>
20 #include <random>
21 #include <cassert>
23 #include "test_macros.h"
24 #include "test_iterators.h"
26 // Stable if and only if PopulationIterator meets the requirements of a
27 // ForwardIterator type.
28 template <class PopulationIterator, class SampleIterator>
29 void test_stability(bool expect_stable) {
30 const unsigned kPopulationSize = 100;
31 int ia[kPopulationSize];
32 for (unsigned i = 0; i < kPopulationSize; ++i)
33 ia[i] = i;
34 PopulationIterator first(ia);
35 PopulationIterator last(ia + kPopulationSize);
37 const unsigned kSampleSize = 20;
38 int oa[kPopulationSize];
39 SampleIterator out(oa);
41 std::minstd_rand g;
43 const int kIterations = 1000;
44 bool unstable = false;
45 for (int i = 0; i < kIterations; ++i) {
46 std::sample(first, last, out, kSampleSize, g);
47 unstable |= !std::is_sorted(oa, oa + kSampleSize);
49 assert(expect_stable == !unstable);
52 int main(int, char**) {
53 test_stability<forward_iterator<int *>, cpp17_output_iterator<int *> >(true);
54 test_stability<cpp17_input_iterator<int *>, random_access_iterator<int *> >(false);
56 return 0;