Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

stresshash.cc

Go to the documentation of this file.
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * WvHashTable stress tester.  Adds a lot of elements to the table and 
00006  * looks them up, iterates through them, etc.  This requires lots of memory.
00007  *
00008  */
00009 
00010 #include "wvhashtable.h"
00011 #include "wvstring.h"
00012 #include <stdio.h>
00013 #include <malloc.h>
00014 #include <assert.h>
00015 
00016 struct Intstr
00017 {
00018     int i;
00019     WvString s;
00020     
00021     Intstr(int _i, const WvString &_s)
00022         { i = _i; s = _s; }
00023 };
00024 
00025 DeclareWvDict(Intstr, WvString, s);
00026 
00027 const unsigned size = 1000, elems = 10000;
00028 
00029 int main()
00030 {
00031     //free(malloc(1)); // enable electric fence
00032     
00033     IntstrDict d(size);
00034     unsigned count, total;
00035     
00036     for (count = 0; count < elems; count++)
00037     {
00038         if (elems > 100 && !(count % (elems/20)))
00039         {
00040             printf("\rAdding %d%%", count / (elems/100));
00041             fflush(stdout);
00042         }
00043         d.add(new Intstr(count, count), true);
00044     }
00045     
00046     printf("\rAdded: total %d (should be %d)\n", d.count(), elems);
00047     
00048     total = 0;
00049     for (count = 0; count < d.numslots; count++)
00050     {
00051         WvLink *head = &d.slots[count].head;
00052         if (!head->next)
00053             total++;
00054     }
00055     printf("%d of %d empty slots in the table.\n", total, d.numslots);
00056     printf("Avg chain length: %d (best %d)\n",
00057            elems / (d.numslots - total), elems / d.numslots);
00058     
00059 
00060     printf("Removing...");
00061     fflush(stdout);
00062     
00063     for (count = 0; count < elems; count += 5)
00064     {
00065         assert(d[count]);
00066         d.remove(d[count]);
00067     }
00068     
00069     printf("\rRemoved.  New count: %d (should be %d)\n",
00070            d.count(), elems - elems/5);
00071     
00072     IntstrDict::Iter i(d);
00073     total = d.count();
00074     count = 0;
00075     
00076     for (i.rewind(); i.next(); )
00077     {
00078         if (total > 20 && !(count % (total/20)))
00079         {
00080             printf("\rIterate %d%%", count / (total/100));
00081             fflush(stdout);
00082         }
00083 
00084         assert(i->s == WvString(i->i));
00085         count++;
00086     }
00087     
00088     printf("\rIterator okay.\n");
00089 
00090     
00091     for (count = 0; count < total; count++)
00092     {
00093         if (total > 100 && !(count % (total/20)))
00094         {
00095             printf("\rSlow Iterate %d%%", count / (total/100));
00096             fflush(stdout);
00097         }
00098 
00099         assert(!(count%5)
00100                || (d[count] && d[count]->s == WvString(d[count]->i)));
00101     }
00102     
00103     printf("\rOne-by-one iterator okay.\n");
00104     
00105     return 0;
00106 }

Generated on Sun Mar 16 01:01:10 2003 for WvStreams by doxygen1.3-rc3