]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/algorithm/sample.cc
Add experimental::sample and experimental::shuffle from N4531
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / algorithm / sample.cc
1 // Copyright (C) 2014-2018 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
20 #include <experimental/algorithm>
21 #include <random>
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
24
25 using __gnu_test::test_container;
26 using __gnu_test::input_iterator_wrapper;
27 using __gnu_test::output_iterator_wrapper;
28 using __gnu_test::forward_iterator_wrapper;
29
30 std::mt19937 rng;
31
32 void
33 test01()
34 {
35 const int pop[] = { 1, 2 };
36 int samp[10] = { };
37
38 // population smaller than desired sample size
39 auto it = std::experimental::sample(pop, pop + 2, samp, 10, rng);
40 VERIFY( it == samp + 2 );
41 VERIFY( std::accumulate(samp, samp + 10, 0) == 3 );
42 }
43
44 void
45 test02()
46 {
47 const int pop[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
48 int samp[10] = { };
49
50 auto it = std::experimental::sample(pop, std::end(pop), samp, 10, rng);
51 VERIFY( it == samp + 10 );
52
53 std::sort(samp, it);
54 auto it2 = std::unique(samp, it);
55 VERIFY( it2 == it );
56 }
57
58 void
59 test03()
60 {
61 const int pop[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, };
62 int samp[5] = { };
63
64 // input iterator for population
65 test_container<const int, input_iterator_wrapper> pop_in{pop};
66 auto it = std::experimental::sample(pop_in.begin(), pop_in.end(),
67 samp,
68 5, rng);
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, rng);
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 }