Merge from trunk @ 138209
[official-gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / heap / 1.cc
blob4032ca82e326fc3779708fd96a25073ce090407e
1 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
2 // Free Software Foundation, Inc.
3 //
4 // This file is part of the GNU ISO C++ Library. This library is free
5 // software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the
7 // Free Software Foundation; either version 2, or (at your option)
8 // any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without Pred the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this library; see the file COPYING. If not, write to the Free
17 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 // USA.
20 // 25.3.6 Heap operations [lib.alg.heap.operations]
22 #include <algorithm>
23 #include <testsuite_hooks.h>
25 const int A[] = {1, 11, 12, 3, 10, 6, 17, 4, 8, 2, 5, 13, 9, 15, 14, 16, 7};
26 const int B[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
27 const int C[] = {17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
28 const int N = sizeof(A) / sizeof(int);
30 // This functor has the equivalent functionality of std::greater<>,
31 // but there is no dependency on <functional> and it also tracks the
32 // number of invocations since creation.
33 class Gt
35 public:
36 static int count() { return _M_count; }
37 static void reset() { _M_count = 0; }
39 bool
40 operator()(const int& x, const int& y)
42 ++_M_count;
43 return x > y;
46 private:
47 static int _M_count;
50 int Gt::_M_count = 0;
52 // Exercise all of the heap functions for operator<. The intermediate
53 // results between push_heap and pop_heap and make_heap and sort_heap
54 // are not checked (they could be).
55 void
56 test01()
58 bool test __attribute__((unused)) = true;
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 bool test __attribute__((unused)) = true;
90 Gt gt;
92 #ifndef _GLIBCXX_DEBUG
93 //const int logN = static_cast<int>(std::log(static_cast<double>(N)) + 0.5);
94 const int logN = 3;
95 #endif
97 int s1[N];
98 std::copy(A, A + N, s1);
99 VERIFY(std::equal(s1, s1 + N, A));
101 for (int i = 2; i <= N; ++i)
103 std::push_heap(s1, s1 + i, gt);
104 #ifndef _GLIBCXX_DEBUG
105 VERIFY(gt.count() <= logN);
106 #endif
107 gt.reset();
110 for (int i = N; i >= 2; --i)
112 std::pop_heap(s1, s1 + i, gt);
113 #ifndef _GLIBCXX_DEBUG
114 VERIFY(gt.count() <= 2 * logN);
115 #endif
116 gt.reset();
119 VERIFY(std::equal(s1, s1 + N, C));
121 // sort array s2 using make_heap/sort_heap
122 int s2[N];
123 std::copy(A, A + N, s2);
124 VERIFY(std::equal(s2, s2 + N, A));
126 std::make_heap(s2, s2 + N, gt);
127 #ifndef _GLIBCXX_DEBUG
128 VERIFY(gt.count() <= 3 * N);
129 #endif
130 gt.reset();
132 std::sort_heap(s2, s2 + N, gt);
133 #ifndef _GLIBCXX_DEBUG
134 VERIFY(gt.count() <= N * logN);
135 #endif
137 VERIFY(std::equal(s2, s2 + N, C));
141 main()
143 test01();
144 test02();
145 return 0;