blob: 13974e63e2becce0f12b97e28b03bae60c7cec81 [file] [log] [blame]
tanjent@gmail.comf67ce942011-03-14 09:11:18 +00001#include "Platform.h"
tanjent@gmail.com2aa29c32011-03-19 08:53:53 +00002#include "Hashes.h"
tanjent@gmail.comad4b3632010-11-05 01:20:58 +00003#include "KeysetTest.h"
4#include "SpeedTest.h"
5#include "AvalancheTest.h"
6#include "DifferentialTest.h"
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +00007
tanjent@gmail.comf67ce942011-03-14 09:11:18 +00008#include <stdio.h>
tanjent@gmail.comad4b3632010-11-05 01:20:58 +00009#include <time.h>
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000010
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000011//-----------------------------------------------------------------------------
12// Configuration. TODO - move these to command-line flags
13
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000014bool g_testAll = false;
15
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000016bool g_testSanity = false;
17bool g_testSpeed = false;
18bool g_testDiff = false;
19bool g_testAvalanche = false;
20bool g_testCyclic = false;
21bool g_testSparse = false;
22bool g_testPermutation = false;
23bool g_testWindow = false;
24bool g_testText = false;
25bool g_testZeroes = false;
26bool g_testSeed = false;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000027
tanjent@gmail.comf67ce942011-03-14 09:11:18 +000028//-----------------------------------------------------------------------------
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000029// This is the list of all hashes that SMHasher can test.
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000030
31struct HashInfo
32{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000033 pfHash hash;
34 int hashbits;
35 uint32_t verification;
36 const char * name;
37 const char * desc;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000038};
39
40HashInfo g_hashes[] =
41{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000042 { DoNothingHash, 32, 0x00000000, "donothing32", "Do-Nothing function (only valid for measuring call overhead)" },
43 { DoNothingHash, 64, 0x00000000, "donothing64", "Do-Nothing function (only valid for measuring call overhead)" },
44 { DoNothingHash, 128, 0x00000000, "donothing128", "Do-Nothing function (only valid for measuring call overhead)" },
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000045
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000046 { crc32, 32, 0x5C7DDD1F, "crc32", "CRC-32" },
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000047
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000048 { md5_32, 32, 0xC10C356B, "md5_32a", "MD5, first 32 bits of result" },
49 { sha1_32a, 32, 0xF9376EA7, "sha1_32a", "SHA1, first 32 bits of result" },
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000050
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000051 { FNV, 32, 0x2B377407, "FNV", "Fowler-Noll-Vo hash, 32-bit" },
52 { lookup3_test, 32, 0xDEC6FD2F, "lookup3", "Bob Jenkins' lookup3" },
53 { SuperFastHash, 32, 0x980ACD1D, "superfast", "Paul Hsieh's SuperFastHash" },
54 { MurmurOAAT, 32, 0x5F424541, "MurmurOAAT", "Murmur one-at-a-time" },
55
56 // MurmurHash2
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000057
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000058 { MurmurHash2_test, 32, 0xA6D95DE6, "Murmur2", "MurmurHash2 for x86, 32-bit" },
59 { MurmurHash2A_test, 32, 0xB79DC030, "Murmur2A", "MurmurHash2A for x86, 32-bit" },
60 { MurmurHash64A_test, 64, 0xDBD7FF4B, "Murmur2B", "MurmurHash2 for x64, 64-bit" },
61 { MurmurHash64B_test, 64, 0x3B861F71, "Murmur2C", "MurmurHash2 for x86, 64-bit" },
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000062
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000063 // MurmurHash3
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000064
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000065 { MurmurHash3_x86_32, 32, 0x3B75AFFD, "Murmur3A", "MurmurHash3 for x86, 32-bit" },
66 { MurmurHash3_x86_128, 128, 0x78C7F0DB, "Murmur3C", "MurmurHash3 for x86, 128-bit" },
67 { MurmurHash3_x64_128, 128, 0x54667393, "Murmur3F", "MurmurHash3 for x64, 128-bit" },
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000068
69};
70
71HashInfo * findHash ( const char * name )
72{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000073 for(int i = 0; i < sizeof(g_hashes) / sizeof(HashInfo); i++)
74 {
75 if(_stricmp(name,g_hashes[i].name) == 0) return &g_hashes[i];
76 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000077
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000078 return NULL;
79}
80
81//-----------------------------------------------------------------------------
82// Self-test on startup - verify that all installed hashes work correctly.
83
84void SelfTest ( void )
85{
86 bool pass = true;
87
88 for(int i = 0; i < sizeof(g_hashes) / sizeof(HashInfo); i++)
89 {
90 HashInfo * info = & g_hashes[i];
91
92 pass &= VerificationTest(info->hash,info->hashbits,info->verification,false);
93 }
94
95 if(!pass)
96 {
97 printf("Self-test FAILED!\n");
98
99 for(int i = 0; i < sizeof(g_hashes) / sizeof(HashInfo); i++)
100 {
101 HashInfo * info = & g_hashes[i];
102
103 pass &= VerificationTest(info->hash,info->hashbits,info->verification,true);
104 }
105
106 exit(1);
107 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000108}
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000109
110//----------------------------------------------------------------------------
111
112template < typename hashtype >
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000113void test ( hashfunc<hashtype> hash, HashInfo * info )
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000114{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000115 const int hashbits = sizeof(hashtype) * 8;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000116
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000117 printf("-------------------------------------------------------------------------------\n");
118 printf("--- Testing %s\n\n",info->name);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000119
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000120 //-----------------------------------------------------------------------------
121 // Sanity tests
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000122
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000123 if(g_testSanity || g_testAll)
124 {
125 printf("[[[ Sanity Tests ]]]\n\n");
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000126
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000127 VerificationTest(hash,hashbits,info->verification,true);
128 SanityTest(hash,hashbits);
129 AppendedZeroesTest(hash,hashbits);
130 printf("\n");
131 }
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000132
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000133 //-----------------------------------------------------------------------------
134 // Speed tests
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000135
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000136 if(g_testSpeed || g_testAll)
137 {
138 printf("[[[ Speed Tests ]]]\n\n");
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000139
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000140 BulkSpeedTest(hash);
141 printf("\n");
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000142
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000143 for(int i = 1; i < 32; i++)
144 {
145 double cycles;
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000146
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000147 TinySpeedTest(hash,sizeof(hashtype),i,true,cycles);
148 }
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000149
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000150 printf("\n");
151 }
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000152
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000153 //-----------------------------------------------------------------------------
154 // Differential tests
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000155
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000156 if(g_testDiff || g_testAll)
157 {
158 printf("[[[ Differential Tests ]]]\n\n");
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000159
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000160 bool result = true;
161 bool dumpCollisions = false;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000162
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000163 result &= DiffTest< Blob<64>, hashtype >(hash,5,1000,dumpCollisions);
164 result &= DiffTest< Blob<128>, hashtype >(hash,4,1000,dumpCollisions);
165 result &= DiffTest< Blob<256>, hashtype >(hash,3,1000,dumpCollisions);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000166
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000167 if(!result) printf("*********FAIL*********\n");
168 printf("\n");
169 }
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000170
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000171 //-----------------------------------------------------------------------------
172 // Avalanche tests
173
174 if(g_testAvalanche || g_testAll)
175 {
176 printf("[[[ Avalanche Tests ]]]\n\n");
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000177
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000178 bool result = true;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000179
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000180 result &= AvalancheTest< Blob< 32>, hashtype > (hash,300000);
181 result &= AvalancheTest< Blob< 40>, hashtype > (hash,300000);
182 result &= AvalancheTest< Blob< 48>, hashtype > (hash,300000);
183 result &= AvalancheTest< Blob< 56>, hashtype > (hash,300000);
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000184
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000185 result &= AvalancheTest< Blob< 64>, hashtype > (hash,300000);
186 result &= AvalancheTest< Blob< 72>, hashtype > (hash,300000);
187 result &= AvalancheTest< Blob< 80>, hashtype > (hash,300000);
188 result &= AvalancheTest< Blob< 88>, hashtype > (hash,300000);
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000189
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000190 result &= AvalancheTest< Blob< 96>, hashtype > (hash,300000);
191 result &= AvalancheTest< Blob<104>, hashtype > (hash,300000);
192 result &= AvalancheTest< Blob<112>, hashtype > (hash,300000);
193 result &= AvalancheTest< Blob<120>, hashtype > (hash,300000);
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000194
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000195 result &= AvalancheTest< Blob<128>, hashtype > (hash,300000);
196 result &= AvalancheTest< Blob<136>, hashtype > (hash,300000);
197 result &= AvalancheTest< Blob<144>, hashtype > (hash,300000);
198 result &= AvalancheTest< Blob<152>, hashtype > (hash,300000);
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000199
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000200 if(!result) printf("*********FAIL*********\n");
201 printf("\n");
202 }
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000203
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000204 //-----------------------------------------------------------------------------
205 // Keyset 'Cyclic'
tanjent@gmail.com2aa29c32011-03-19 08:53:53 +0000206
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000207 if(g_testCyclic || g_testAll)
208 {
209 printf("[[[ Keyset 'Cyclic' Tests ]]]\n\n");
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000210
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000211 bool result = true;
212 bool drawDiagram = false;
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000213
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000214 result &= CyclicKeyTest<hashtype>(hash,sizeof(hashtype)+0,8,10000000,drawDiagram);
215 result &= CyclicKeyTest<hashtype>(hash,sizeof(hashtype)+1,8,10000000,drawDiagram);
216 result &= CyclicKeyTest<hashtype>(hash,sizeof(hashtype)+2,8,10000000,drawDiagram);
217 result &= CyclicKeyTest<hashtype>(hash,sizeof(hashtype)+3,8,10000000,drawDiagram);
218 result &= CyclicKeyTest<hashtype>(hash,sizeof(hashtype)+4,8,10000000,drawDiagram);
219
220 if(!result) printf("*********FAIL*********\n");
221 printf("\n");
222 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000223
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000224 //-----------------------------------------------------------------------------
225 // Keyset 'Sparse'
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000226
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000227 if(g_testSparse || g_testAll)
228 {
229 printf("[[[ Keyset 'Sparse' Tests ]]]\n\n");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000230
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000231 bool result = true;
232 bool drawDiagram = false;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000233
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000234 result &= SparseKeyTest< 32,hashtype>(hash,6,true,true,true,drawDiagram);
235 result &= SparseKeyTest< 40,hashtype>(hash,6,true,true,true,drawDiagram);
236 result &= SparseKeyTest< 48,hashtype>(hash,5,true,true,true,drawDiagram);
237 result &= SparseKeyTest< 56,hashtype>(hash,5,true,true,true,drawDiagram);
238 result &= SparseKeyTest< 64,hashtype>(hash,5,true,true,true,drawDiagram);
239 result &= SparseKeyTest< 96,hashtype>(hash,4,true,true,true,drawDiagram);
240 result &= SparseKeyTest< 256,hashtype>(hash,3,true,true,true,drawDiagram);
241 result &= SparseKeyTest<2048,hashtype>(hash,2,true,true,true,drawDiagram);
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000242
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000243 if(!result) printf("*********FAIL*********\n");
244 printf("\n");
245 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000246
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000247 //-----------------------------------------------------------------------------
248 // Keyset 'Permutation'
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000249
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000250 if(g_testPermutation || g_testAll)
251 {
252 {
253 // This one breaks lookup3, surprisingly
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000254
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000255 printf("[[[ Keyset 'Combination Lowbits' Tests ]]]\n\n");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000256
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000257 bool result = true;
258 bool drawDiagram = false;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000259
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000260 uint32_t blocks[] =
261 {
262 0x00000000,
263
264 0x00000001, 0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00000006, 0x00000007,
265 };
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000266
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000267 result &= CombinationKeyTest<hashtype>(hash,8,blocks,sizeof(blocks) / sizeof(uint32_t),true,true,drawDiagram);
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000268
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000269 if(!result) printf("*********FAIL*********\n");
270 printf("\n");
271 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000272
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000273 {
274 printf("[[[ Keyset 'Combination Highbits' Tests ]]]\n\n");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000275
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000276 bool result = true;
277 bool drawDiagram = false;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000278
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000279 uint32_t blocks[] =
280 {
281 0x00000000,
282
283 0x20000000, 0x40000000, 0x60000000, 0x80000000, 0xA0000000, 0xC0000000, 0xE0000000
284 };
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000285
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000286 result &= CombinationKeyTest<hashtype>(hash,8,blocks,sizeof(blocks) / sizeof(uint32_t),true,true,drawDiagram);
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000287
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000288 if(!result) printf("*********FAIL*********\n");
289 printf("\n");
290 }
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000291
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000292 {
293 printf("[[[ Keyset 'Combination 0x8000000' Tests ]]]\n\n");
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000294
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000295 bool result = true;
296 bool drawDiagram = false;
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000297
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000298 uint32_t blocks[] =
299 {
300 0x00000000,
301
302 0x80000000,
303 };
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000304
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000305 result &= CombinationKeyTest<hashtype>(hash,20,blocks,sizeof(blocks) / sizeof(uint32_t),true,true,drawDiagram);
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000306
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000307 if(!result) printf("*********FAIL*********\n");
308 printf("\n");
309 }
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000310
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000311 {
312 printf("[[[ Keyset 'Combination 0x0000001' Tests ]]]\n\n");
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000313
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000314 bool result = true;
315 bool drawDiagram = false;
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000316
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000317 uint32_t blocks[] =
318 {
319 0x00000000,
320
321 0x00000001,
322 };
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000323
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000324 result &= CombinationKeyTest<hashtype>(hash,20,blocks,sizeof(blocks) / sizeof(uint32_t),true,true,drawDiagram);
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000325
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000326 if(!result) printf("*********FAIL*********\n");
327 printf("\n");
328 }
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000329
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000330 {
331 printf("[[[ Keyset 'Combination Hi-Lo' Tests ]]]\n\n");
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000332
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000333 bool result = true;
334 bool drawDiagram = false;
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000335
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000336 uint32_t blocks[] =
337 {
338 0x00000000,
339
340 0x00000001, 0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00000006, 0x00000007,
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000341
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000342 0x80000000, 0x40000000, 0xC0000000, 0x20000000, 0xA0000000, 0x60000000, 0xE0000000
343 };
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000344
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000345 result &= CombinationKeyTest<hashtype>(hash,6,blocks,sizeof(blocks) / sizeof(uint32_t),true,true,drawDiagram);
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000346
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000347 if(!result) printf("*********FAIL*********\n");
348 printf("\n");
349 }
350 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000351
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000352 //-----------------------------------------------------------------------------
353 // Keyset 'Window'
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000354
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000355 // Skip distribution test for these - they're too easy to distribute well,
356 // and it generates a _lot_ of testing
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000357
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000358 if(g_testWindow || g_testAll)
359 {
360 printf("[[[ Keyset 'Window' Tests ]]]\n\n");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000361
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000362 bool result = true;
363 bool testCollision = true;
364 bool testDistribution = false;
365 bool drawDiagram = false;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000366
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000367 result &= WindowedKeyTest< Blob<hashbits*2>, hashtype > ( hash, 20, testCollision, testDistribution, drawDiagram );
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000368
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000369 if(!result) printf("*********FAIL*********\n");
370 printf("\n");
371 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000372
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000373 //-----------------------------------------------------------------------------
374 // Keyset 'Text'
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000375
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000376 if(g_testText || g_testAll)
377 {
378 printf("[[[ Keyset 'Text' Tests ]]]\n\n");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000379
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000380 bool result = true;
381 bool drawDiagram = false;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000382
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000383 const char * alnum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000384
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000385 result &= TextKeyTest( hash, "Foo", alnum,4, "Bar", drawDiagram );
386 result &= TextKeyTest( hash, "FooBar", alnum,4, "", drawDiagram );
387 result &= TextKeyTest( hash, "", alnum,4, "FooBar", drawDiagram );
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000388
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000389 if(!result) printf("*********FAIL*********\n");
390 printf("\n");
391 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000392
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000393 //-----------------------------------------------------------------------------
394 // Keyset 'Zeroes'
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000395
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000396 if(g_testZeroes || g_testAll)
397 {
398 printf("[[[ Keyset 'Zeroes' Tests ]]]\n\n");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000399
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000400 bool result = true;
401 bool drawDiagram = false;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000402
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000403 result &= ZeroKeyTest<hashtype>( hash, drawDiagram );
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000404
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000405 if(!result) printf("*********FAIL*********\n");
406 printf("\n");
407 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000408
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000409 //-----------------------------------------------------------------------------
410 // Keyset 'Seed'
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000411
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000412 if(g_testSeed || g_testAll)
413 {
414 printf("[[[ Keyset 'Seed' Tests ]]]\n\n");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000415
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000416 bool result = true;
417 bool drawDiagram = false;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000418
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000419 result &= SeedTest<hashtype>( hash, 1000000, drawDiagram );
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000420
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000421 if(!result) printf("*********FAIL*********\n");
422 printf("\n");
423 }
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000424}
425
426//-----------------------------------------------------------------------------
427
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000428void testHash ( const char * name )
429{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000430 HashInfo * pInfo = findHash(name);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000431
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000432 if(pInfo == NULL)
433 {
434 printf("Invalid hash '%s' specified\n",name);
435 return;
436 }
437 else
438 {
439 if(pInfo->hashbits == 32)
440 {
441 test<uint32_t>( pInfo->hash, pInfo );
442 }
443 else if(pInfo->hashbits == 64)
444 {
445 test<uint64_t>( pInfo->hash, pInfo );
446 }
447 else if(pInfo->hashbits == 128)
448 {
449 test<uint128_t>( pInfo->hash, pInfo );
450 }
451 else if(pInfo->hashbits == 256)
452 {
453 test<uint256_t>( pInfo->hash, pInfo );
454 }
455 else
456 {
457 printf("Invalid hash bit width %d for hash '%s'",pInfo->hashbits,pInfo->name);
458 }
459 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000460}
461//-----------------------------------------------------------------------------
462
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000463int main ( int argc, char ** argv )
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000464{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000465 SetAffinity(2);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000466
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000467 SelfTest();
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000468
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000469 int timeBegin = clock();
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000470
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000471 g_testAll = true;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000472
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000473 //g_testSanity = true;
474 //g_testSpeed = true;
475 //g_testAvalanche = true;
476 //g_testCyclic = true;
477 //g_testDiff = true;
478 //g_testSparse = true;
479 //g_testPermutation = true;
480 //g_testZeroes = true;
tanjent@gmail.com31a9e8e2010-11-09 20:29:19 +0000481
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000482 testHash("murmur3a");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000483
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000484 //----------
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000485
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000486 int timeEnd = clock();
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +0000487
tanjent@gmail.com2aa29c32011-03-19 08:53:53 +0000488 printf("\n");
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000489 printf("Testing took %f seconds\n",double(timeEnd-timeBegin)/double(CLOCKS_PER_SEC));
tanjent@gmail.com2aa29c32011-03-19 08:53:53 +0000490 printf("-------------------------------------------------------------------------------\n");
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000491 return 0;
tanjent@gmail.com2aa29c32011-03-19 08:53:53 +0000492}