Merged revisions 195034,195219,195245,195357,195374,195428,195599,195673,195809 via...
[official-gcc.git] / main / libstdc++-v3 / testsuite / ext / profile / profiler_algos.cc
blobf8331e366f415794e87902e7a5a58329871d2814
1 // { dg-require-profile-mode "" }
3 // -*- C++ -*-
5 // Unit tests for profile/impl/profile_algos.h.
7 // Copyright (C) 2010-2013 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.
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.
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/>.
24 #include <vector>
25 #include <profile/impl/profiler.h>
27 using std::_GLIBCXX_STD_C::vector;
29 enum Failure
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
42 static int
43 test_insert_top_n()
45 vector<int> v;
47 for (int i = 0; i < 10; i++)
48 v.push_back(10 - i);
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;
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;
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;
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;
77 return NO_FAILURES;
80 static int
81 test_top_n()
83 vector<int> v, out;
85 for (int i = 0; i < 100; i++)
87 v.push_back(100 + i);
88 v.push_back(100 - i);
91 __gnu_profile::__top_n(v, out, 10);
93 for (int i = 0; i < 10; i++)
94 if (out[i] != 199 - i)
95 return TOP_N;
97 return NO_FAILURES;
100 struct test_for_each_helper
102 static int sum;
103 void operator ()(int i) {
104 sum += i;
108 int test_for_each_helper::sum = 0;
110 static int
111 test_for_each()
113 vector<int> v;
114 test_for_each_helper helper;
115 int checksum = 0;
117 for (int i = 0; i < 10; i++)
119 v.push_back(i);
120 checksum += i;
123 __gnu_profile::__for_each(v.begin(), v.end(), helper);
125 return helper.sum == checksum ? NO_FAILURES : FOR_EACH;
128 static int
129 test_remove()
131 vector<char> v;
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');
140 return __gnu_profile::__remove(v.begin(), v.end(), ' ') == v.begin() + 2
141 ? NO_FAILURES : REMOVE;
144 int main()
146 return test_insert_top_n() | test_top_n() | test_for_each() | test_remove();