1 /** @file exam_hashmap.cpp
3 * Regression tests for the exhashmap<> container. */
6 * GiNaC Copyright (C) 1999-2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 using namespace GiNaC;
28 unsigned exam_hashmap()
33 cout << "examining hash maps" << flush;
35 // Create empty container
36 exhashmap<unsigned> M1;
38 clog << "Newly constructed container has size() != 0" << endl;
42 clog << "Newly constructed container is not empty" << endl;
49 for (unsigned i = 0; i < N; ++i)
50 M1.insert(make_pair(i, i));
53 clog << "After " << N << " insertions, size() returns " << M1.size() << " instead of " << N << endl;
57 for (unsigned i = 0; i < N; ++i) {
59 clog << "Lookup of key " << i << " in M1 didn't return correct value" << endl;
66 clog << "After " << N << " lookups, size() returns " << M1.size() << " instead of " << N << endl;
72 // Test constructor from two iterators and operator==
73 exhashmap<unsigned> M2(M1.begin(), M1.end());
74 if (M2.size() != M1.size()) {
75 clog << "Constructor from two iterators: size of destination container (" << M2.size() << ") not equal to size of source (" << M1.size() << ")" << endl;
79 for (unsigned i = 0; i < N; ++i) {
81 clog << "Lookup of key " << i << " in M2 didn't return correct value" << endl;
88 clog << "Copied container not equal to source" << endl;
94 // Test assignment operator
95 exhashmap<unsigned> M3;
98 clog << "Assignment operator: size of assigned container not equal to size of original" << endl;
102 for (unsigned i = 0; i < N; ++i) {
104 clog << "Lookup of key " << i << " in M3 didn't return correct value" << endl;
110 cout << '.' << flush;
112 // Test insert(it, it)
113 exhashmap<unsigned> M4;
114 M4.insert(M1.begin(), M1.end());
116 if (M4.size() != M1.size()) {
117 clog << "insert(it, it): size of destination container not equal to size of source" << endl;
121 for (unsigned i = 0; i < N; ++i) {
123 clog << "Lookup of key " << i << " in M4 didn't return correct value" << endl;
129 cout << '.' << flush;
131 // Test insert()/find()
132 symbol x("x"), y("y");
133 exhashmap<unsigned> M5;
134 M5.insert(make_pair(x-2, 1));
135 M5.insert(make_pair(sin(x+y), 2));
136 M5.insert(make_pair(Pi, 3));
137 M5.insert(make_pair(0, 4));
138 M5.insert(make_pair(4*pow(x, y), 5));
139 if (M5.size() != 5) {
140 clog << "After 5 insertions, size() returns " << M5.size() << " instead of 5" << endl;
144 exhashmap<unsigned>::const_iterator cit = M5.find(sin(x+y));
145 if (cit == M5.end()) {
146 clog << "Lookup of sin(x+y) didn't find anything" << endl;
149 if (!cit->first.is_equal(sin(x+y))) {
150 clog << "Lookup of sin(x+y) returned an incorrect iterator" << endl;
153 if (cit->second != 2) {
154 clog << "Lookup of sin(x+y) returned wrong value" << endl;
158 cout << '.' << flush;
160 // Test re-inserting insert()
161 pair<exhashmap<unsigned>::iterator, bool> pit = M5.insert(make_pair(sin(x+y), 42));
163 clog << "Reinsertion of sin(x+y) inserted a new value" << endl;
166 if (!pit.first->first.is_equal(sin(x+y))) {
167 clog << "Reinsertion of sin(x+y) returned an incorrect iterator" << endl;
170 if (pit.first->second != 2) {
171 clog << "Reinsertion of sin(x+y) changed the value" << endl;
175 cout << '.' << flush;
178 unsigned v = M5[sin(x+y)];
179 if (M5.size() != 5) {
180 clog << "operator[] with an existing key changed the container size" << endl;
184 clog << "operator[] with an existing key returned the wrong value" << endl;
189 if (M5.size() != 6) {
190 clog << "operator[] with a new key didn't insert a new value" << endl;
194 clog << "operator[] with a new key returned the wrong value" << endl;
198 cout << '.' << flush;
201 exhashmap<unsigned>::iterator it = M5.find(y+1);
202 if (it == M5.end()) {
203 clog << "Key y+1 wasn't found" << endl;
206 if (!it->first.is_equal(y+1)) {
207 clog << "find() returned an incorrect iterator" << endl;
210 if (it->second != 0) {
211 clog << "find() returned an incorrect value" << endl;
216 if (M5.size() != 5) {
217 clog << "erase(it) didn't reduce the size of the container" << endl;
222 if (it != M5.end()) {
223 clog << "Key was still found after erase()" << endl;
227 exhashmap<unsigned>::size_type n = M5.erase(Pi);
229 clog << "erase(Pi) returned " << n << " instead of 1" << endl;
232 if (M5.size() != 4) {
233 clog << "erase(Pi) didn't reduce the size of the container" << endl;
239 clog << "erase(42) returned " << n << " instead of 0" << endl;
242 if (M5.size() != 4) {
243 clog << "erase(42) reduced the size of the container" << endl;
247 cout << '.' << flush;
250 exhashmap<unsigned> M6;
252 if (M6.size() != N) {
253 clog << "After swap, size() returns " << M6.size() << " instead of " << N << endl;
256 if (M1.size() != 0) {
257 clog << "After swap with empty container, size() returns " << M1.size() << " instead of 0" << endl;
261 cout << '.' << flush;
265 if (M2.size() != 0) {
266 clog << "Size of cleared container is " << M5.size() << " instead of 0" << endl;
270 cout << '.' << flush;
275 clog << "count(Pi) returns " << n << " instead of 0" << endl;
279 n = M5.count(4*pow(x, y));
281 clog << "count(4*x^y) returns " << n << " instead of 1" << endl;
284 cout << '.' << flush;
289 int main(int argc, char** argv)
291 return exam_hashmap();