From 4f96052772c46ceba8ca074e4206646661462533 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 17 Mar 2011 17:39:59 +0000 Subject: david - added a hash table stress unit test --- src/unit/hash.unit | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'src/unit') diff --git a/src/unit/hash.unit b/src/unit/hash.unit index 64e70b6..d2217e7 100644 --- a/src/unit/hash.unit +++ b/src/unit/hash.unit @@ -10,6 +10,8 @@ #include "bu/hash.h" #include +#include +#include typedef Bu::Hash StrIntHash; typedef Bu::Hash StrStrHash; @@ -97,4 +99,59 @@ suite Hash StrIntHash h3 = h.clone(); unitTest( !h3.isCoreShared( h ) ); } + + test stress + { + srandom(time(NULL)); + + IntStrHash hTest; + setStepCount( 1000000 ); + + for( int i=0; i<1000000; ++i ) + { + char buf[40]; snprintf(buf,40,"num%d",i); + hTest[i] = buf; + incProgress(); + + /*printf("(%s) size: %u fill: %u cap:%u\n", + buf, + hTest.getSize(), + hTest.getFill(), + hTest.getCapacity() + );*/ + + if( ((double)random()/(double)RAND_MAX)<.3 ) + { + Bu::List lKeys = hTest.getKeys(); + int iIdx = (int)(((double)random()/(double)RAND_MAX) + *lKeys.getSize()); + Bu::List::const_iterator it = lKeys.begin(); + for( int k=0; it; ++it, ++k ) + { + if( k == iIdx ) + hTest.erase( *it ); + } + } + + if( hTest.getSize() > 100 ) + { + int iK = (int)(((double)random()/(double)RAND_MAX)*50)+10; + for( int j=0; j lKeys = hTest.getKeys(); + int iIdx = (int)(((double)random()/(double)RAND_MAX) + *lKeys.getSize()); + Bu::List::const_iterator it = lKeys.begin(); + for( int k=0; it; ++it, ++k ) + { + if( k == iIdx ) + { + hTest.erase( *it ); + break; + } + } + } + } + } + } } -- cgit v1.2.3