Add missing dg-require-cstdint directives to tests
[official-gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / sample / 1.cc
blob98bab7950c952e01aae5ec7c27e9763d42f84190
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.
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-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-cstdint "" }
22 #include <algorithm>
23 #include <random>
24 #include <testsuite_hooks.h>
25 #include <testsuite_iterators.h>
27 std::mt19937 rng;
29 using std::sample;
30 using __gnu_test::test_container;
31 using __gnu_test::input_iterator_wrapper;
32 using __gnu_test::output_iterator_wrapper;
33 using __gnu_test::forward_iterator_wrapper;
34 using __gnu_test::random_access_iterator_wrapper;
36 void
37 test01()
39 const int in[] = { 1, 2 };
40 test_container<const int, random_access_iterator_wrapper> pop(in);
41 const int sample_size = 10;
42 int samp[sample_size] = { };
44 // population smaller than desired sample size
45 auto it = sample(pop.begin(), pop.end(), samp, sample_size, rng);
46 VERIFY( it == samp + std::distance(pop.begin(), pop.end()) );
47 const auto sum = std::accumulate(pop.begin(), pop.end(), 0);
48 VERIFY( std::accumulate(samp, samp + sample_size, 0) == sum );
51 void
52 test02()
54 const int in[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
55 test_container<const int, random_access_iterator_wrapper> pop(in);
56 const int sample_size = 10;
57 int samp[sample_size] = { };
59 auto it = sample(pop.begin(), pop.end(), samp, sample_size, rng);
60 VERIFY( it == samp + sample_size );
62 // verify no duplicates
63 std::sort(samp, it);
64 auto it2 = std::unique(samp, it);
65 VERIFY( it2 == it );
68 void
69 test03()
71 const int in[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
72 test_container<const int, input_iterator_wrapper> pop(in);
73 const int sample_size = 5;
74 int samp[sample_size] = { };
76 // input iterator for population
77 auto it = sample(pop.begin(), pop.end(), samp, sample_size, rng);
78 VERIFY( it == samp + sample_size );
80 // verify no duplicates
81 std::sort(samp, it);
82 auto it2 = std::unique(samp, it);
83 VERIFY( it2 == it );
86 void
87 test04()
89 const int in[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
90 test_container<const int, forward_iterator_wrapper> pop(in);
91 const int sample_size = 5;
92 int out[sample_size];
93 test_container<int, output_iterator_wrapper> samp(out);
95 // forward iterator for population and output iterator for result
96 auto res = sample(pop.begin(), pop.end(), samp.begin(), sample_size, rng);
98 // verify no duplicates
99 std::sort(std::begin(out), std::end(out));
100 auto it = std::unique(std::begin(out), std::end(out));
101 VERIFY( it == std::end(out) );
105 main()
107 test01();
108 test02();
109 test03();
110 test04();