]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/algorithm/sample.cc
1 // Copyright (C) 2014-2018 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>
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
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
;
35 const int pop
[] = { 1, 2 };
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 );
47 const int pop
[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
50 auto it
= std::experimental::sample(pop
, std::end(pop
), samp
, 10, rng
);
51 VERIFY( it
== samp
+ 10 );
54 auto it2
= std::unique(samp
, it
);
61 const int pop
[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, };
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(),
69 VERIFY( it
== samp
+ 5 );
72 auto it2
= std::unique(samp
, it
);
79 const int pop
[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
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
);
88 VERIFY( std::distance(samp
, it
.ptr
) == 5 );
90 std::sort(samp
, it
.ptr
);
91 auto it2
= std::unique(samp
, it
.ptr
);
92 VERIFY( it2
== it
.ptr
);