d: Merge dmd, druntime c7902293d7, phobos 03aeafd20
[gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / nth_element / constrained.cc
blob3189b225610e8fe3bc6fa466f133dc065b7434e3
1 // Copyright (C) 2020-2025 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-do run { target c++20 } }
19 // { dg-require-cstdint "" }
21 #include <algorithm>
22 #include <random>
23 #include <testsuite_hooks.h>
24 #include <testsuite_iterators.h>
26 using __gnu_test::test_container;
27 using __gnu_test::test_range;
28 using __gnu_test::random_access_iterator_wrapper;
30 namespace ranges = std::ranges;
32 void
33 test01()
35 int x[50];
36 std::iota(x, x+50, 0);
38 auto pred = std::greater{};
39 auto proj = [] (int a) { return -a; };
40 for (int i = 0; i < 50; i++)
42 test_range<int, random_access_iterator_wrapper> rx(x);
43 std::ranlux48_base g(i);
44 ranges::shuffle(rx, g);
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])) );
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]) );
62 constexpr bool
63 test02()
65 int x[] = {5,2,1,3,4};
66 ranges::nth_element(x, x+3);
67 return x[3] == 4;
70 int
71 main()
73 test01();
74 static_assert(test02());