proj home

Files   Classes   Functions   Hierarchy  

hashtabletest.cpp

Go to the documentation of this file.
00001 #include <cmath>
00002 #include <iostream>
00003 #include <cstdlib>
00004 #include <string>
00005 #include <vector>
00006 using namespace std;
00007 
00008 #include <aclock.h>
00009 #include <bucketlink.h>
00010 #include <hashtable.h>
00011 #include <hashtable2.h>
00012 #include <hashtabletest.h>
00013 #include <print.h>
00014 #include <stringhash.h>
00015 #include <stringpairhash.h>
00016 #include <typedefs.h>
00017 
00018 
00019 string hashtabletest::doc[] = 
00020 {
00021   "",
00022   "Basic interactive looking up a dictionary test.",
00023   "Unit test: Inserting and retrieving integers.",
00024   "Unit test: hash table same size as data, inserting and retriving integers and timing.",
00025   "Unit test: hashtable2 random integer insert and retrieving.",
00026   "Iterating through hash table code - prelude to hashtableiterator.",
00027   "hashtableiterator - iterating though a hashtable container. ",
00028   "Iteratively remove elements from a hash table.",
00029   "Write to a hash element, changing it."
00030 };
00031 
00032 
00033 void hashtabletest::test01()
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 }
00076 
00077 
00078 int hashtabletest::test02unit()
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 }
00134 
00135 
00136 boolc hashtabletest::insertandretrievetest
00137 (
00138   uintc n, 
00139   uintc hashtablesize
00140 )
00141 {
00142   int t;
00143   uint kmax = 7;
00144 
00145   vector<int> v(n);
00146   vector< bucketlink<int> > bv(n);
00147   uint sz = n;
00148 
00149   // Make some data to be hashed.
00150   // Put this data in vector v.
00151   for (uint i=0; i<sz; ++i)
00152   {
00153     t = i;
00154 
00155     for (uint k=0; k<kmax; ++k)
00156     {
00157       t *= i;
00158       t = t % n;
00159     }
00160     v[i] = t;
00161   }
00162 
00163   cout << sz;
00164 
00165   hashfunction01 h(8);
00166   hashtable<int,hashfunction01&> H(hashtablesize,h);
00167 
00168   aclock c;
00169   c.measure();
00170 
00171   for (uint i=0; i<sz; ++i)
00172   {
00173     bv[i] = bucketlink<int>(&v[i]);
00174     H.insert( & bv[i] );
00175   }
00176 
00177   c.measure();
00178   cout << "  " << c.diff_s();
00179 
00180 
00181   bucketlink<int> * cp;
00182 
00183   c.measure();
00184   for (uint i=0; i<sz; ++i)
00185   {
00186     cp = H.insertInverse( v[i] );
00187     assert( cp != 0 );
00188     if (cp==0)
00189       return false;
00190   }
00191   c.measure();
00192   cout << "  " << c.diff_s() << endl;
00193 
00194   return true;
00195 }
00196 
00197 int hashtabletest::test03unit()
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 }
00219 
00220 int hashtabletest::test04unit()
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 }
00249 
00250 
00251 void hashtabletest::test05()
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 }
00286 
00287 
00288 void hashtabletest::test06()
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 }
00317 
00318 
00319 void hashtabletest::test07()
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 }
00370 
00371 
00372 void hashtabletest::test08()
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 }
00416 
00417 
00418 

Generated on Fri Mar 4 00:49:26 2011 for Chelton Evans Source by  doxygen 1.5.8