sample.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <range/v3/view/sample.hpp>
  2. #include <range/v3/algorithm/equal.hpp>
  3. #include <numeric>
  4. #include <vector>
  5. #include <random>
  6. #include "../simple_test.hpp"
  7. #include "../test_utils.hpp"
  8. using namespace ranges;
  9. int main ()
  10. {
  11. std::mt19937 engine;
  12. std::vector<int> pop(100);
  13. std::iota(std::begin(pop), std::end(pop), 0);
  14. {
  15. constexpr int N = 32;
  16. std::array<int, N> tmp;
  17. auto rng = pop | views::sample(N, engine);
  18. using Rng = decltype(rng);
  19. CPP_assert(input_range<Rng> && view_<Rng>);
  20. CPP_assert(!forward_range<Rng>);
  21. ranges::copy(rng, tmp.begin());
  22. rng = pop | views::sample(N, engine);
  23. CHECK(!ranges::equal(rng, tmp));
  24. engine = decltype(engine){};
  25. rng = pop | views::sample(N, engine);
  26. CHECK(ranges::equal(rng, tmp));
  27. }
  28. {
  29. int const some_ints[] = {0,1,2,3,4,5,6,7,8};
  30. auto rng = debug_input_view<int const>{some_ints} | views::sample(4, engine);
  31. using Rng = decltype(rng);
  32. CPP_assert(input_range<Rng> && view_<Rng>);
  33. CPP_assert(!forward_range<Rng>);
  34. CHECK(ranges::distance(rng) == 4);
  35. }
  36. return ::test_result();
  37. }