]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/25_algorithms/partial_sort/constrained.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / partial_sort / 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 <vector>
25#include <testsuite_hooks.h>
26#include <testsuite_iterators.h>
27
28using __gnu_test::test_container;
29using __gnu_test::test_range;
30using __gnu_test::random_access_iterator_wrapper;
31
32namespace ranges = std::ranges;
33
34void
35test01()
36{
37 for (unsigned size = 0; size < 50; ++size)
38 {
39 std::vector<int> vref(size);
40 std::iota(vref.begin(), vref.end(), 0);
41 std::vector<int> v1(vref), v2(vref);
42 test_container<int, random_access_iterator_wrapper> c
10a32d47 43 = {v1.data(), v1.data() + size};
bc464641 44 test_range<int, random_access_iterator_wrapper> r
10a32d47 45 = {v2.data(), v2.data() + size};
bc464641
PP
46
47 std::ranlux48_base g1(size), g2(size + 1);
48 ranges::shuffle(c, g1);
49 ranges::shuffle(ranges::begin(r), ranges::end(r), g2);
50
51 for (unsigned middle = 0; middle < std::min(size, 10U); ++middle)
52 {
53 auto res1 = ranges::partial_sort(c.begin(), c.begin()+middle, c.end(),
54 {}, std::negate<>{});
55 VERIFY( res1 == c.end() );
56
57 auto res2 = ranges::partial_sort(r,
58 ranges::begin(r)+middle,
59 ranges::greater{});
60 VERIFY( res2 == ranges::end(r) );
61
62 VERIFY( ranges::equal(c.begin(), c.begin()+middle,
63 r.begin(), r.begin()+middle) );
64 VERIFY( ranges::equal(c.begin(), c.begin()+middle,
65 vref.rbegin(), vref.rbegin()+middle) );
66 }
67 }
68}
69
70constexpr bool
71test02()
72{
73 int x[] = { 5,4,1,3,2 };
74 const int y[] = { 1,2,3 };
75 ranges::partial_sort(x, x+3, x+5);
76 return ranges::equal(x, x+3, y, y+3);
77}
78
79int
80main()
81{
82 test01();
83 static_assert(test02());
84}