1e7154709SEric Fiselier //===----------------------------------------------------------------------===//
2e7154709SEric 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
6e7154709SEric Fiselier //
7e7154709SEric Fiselier //===----------------------------------------------------------------------===//
8e7154709SEric Fiselier 
931cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14
10e7154709SEric Fiselier 
11e7154709SEric Fiselier // <algorithm>
12e7154709SEric Fiselier 
13e7154709SEric Fiselier // template <class PopulationIterator, class SampleIterator, class Distance,
14e7154709SEric Fiselier //           class UniformRandomNumberGenerator>
15e7154709SEric Fiselier // SampleIterator sample(PopulationIterator first, PopulationIterator last,
16e7154709SEric Fiselier //                       SampleIterator out, Distance n,
17e7154709SEric Fiselier //                       UniformRandomNumberGenerator &&g);
18e7154709SEric Fiselier 
19e7154709SEric Fiselier #include <algorithm>
20e7154709SEric Fiselier #include <random>
21e7154709SEric Fiselier #include <cassert>
22e7154709SEric Fiselier 
237fc6a556SMarshall Clow #include "test_macros.h"
24e7154709SEric Fiselier #include "test_iterators.h"
25e7154709SEric Fiselier 
26e7154709SEric Fiselier // Stable if and only if PopulationIterator meets the requirements of a
27e7154709SEric Fiselier // ForwardIterator type.
28e7154709SEric Fiselier template <class PopulationIterator, class SampleIterator>
test_stability(bool expect_stable)29e7154709SEric Fiselier void test_stability(bool expect_stable) {
30e7154709SEric Fiselier   const unsigned kPopulationSize = 100;
31e7154709SEric Fiselier   int ia[kPopulationSize];
32e7154709SEric Fiselier   for (unsigned i = 0; i < kPopulationSize; ++i)
33e7154709SEric Fiselier     ia[i] = i;
34e7154709SEric Fiselier   PopulationIterator first(ia);
35e7154709SEric Fiselier   PopulationIterator last(ia + kPopulationSize);
36e7154709SEric Fiselier 
37e7154709SEric Fiselier   const unsigned kSampleSize = 20;
38e7154709SEric Fiselier   int oa[kPopulationSize];
39e7154709SEric Fiselier   SampleIterator out(oa);
40e7154709SEric Fiselier 
41e7154709SEric Fiselier   std::minstd_rand g;
42e7154709SEric Fiselier 
43e7154709SEric Fiselier   const int kIterations = 1000;
44e7154709SEric Fiselier   bool unstable = false;
45e7154709SEric Fiselier   for (int i = 0; i < kIterations; ++i) {
46e7154709SEric Fiselier     std::sample(first, last, out, kSampleSize, g);
47e7154709SEric Fiselier     unstable |= !std::is_sorted(oa, oa + kSampleSize);
48e7154709SEric Fiselier   }
49e7154709SEric Fiselier   assert(expect_stable == !unstable);
50e7154709SEric Fiselier }
51e7154709SEric Fiselier 
main(int,char **)522df59c50SJF Bastien int main(int, char**) {
53*5e97d37bSMark de Wever   test_stability<forward_iterator<int *>, cpp17_output_iterator<int *> >(true);
54773ae441SChristopher Di Bella   test_stability<cpp17_input_iterator<int *>, random_access_iterator<int *> >(false);
552df59c50SJF Bastien 
562df59c50SJF Bastien   return 0;
57e7154709SEric Fiselier }
58