1 // Copyright (C) 2001-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 // 25.3.6 Heap operations [lib.alg.heap.operations]
21 #include <testsuite_hooks.h>
23 const int A
[] = {1, 11, 12, 3, 10, 6, 17, 4, 8, 2, 5, 13, 9, 15, 14, 16, 7};
24 const int B
[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
25 const int C
[] = {17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
26 const int N
= sizeof(A
) / sizeof(int);
28 // This functor has the equivalent functionality of std::greater<>,
29 // but there is no dependency on <functional> and it also tracks the
30 // number of invocations since creation.
34 static int count() { return _M_count
; }
35 static void reset() { _M_count
= 0; }
38 operator()(const int& x
, const int& y
)
50 // Exercise all of the heap functions for operator<. The intermediate
51 // results between push_heap and pop_heap and make_heap and sort_heap
52 // are not checked (they could be).
56 // sort array s1 using push_heap/pop_heap
58 std::copy(A
, A
+ N
, s1
);
59 VERIFY(std::equal(s1
, s1
+ N
, A
));
61 for (int i
= 2; i
<= N
; ++i
)
62 std::push_heap(s1
, s1
+ i
);
64 for (int i
= N
; i
>= 2; --i
)
65 std::pop_heap(s1
, s1
+ i
);
67 VERIFY(std::equal(s1
, s1
+ N
, B
));
69 // sort array s2 using make_heap/sort_heap
71 std::copy(A
, A
+ N
, s2
);
72 VERIFY(std::equal(s2
, s2
+ N
, A
));
74 std::make_heap(s2
, s2
+ N
);
75 std::sort_heap(s2
, s2
+ N
);
76 VERIFY(std::equal(s2
, s2
+ N
, B
));
79 // Perform same tests as above but with the comparison predicate
80 // versions, and add complexity constraint checks.
86 #ifndef _GLIBCXX_DEBUG
87 //const int logN = static_cast<int>(std::log(static_cast<double>(N)) + 0.5);
92 std::copy(A
, A
+ N
, s1
);
93 VERIFY(std::equal(s1
, s1
+ N
, A
));
95 for (int i
= 2; i
<= N
; ++i
)
97 std::push_heap(s1
, s1
+ i
, gt
);
98 #ifndef _GLIBCXX_DEBUG
99 VERIFY(gt
.count() <= logN
);
104 for (int i
= N
; i
>= 2; --i
)
106 std::pop_heap(s1
, s1
+ i
, gt
);
107 #ifndef _GLIBCXX_DEBUG
108 VERIFY(gt
.count() <= 2 * logN
);
113 VERIFY(std::equal(s1
, s1
+ N
, C
));
115 // sort array s2 using make_heap/sort_heap
117 std::copy(A
, A
+ N
, s2
);
118 VERIFY(std::equal(s2
, s2
+ N
, A
));
120 std::make_heap(s2
, s2
+ N
, gt
);
121 #ifndef _GLIBCXX_DEBUG
122 VERIFY(gt
.count() <= 3 * N
);
126 std::sort_heap(s2
, s2
+ N
, gt
);
127 #ifndef _GLIBCXX_DEBUG
128 VERIFY(gt
.count() <= N
* logN
);
131 VERIFY(std::equal(s2
, s2
+ N
, C
));