1 // Copyright (C) 2020-2023 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-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20 // { dg-require-cstdint "" }
24 #include <testsuite_hooks.h>
25 #include <testsuite_iterators.h>
27 using __gnu_test::test_container
;
28 using __gnu_test::test_range
;
29 using __gnu_test::random_access_iterator_wrapper
;
31 namespace ranges
= std::ranges
;
33 template<template<typename
, template<typename
> typename
> typename container
>
39 auto pred
= std::greater
{};
40 auto proj
= [] (int a
) { return -a
; };
41 for (int i
= 0; i
< 50; i
++)
43 std::iota(x
, x
+50, 1);
44 container
<int, random_access_iterator_wrapper
> rx(x
);
46 std::ranlux48_base
g(i
);
47 ranges::shuffle(rx
, g
);
49 auto iter
= ranges::make_heap(rx
, pred
, proj
);
50 VERIFY( iter
== rx
.end() );
51 VERIFY( ranges::is_heap(rx
, pred
, proj
) );
52 VERIFY( ranges::is_heap_until(rx
, pred
, proj
) == rx
.end() );
54 iter
= ranges::pop_heap(rx
, pred
, proj
);
55 VERIFY( iter
== rx
.end() );
56 VERIFY( *(iter
-1) == 50 );
57 VERIFY( ranges::is_heap_until(rx
, pred
, proj
) == iter
-1 );
59 iter
= ranges::pop_heap(rx
.begin(), iter
-1, pred
, proj
);
60 VERIFY( iter
+1 == rx
.end() );
61 VERIFY( *(iter
-1) == 49 );
62 VERIFY( ranges::is_heap_until(rx
, pred
, proj
) == iter
-1 );
65 iter
= ranges::push_heap(rx
.begin(), iter
, pred
, proj
);
66 VERIFY( iter
+1 == rx
.end() );
67 VERIFY( ranges::is_heap_until(rx
, pred
, proj
) == iter
);
70 iter
= ranges::push_heap(rx
.begin(), rx
.end(), pred
, proj
);
71 VERIFY( iter
== rx
.end() );
72 VERIFY( ranges::is_heap_until(rx
, pred
, proj
) == iter
);
74 *(rx
.begin()+1) *= -1;
75 VERIFY( !ranges::is_heap(rx
, pred
, proj
) );
76 *(rx
.begin()+1) *= -1;
77 VERIFY( ranges::is_heap(rx
, pred
, proj
) );
79 iter
= ranges::sort_heap(rx
, pred
, proj
);
80 VERIFY( iter
== rx
.end() );
81 VERIFY( ranges::is_sorted(rx
, pred
, proj
) );
89 int x
[] = {1,2,3,4,5};
94 ok
&= ranges::is_heap(x
);
95 ok
&= ranges::is_heap_until(x
) == x
+5;
97 ok
&= ranges::equal(x
, (int[]){1,2,3,4,7});
104 test01
<test_range
>();
105 test01
<test_container
>();
106 static_assert(test02());