Debian Codegraph @

libstdc++-v3/testsuite/experimental/algorithm/sample-2.cc

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