| 1 |
// Copyright (C) 2001-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 |
// 25.3.6 Heap operations [lib.alg.heap.operations] |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
|
| 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); |
| 27 |
|
| 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. |
| 31 |
class Gt |
| 32 |
{ |
| 33 |
public: |
| 34 |
static int count() { return _M_count; } |
| 35 |
static void reset() { _M_count = 0; } |
| 36 |
|
| 37 |
bool |
| 38 |
operator()(const int& x, const int& y) |
| 39 |
{ |
| 40 |
++_M_count; |
| 41 |
return x > y; |
| 42 |
} |
| 43 |
|
| 44 |
private: |
| 45 |
static int _M_count; |
| 46 |
}; |
| 47 |
|
| 48 |
int Gt::_M_count = 0; |
| 49 |
|
| 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). |
| 53 |
void |
| 54 |
test01() |
| 55 |
{ |
| 56 |
// sort array s1 using push_heap/pop_heap |
| 57 |
int s1[N]; |
| 58 |
std::copy(A, A + N, s1); |
| 59 |
VERIFY(std::equal(s1, s1 + N, A)); |
| 60 |
|
| 61 |
for (int i = 2; i <= N; ++i) |
| 62 |
std::push_heap(s1, s1 + i); |
| 63 |
|
| 64 |
for (int i = N; i >= 2; --i) |
| 65 |
std::pop_heap(s1, s1 + i); |
| 66 |
|
| 67 |
VERIFY(std::equal(s1, s1 + N, B)); |
| 68 |
|
| 69 |
// sort array s2 using make_heap/sort_heap |
| 70 |
int s2[N]; |
| 71 |
std::copy(A, A + N, s2); |
| 72 |
VERIFY(std::equal(s2, s2 + N, A)); |
| 73 |
|
| 74 |
std::make_heap(s2, s2 + N); |
| 75 |
std::sort_heap(s2, s2 + N); |
| 76 |
VERIFY(std::equal(s2, s2 + N, B)); |
| 77 |
} |
| 78 |
|
| 79 |
// Perform same tests as above but with the comparison predicate |
| 80 |
// versions, and add complexity constraint checks. |
| 81 |
void |
| 82 |
test02() |
| 83 |
{ |
| 84 |
Gt gt; |
| 85 |
|
| 86 |
#ifndef _GLIBCXX_DEBUG |
| 87 |
//const int logN = static_cast<int>(std::log(static_cast<double>(N)) + 0.5); |
| 88 |
const int logN = 3; |
| 89 |
#endif |
| 90 |
|
| 91 |
int s1[N]; |
| 92 |
std::copy(A, A + N, s1); |
| 93 |
VERIFY(std::equal(s1, s1 + N, A)); |
| 94 |
|
| 95 |
for (int i = 2; i <= N; ++i) |
| 96 |
{ |
| 97 |
std::push_heap(s1, s1 + i, gt); |
| 98 |
#ifndef _GLIBCXX_DEBUG |
| 99 |
VERIFY(gt.count() <= logN); |
| 100 |
#endif |
| 101 |
gt.reset(); |
| 102 |
} |
| 103 |
|
| 104 |
for (int i = N; i >= 2; --i) |
| 105 |
{ |
| 106 |
std::pop_heap(s1, s1 + i, gt); |
| 107 |
#ifndef _GLIBCXX_DEBUG |
| 108 |
VERIFY(gt.count() <= 2 * logN); |
| 109 |
#endif |
| 110 |
gt.reset(); |
| 111 |
} |
| 112 |
|
| 113 |
VERIFY(std::equal(s1, s1 + N, C)); |
| 114 |
|
| 115 |
// sort array s2 using make_heap/sort_heap |
| 116 |
int s2[N]; |
| 117 |
std::copy(A, A + N, s2); |
| 118 |
VERIFY(std::equal(s2, s2 + N, A)); |
| 119 |
|
| 120 |
std::make_heap(s2, s2 + N, gt); |
| 121 |
#ifndef _GLIBCXX_DEBUG |
| 122 |
VERIFY(gt.count() <= 3 * N); |
| 123 |
#endif |
| 124 |
gt.reset(); |
| 125 |
|
| 126 |
std::sort_heap(s2, s2 + N, gt); |
| 127 |
#ifndef _GLIBCXX_DEBUG |
| 128 |
VERIFY(gt.count() <= N * logN); |
| 129 |
#endif |
| 130 |
|
| 131 |
VERIFY(std::equal(s2, s2 + N, C)); |
| 132 |
} |
| 133 |
|
| 134 |
int |
| 135 |
main() |
| 136 |
{ |
| 137 |
test01(); |
| 138 |
test02(); |
| 139 |
return 0; |
| 140 |
} |