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