]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/algorithm/sample.cc
1 // Copyright (C) 2014-2017 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++14 } }
20 #include <experimental/algorithm>
23 #include <forward_list>
27 #include <testsuite_hooks.h>
31 using std::istream_iterator
;
32 using std::ostream_iterator
;
37 const int pop
[] = { 1, 2 };
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 );
49 const int pop
[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
52 auto it
= std::experimental::sample(pop
, std::end(pop
), samp
, 10, rng
);
53 VERIFY( it
== samp
+ 10 );
56 auto it2
= std::unique(samp
, it
);
63 std::istringstream
pop("0 1 2 3 4 5 6 7 8 9");
66 // input iterator for population
67 auto it
= std::experimental::sample(istream_iterator
<int>{pop
}, {},
70 VERIFY( it
== samp
+ 5 );
73 auto it2
= std::unique(samp
, it
);
80 std::forward_list
<int> pop
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
81 std::stringstream samp
;
83 // forward iterator for population and output iterator for result
84 std::experimental::sample(pop
.begin(), pop
.end(),
85 ostream_iterator
<int>{samp
, " "},
88 // samp.rdbuf()->pubseekoff(0, std::ios::beg);
89 std::vector
<int> v(istream_iterator
<int>{samp
}, {});
90 VERIFY( v
.size() == 5 );
92 std::sort(v
.begin(), v
.end());
93 auto it
= std::unique(v
.begin(), v
.end());
94 VERIFY( it
== v
.end() );