diff options
author | Eric Fiselier <eric@efcs.ca> | 2016-08-28 22:14:37 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2016-08-28 22:14:37 +0000 |
commit | e7154709e02a388db2d8dce91860f2452f4145e0 (patch) | |
tree | d7cd66939217540b5b74d9a8484da38ed3179f42 /libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample/sample.stable.pass.cpp | |
parent | 040411762fde039dcd1a7f513d95d01266bfa94d (diff) | |
download | bcm5719-llvm-e7154709e02a388db2d8dce91860f2452f4145e0.tar.gz bcm5719-llvm-e7154709e02a388db2d8dce91860f2452f4145e0.zip |
Implement C++17 std::sample.
This patch implements the std::sample function added to C++17 from LFTS. It
also removes the std::experimental::sample implementation which now forwards
to std::sample.
llvm-svn: 279948
Diffstat (limited to 'libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample/sample.stable.pass.cpp')
-rw-r--r-- | libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample/sample.stable.pass.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample/sample.stable.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample/sample.stable.pass.cpp new file mode 100644 index 00000000000..db484238c95 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample/sample.stable.pass.cpp @@ -0,0 +1,55 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// <algorithm> + +// template <class PopulationIterator, class SampleIterator, class Distance, +// class UniformRandomNumberGenerator> +// SampleIterator sample(PopulationIterator first, PopulationIterator last, +// SampleIterator out, Distance n, +// UniformRandomNumberGenerator &&g); + +#include <algorithm> +#include <random> +#include <cassert> + +#include "test_iterators.h" + +// Stable if and only if PopulationIterator meets the requirements of a +// ForwardIterator type. +template <class PopulationIterator, class SampleIterator> +void test_stability(bool expect_stable) { + const unsigned kPopulationSize = 100; + int ia[kPopulationSize]; + for (unsigned i = 0; i < kPopulationSize; ++i) + ia[i] = i; + PopulationIterator first(ia); + PopulationIterator last(ia + kPopulationSize); + + const unsigned kSampleSize = 20; + int oa[kPopulationSize]; + SampleIterator out(oa); + + std::minstd_rand g; + + const int kIterations = 1000; + bool unstable = false; + for (int i = 0; i < kIterations; ++i) { + std::sample(first, last, out, kSampleSize, g); + unstable |= !std::is_sorted(oa, oa + kSampleSize); + } + assert(expect_stable == !unstable); +} + +int main() { + test_stability<forward_iterator<int *>, output_iterator<int *> >(true); + test_stability<input_iterator<int *>, random_access_iterator<int *> >(false); +} |