2002-11-21 Phil Edwards <pme@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / heap.cc
blobe78fae8477d7bf8e5d7accb7c4a979291b25b1b3
1 // Copyright (C) 2001 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 2, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without Pred 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 COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 // USA.
19 // 25.3.6 Heap operations [lib.alg.heap.operations]
21 #include <algorithm>
22 //#include <cmath>
23 #include <testsuite_hooks.h>
25 bool test = true;
27 const int A[] = {1, 11, 12, 3, 10, 6, 17, 4, 8, 2, 5, 13, 9, 15, 14, 16, 7};
28 const int B[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
29 const int C[] = {17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
30 const int N = sizeof(A) / sizeof(int);
32 // This functor has the equivalent functionality of std::geater<>,
33 // but there is no dependency on <functional> and it also tracks the
34 // number of invocations since creation.
35 class Gt
37 public:
38 static int count() { return itsCount; }
39 static void reset() { itsCount = 0; }
41 bool
42 operator()(const int& x, const int& y)
44 ++itsCount;
45 return x > y;
48 private:
49 static int itsCount;
52 int Gt::itsCount = 0;
54 // Exercise all of the heap functions for operator<. The
55 // intermediate results between push_heap and pop_heap and
56 // make_heap and sort_heap are not checked (they could be).
57 void
58 test01()
60 // sort array s1 using push_heap/pop_heap
61 int s1[N];
62 std::copy(A, A + N, s1);
63 VERIFY(std::equal(s1, s1 + N, A));
65 for (int i = 2; i <= N; ++i) {
66 std::push_heap(s1, s1 + i);
68 for (int i = N; i >= 2; --i) {
69 std::pop_heap(s1, s1 + i);
71 VERIFY(std::equal(s1, s1 + N, B));
73 // sort array s2 using make_heap/sort_heap
74 int s2[N];
75 std::copy(A, A + N, s2);
76 VERIFY(std::equal(s2, s2 + N, A));
78 std::make_heap(s2, s2 + N);
79 std::sort_heap(s2, s2 + N);
80 VERIFY(std::equal(s2, s2 + N, B));
83 // Perform same tests as above but with the comparison predicate
84 // versions, and add complexity constraint checks.
85 void
86 test02()
88 Gt gt;
89 // const int logN = static_cast<int>(std::log(static_cast<double>(N)) + 0.5);
90 const int logN = 3;
92 int s1[N];
93 std::copy(A, A + N, s1);
94 VERIFY(std::equal(s1, s1 + N, A));
96 for (int i = 2; i <= N; ++i) {
97 std::push_heap(s1, s1 + i, gt);
98 VERIFY(gt.count() <= logN);
99 gt.reset();
102 for (int i = N; i >= 2; --i) {
103 std::pop_heap(s1, s1 + i, gt);
104 VERIFY(gt.count() <= 2 * logN);
105 gt.reset();
108 VERIFY(std::equal(s1, s1 + N, C));
110 // sort array s2 using make_heap/sort_heap
111 int s2[N];
112 std::copy(A, A + N, s2);
113 VERIFY(std::equal(s2, s2 + N, A));
115 std::make_heap(s2, s2 + N, gt);
116 VERIFY(gt.count() <= 3 * N);
117 gt.reset();
119 std::sort_heap(s2, s2 + N, gt);
120 VERIFY(gt.count() <= N * logN);
122 VERIFY(std::equal(s2, s2 + N, C));
126 main(int argc, char* argv[])
128 test01();
129 test02();
131 return !test;