]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/experimental/algorithm/sample.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / algorithm / sample.cc
CommitLineData
a945c346 1// Copyright (C) 2014-2024 Free Software Foundation, Inc.
43e9f722
JW
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
52066eae 18// { dg-do run { target c++14 } }
49ba2588 19// { dg-require-cstdint "" }
43e9f722
JW
20
21#include <experimental/algorithm>
40799fe5 22#include <random>
43e9f722 23#include <testsuite_hooks.h>
37cb7887 24#include <testsuite_iterators.h>
43e9f722 25
37cb7887
JW
26using __gnu_test::test_container;
27using __gnu_test::input_iterator_wrapper;
28using __gnu_test::output_iterator_wrapper;
29using __gnu_test::forward_iterator_wrapper;
43e9f722 30
37cb7887 31std::mt19937 rng;
43e9f722
JW
32
33void
34test01()
35{
36 const int pop[] = { 1, 2 };
37 int samp[10] = { };
38
39 // population smaller than desired sample size
17739146 40 auto it = std::experimental::sample(pop, pop + 2, samp, 10, rng);
43e9f722
JW
41 VERIFY( it == samp + 2 );
42 VERIFY( std::accumulate(samp, samp + 10, 0) == 3 );
43}
44
45void
46test02()
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
17739146 51 auto it = std::experimental::sample(pop, std::end(pop), samp, 10, rng);
43e9f722
JW
52 VERIFY( it == samp + 10 );
53
54 std::sort(samp, it);
55 auto it2 = std::unique(samp, it);
56 VERIFY( it2 == it );
57}
58
59void
60test03()
61{
37cb7887 62 const int pop[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, };
43e9f722
JW
63 int samp[5] = { };
64
65 // input iterator for population
37cb7887
JW
66 test_container<const int, input_iterator_wrapper> pop_in{pop};
67 auto it = std::experimental::sample(pop_in.begin(), pop_in.end(),
17739146
JW
68 samp,
69 5, rng);
43e9f722
JW
70 VERIFY( it == samp + 5 );
71
72 std::sort(samp, it);
73 auto it2 = std::unique(samp, it);
74 VERIFY( it2 == it );
75}
76
77void
78test04()
79{
37cb7887
JW
80 const int pop[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
81 int samp[5] = { };
43e9f722
JW
82
83 // forward iterator for population and output iterator for result
37cb7887
JW
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);
43e9f722 88
37cb7887 89 VERIFY( std::distance(samp, it.ptr) == 5 );
43e9f722 90
37cb7887
JW
91 std::sort(samp, it.ptr);
92 auto it2 = std::unique(samp, it.ptr);
93 VERIFY( it2 == it.ptr );
43e9f722
JW
94}
95
96int
97main()
98{
99 test01();
100 test02();
101 test03();
102 test04();
103}