1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
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)
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 "" }
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 template<template<typename
, template<typename
> typename
> typename container
>
38 auto pred
= std::greater
{};
39 auto proj
= [] (int a
) { return -a
; };
40 for (int i
= 0; i
< 50; i
++)
42 std::iota(x
, x
+50, 1);
43 container
<int, random_access_iterator_wrapper
> rx(x
);
45 std::ranlux48_base
g(i
);
46 ranges::shuffle(rx
, g
);
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() );
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 );
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 );
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
);
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
);
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
) );
78 iter
= ranges::sort_heap(rx
, pred
, proj
);
79 VERIFY( iter
== rx
.end() );
80 VERIFY( ranges::is_sorted(rx
, pred
, proj
) );
88 int x
[] = {1,2,3,4,5};
93 ok
&= ranges::is_heap(x
);
94 ok
&= ranges::is_heap_until(x
) == x
+5;
96 ok
&= ranges::equal(x
, (int[]){1,2,3,4,7});
103 test01
<test_range
>();
104 test01
<test_container
>();
105 static_assert(test02());