]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/algorithm/sample.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / algorithm / sample.cc
1 // Copyright (C) 2014-2017 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 <iterator>
22 #include <sstream>
23 #include <forward_list>
24 #include <vector>
25 #include <random>
26 #include <algorithm>
27 #include <testsuite_hooks.h>
28
29 std::mt19937 rng;
30
31 using std::istream_iterator;
32 using std::ostream_iterator;
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, rng);
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, rng);
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 std::istringstream pop("0 1 2 3 4 5 6 7 8 9");
64 int samp[5] = { };
65
66 // input iterator for population
67 auto it = std::experimental::sample(istream_iterator<int>{pop}, {},
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 std::forward_list<int> pop{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
81 std::stringstream samp;
82
83 // forward iterator for population and output iterator for result
84 std::experimental::sample(pop.begin(), pop.end(),
85 ostream_iterator<int>{samp, " "},
86 5, rng);
87
88 // samp.rdbuf()->pubseekoff(0, std::ios::beg);
89 std::vector<int> v(istream_iterator<int>{samp}, {});
90 VERIFY( v.size() == 5 );
91
92 std::sort(v.begin(), v.end());
93 auto it = std::unique(v.begin(), v.end());
94 VERIFY( it == v.end() );
95 }
96
97 int
98 main()
99 {
100 test01();
101 test02();
102 test03();
103 test04();
104 }