Files Classes Functions Hierarchy
#include <hashtabletest.h>
Static Public Member Functions | |
| static void | test01 () |
| Basic interactive looking up a dictionary test. | |
| static int | test02unit () |
| Unit test: Inserting and retrieving integers. | |
| static int | test03unit () |
| Unit test: hash table same size as data, inserting and retriving integers and timming. | |
| static int | test04unit () |
| Unit test: hashtable2 random integer insert and retrieving. | |
| static void | test05 () |
| Iterating through hash table code - prelude to hashtableiterator. | |
| static void | test06 () |
| hashtableiterator - iterating though a hashtable container. | |
| static void | test07 () |
| Interatively remove elements from a hash table. | |
| static void | test08 () |
| Write to a hash element, changing it. | |
Static Public Attributes | |
| static string | doc [] |
| Brief description of each test. | |
Definition at line 7 of file hashtabletest.h.
| void hashtabletest::test01 | ( | ) | [static] |
Basic interactive looking up a dictionary test.
Definition at line 33 of file hashtabletest.cpp.
References hashtable< T, HFN >::contains(), hashtable< T, HFN >::insert(), hashtable< T, HFN >::insertInverse(), print(), and SHOW.
Referenced by main().
00034 { 00035 cout << "Testing Hash Table for Strings" << endl; 00036 00037 string dict[] = { "abc", "nogo", "python" }; 00038 00039 cout << "The dictionary is: "; 00040 cout << print(dict,dict+3) << endl; 00041 00042 uintc n(sizeof(dict)/sizeof(string)); 00043 00044 vector< bucketlink<string> > v(n); 00045 for (uint i=0; i<n; ++i) 00046 v[i] = bucketlink<string>(&dict[i]); 00047 00048 uintc hashtablesize(70); 00049 stringhash h(32,8); 00050 hashtable<string,stringhash&> H(hashtablesize,h); 00051 00052 for (uint i=0; i<n; ++i) 00053 H.insert( & v[i] ); 00054 00055 bucketlink<string> * cp; 00056 00057 string k; 00058 for ( ; k!="exit"; ) 00059 { 00060 cout << "Look to see if string is in the hash table" << endl; 00061 cout << "Enter \"exit\" to terminate." << endl; 00062 cout << "Enter string k: "; 00063 cin >> k; 00064 00065 if (k=="exit") 00066 return; 00067 00068 cp = H.insertInverse(k); 00069 if (cp==0) 00070 cout << "not found" << endl; 00071 else 00072 cout << "found" << endl; 00073 cout << SHOW(H.contains(k)) << endl; 00074 } 00075 }
| int hashtabletest::test02unit | ( | ) | [static] |
Unit test: Inserting and retrieving integers.
Definition at line 78 of file hashtabletest.cpp.
References hashtable< T, HFN >::insert(), and hashtable< T, HFN >::insertInverse().
Referenced by main().
00079 { 00080 cout << "Testing Hash Table" << endl << endl; 00081 00082 cout << "Testing if all inserted integers in hash table " << endl; 00083 cout << " can be retrieved." << endl; 00084 cout << endl; 00085 00086 int p = 1003; 00087 int t; 00088 uint kmax = 7; 00089 00090 vector<int> v; 00091 vector< bucketlink<int> > bv; 00092 uint sz = 4000; 00093 00094 cout << "Inserting " << sz << " numbers into the hash table." << endl; 00095 00096 for (uint i=0; i<sz; ++i) 00097 { 00098 t = i; 00099 00100 for (uint k=0; k<kmax; ++k) 00101 { 00102 t *= i; 00103 t = t % p; 00104 } 00105 v.push_back(t); 00106 } 00107 00108 hashfunction01 h(8); 00109 hashtable<int,hashfunction01&> H(60,h); 00110 00111 for (uint i=0; i<sz; ++i) 00112 { 00113 bv.push_back( bucketlink<int>(&v[i]) ); 00114 H.insert( & bv[i] ); 00115 } 00116 00117 00118 cout << "Testing if the inserted numbers are in the hash table." << endl; 00119 00120 for (uint i=0; i<sz; ++i) 00121 { 00122 if (H.insertInverse( v[i] ) != 0) 00123 cout << v[i] << " "; 00124 assert( H.insertInverse( v[i] ) != 0 ); 00125 if (H.insertInverse(v[i])==0) 00126 return 1; 00127 } 00128 cout << endl; 00129 00130 cout << "success" << endl; 00131 00132 return 0; 00133 }
| int hashtabletest::test03unit | ( | ) | [static] |
Unit test: hash table same size as data, inserting and retriving integers and timming.
Definition at line 197 of file hashtabletest.cpp.
Referenced by main().
00198 { 00199 cout << "Testing Hash Table" << endl; 00200 cout << "same size of table as data" << endl; 00201 cout << endl; 00202 cout << "Testing Hash Table" << endl << endl; 00203 00204 cout << "Testing if all inserted integers in hash table " << endl; 00205 cout << " can be retrieved and timing." << endl; 00206 cout << endl; 00207 00208 uint u=1000; 00209 for (uint k=0; k<12; ++k) 00210 { 00211 if ( insertandretrievetest(u,u) == false) 00212 return 1; 00213 00214 u *= 2; 00215 } 00216 cout << "success" << endl; 00217 return 0; 00218 }
| int hashtabletest::test04unit | ( | ) | [static] |
Unit test: hashtable2 random integer insert and retrieving.
Definition at line 220 of file hashtabletest.cpp.
References hashtable< T, HFN >::contains(), hashtable2< T, HFN >::insert(), and SHOW.
Referenced by main().
00221 { 00222 00223 uintc vsz(10); 00224 vector<int> v(vsz); 00225 00226 for (uint i=0; i<vsz; ++i) 00227 v[i]=rand(); 00228 00229 cout << "Inserting " << vsz << " random integers." << endl; 00230 00231 hashfunction01 hfn(8); 00232 hashtable2<int,hashfunction01> h(60,hfn); 00233 h.insert(&v[0],&v[0]+vsz); 00234 00235 cout << "Testing if they are in the hashtable2 container." << endl; 00236 00237 for (uint i=0; i<vsz; ++i) 00238 { 00239 cout << SHOW(v[i]) << " "; 00240 assert( h.contains(v[i]) == true ); 00241 if (h.contains(v[i])!=true) 00242 return 1; 00243 } 00244 cout << endl; 00245 00246 cout << "success" << endl; 00247 return 0; 00248 }
| void hashtabletest::test05 | ( | ) | [static] |
Iterating through hash table code - prelude to hashtableiterator.
Definition at line 251 of file hashtabletest.cpp.
References hashtable< T, HFN >::bucketsize, hashtable< T, HFN >::insert(), and hashtable< T, HFN >::vb.
Referenced by main().
00252 { 00253 00254 typedef stringpair<int> spi; 00255 spi wi[] = 00256 { 00257 spi("cat",87), spi("sam",840), spi("troghe",92), spi("tx67",89), 00258 spi("zsh",11) 00259 }; 00260 uintc N=5; 00261 vector<spi> vi(wi,wi+5); 00262 typedef bucketlink<spi> bl; 00263 vector<bl> bv(N); 00264 00265 00266 typedef stringpairhash<int> sphi; 00267 sphi sph; 00268 hashtable<spi,sphi&> h1(N*2,sph); 00269 00270 for (uint i=0; i<N; ++i) 00271 { 00272 bv[i] = bl(&vi[i]); 00273 h1.insert(&bv[i]); 00274 } 00275 00276 for (uint i=0; i<h1.bucketsize; ++i) 00277 { 00278 if (h1.vb[i]==0) 00279 continue; 00280 00281 bl* x = h1.vb[i]; 00282 for ( ;x!=0; x=x->next ) 00283 cout << x->data->id << endl; 00284 } 00285 }
| void hashtabletest::test06 | ( | ) | [static] |
hashtableiterator - iterating though a hashtable container.
Definition at line 288 of file hashtabletest.cpp.
References hashtable< T, HFN >::insert(), and hashtableiterator< H >::reset().
Referenced by main().
00289 { 00290 00291 typedef stringpair<int> spi; 00292 spi wi[] = 00293 { 00294 spi("cat",87), spi("sam",840), spi("troghe",92), spi("tx67",89), 00295 spi("zsh",11) 00296 }; 00297 uintc N=5; 00298 vector<spi> vi(wi,wi+N); 00299 typedef bucketlink<spi> bl; 00300 vector<bl> bv(N); 00301 00302 typedef stringpairhash<int> sphi; 00303 sphi sph; 00304 hashtable<spi,sphi&> h1(N*2,sph); 00305 00306 for (uint i=0; i<N; ++i) 00307 { 00308 bv[i] = bl(&vi[i]); 00309 h1.insert(&bv[i]); 00310 } 00311 00312 hashtableiterator< hashtable<spi,sphi&> > hi(h1); 00313 00314 for (hi.reset(); !hi; ++hi) 00315 cout << hi()->data->id << endl; 00316 }
| void hashtabletest::test07 | ( | ) | [static] |
Interatively remove elements from a hash table.
Definition at line 319 of file hashtabletest.cpp.
References hashtable< T, HFN >::contains(), hashtable< T, HFN >::insert(), print(), hashtable< T, HFN >::remove(), hashtableiterator< H >::reset(), and SHOW.
Referenced by main().
00320 { 00321 cout << "Testing Hash Table for Strings" << endl; 00322 00323 string dict[] = 00324 { 00325 "abc", "nogo", "python", "moon", "zelda", 00326 "release", "light", "matrix" 00327 }; 00328 uintc N(sizeof(dict)/sizeof(string)); 00329 00330 cout << "The dictionary is: " << endl; 00331 cout << print(dict,dict+N) << endl; 00332 00333 vector< bucketlink<string> > vi(N); 00334 for (uint i=0; i<N; ++i) 00335 vi[i] = bucketlink<string>(&dict[i]); 00336 00337 uintc hashtablesize(70); 00338 stringhash h(32,8); 00339 hashtable<string,stringhash&> H(hashtablesize,h); 00340 00341 for (uint i=0; i<N; ++i) 00342 H.insert( & vi[i] ); 00343 00344 // bucketlink<string> * cp; 00345 00346 bool res; 00347 00348 string k; 00349 for ( ; k!="exit"; ) 00350 { 00351 cout << "Delete a string in the hash table" << endl; 00352 cout << "Enter \"exit\" to terminate." << endl; 00353 cout << "Enter string k: "; 00354 cin >> k; 00355 00356 if (k=="exit") 00357 break; 00358 00359 res = H.remove(k); 00360 cout << SHOW(res) << endl; 00361 cout << SHOW(H.contains(k)) << endl; 00362 } 00363 00364 cout << "The dictionary now is: " << endl; 00365 hashtableiterator< hashtable<string,stringhash&> > hi(H); 00366 for (hi.reset(); !hi; ++hi) 00367 cout << hi()->data->c_str() << " "; 00368 cout << endl; 00369 }
| void hashtabletest::test08 | ( | ) | [static] |
Write to a hash element, changing it.
Definition at line 372 of file hashtabletest.cpp.
References hashtableiterator< H >::reset().
Referenced by main().
00373 { 00374 typedef stringpair<int> spi; 00375 spi wi[] = 00376 { 00377 spi("cat",87), spi("sam",840), spi("troghe",92), spi("tx67",89), 00378 spi("zsh",11), spi("grey",111), spi("drow",192) 00379 }; 00380 uintc N=7; 00381 vector<spi> vi(wi,wi+N); 00382 typedef bucketlink<spi> bl; 00383 vector<bl> bv(N); 00384 00385 typedef stringpairhash<int> sphi; 00386 sphi sph; 00387 typedef hashtable<spi,sphi&> htable; 00388 htable h1(N*2,sph); 00389 00390 for (uint i=0; i<N; ++i) 00391 { 00392 bv[i] = bl(&vi[i]); 00393 h1.insert(&bv[i]); 00394 } 00395 00396 hashtableiterator< htable > hi(h1); 00397 00398 for (hi.reset(); !hi; ++hi) 00399 cout << hi()->data->id << ":" << hi()->data->data << " "; 00400 cout << endl; 00401 00402 cout << "Write over the cat element, setting its data to 0." << endl; 00403 00404 htable::blink x; 00405 x = h1.insertInverse( stringpair<int>("cat",0) ); 00406 if (x) 00407 { 00408 cout << x->data->id << ":" << x->data->data << endl; 00409 x->data->data = 0; 00410 } 00411 00412 for (hi.reset(); !hi; ++hi) 00413 cout << hi()->data->id << ":" << hi()->data->data << " "; 00414 cout << endl; 00415 }
string hashtabletest::doc [static] |
Initial value:
{
"",
"Basic interactive looking up a dictionary test.",
"Unit test: Inserting and retrieving integers.",
"Unit test: hash table same size as data, inserting and retriving integers and timing.",
"Unit test: hashtable2 random integer insert and retrieving.",
"Iterating through hash table code - prelude to hashtableiterator.",
"hashtableiterator - iterating though a hashtable container. ",
"Iteratively remove elements from a hash table.",
"Write to a hash element, changing it."
}
Definition at line 17 of file hashtabletest.h.
Referenced by main().
1.5.8