| 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 <testsuite_hooks.h> |
| 24 |
#include <testsuite_iterators.h> |
| 25 |
|
| 26 |
using __gnu_test::test_container; |
| 27 |
using __gnu_test::test_range; |
| 28 |
using __gnu_test::random_access_iterator_wrapper; |
| 29 |
|
| 30 |
namespace ranges = std::ranges; |
| 31 |
|
| 32 |
template<template<typename, template<typename> typename> typename container> |
| 33 |
void |
| 34 |
test01() |
| 35 |
{ |
| 36 |
int x[50]; |
| 37 |
|
| 38 |
auto pred = std::greater{}; |
| 39 |
auto proj = [] (int a) { return -a; }; |
| 40 |
for (int i = 0; i < 50; i++) |
| 41 |
{ |
| 42 |
std::iota(x, x+50, 1); |
| 43 |
container<int, random_access_iterator_wrapper> rx(x); |
| 44 |
|
| 45 |
std::ranlux48_base g(i); |
| 46 |
ranges::shuffle(rx, g); |
| 47 |
|
| 48 |
auto iter = ranges::make_heap(rx, pred, proj); |
| 49 |
VERIFY( iter == rx.end() ); |
| 50 |
VERIFY( ranges::is_heap(rx, pred, proj) ); |
| 51 |
VERIFY( ranges::is_heap_until(rx, pred, proj) == rx.end() ); |
| 52 |
|
| 53 |
iter = ranges::pop_heap(rx, pred, proj); |
| 54 |
VERIFY( iter == rx.end() ); |
| 55 |
VERIFY( *(iter-1) == 50 ); |
| 56 |
VERIFY( ranges::is_heap_until(rx, pred, proj) == iter-1 ); |
| 57 |
|
| 58 |
iter = ranges::pop_heap(rx.begin(), iter-1, pred, proj); |
| 59 |
VERIFY( iter+1 == rx.end() ); |
| 60 |
VERIFY( *(iter-1) == 49 ); |
| 61 |
VERIFY( ranges::is_heap_until(rx, pred, proj) == iter-1 ); |
| 62 |
|
| 63 |
*(iter-1) = i; |
| 64 |
iter = ranges::push_heap(rx.begin(), iter, pred, proj); |
| 65 |
VERIFY( iter+1 == rx.end() ); |
| 66 |
VERIFY( ranges::is_heap_until(rx, pred, proj) == iter ); |
| 67 |
|
| 68 |
*iter = 2*i; |
| 69 |
iter = ranges::push_heap(rx.begin(), rx.end(), pred, proj); |
| 70 |
VERIFY( iter == rx.end() ); |
| 71 |
VERIFY( ranges::is_heap_until(rx, pred, proj) == iter ); |
| 72 |
|
| 73 |
*(rx.begin()+1) *= -1; |
| 74 |
VERIFY( !ranges::is_heap(rx, pred, proj) ); |
| 75 |
*(rx.begin()+1) *= -1; |
| 76 |
VERIFY( ranges::is_heap(rx, pred, proj) ); |
| 77 |
|
| 78 |
iter = ranges::sort_heap(rx, pred, proj); |
| 79 |
VERIFY( iter == rx.end() ); |
| 80 |
VERIFY( ranges::is_sorted(rx, pred, proj) ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
constexpr bool |
| 85 |
test02() |
| 86 |
{ |
| 87 |
bool ok = true; |
| 88 |
int x[] = {1,2,3,4,5}; |
| 89 |
ranges::make_heap(x); |
| 90 |
ranges::pop_heap(x); |
| 91 |
x[4] = 7; |
| 92 |
ranges::push_heap(x); |
| 93 |
ok &= ranges::is_heap(x); |
| 94 |
ok &= ranges::is_heap_until(x) == x+5; |
| 95 |
ranges::sort_heap(x); |
| 96 |
ok &= ranges::equal(x, (int[]){1,2,3,4,7}); |
| 97 |
return ok; |
| 98 |
} |
| 99 |
|
| 100 |
int |
| 101 |
main() |
| 102 |
{ |
| 103 |
test01<test_range>(); |
| 104 |
test01<test_container>(); |
| 105 |
static_assert(test02()); |
| 106 |
} |