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