summaryrefslogtreecommitdiffstats
path: root/libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample
diff options
context:
space:
mode:
authorStephan T. Lavavej <stl@exchange.microsoft.com>2016-11-18 21:54:43 +0000
committerStephan T. Lavavej <stl@exchange.microsoft.com>2016-11-18 21:54:43 +0000
commit09c311b9a46c72ffea99026755db046cc2b0364f (patch)
treef80ae06ef8fa4ec2955431485106f7b0208d4809 /libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample
parentb04c795e24654fc57a94a9032f54ac72e10d0b5e (diff)
downloadbcm5719-llvm-09c311b9a46c72ffea99026755db046cc2b0364f.tar.gz
bcm5719-llvm-09c311b9a46c72ffea99026755db046cc2b0364f.zip
[libcxx] [test] D26816: Fix non-Standard assumptions when testing sample().
sample() isn't specified with a reproducible algorithm, so expecting exact output is non-Standard. Mark those tests with LIBCPP_ASSERT. In test_small_population(), we're guaranteed to get all of the elements, but not necessarily in their original order. When PopulationCategory is forward, we're guaranteed stability (and can therefore test equal()). Otherwise, we can only test is_permutation(). (As it happens, both libcxx and MSVC's STL provide stability in this scenario for input-only iterators.) llvm-svn: 287383
Diffstat (limited to 'libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample')
-rw-r--r--libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample/sample.pass.cpp17
1 files changed, 14 insertions, 3 deletions
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample/sample.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample/sample.pass.cpp
index ed23d690c8f..0cb00e3670e 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample/sample.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.random.sample/sample.pass.cpp
@@ -19,9 +19,11 @@
#include <algorithm>
#include <random>
+#include <type_traits>
#include <cassert>
#include "test_iterators.h"
+#include "test_macros.h"
struct ReservoirSampleExpectations {
enum { os = 4 };
@@ -60,19 +62,23 @@ void test() {
const unsigned os = Expectations::os;
SampleItem oa[os];
const int *oa1 = Expectations::oa1;
+ ((void)oa1); // Prevent unused warning
const int *oa2 = Expectations::oa2;
+ ((void)oa2); // Prevent unused warning
std::minstd_rand g;
SampleIterator end;
end = std::sample(PopulationIterator(ia),
PopulationIterator(ia + is),
SampleIterator(oa), os, g);
assert(end.base() - oa == std::min(os, is));
- assert(std::equal(oa, oa + os, oa1));
+ // sample() is deterministic but non-reproducible;
+ // its results can vary between implementations.
+ LIBCPP_ASSERT(std::equal(oa, oa + os, oa1));
end = std::sample(PopulationIterator(ia),
PopulationIterator(ia + is),
SampleIterator(oa), os, std::move(g));
assert(end.base() - oa == std::min(os, is));
- assert(std::equal(oa, oa + os, oa2));
+ LIBCPP_ASSERT(std::equal(oa, oa + os, oa2));
}
template <template<class...> class PopulationIteratorType, class PopulationItem,
@@ -122,7 +128,12 @@ void test_small_population() {
PopulationIterator(ia + is),
SampleIterator(oa), os, g);
assert(end.base() - oa == std::min(os, is));
- assert(std::equal(oa, end.base(), oa1));
+ typedef typename std::iterator_traits<PopulationIterator>::iterator_category PopulationCategory;
+ if (std::is_base_of<std::forward_iterator_tag, PopulationCategory>::value) {
+ assert(std::equal(oa, end.base(), oa1));
+ } else {
+ assert(std::is_permutation(oa, end.base(), oa1));
+ }
}
int main() {
OpenPOWER on IntegriCloud