]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/25_algorithms/nth_element/constrained.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / nth_element / constrained.cc
CommitLineData
a945c346 1// Copyright (C) 2020-2024 Free Software Foundation, Inc.
bc464641
PP
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
7dbb6913 18// { dg-do run { target c++20 } }
bc464641
PP
19// { dg-require-cstdint "" }
20
21#include <algorithm>
22#include <random>
23#include <testsuite_hooks.h>
24#include <testsuite_iterators.h>
25
26using __gnu_test::test_container;
27using __gnu_test::test_range;
28using __gnu_test::random_access_iterator_wrapper;
29
30namespace ranges = std::ranges;
31
32void
33test01()
34{
35 int x[50];
36 std::iota(x, x+50, 0);
37
38 auto pred = std::greater{};
39 auto proj = [] (int a) { return -a; };
40 for (int i = 0; i < 50; i++)
41 {
42 test_range<int, random_access_iterator_wrapper> rx(x);
43 std::ranlux48_base g(i);
44 ranges::shuffle(rx, g);
45
46 auto result = ranges::nth_element(rx, rx.begin()+i, pred, proj);
47 VERIFY( result == rx.end() );
48 VERIFY( x[i] == i );
49 for (int j = 0; j < i; j++)
50 for (int k = i; k < 50; k++)
51 VERIFY( !pred(proj(x[k]), proj(x[j])) );
52
53 result = ranges::nth_element(rx, rx.begin()+i, pred);
54 VERIFY( result == rx.end() );
55 VERIFY( x[i] == 49-i );
56 for (int j = 0; j < i; j++)
57 for (int k = i; k < 50; k++)
58 VERIFY( !pred(x[k], x[j]) );
59 }
60}
61
62constexpr bool
63test02()
64{
65 int x[] = {5,2,1,3,4};
66 ranges::nth_element(x, x+3);
67 return x[3] == 4;
68}
69
70int
71main()
72{
73 test01();
74 static_assert(test02());
75}