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// UNSUPPORTED: c++98, c++03, c++11, c++14 11 12// <algorithm> 13 14// template <class PopulationIterator, class SampleIterator, class Distance, 15// class UniformRandomNumberGenerator> 16// SampleIterator sample(PopulationIterator first, PopulationIterator last, 17// SampleIterator out, Distance n, 18// UniformRandomNumberGenerator &&g); 19 20#include <algorithm> 21#include <random> 22#include <cassert> 23 24#include "test_iterators.h" 25 26// Stable if and only if PopulationIterator meets the requirements of a 27// ForwardIterator type. 28template <class PopulationIterator, class SampleIterator> 29void 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); 36 37 const unsigned kSampleSize = 20; 38 int oa[kPopulationSize]; 39 SampleIterator out(oa); 40 41 std::minstd_rand g; 42 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); 48 } 49 assert(expect_stable == !unstable); 50} 51 52int main() { 53 test_stability<forward_iterator<int *>, output_iterator<int *> >(true); 54 test_stability<input_iterator<int *>, random_access_iterator<int *> >(false); 55} 56