sample.stable.pass.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // <algorithm>
  11. // template <class PopulationIterator, class SampleIterator, class Distance,
  12. // class UniformRandomNumberGenerator>
  13. // SampleIterator sample(PopulationIterator first, PopulationIterator last,
  14. // SampleIterator out, Distance n,
  15. // UniformRandomNumberGenerator &&g);
  16. #include <algorithm>
  17. #include <random>
  18. #include <cassert>
  19. #include "test_iterators.h"
  20. // Stable if and only if PopulationIterator meets the requirements of a
  21. // ForwardIterator type.
  22. template <class PopulationIterator, class SampleIterator>
  23. void test_stability(bool expect_stable) {
  24. const unsigned kPopulationSize = 100;
  25. int ia[kPopulationSize];
  26. for (unsigned i = 0; i < kPopulationSize; ++i)
  27. ia[i] = i;
  28. PopulationIterator first(ia);
  29. PopulationIterator last(ia + kPopulationSize);
  30. const unsigned kSampleSize = 20;
  31. int oa[kPopulationSize];
  32. SampleIterator out(oa);
  33. std::minstd_rand g;
  34. const int kIterations = 1000;
  35. bool unstable = false;
  36. for (int i = 0; i < kIterations; ++i) {
  37. std::sample(first, last, out, kSampleSize, g);
  38. unstable |= !std::is_sorted(oa, oa + kSampleSize);
  39. }
  40. assert(expect_stable == !unstable);
  41. }
  42. int main() {
  43. test_stability<forward_iterator<int *>, output_iterator<int *> >(true);
  44. test_stability<input_iterator<int *>, random_access_iterator<int *> >(false);
  45. }