OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libstdc++-v3/] [testsuite/] [25_algorithms/] [heap/] [heap.cc] - Blame information for rev 19

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jlechner
// Copyright (C) 2001, 2002, 2003, 2004, 2005 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.
8
 
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.
13
 
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17
// USA.
18
 
19
// 25.3.6 Heap operations [lib.alg.heap.operations]
20
 
21
#include <algorithm>
22
#include <testsuite_hooks.h>
23
 
24
bool test __attribute__((unused)) = true;
25
 
26
const int A[] = {1, 11, 12, 3, 10, 6, 17, 4, 8, 2, 5, 13, 9, 15, 14, 16, 7};
27
const int B[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
28
const int C[] = {17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
29
const int N = sizeof(A) / sizeof(int);
30
 
31
// This functor has the equivalent functionality of std::greater<>,
32
// but there is no dependency on <functional> and it also tracks the
33
// number of invocations since creation.
34
class Gt
35
{
36
public:
37
  static int count() { return itsCount; }
38
  static void reset() { itsCount = 0; }
39
 
40
  bool
41
  operator()(const int& x, const int& y)
42
  {
43
    ++itsCount;
44
    return x > y;
45
  }
46
 
47
private:
48
  static int itsCount;
49
};
50
 
51
int Gt::itsCount = 0;
52
 
53
// Exercise all of the heap functions for operator<.  The
54
// intermediate results between push_heap and pop_heap and
55
// make_heap and sort_heap are not checked (they could be).
56
void
57
test01()
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
  Gt gt;
88
//    const int logN = static_cast<int>(std::log(static_cast<double>(N)) + 0.5);
89
  const int logN = 3;
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
      VERIFY(gt.count() <= logN);
99
      gt.reset();
100
    }
101
 
102
  for (int i = N; i >= 2; --i)
103
    {
104
      std::pop_heap(s1, s1 + i, gt);
105
      VERIFY(gt.count() <= 2 * logN);
106
      gt.reset();
107
    }
108
 
109
  VERIFY(std::equal(s1, s1 + N, C));
110
 
111
  // sort array s2 using make_heap/sort_heap
112
  int s2[N];
113
  std::copy(A, A + N, s2);
114
  VERIFY(std::equal(s2, s2 + N, A));
115
 
116
  std::make_heap(s2, s2 + N, gt);
117
  VERIFY(gt.count() <= 3 * N);
118
  gt.reset();
119
 
120
  std::sort_heap(s2, s2 + N, gt);
121
  VERIFY(gt.count() <= N * logN);
122
 
123
  VERIFY(std::equal(s2, s2 + N, C));
124
}
125
 
126
int
127
main()
128
{
129
  test01();
130
  test02();
131
  return 0;
132
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.