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