summaryrefslogtreecommitdiff
path: root/src/unit/hash.unit
diff options
context:
space:
mode:
authorDavid <david@xagasoft.com>2011-03-17 17:39:59 +0000
committerDavid <david@xagasoft.com>2011-03-17 17:39:59 +0000
commit4f96052772c46ceba8ca074e4206646661462533 (patch)
tree4ca788616876b68a742ba6c60dae6c33bee019db /src/unit/hash.unit
parent616ec97dec2f057a1072c29ea78df08f66fbbdd4 (diff)
downloadlibbu++-4f96052772c46ceba8ca074e4206646661462533.tar.gz
libbu++-4f96052772c46ceba8ca074e4206646661462533.tar.bz2
libbu++-4f96052772c46ceba8ca074e4206646661462533.tar.xz
libbu++-4f96052772c46ceba8ca074e4206646661462533.zip
david - added a hash table stress unit test
Diffstat (limited to 'src/unit/hash.unit')
-rw-r--r--src/unit/hash.unit57
1 files changed, 57 insertions, 0 deletions
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 @@
10#include "bu/hash.h" 10#include "bu/hash.h"
11 11
12#include <stdio.h> 12#include <stdio.h>
13#include <stdlib.h>
14#include <time.h>
13 15
14typedef Bu::Hash<Bu::String, int> StrIntHash; 16typedef Bu::Hash<Bu::String, int> StrIntHash;
15typedef Bu::Hash<Bu::String, Bu::String> StrStrHash; 17typedef Bu::Hash<Bu::String, Bu::String> StrStrHash;
@@ -97,4 +99,59 @@ suite Hash
97 StrIntHash h3 = h.clone(); 99 StrIntHash h3 = h.clone();
98 unitTest( !h3.isCoreShared( h ) ); 100 unitTest( !h3.isCoreShared( h ) );
99 } 101 }
102
103 test stress
104 {
105 srandom(time(NULL));
106
107 IntStrHash hTest;
108 setStepCount( 1000000 );
109
110 for( int i=0; i<1000000; ++i )
111 {
112 char buf[40]; snprintf(buf,40,"num%d",i);
113 hTest[i] = buf;
114 incProgress();
115
116 /*printf("(%s) size: %u fill: %u cap:%u\n",
117 buf,
118 hTest.getSize(),
119 hTest.getFill(),
120 hTest.getCapacity()
121 );*/
122
123 if( ((double)random()/(double)RAND_MAX)<.3 )
124 {
125 Bu::List<int> lKeys = hTest.getKeys();
126 int iIdx = (int)(((double)random()/(double)RAND_MAX)
127 *lKeys.getSize());
128 Bu::List<int>::const_iterator it = lKeys.begin();
129 for( int k=0; it; ++it, ++k )
130 {
131 if( k == iIdx )
132 hTest.erase( *it );
133 }
134 }
135
136 if( hTest.getSize() > 100 )
137 {
138 int iK = (int)(((double)random()/(double)RAND_MAX)*50)+10;
139 for( int j=0; j<iK; ++j )
140 {
141 Bu::List<int> lKeys = hTest.getKeys();
142 int iIdx = (int)(((double)random()/(double)RAND_MAX)
143 *lKeys.getSize());
144 Bu::List<int>::const_iterator it = lKeys.begin();
145 for( int k=0; it; ++it, ++k )
146 {
147 if( k == iIdx )
148 {
149 hTest.erase( *it );
150 break;
151 }
152 }
153 }
154 }
155 }
156 }
100} 157}