Imported Upstream version 4.7.3
[platform/upstream/gcc48.git] / libstdc++-v3 / testsuite / 25_algorithms / heap / 1.cc
1 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009
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 3, or (at your option)
8 // any later version.
9
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License along
16 // with this library; see the file COPYING3.  If not see
17 // <http://www.gnu.org/licenses/>.
18
19 // 25.3.6 Heap operations [lib.alg.heap.operations]
20
21 #include <algorithm>
22 #include <testsuite_hooks.h>
23
24 const int A[] = {1, 11, 12, 3, 10, 6, 17, 4, 8, 2, 5, 13, 9, 15, 14, 16, 7};
25 const int B[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
26 const int C[] = {17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
27 const int N = sizeof(A) / sizeof(int);
28
29 // This functor has the equivalent functionality of std::greater<>,
30 // but there is no dependency on <functional> and it also tracks the
31 // number of invocations since creation.
32 class Gt
33 {
34 public:
35   static int count() { return _M_count; }
36   static void reset() { _M_count = 0; }
37   
38   bool
39   operator()(const int& x, const int& y)
40   {
41     ++_M_count;
42     return x > y; 
43   }
44
45 private:
46   static int _M_count;
47 };
48
49 int Gt::_M_count = 0;
50
51 // Exercise all of the heap functions for operator<.  The intermediate
52 // results between push_heap and pop_heap and make_heap and sort_heap
53 // are not checked (they could be).
54 void
55 test01()
56 {
57   bool test __attribute__((unused)) = true;
58
59   // sort array s1 using push_heap/pop_heap
60   int s1[N];
61   std::copy(A, A + N, s1);
62   VERIFY(std::equal(s1, s1 + N, A));
63   
64   for (int i = 2; i <= N; ++i)
65     std::push_heap(s1, s1 + i);
66   
67   for (int i = N; i >= 2; --i)
68     std::pop_heap(s1, s1 + i);
69   
70   VERIFY(std::equal(s1, s1 + N, B));
71
72   // sort array s2 using make_heap/sort_heap
73   int s2[N];
74   std::copy(A, A + N, s2);
75   VERIFY(std::equal(s2, s2 + N, A));
76   
77   std::make_heap(s2, s2 + N);
78   std::sort_heap(s2, s2 + N);
79   VERIFY(std::equal(s2, s2 + N, B));
80 }
81
82 // Perform same tests as above but with the comparison predicate
83 // versions, and add complexity constraint checks.
84 void
85 test02()
86 {
87   bool test __attribute__((unused)) = true;
88
89   Gt gt;
90
91 #ifndef _GLIBCXX_DEBUG
92   //const int logN = static_cast<int>(std::log(static_cast<double>(N)) + 0.5);
93   const int logN = 3;
94 #endif
95   
96   int s1[N];
97   std::copy(A, A + N, s1);
98   VERIFY(std::equal(s1, s1 + N, A));
99   
100   for (int i = 2; i <= N; ++i)
101     {
102       std::push_heap(s1, s1 + i, gt);
103 #ifndef _GLIBCXX_DEBUG
104       VERIFY(gt.count() <= logN);
105 #endif
106       gt.reset();
107     }
108
109   for (int i = N; i >= 2; --i)
110     {
111       std::pop_heap(s1, s1 + i, gt);
112 #ifndef _GLIBCXX_DEBUG
113       VERIFY(gt.count() <= 2 * logN);
114 #endif
115       gt.reset();
116     }
117
118   VERIFY(std::equal(s1, s1 + N, C));
119   
120   // sort array s2 using make_heap/sort_heap
121   int s2[N];
122   std::copy(A, A + N, s2);
123   VERIFY(std::equal(s2, s2 + N, A));
124   
125   std::make_heap(s2, s2 + N, gt);
126 #ifndef _GLIBCXX_DEBUG
127   VERIFY(gt.count() <= 3 * N);
128 #endif
129   gt.reset();
130
131   std::sort_heap(s2, s2 + N, gt);
132 #ifndef _GLIBCXX_DEBUG
133   VERIFY(gt.count() <= N * logN);
134 #endif
135   
136   VERIFY(std::equal(s2, s2 + N, C));
137 }
138
139 int
140 main()
141 {
142   test01();
143   test02();
144   return 0;
145 }