]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/sample/constrained.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / sample / constrained.cc
1 // Copyright (C) 2020-2021 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-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20 // { dg-require-cstdint "" }
21
22 #include <algorithm>
23 #include <random>
24 #include <testsuite_hooks.h>
25 #include <testsuite_iterators.h>
26
27 using __gnu_test::test_range;
28 using __gnu_test::forward_iterator_wrapper;
29 using __gnu_test::input_iterator_wrapper;
30 using __gnu_test::output_iterator_wrapper;
31 using __gnu_test::random_access_iterator_wrapper;
32
33 namespace ranges = std::ranges;
34
35 std::mt19937 rng;
36
37 template<template<typename> typename in_wrapper,
38 template<typename> typename out_wrapper>
39 void
40 test01()
41 {
42 const int x[] = {1,2,3,4,5,6,7,8,9,10};
43 test_range<const int, in_wrapper> rx(x);
44 int y[10];
45 test_range<int, out_wrapper> ry(y);
46 auto out = ranges::sample(rx.begin(), rx.end(), ry.begin(), 20, rng);
47 VERIFY( out.ptr == y+10 );
48 VERIFY( ranges::equal(x, y) );
49
50 for (int i = 0; i < 100; i++)
51 {
52 int z[5] = {0};
53 test_range<int, out_wrapper> rz(z);
54 rx.bounds.first = x;
55 auto out = ranges::sample(rx, rz.begin(), 5, rng);
56 VERIFY( out.ptr == z+5 );
57 ranges::sort(z);
58 VERIFY( ranges::adjacent_find(z) == out.ptr );
59 VERIFY( ranges::includes(x, z) );
60 }
61 }
62
63 int
64 main()
65 {
66 test01<forward_iterator_wrapper, output_iterator_wrapper>();
67 test01<input_iterator_wrapper, random_access_iterator_wrapper>();
68 }