1// Copyright (C) 2001-2015 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 23const int A[] = {1, 11, 12, 3, 10, 6, 17, 4, 8, 2, 5, 13, 9, 15, 14, 16, 7}; 24const int B[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; 25const int C[] = {17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; 26const 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. 31class Gt 32{ 33public: 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 44private: 45 static int _M_count; 46}; 47 48int 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). 53void 54test01() 55{ 56 bool test __attribute__((unused)) = true; 57 58 // sort array s1 using push_heap/pop_heap 59 int s1[N]; 60 std::copy(A, A + N, s1); 61 VERIFY(std::equal(s1, s1 + N, A)); 62 63 for (int i = 2; i <= N; ++i) 64 std::push_heap(s1, s1 + i); 65 66 for (int i = N; i >= 2; --i) 67 std::pop_heap(s1, s1 + i); 68 69 VERIFY(std::equal(s1, s1 + N, B)); 70 71 // sort array s2 using make_heap/sort_heap 72 int s2[N]; 73 std::copy(A, A + N, s2); 74 VERIFY(std::equal(s2, s2 + N, A)); 75 76 std::make_heap(s2, s2 + N); 77 std::sort_heap(s2, s2 + N); 78 VERIFY(std::equal(s2, s2 + N, B)); 79} 80 81// Perform same tests as above but with the comparison predicate 82// versions, and add complexity constraint checks. 83void 84test02() 85{ 86 bool test __attribute__((unused)) = true; 87 88 Gt gt; 89 90#ifndef _GLIBCXX_DEBUG 91 //const int logN = static_cast<int>(std::log(static_cast<double>(N)) + 0.5); 92 const int logN = 3; 93#endif 94 95 int s1[N]; 96 std::copy(A, A + N, s1); 97 VERIFY(std::equal(s1, s1 + N, A)); 98 99 for (int i = 2; i <= N; ++i) 100 { 101 std::push_heap(s1, s1 + i, gt); 102#ifndef _GLIBCXX_DEBUG 103 VERIFY(gt.count() <= logN); 104#endif 105 gt.reset(); 106 } 107 108 for (int i = N; i >= 2; --i) 109 { 110 std::pop_heap(s1, s1 + i, gt); 111#ifndef _GLIBCXX_DEBUG 112 VERIFY(gt.count() <= 2 * logN); 113#endif 114 gt.reset(); 115 } 116 117 VERIFY(std::equal(s1, s1 + N, C)); 118 119 // sort array s2 using make_heap/sort_heap 120 int s2[N]; 121 std::copy(A, A + N, s2); 122 VERIFY(std::equal(s2, s2 + N, A)); 123 124 std::make_heap(s2, s2 + N, gt); 125#ifndef _GLIBCXX_DEBUG 126 VERIFY(gt.count() <= 3 * N); 127#endif 128 gt.reset(); 129 130 std::sort_heap(s2, s2 + N, gt); 131#ifndef _GLIBCXX_DEBUG 132 VERIFY(gt.count() <= N * logN); 133#endif 134 135 VERIFY(std::equal(s2, s2 + N, C)); 136} 137 138int 139main() 140{ 141 test01(); 142 test02(); 143 return 0; 144} 145