OSDN Git Service

2011-01-30 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / ext / profile / profiler_algos.cc
1 // { dg-require-profile-mode "" }
2
3 // -*- C++ -*-
4
5 // Unit tests for profile/impl/profile_algos.h.
6
7 // Copyright (C) 2010, 2011 Free Software Foundation, Inc.
8 //
9 // This file is part of the GNU ISO C++ Library.  This library is free
10 // software; you can redistribute it and/or modify it under the
11 // terms of the GNU General Public License as published by the
12 // Free Software Foundation; either version 3, or (at your option)
13 // any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License along
21 // with this library; see the file COPYING3.  If not see
22 // <http://www.gnu.org/licenses/>.
23
24 #include <vector>
25 #include <profile/impl/profiler.h>
26
27 using std::_GLIBCXX_STD_C::vector;
28
29 enum Failure
30 {
31   NO_FAILURES = 0x0,
32   INSERT_AFTER_N = 0x1,
33   INSERT_AT_HEAD = 0x2,
34   INSERT_AT_TAIL = 0x4,
35   INSERT_IN_THE_MIDDLE = 0x8,
36   TOP_N = 0x10,
37   FOR_EACH = 0x20,
38   REMOVE = 0x40
39 };
40   
41
42 static int
43 test_insert_top_n()
44 {
45   vector<int> v;
46
47   for (int i = 0; i < 10; i++)
48     v.push_back(10 - i);
49
50   // Inserting -5 should have no effect if size is limited to 10.
51   __gnu_profile::__insert_top_n(v, -5, 10);
52   for (int i = 0; i < 10; i++)
53     if (v[i] != 10 - i)
54       return INSERT_AFTER_N;
55
56   // Insert at head.
57   __gnu_profile::__insert_top_n(v, 11, 10);
58   for (int i = 0; i < 11; i++)
59     if (v[i] != 11 - i)
60       return INSERT_AT_HEAD;
61
62   // Insert at end.
63   __gnu_profile::__insert_top_n(v, 0, 100);
64   for (int i = 0; i < 12; i++)
65     if (v[i] != 11 - i)
66       return INSERT_AT_TAIL;
67
68   // Insert in the middle.
69   __gnu_profile::__insert_top_n(v, 6, 11);
70   for (int i = 0; i < 6; i++)
71     if (v[i] != 11 - i)
72       return INSERT_IN_THE_MIDDLE;
73   for (int i = 6; i < 13; i++)
74     if (v[i] != 12 - i)
75       return INSERT_IN_THE_MIDDLE;
76
77   return NO_FAILURES;
78 }
79
80 static int
81 test_top_n()
82 {
83   vector<int> v, out;
84
85   for (int i = 0; i < 100; i++)
86     {
87       v.push_back(100 + i);
88       v.push_back(100 - i);
89     }
90
91   __gnu_profile::__top_n(v, out, 10);
92
93   for (int i = 0; i < 10; i++)
94     if (out[i] != 199 - i)
95       return TOP_N;
96
97   return NO_FAILURES;
98 }
99
100 struct test_for_each_helper
101 {
102   static int sum;
103   void operator ()(int i) { 
104     sum += i;
105   }
106 };
107
108 int test_for_each_helper::sum = 0;
109
110 static int
111 test_for_each()
112 {
113   vector<int> v;
114   test_for_each_helper helper;
115   int checksum = 0;
116
117   for (int i = 0; i < 10; i++)
118     {
119       v.push_back(i);
120       checksum += i;
121     }
122
123   __gnu_profile::__for_each(v.begin(), v.end(), helper);
124
125   return helper.sum == checksum ? NO_FAILURES : FOR_EACH;
126 }
127
128 static int
129 test_remove()
130 {
131   vector<char> v;
132
133   for (int i = 0; i < 10; i++)
134     v.push_back(' ');
135   v.push_back('x');
136   for (int i = 0; i < 10; i++)
137     v.push_back(' ');
138   v.push_back('x');
139
140   return __gnu_profile::__remove(v.begin(), v.end(), ' ') == v.begin() + 2
141       ? NO_FAILURES : REMOVE;
142 }
143
144 int main()
145 {
146   return test_insert_top_n() | test_top_n() | test_for_each() | test_remove();
147 }