1 /** @file exam_hashmap.cpp
3 * Regression tests for the exhashmap<> container. */
6 * GiNaC Copyright (C) 1999-2010 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
24 using namespace GiNaC;
29 unsigned exam_hashmap()
34 cout << "examining hash maps" << flush;
36 // Create empty container
37 exhashmap<unsigned> M1;
39 clog << "Newly constructed container has size() != 0" << endl;
43 clog << "Newly constructed container is not empty" << endl;
50 for (unsigned i = 0; i < N; ++i)
51 M1.insert(make_pair(i, i));
54 clog << "After " << N << " insertions, size() returns " << M1.size() << " instead of " << N << endl;
58 for (unsigned i = 0; i < N; ++i) {
60 clog << "Lookup of key " << i << " in M1 didn't return correct value" << endl;
67 clog << "After " << N << " lookups, size() returns " << M1.size() << " instead of " << N << endl;
73 // Test constructor from two iterators and operator==
74 exhashmap<unsigned> M2(M1.begin(), M1.end());
75 if (M2.size() != M1.size()) {
76 clog << "Constructor from two iterators: size of destination container (" << M2.size() << ") not equal to size of source (" << M1.size() << ")" << endl;
80 for (unsigned i = 0; i < N; ++i) {
82 clog << "Lookup of key " << i << " in M2 didn't return correct value" << endl;
89 clog << "Copied container not equal to source" << endl;
95 // Test assignment operator
96 exhashmap<unsigned> M3;
99 clog << "Assignment operator: size of assigned container not equal to size of original" << endl;
103 for (unsigned i = 0; i < N; ++i) {
105 clog << "Lookup of key " << i << " in M3 didn't return correct value" << endl;
111 cout << '.' << flush;
113 // Test insert(it, it)
114 exhashmap<unsigned> M4;
115 M4.insert(M1.begin(), M1.end());
117 if (M4.size() != M1.size()) {
118 clog << "insert(it, it): size of destination container not equal to size of source" << endl;
122 for (unsigned i = 0; i < N; ++i) {
124 clog << "Lookup of key " << i << " in M4 didn't return correct value" << endl;
130 cout << '.' << flush;
132 // Test insert()/find()
133 symbol x("x"), y("y");
134 exhashmap<unsigned> M5;
135 M5.insert(make_pair(x-2, 1));
136 M5.insert(make_pair(sin(x+y), 2));
137 M5.insert(make_pair(Pi, 3));
138 M5.insert(make_pair(0, 4));
139 M5.insert(make_pair(4*pow(x, y), 5));
140 if (M5.size() != 5) {
141 clog << "After 5 insertions, size() returns " << M5.size() << " instead of 5" << endl;
145 exhashmap<unsigned>::const_iterator cit = M5.find(sin(x+y));
146 if (cit == M5.end()) {
147 clog << "Lookup of sin(x+y) didn't find anything" << endl;
150 if (!cit->first.is_equal(sin(x+y))) {
151 clog << "Lookup of sin(x+y) returned an incorrect iterator" << endl;
154 if (cit->second != 2) {
155 clog << "Lookup of sin(x+y) returned wrong value" << endl;
159 cout << '.' << flush;
161 // Test re-inserting insert()
162 pair<exhashmap<unsigned>::iterator, bool> pit = M5.insert(make_pair(sin(x+y), 42));
164 clog << "Reinsertion of sin(x+y) inserted a new value" << endl;
167 if (!pit.first->first.is_equal(sin(x+y))) {
168 clog << "Reinsertion of sin(x+y) returned an incorrect iterator" << endl;
171 if (pit.first->second != 2) {
172 clog << "Reinsertion of sin(x+y) changed the value" << endl;
176 cout << '.' << flush;
179 unsigned v = M5[sin(x+y)];
180 if (M5.size() != 5) {
181 clog << "operator[] with an existing key changed the container size" << endl;
185 clog << "operator[] with an existing key returned the wrong value" << endl;
190 if (M5.size() != 6) {
191 clog << "operator[] with a new key didn't insert a new value" << endl;
195 clog << "operator[] with a new key returned the wrong value" << endl;
199 cout << '.' << flush;
202 exhashmap<unsigned>::iterator it = M5.find(y+1);
203 if (it == M5.end()) {
204 clog << "Key y+1 wasn't found" << endl;
207 if (!it->first.is_equal(y+1)) {
208 clog << "find() returned an incorrect iterator" << endl;
211 if (it->second != 0) {
212 clog << "find() returned an incorrect value" << endl;
217 if (M5.size() != 5) {
218 clog << "erase(it) didn't reduce the size of the container" << endl;
223 if (it != M5.end()) {
224 clog << "Key was still found after erase()" << endl;
228 exhashmap<unsigned>::size_type n = M5.erase(Pi);
230 clog << "erase(Pi) returned " << n << " instead of 1" << endl;
233 if (M5.size() != 4) {
234 clog << "erase(Pi) didn't reduce the size of the container" << endl;
240 clog << "erase(42) returned " << n << " instead of 0" << endl;
243 if (M5.size() != 4) {
244 clog << "erase(42) reduced the size of the container" << endl;
248 cout << '.' << flush;
251 exhashmap<unsigned> M6;
253 if (M6.size() != N) {
254 clog << "After swap, size() returns " << M6.size() << " instead of " << N << endl;
257 if (M1.size() != 0) {
258 clog << "After swap with empty container, size() returns " << M1.size() << " instead of 0" << endl;
262 cout << '.' << flush;
266 if (M2.size() != 0) {
267 clog << "Size of cleared container is " << M5.size() << " instead of 0" << endl;
271 cout << '.' << flush;
276 clog << "count(Pi) returns " << n << " instead of 0" << endl;
280 n = M5.count(4*pow(x, y));
282 clog << "count(4*x^y) returns " << n << " instead of 1" << endl;
285 cout << '.' << flush;
290 int main(int argc, char** argv)
292 return exam_hashmap();