]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/nth_element/constrained.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / nth_element / constrained.cc
1 // Copyright (C) 2020-2022 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_container;
28 using __gnu_test::test_range;
29 using __gnu_test::random_access_iterator_wrapper;
30
31 namespace ranges = std::ranges;
32
33 void
34 test01()
35 {
36 int x[50];
37 std::iota(x, x+50, 0);
38
39 auto pred = std::greater{};
40 auto proj = [] (int a) { return -a; };
41 for (int i = 0; i < 50; i++)
42 {
43 test_range<int, random_access_iterator_wrapper> rx(x);
44 std::ranlux48_base g(i);
45 ranges::shuffle(rx, g);
46
47 auto result = ranges::nth_element(rx, rx.begin()+i, pred, proj);
48 VERIFY( result == rx.end() );
49 VERIFY( x[i] == i );
50 for (int j = 0; j < i; j++)
51 for (int k = i; k < 50; k++)
52 VERIFY( !pred(proj(x[k]), proj(x[j])) );
53
54 result = ranges::nth_element(rx, rx.begin()+i, pred);
55 VERIFY( result == rx.end() );
56 VERIFY( x[i] == 49-i );
57 for (int j = 0; j < i; j++)
58 for (int k = i; k < 50; k++)
59 VERIFY( !pred(x[k], x[j]) );
60 }
61 }
62
63 constexpr bool
64 test02()
65 {
66 int x[] = {5,2,1,3,4};
67 ranges::nth_element(x, x+3);
68 return x[3] == 4;
69 }
70
71 int
72 main()
73 {
74 test01();
75 static_assert(test02());
76 }