]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/25_algorithms/nth_element/constrained.cc
libstdc++: Remove dg-options "-std=gnu++20" from <concepts> and <ranges> tests
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / nth_element / constrained.cc
CommitLineData
83ffe9cd 1// Copyright (C) 2020-2023 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
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
27using __gnu_test::test_container;
28using __gnu_test::test_range;
29using __gnu_test::random_access_iterator_wrapper;
30
31namespace ranges = std::ranges;
32
33void
34test01()
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
63constexpr bool
64test02()
65{
66 int x[] = {5,2,1,3,4};
67 ranges::nth_element(x, x+3);
68 return x[3] == 4;
69}
70
71int
72main()
73{
74 test01();
75 static_assert(test02());
76}