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