1 /*---------------------------------------------------------------------------* 2 * testhashmap.cpp * 3 * * 4 * Copyright 2007, 2008 Nuance Communciations, Inc. * 5 * * 6 * Licensed under the Apache License, Version 2.0 (the 'License'); * 7 * you may not use this file except in compliance with the License. * 8 * * 9 * You may obtain a copy of the License at * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, software * 13 * distributed under the License is distributed on an 'AS IS' BASIS, * 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 15 * See the License for the specific language governing permissions and * 16 * limitations under the License. * 17 * * 18 *---------------------------------------------------------------------------*/ 19 20 21 22 #include <string> 23 #include <fstream> 24 #include <iostream> 25 #include "../src/hashmap.h" 26 using namespace std; 27 28 29 #if 1 30 void test1(); 31 void test2(); 32 33 34 void main(int argc, char* argv[]) 35 { 36 test2(); 37 } 38 39 // (INT,INT) hash 40 void test1() 41 { 42 HashMap<int,int > myHash; 43 int value; 44 int i; 45 i=10; 46 myHash.setName("TestHash"); 47 myHash.insert(1, i); 48 myHash.getValue(1, &value); 49 std::cout << "Index 1 has value= " << value <<std::endl; 50 myHash.getIndex( 10, &i ); 51 std::cout << "value " << value << " has index " << i <<std::endl; 52 unsigned int j; 53 myHash.getNumericIndex(i, &j); 54 std::cout << "index " << i << " has numeric index " << j <<std::endl; 55 myHash.getNumericIndexByValue(value, &j); 56 std::cout << "value " << value << " has numeric index " << j <<std::endl; 57 58 myHash.print(); 59 myHash.remove(1); 60 myHash.print(); 61 } 62 63 64 // (INT,STRING) hash 65 void test2() 66 { 67 HashMap<int,string> myHash; 68 string value = "hello"; 69 int i; 70 i=10; 71 myHash.setName("TestHash"); 72 myHash.insert(1, value); 73 myHash.insert(2, "world"); 74 75 myHash.getValue(1, &value); 76 std::cout << "Index 1 has value= " << value <<std::endl; 77 myHash.getIndex( value, &i ); 78 std::cout << "value " << value << " has index " << i <<std::endl; 79 unsigned int j; 80 myHash.getNumericIndex(i, &j); 81 std::cout << "index " << i << " has numeric index " << j <<std::endl; 82 myHash.getNumericIndexByValue(value, &j); 83 std::cout << "value " << value << " has numeric index " << j <<std::endl; 84 85 myHash.print(); 86 myHash.getFirst(&i, &value); 87 std::cout << "First iterator values are " << i <<", " << value <<std::endl; 88 if (myHash.getNext(&i, &value)) { 89 std::cout << "Iterator values are " << i <<", " << value <<std::endl; 90 } 91 else { 92 std::cout << "No first index - map is empty" <<std::endl; 93 } 94 myHash.remove(1); 95 myHash.getFirst(&i, &value); 96 std::cout << "First iterator values are " << i <<", " << value <<std::endl; 97 if (myHash.getNext(&i, &value)) { 98 std::cout << "Iterator values are " << i <<", " << value <<std::endl; 99 } 100 else { 101 std::cout << "No next index - map is empty" <<std::endl; 102 } 103 104 105 myHash.print(); 106 } 107 108 109 110 111 112 113 114 115 116 117 #else 118 119 void findi(string s); 120 void finds(int i); 121 void insert(int i, const string &s); 122 void remove( int i ); 123 124 HashMap<int,string> myHash; 125 126 void main(int argc, char* argv[]) 127 { 128 string s; 129 s = "hello"; 130 insert(1,s); 131 insert(2,"world"); 132 133 finds(2); 134 finds(1); 135 finds(99); 136 findi("hello"); 137 findi("world"); 138 findi("xox"); 139 140 s = "bollocks"; 141 findi("hello"); 142 finds(1); 143 insert(3,s); 144 finds(3); 145 insert(3,"zzz"); 146 finds(3); 147 remove(3); 148 insert(3,"zzz"); 149 finds(3); 150 151 } 152 153 154 void findi(string s) 155 { 156 int i; 157 if ( myHash.getIndex(s, &i) ) { 158 cout << "'" << s << "' has index of " << i <<endl; 159 } 160 else { 161 cout << "'" << s << "' not found!" << endl; 162 } 163 } 164 165 void finds(int i) 166 { 167 string s; 168 if ( myHash.getValue(i, &s) ) { 169 cout << "'" << i << "' has value of " << s <<endl; 170 } 171 else { 172 cout << "'" << i << "' not found!" << endl; 173 } 174 } 175 176 void insert( int i, const string &s) 177 { 178 string ss; 179 if (!myHash.getValue(i, &ss) ) { 180 if ( myHash.insert(i, s) ) { 181 cout << "Inserted: " << i << "," << s <<endl; 182 } 183 } 184 else { 185 cout << "Failed to insert '" << i << "," << s <<"'" << endl; 186 } 187 } 188 189 void remove( int i ) 190 { 191 string ss; 192 if (myHash.getValue(i, &ss) ) { 193 if ( myHash.remove(i) ) { 194 cout << "Removed: " << i << endl; 195 } 196 } 197 else { 198 cout << "Failed to remove '" << i << "'" << endl; 199 } 200 } 201 202 203 #endif 204