| 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++20 } } |
| 19 | // { dg-require-cstdint "" } |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <random> |
| 23 | #include <testsuite_hooks.h> |
| 24 | #include <testsuite_iterators.h> |
| 25 | |
| 26 | using __gnu_test::test_range; |
| 27 | using __gnu_test::forward_iterator_wrapper; |
| 28 | using __gnu_test::input_iterator_wrapper; |
| 29 | using __gnu_test::output_iterator_wrapper; |
| 30 | using __gnu_test::random_access_iterator_wrapper; |
| 31 | |
| 32 | namespace ranges = std::ranges; |
| 33 | |
| 34 | std::mt19937 rng; |
| 35 | |
| 36 | template<template<typename> typename in_wrapper, |
| 37 | template<typename> typename out_wrapper> |
| 38 | void |
| 39 | test01() |
| 40 | { |
| 41 | const int x[] = {1,2,3,4,5,6,7,8,9,10}; |
| 42 | test_range<const int, in_wrapper> rx(x); |
| 43 | int y[10]; |
| 44 | test_range<int, out_wrapper> ry(y); |
| 45 | auto out = ranges::sample(rx.begin(), rx.end(), ry.begin(), 20, rng); |
| 46 | VERIFY( out.ptr == y+10 ); |
| 47 | VERIFY( ranges::equal(x, y) ); |
| 48 | |
| 49 | for (int i = 0; i < 100; i++) |
| 50 | { |
| 51 | int z[5] = {0}; |
| 52 | test_range<int, out_wrapper> rz(z); |
| 53 | rx.bounds.first = x; |
| 54 | auto out = ranges::sample(rx, rz.begin(), 5, rng); |
| 55 | VERIFY( out.ptr == z+5 ); |
| 56 | ranges::sort(z); |
| 57 | VERIFY( ranges::adjacent_find(z) == out.ptr ); |
| 58 | VERIFY( ranges::includes(x, z) ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | int |
| 63 | main() |
| 64 | { |
| 65 | test01<forward_iterator_wrapper, output_iterator_wrapper>(); |
| 66 | test01<input_iterator_wrapper, random_access_iterator_wrapper>(); |
| 67 | } |