diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-11-05 22:41:51 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-11-05 22:41:51 +0000 |
commit | ec05778d5718a7912e506764d443a78d6a6179e3 (patch) | |
tree | 78a9a01532180030c095acefc45763f07c14edb8 /src/unit/hash.unit | |
parent | b20414ac1fe80a71a90601f4cd1767fa7014a9ba (diff) | |
download | libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.gz libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.bz2 libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.xz libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.zip |
Converted tabs to spaces with tabconv.
Diffstat (limited to '')
-rw-r--r-- | src/unit/hash.unit | 378 |
1 files changed, 189 insertions, 189 deletions
diff --git a/src/unit/hash.unit b/src/unit/hash.unit index 3abc6a5..e30c62b 100644 --- a/src/unit/hash.unit +++ b/src/unit/hash.unit | |||
@@ -19,193 +19,193 @@ typedef Bu::Hash<int, Bu::String> IntStrHash; | |||
19 | 19 | ||
20 | suite Hash | 20 | suite Hash |
21 | { | 21 | { |
22 | test probe1 | 22 | test probe1 |
23 | { | 23 | { |
24 | StrIntHash h; | 24 | StrIntHash h; |
25 | char buf[20]; | 25 | char buf[20]; |
26 | for(int i=1;i<10000;i++) | 26 | for(int i=1;i<10000;i++) |
27 | { | 27 | { |
28 | sprintf(buf,"%d",i); | 28 | sprintf(buf,"%d",i); |
29 | Bu::String sTmp(buf); | 29 | Bu::String sTmp(buf); |
30 | h[sTmp] = i; | 30 | h[sTmp] = i; |
31 | unitTest( h.has(sTmp) ); | 31 | unitTest( h.has(sTmp) ); |
32 | } | 32 | } |
33 | } | 33 | } |
34 | 34 | ||
35 | test insert1 | 35 | test insert1 |
36 | { | 36 | { |
37 | StrIntHash h; | 37 | StrIntHash h; |
38 | h["Hi"] = 42; | 38 | h["Hi"] = 42; |
39 | unitTest( h["Hi"] == 42 ); | 39 | unitTest( h["Hi"] == 42 ); |
40 | } | 40 | } |
41 | 41 | ||
42 | test insert2 | 42 | test insert2 |
43 | { | 43 | { |
44 | StrStrHash h; | 44 | StrStrHash h; |
45 | h["Hi"] = "Yo"; | 45 | h["Hi"] = "Yo"; |
46 | h["Bye"] = "Later"; | 46 | h["Bye"] = "Later"; |
47 | unitTest( h["Hi"].getValue() == "Yo" ); | 47 | unitTest( h["Hi"].getValue() == "Yo" ); |
48 | 48 | ||
49 | StrStrHash h2(h); | 49 | StrStrHash h2(h); |
50 | unitTest( h2["Hi"].getValue() == "Yo" ); | 50 | unitTest( h2["Hi"].getValue() == "Yo" ); |
51 | unitTest( h2["Bye"].getValue() == "Later" ); | 51 | unitTest( h2["Bye"].getValue() == "Later" ); |
52 | 52 | ||
53 | StrStrHash h3; | 53 | StrStrHash h3; |
54 | h3 = h; | 54 | h3 = h; |
55 | unitTest( h3["Hi"].getValue() == "Yo" ); | 55 | unitTest( h3["Hi"].getValue() == "Yo" ); |
56 | unitTest( h3["Bye"].getValue() == "Later" ); | 56 | unitTest( h3["Bye"].getValue() == "Later" ); |
57 | } | 57 | } |
58 | 58 | ||
59 | test insert3 | 59 | test insert3 |
60 | { | 60 | { |
61 | IntStrHash h; | 61 | IntStrHash h; |
62 | 62 | ||
63 | for( unsigned int i=1; i<50; i++ ) | 63 | for( unsigned int i=1; i<50; i++ ) |
64 | { | 64 | { |
65 | h[i] = "testing"; | 65 | h[i] = "testing"; |
66 | unitTest( h.getSize() == i ); | 66 | unitTest( h.getSize() == i ); |
67 | } | 67 | } |
68 | } | 68 | } |
69 | 69 | ||
70 | test erase1 | 70 | test erase1 |
71 | { | 71 | { |
72 | StrIntHash h; | 72 | StrIntHash h; |
73 | h.insert("Number 1", 1 ); | 73 | h.insert("Number 1", 1 ); |
74 | h.insert("Number 2", 2 ); | 74 | h.insert("Number 2", 2 ); |
75 | h.insert("Number 3", 3 ); | 75 | h.insert("Number 3", 3 ); |
76 | h.erase("Number 2"); | 76 | h.erase("Number 2"); |
77 | h.get("Number 3"); | 77 | h.get("Number 3"); |
78 | try { | 78 | try { |
79 | h.get("Number 2"); | 79 | h.get("Number 2"); |
80 | unitFailed("h.get(\"Number 2\") should have thrown an exception."); | 80 | unitFailed("h.get(\"Number 2\") should have thrown an exception."); |
81 | } catch( Bu::HashException &e ) { } | 81 | } catch( Bu::HashException &e ) { } |
82 | 82 | ||
83 | /* printf("\n"); | 83 | /* printf("\n"); |
84 | for( StrIntHash::iterator i = h.begin(); i != h.end(); i++ ) | 84 | for( StrIntHash::iterator i = h.begin(); i != h.end(); i++ ) |
85 | { | 85 | { |
86 | printf(" - \"%s\" = %d\n", i.getKey().getStr(), i.getValue() ); | 86 | printf(" - \"%s\" = %d\n", i.getKey().getStr(), i.getValue() ); |
87 | } */ | 87 | } */ |
88 | } | 88 | } |
89 | 89 | ||
90 | test copy | 90 | test copy |
91 | { | 91 | { |
92 | StrIntHash h; | 92 | StrIntHash h; |
93 | h.insert("hello", 55 ); | 93 | h.insert("hello", 55 ); |
94 | h.insert("goodbye", -1812 ); | 94 | h.insert("goodbye", -1812 ); |
95 | 95 | ||
96 | StrIntHash h2 = h; | 96 | StrIntHash h2 = h; |
97 | unitTest( h2.isCoreShared( h ) ); | 97 | unitTest( h2.isCoreShared( h ) ); |
98 | 98 | ||
99 | StrIntHash h3 = h.clone(); | 99 | StrIntHash h3 = h.clone(); |
100 | unitTest( !h3.isCoreShared( h ) ); | 100 | unitTest( !h3.isCoreShared( h ) ); |
101 | } | 101 | } |
102 | 102 | ||
103 | test reHashSpecial | 103 | test reHashSpecial |
104 | { | 104 | { |
105 | IntStrHash hTest; | 105 | IntStrHash hTest; |
106 | 106 | ||
107 | setStepCount( 1000000 ); | 107 | setStepCount( 1000000 ); |
108 | for( int i=0; i<1000000; ++i ) | 108 | for( int i=0; i<1000000; ++i ) |
109 | { | 109 | { |
110 | char buf[40]; snprintf(buf,40,"num%d",i); | 110 | char buf[40]; snprintf(buf,40,"num%d",i); |
111 | incProgress(); | 111 | incProgress(); |
112 | 112 | ||
113 | if( ((double)random()/(double)RAND_MAX)<.01 ) | 113 | if( ((double)random()/(double)RAND_MAX)<.01 ) |
114 | { | 114 | { |
115 | //printf("ERASE: %d (%s)\n",i%64,buf); | 115 | //printf("ERASE: %d (%s)\n",i%64,buf); |
116 | hTest.erase(i%64); | 116 | hTest.erase(i%64); |
117 | } | 117 | } |
118 | else | 118 | else |
119 | { | 119 | { |
120 | hTest[i%64]=buf; | 120 | hTest[i%64]=buf; |
121 | } | 121 | } |
122 | 122 | ||
123 | /*printf("(%d) size: %u fill: %u cap:%u\n", | 123 | /*printf("(%d) size: %u fill: %u cap:%u\n", |
124 | i, | 124 | i, |
125 | hTest.getSize(), | 125 | hTest.getSize(), |
126 | hTest.getFill(), | 126 | hTest.getFill(), |
127 | hTest.getCapacity() | 127 | hTest.getCapacity() |
128 | );*/ | 128 | );*/ |
129 | } | 129 | } |
130 | } | 130 | } |
131 | 131 | ||
132 | test stress | 132 | test stress |
133 | { | 133 | { |
134 | srandom(time(NULL)); | 134 | srandom(time(NULL)); |
135 | 135 | ||
136 | IntStrHash hTest; | 136 | IntStrHash hTest; |
137 | IntStrHash hReUse; | 137 | IntStrHash hReUse; |
138 | setStepCount( 100000 ); | 138 | setStepCount( 100000 ); |
139 | 139 | ||
140 | for( int i=0; i<100000; ++i ) | 140 | for( int i=0; i<100000; ++i ) |
141 | { | 141 | { |
142 | char buf[40]; snprintf(buf,40,"num%d",i); | 142 | char buf[40]; snprintf(buf,40,"num%d",i); |
143 | hTest[i] = buf; | 143 | hTest[i] = buf; |
144 | incProgress(); | 144 | incProgress(); |
145 | 145 | ||
146 | 146 | ||
147 | if( ((double)random()/(double)RAND_MAX)<.01 ) | 147 | if( ((double)random()/(double)RAND_MAX)<.01 ) |
148 | { | 148 | { |
149 | Bu::List<int> lKeys = hReUse.getKeys(); | 149 | Bu::List<int> lKeys = hReUse.getKeys(); |
150 | Bu::List<int>::const_iterator it = lKeys.begin(); | 150 | Bu::List<int>::const_iterator it = lKeys.begin(); |
151 | int iIdx = (int)(((double)random()/(double)RAND_MAX) | 151 | int iIdx = (int)(((double)random()/(double)RAND_MAX) |
152 | *lKeys.getSize()); | 152 | *lKeys.getSize()); |
153 | for( int l=0; it; ++it, ++l ) | 153 | for( int l=0; it; ++it, ++l ) |
154 | { | 154 | { |
155 | if( l==iIdx ) | 155 | if( l==iIdx ) |
156 | { | 156 | { |
157 | int iVal = *it; | 157 | int iVal = *it; |
158 | hReUse.erase( *it ); | 158 | hReUse.erase( *it ); |
159 | char buf[40]; snprintf(buf,40,"num%d",iVal); | 159 | char buf[40]; snprintf(buf,40,"num%d",iVal); |
160 | hTest[i] = buf; | 160 | hTest[i] = buf; |
161 | //printf("re-use: %s\n",buf); | 161 | //printf("re-use: %s\n",buf); |
162 | } | 162 | } |
163 | } | 163 | } |
164 | } | 164 | } |
165 | 165 | ||
166 | /*printf("(%s) size: %u fill: %u cap:%u\n", | 166 | /*printf("(%s) size: %u fill: %u cap:%u\n", |
167 | buf, | 167 | buf, |
168 | hTest.getSize(), | 168 | hTest.getSize(), |
169 | hTest.getFill(), | 169 | hTest.getFill(), |
170 | hTest.getCapacity() | 170 | hTest.getCapacity() |
171 | );*/ | 171 | );*/ |
172 | 172 | ||
173 | if( ((double)random()/(double)RAND_MAX)<.3 ) | 173 | if( ((double)random()/(double)RAND_MAX)<.3 ) |
174 | { | 174 | { |
175 | Bu::List<int> lKeys = hTest.getKeys(); | 175 | Bu::List<int> lKeys = hTest.getKeys(); |
176 | int iIdx = (int)(((double)random()/(double)RAND_MAX) | 176 | int iIdx = (int)(((double)random()/(double)RAND_MAX) |
177 | *lKeys.getSize()); | 177 | *lKeys.getSize()); |
178 | Bu::List<int>::const_iterator it = lKeys.begin(); | 178 | Bu::List<int>::const_iterator it = lKeys.begin(); |
179 | for( int k=0; it; ++it, ++k ) | 179 | for( int k=0; it; ++it, ++k ) |
180 | { | 180 | { |
181 | if( k == iIdx ) | 181 | if( k == iIdx ) |
182 | { | 182 | { |
183 | hReUse.insert(*it,"yo"); | 183 | hReUse.insert(*it,"yo"); |
184 | hTest.erase( *it ); | 184 | hTest.erase( *it ); |
185 | } | 185 | } |
186 | } | 186 | } |
187 | } | 187 | } |
188 | 188 | ||
189 | if( hTest.getSize() > 100 ) | 189 | if( hTest.getSize() > 100 ) |
190 | { | 190 | { |
191 | int iK = (int)(((double)random()/(double)RAND_MAX)*50)+10; | 191 | int iK = (int)(((double)random()/(double)RAND_MAX)*50)+10; |
192 | for( int j=0; j<iK; ++j ) | 192 | for( int j=0; j<iK; ++j ) |
193 | { | 193 | { |
194 | Bu::List<int> lKeys = hTest.getKeys(); | 194 | Bu::List<int> lKeys = hTest.getKeys(); |
195 | int iIdx = (int)(((double)random()/(double)RAND_MAX) | 195 | int iIdx = (int)(((double)random()/(double)RAND_MAX) |
196 | *lKeys.getSize()); | 196 | *lKeys.getSize()); |
197 | Bu::List<int>::const_iterator it = lKeys.begin(); | 197 | Bu::List<int>::const_iterator it = lKeys.begin(); |
198 | for( int k=0; it; ++it, ++k ) | 198 | for( int k=0; it; ++it, ++k ) |
199 | { | 199 | { |
200 | if( k == iIdx ) | 200 | if( k == iIdx ) |
201 | { | 201 | { |
202 | hReUse.insert(*it,"yo"); | 202 | hReUse.insert(*it,"yo"); |
203 | hTest.erase( *it ); | 203 | hTest.erase( *it ); |
204 | break; | 204 | break; |
205 | } | 205 | } |
206 | } | 206 | } |
207 | } | 207 | } |
208 | } | 208 | } |
209 | } | 209 | } |
210 | } | 210 | } |
211 | } | 211 | } |