1 /** @file exam_hashmap.cpp
3 * Regression tests for the exhashmap<> container. */
6 * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 unsigned exam_hashmap()
30 cout << "examining hash maps" << flush;
31 clog << "----------hash maps:" << endl;
33 // Create empty container
34 exhashmap<unsigned> M1;
36 clog << "Newly constructed container has size() != 0" << endl;
40 clog << "Newly constructed container is not empty" << endl;
47 for (unsigned i = 0; i < N; ++i)
48 M1.insert(make_pair(i, i));
51 clog << "After " << N << " insertions, size() returns " << M1.size() << " instead of " << N << endl;
55 for (unsigned i = 0; i < N; ++i) {
57 clog << "Lookup of key " << i << " in M1 didn't return correct value" << endl;
64 clog << "After " << N << " lookups, size() returns " << M1.size() << " instead of " << N << endl;
70 // Test constructor from two iterators and operator==
71 exhashmap<unsigned> M2(M1.begin(), M1.end());
72 if (M2.size() != M1.size()) {
73 clog << "Constructor from two iterators: size of destination container (" << M2.size() << ") not equal to size of source (" << M1.size() << ")" << endl;
77 for (unsigned i = 0; i < N; ++i) {
79 clog << "Lookup of key " << i << " in M2 didn't return correct value" << endl;
86 clog << "Copied container not equal to source" << endl;
92 // Test assignment operator
93 exhashmap<unsigned> M3;
96 clog << "Assignment operator: size of assigned container not equal to size of original" << endl;
100 for (unsigned i = 0; i < N; ++i) {
102 clog << "Lookup of key " << i << " in M3 didn't return correct value" << endl;
108 cout << '.' << flush;
110 // Test insert(it, it)
111 exhashmap<unsigned> M4;
112 M4.insert(M1.begin(), M1.end());
114 if (M4.size() != M1.size()) {
115 clog << "insert(it, it): size of destination container not equal to size of source" << endl;
119 for (unsigned i = 0; i < N; ++i) {
121 clog << "Lookup of key " << i << " in M4 didn't return correct value" << endl;
127 cout << '.' << flush;
129 // Test insert()/find()
130 symbol x("x"), y("y");
131 exhashmap<unsigned> M5;
132 M5.insert(make_pair(x-2, 1));
133 M5.insert(make_pair(sin(x+y), 2));
134 M5.insert(make_pair(Pi, 3));
135 M5.insert(make_pair(0, 4));
136 M5.insert(make_pair(4*pow(x, y), 5));
137 if (M5.size() != 5) {
138 clog << "After 5 insertions, size() returns " << M5.size() << " instead of 5" << endl;
142 exhashmap<unsigned>::const_iterator cit = M5.find(sin(x+y));
143 if (cit == M5.end()) {
144 clog << "Lookup of sin(x+y) didn't find anything" << endl;
147 if (!cit->first.is_equal(sin(x+y))) {
148 clog << "Lookup of sin(x+y) returned an incorrect iterator" << endl;
151 if (cit->second != 2) {
152 clog << "Lookup of sin(x+y) returned wrong value" << endl;
156 cout << '.' << flush;
158 // Test re-inserting insert()
159 pair<exhashmap<unsigned>::iterator, bool> pit = M5.insert(make_pair(sin(x+y), 42));
161 clog << "Reinsertion of sin(x+y) inserted a new value" << endl;
164 if (!pit.first->first.is_equal(sin(x+y))) {
165 clog << "Reinsertion of sin(x+y) returned an incorrect iterator" << endl;
168 if (pit.first->second != 2) {
169 clog << "Reinsertion of sin(x+y) changed the value" << endl;
173 cout << '.' << flush;
176 unsigned v = M5[sin(x+y)];
177 if (M5.size() != 5) {
178 clog << "operator[] with an existing key changed the container size" << endl;
182 clog << "operator[] with an existing key returned the wrong value" << endl;
187 if (M5.size() != 6) {
188 clog << "operator[] with a new key didn't insert a new value" << endl;
192 clog << "operator[] with a new key returned the wrong value" << endl;
196 cout << '.' << flush;
199 exhashmap<unsigned>::iterator it = M5.find(y+1);
200 if (it == M5.end()) {
201 clog << "Key y+1 wasn't found" << endl;
204 if (!it->first.is_equal(y+1)) {
205 clog << "find() returned an incorrect iterator" << endl;
208 if (it->second != 0) {
209 clog << "find() returned an incorrect value" << endl;
214 if (M5.size() != 5) {
215 clog << "erase(it) didn't reduce the size of the container" << endl;
220 if (it != M5.end()) {
221 clog << "Key was still found after erase()" << endl;
225 exhashmap<unsigned>::size_type n = M5.erase(Pi);
227 clog << "erase(Pi) returned " << n << " instead of 1" << endl;
230 if (M5.size() != 4) {
231 clog << "erase(Pi) didn't reduce the size of the container" << endl;
237 clog << "erase(42) returned " << n << " instead of 0" << endl;
240 if (M5.size() != 4) {
241 clog << "erase(42) reduced the size of the container" << endl;
245 cout << '.' << flush;
248 exhashmap<unsigned> M6;
250 if (M6.size() != N) {
251 clog << "After swap, size() returns " << M6.size() << " instead of " << N << endl;
254 if (M1.size() != 0) {
255 clog << "After swap with empty container, size() returns " << M1.size() << " instead of 0" << endl;
259 cout << '.' << flush;
263 if (M2.size() != 0) {
264 clog << "Size of cleared container is " << M5.size() << " instead of 0" << endl;
268 cout << '.' << flush;
273 clog << "count(Pi) returns " << n << " instead of 0" << endl;
277 n = M5.count(4*pow(x, y));
279 clog << "count(4*x^y) returns " << n << " instead of 1" << endl;
283 cout << '.' << flush;
286 cout << " passed " << endl;
287 clog << "(no output)" << endl;
289 cout << " failed " << endl;