| 1 | // Copyright (C) 2014-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++17 } } |
| 19 | // { dg-require-cstdint "" } |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <random> |
| 23 | #include <testsuite_hooks.h> |
| 24 | #include <testsuite_iterators.h> |
| 25 | |
| 26 | std::mt19937 rng; |
| 27 | |
| 28 | using std::sample; |
| 29 | using __gnu_test::test_container; |
| 30 | using __gnu_test::input_iterator_wrapper; |
| 31 | using __gnu_test::output_iterator_wrapper; |
| 32 | using __gnu_test::forward_iterator_wrapper; |
| 33 | using __gnu_test::random_access_iterator_wrapper; |
| 34 | |
| 35 | void |
| 36 | test01() |
| 37 | { |
| 38 | const int in[] = { 1, 2 }; |
| 39 | test_container<const int, random_access_iterator_wrapper> pop(in); |
| 40 | const int sample_size = 10; |
| 41 | int samp[sample_size] = { }; |
| 42 | |
| 43 | // population smaller than desired sample size |
| 44 | auto it = sample(pop.begin(), pop.end(), samp, sample_size, rng); |
| 45 | VERIFY( it == samp + std::distance(pop.begin(), pop.end()) ); |
| 46 | const auto sum = std::accumulate(pop.begin(), pop.end(), 0); |
| 47 | VERIFY( std::accumulate(samp, samp + sample_size, 0) == sum ); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | test02() |
| 52 | { |
| 53 | const int in[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; |
| 54 | test_container<const int, random_access_iterator_wrapper> pop(in); |
| 55 | const int sample_size = 10; |
| 56 | int samp[sample_size] = { }; |
| 57 | |
| 58 | auto it = sample(pop.begin(), pop.end(), samp, sample_size, rng); |
| 59 | VERIFY( it == samp + sample_size ); |
| 60 | |
| 61 | // verify no duplicates |
| 62 | std::sort(samp, it); |
| 63 | auto it2 = std::unique(samp, it); |
| 64 | VERIFY( it2 == it ); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | test03() |
| 69 | { |
| 70 | const int in[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 71 | test_container<const int, input_iterator_wrapper> pop(in); |
| 72 | const int sample_size = 5; |
| 73 | int samp[sample_size] = { }; |
| 74 | |
| 75 | // input iterator for population |
| 76 | auto it = sample(pop.begin(), pop.end(), samp, sample_size, rng); |
| 77 | VERIFY( it == samp + sample_size ); |
| 78 | |
| 79 | // verify no duplicates |
| 80 | std::sort(samp, it); |
| 81 | auto it2 = std::unique(samp, it); |
| 82 | VERIFY( it2 == it ); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | test04() |
| 87 | { |
| 88 | const int in[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 89 | test_container<const int, forward_iterator_wrapper> pop(in); |
| 90 | const int sample_size = 5; |
| 91 | int out[sample_size]; |
| 92 | test_container<int, output_iterator_wrapper> samp(out); |
| 93 | |
| 94 | // forward iterator for population and output iterator for result |
| 95 | auto res = sample(pop.begin(), pop.end(), samp.begin(), sample_size, rng); |
| 96 | VERIFY( res.ptr == (out + sample_size) ); |
| 97 | |
| 98 | // verify no duplicates |
| 99 | std::sort(std::begin(out), std::end(out)); |
| 100 | auto it = std::unique(std::begin(out), std::end(out)); |
| 101 | VERIFY( it == std::end(out) ); |
| 102 | } |
| 103 | |
| 104 | int |
| 105 | main() |
| 106 | { |
| 107 | test01(); |
| 108 | test02(); |
| 109 | test03(); |
| 110 | test04(); |
| 111 | } |