| 1 |
// xxHash/tests/sanity_test.c |
| 2 |
// SPDX-License-Identifier: GPL-2.0-only |
| 3 |
// |
| 4 |
// Building |
| 5 |
// ======== |
| 6 |
// |
| 7 |
// cc sanity_test.c && ./a.out |
| 8 |
// |
| 9 |
/* |
| 10 |
notes or changes: |
| 11 |
|
| 12 |
main() |
| 13 |
------ |
| 14 |
|
| 15 |
- All test methods (XXH32, XXH64, ...) are context free. |
| 16 |
- It means that there's no restriction by order of tests and # of test. (Ready for multi-threaded / distributed test) |
| 17 |
- To achieve it, some test has dedicated 'randSeed' to decouple dependency between tests. |
| 18 |
- Note that for() loop is not ready for distributed test. |
| 19 |
- randSeed still needs to be computed step by step in the for() loop so far. |
| 20 |
|
| 21 |
*/ |
| 22 |
#define XXH_STATIC_LINKING_ONLY |
| 23 |
#define XXH_IMPLEMENTATION /* access definitions */ |
| 24 |
#include "../cli/xsum_arch.h" |
| 25 |
#include "../cli/xsum_os_specific.h" |
| 26 |
#include "../cli/xsum_output.h" |
| 27 |
#include "../cli/xsum_output.c" |
| 28 |
#define XSUM_NO_MAIN 1 |
| 29 |
#include "../cli/xsum_os_specific.h" |
| 30 |
#include "../cli/xsum_os_specific.c" |
| 31 |
#include "../xxhash.h" |
| 32 |
#include "sanity_test_vectors.h" |
| 33 |
|
| 34 |
#include <assert.h> /* assert */ |
| 35 |
|
| 36 |
/* use #define to make them constant, required for initialization */ |
| 37 |
#define PRIME32 2654435761U |
| 38 |
#define PRIME64 11400714785074694797ULL |
| 39 |
|
| 40 |
#define SANITY_BUFFER_SIZE (4096 + 64 + 1) |
| 41 |
|
| 42 |
|
| 43 |
/**/ |
| 44 |
static int abortByError = 1; |
| 45 |
|
| 46 |
|
| 47 |
/**/ |
| 48 |
static void abortSanityTest(void) { |
| 49 |
/* ??? : Should we show this message? */ |
| 50 |
XSUM_log("\rNote: If you modified the hash functions, make sure to either update tests/sanity_test_vectors.h with the following command\n" |
| 51 |
"\r make -C tests sanity_test_vectors.h\n"); |
| 52 |
XSUM_log("\rAbort.\n"); |
| 53 |
exit(1); |
| 54 |
} |
| 55 |
|
| 56 |
/* TODO : Share this function with sanity_check.c and xsum_sanity_check.c */ |
| 57 |
/* |
| 58 |
* Fills a test buffer with pseudorandom data. |
| 59 |
* |
| 60 |
* This is used in the sanity check - its values must not be changed. |
| 61 |
*/ |
| 62 |
static void fillTestBuffer(XSUM_U8* buffer, size_t bufferLenInBytes) |
| 63 |
{ |
| 64 |
XSUM_U64 byteGen = PRIME32; |
| 65 |
size_t i; |
| 66 |
|
| 67 |
assert(buffer != NULL); |
| 68 |
|
| 69 |
for (i = 0; i < bufferLenInBytes; ++i) { |
| 70 |
buffer[i] = (XSUM_U8)(byteGen>>56); |
| 71 |
byteGen *= PRIME64; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/* TODO : Share this function with sanity_check.c and xsum_sanity_check.c */ |
| 77 |
/* |
| 78 |
* Create (malloc) and fill buffer with pseudorandom data for sanity check. |
| 79 |
* |
| 80 |
* Use releaseSanityBuffer() to delete the buffer. |
| 81 |
*/ |
| 82 |
static XSUM_U8* createSanityBuffer(size_t bufferLenInBytes) |
| 83 |
{ |
| 84 |
XSUM_U8* buffer = (XSUM_U8*) malloc(bufferLenInBytes); |
| 85 |
assert(buffer != NULL); |
| 86 |
fillTestBuffer(buffer, bufferLenInBytes); |
| 87 |
return buffer; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
/* TODO : Share this function with sanity_check.c and xsum_sanity_check.c */ |
| 92 |
/* |
| 93 |
* Delete (free) the buffer which has been genereated by createSanityBuffer() |
| 94 |
*/ |
| 95 |
static void releaseSanityBuffer(XSUM_U8* buffer) |
| 96 |
{ |
| 97 |
assert(buffer != NULL); |
| 98 |
free(buffer); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 103 |
/**/ |
| 104 |
static void checkResult32(XXH32_hash_t r1, XXH32_hash_t r2, const char* testName, size_t testNb, size_t lineNb) |
| 105 |
{ |
| 106 |
if(r1 == r2) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
XSUM_log("\rError: %s #%zd, line #%zd: Sanity check failed!\n", testName, testNb, lineNb); |
| 111 |
XSUM_log("\rGot 0x%08X, expected 0x%08X.\n", (unsigned)r1, (unsigned)r2); |
| 112 |
|
| 113 |
if(abortByError) { |
| 114 |
abortSanityTest(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 120 |
/**/ |
| 121 |
static void checkResult64(XXH64_hash_t r1, XXH64_hash_t r2, const char* testName, size_t testNb, size_t lineNb) |
| 122 |
{ |
| 123 |
if(r1 == r2) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
XSUM_log("\rError: %s #%zd, line #%zd: Sanity check failed!\n", testName, testNb, lineNb); |
| 128 |
XSUM_log("\rGot 0x%08X%08XULL, expected 0x%08X%08XULL.\n", |
| 129 |
(unsigned)(r1>>32), (unsigned)r1, (unsigned)(r2>>32), (unsigned)r2); |
| 130 |
|
| 131 |
if(abortByError) { |
| 132 |
abortSanityTest(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 138 |
/**/ |
| 139 |
static void checkResult128(XXH128_hash_t r1, XXH128_hash_t r2, const char* testName, size_t testNb, size_t lineNb) |
| 140 |
{ |
| 141 |
if ((r1.low64 == r2.low64) && (r1.high64 == r2.high64)) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
XSUM_log("\rError: %s #%zd, line #%zd: Sanity check failed!\n", testName, testNb, lineNb); |
| 146 |
XSUM_log("\rGot { 0x%08X%08XULL, 0x%08X%08XULL }, expected { 0x%08X%08XULL, 0x%08X%08XULL } \n", |
| 147 |
(unsigned)(r1.low64>>32), (unsigned)r1.low64, (unsigned)(r1.high64>>32), (unsigned)r1.high64, |
| 148 |
(unsigned)(r2.low64>>32), (unsigned)r2.low64, (unsigned)(r2.high64>>32), (unsigned)r2.high64 ); |
| 149 |
|
| 150 |
if(abortByError) { |
| 151 |
abortSanityTest(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 157 |
/**/ |
| 158 |
static void checkResultTestDataSample(const XSUM_U8* r1, const XSUM_U8* r2, const char* testName, size_t testNb, size_t lineNb) |
| 159 |
{ |
| 160 |
if(memcmp(r1, r2, SECRET_SAMPLE_NBBYTES) == 0) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
XSUM_log("\rError: %s #%zd, line #%zd: Sanity check failed!\n", testName, testNb, lineNb); |
| 165 |
XSUM_log("\rGot { 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X }, expected { 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X } \n", |
| 166 |
r1[0], r1[1], r1[2], r1[3], r1[4], |
| 167 |
r2[0], r2[1], r2[2], r2[3], r2[4] ); |
| 168 |
|
| 169 |
if(abortByError) { |
| 170 |
abortSanityTest(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 176 |
/**/ |
| 177 |
static void testXXH32( |
| 178 |
const void* data, |
| 179 |
const XSUM_testdata32_t* testData, |
| 180 |
const char* testName, |
| 181 |
size_t testNb |
| 182 |
) { |
| 183 |
size_t const len = testData->len; |
| 184 |
XSUM_U32 const seed = testData->seed; |
| 185 |
XSUM_U32 const Nresult = testData->Nresult; |
| 186 |
|
| 187 |
XXH32_state_t * const state = XXH32_createState(); |
| 188 |
|
| 189 |
if (len == 0) { |
| 190 |
data = NULL; |
| 191 |
} else { |
| 192 |
assert(data != NULL); |
| 193 |
} |
| 194 |
|
| 195 |
assert(state != NULL); |
| 196 |
|
| 197 |
checkResult32(XXH32(data, len, seed), Nresult, testName, testNb, __LINE__); |
| 198 |
|
| 199 |
(void)XXH32_reset(state, seed); |
| 200 |
(void)XXH32_update(state, data, len); |
| 201 |
checkResult32(XXH32_digest(state), Nresult, testName, testNb, __LINE__); |
| 202 |
|
| 203 |
(void)XXH32_reset(state, seed); |
| 204 |
{ |
| 205 |
size_t pos; |
| 206 |
for (pos = 0; pos < len; ++pos) { |
| 207 |
(void)XXH32_update(state, ((const char*)data)+pos, 1); |
| 208 |
} |
| 209 |
} |
| 210 |
checkResult32(XXH32_digest(state), Nresult, testName, testNb, __LINE__); |
| 211 |
|
| 212 |
XXH32_freeState(state); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 217 |
/**/ |
| 218 |
static void testXXH64( |
| 219 |
const void* data, |
| 220 |
const XSUM_testdata64_t* testData, |
| 221 |
const char* testName, |
| 222 |
size_t testNb |
| 223 |
) |
| 224 |
{ |
| 225 |
size_t const len = (size_t)testData->len; |
| 226 |
XSUM_U64 const seed = testData->seed; |
| 227 |
XSUM_U64 const Nresult = testData->Nresult; |
| 228 |
|
| 229 |
XXH64_state_t * const state = XXH64_createState(); |
| 230 |
|
| 231 |
if (len == 0) { |
| 232 |
data = NULL; |
| 233 |
} else { |
| 234 |
assert(data != NULL); |
| 235 |
} |
| 236 |
|
| 237 |
assert(state != NULL); |
| 238 |
|
| 239 |
checkResult64(XXH64(data, len, seed), Nresult, testName, testNb, __LINE__); |
| 240 |
|
| 241 |
(void)XXH64_reset(state, seed); |
| 242 |
(void)XXH64_update(state, data, len); |
| 243 |
checkResult64(XXH64_digest(state), Nresult, testName, testNb, __LINE__); |
| 244 |
|
| 245 |
(void)XXH64_reset(state, seed); |
| 246 |
{ |
| 247 |
size_t pos; |
| 248 |
for (pos = 0; pos < len; ++pos) { |
| 249 |
(void)XXH64_update(state, ((const char*)data)+pos, 1); |
| 250 |
} |
| 251 |
} |
| 252 |
checkResult64(XXH64_digest(state), Nresult, testName, testNb, __LINE__); |
| 253 |
XXH64_freeState(state); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 258 |
/* |
| 259 |
* Used to get "random" (but actually 100% reproducible) lengths for |
| 260 |
* XSUM_XXH3_randomUpdate. |
| 261 |
*/ |
| 262 |
static XSUM_U32 SANITY_TEST_rand(XSUM_U64* pRandSeed) |
| 263 |
{ |
| 264 |
XSUM_U64 seed = *pRandSeed; |
| 265 |
seed *= PRIME64; |
| 266 |
*pRandSeed = seed; |
| 267 |
return (XSUM_U32)(seed >> 40); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 272 |
/**/ |
| 273 |
static XSUM_U64 SANITY_TEST_computeRandSeed(size_t step) |
| 274 |
{ |
| 275 |
XSUM_U64 randSeed = PRIME32; |
| 276 |
size_t i = 0; |
| 277 |
for(i = 0; i < step; ++i) { |
| 278 |
SANITY_TEST_rand(&randSeed); |
| 279 |
} |
| 280 |
return randSeed; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
/* TODO : Share this definition with xsum_sanity_check.c */ |
| 285 |
/* |
| 286 |
* Technically, XXH3_64bits_update is identical to XXH3_128bits_update as of |
| 287 |
* v0.8.0, but we treat them as separate. |
| 288 |
*/ |
| 289 |
typedef XXH_errorcode (*SANITY_TEST_XXH3_update_t)(XXH3_state_t* state, const void* input, size_t length); |
| 290 |
|
| 291 |
|
| 292 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 293 |
/* |
| 294 |
* Runs the passed XXH3_update variant on random lengths. This is to test the |
| 295 |
* more complex logic of the update function, catching bugs like this one: |
| 296 |
* https://github.com/Cyan4973/xxHash/issues/378 |
| 297 |
*/ |
| 298 |
static void SANITY_TEST_XXH3_randomUpdate( |
| 299 |
XXH3_state_t* state, |
| 300 |
const void* data, |
| 301 |
size_t len, |
| 302 |
XSUM_U64* pRandSeed, |
| 303 |
SANITY_TEST_XXH3_update_t update_fn |
| 304 |
) |
| 305 |
{ |
| 306 |
size_t p = 0; |
| 307 |
while (p < len) { |
| 308 |
size_t const modulo = len > 2 ? len : 2; |
| 309 |
size_t l = (size_t)(SANITY_TEST_rand(pRandSeed)) % modulo; |
| 310 |
if (p + l > len) l = len - p; |
| 311 |
(void)update_fn(state, (const char*)data+p, l); |
| 312 |
p += l; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 318 |
/**/ |
| 319 |
static void testXXH3( |
| 320 |
const void* data, |
| 321 |
const void* secret, |
| 322 |
size_t secretSize, |
| 323 |
const XSUM_testdata64_t* testData, |
| 324 |
XSUM_U64* pRandSeed, |
| 325 |
const char* testName, |
| 326 |
size_t testNb |
| 327 |
) |
| 328 |
{ |
| 329 |
size_t const len = testData->len; |
| 330 |
XSUM_U64 const seed = testData->seed; |
| 331 |
XSUM_U64 const Nresult = testData->Nresult; |
| 332 |
if (len == 0) { |
| 333 |
data = NULL; |
| 334 |
} else { |
| 335 |
assert(data != NULL); |
| 336 |
} |
| 337 |
{ XSUM_U64 const Dresult = XXH3_64bits_withSeed(data, len, seed); |
| 338 |
checkResult64(Dresult, Nresult, testName, testNb, __LINE__); |
| 339 |
} |
| 340 |
|
| 341 |
/* check that the no-seed variant produces same result as seed==0 */ |
| 342 |
if (seed == 0) { |
| 343 |
XSUM_U64 const Dresult = XXH3_64bits(data, len); |
| 344 |
checkResult64(Dresult, Nresult, testName, testNb, __LINE__); |
| 345 |
} |
| 346 |
|
| 347 |
/* check that the combination of |
| 348 |
* XXH3_generateSecret_fromSeed() and XXH3_64bits_withSecretandSeed() |
| 349 |
* results in exactly the same hash generation as XXH3_64bits_withSeed() */ |
| 350 |
{ char secretBuffer[XXH3_SECRET_DEFAULT_SIZE+1]; |
| 351 |
char* const secretFromSeed = secretBuffer + 1; /* intentional unalignment */ |
| 352 |
XXH3_generateSecret_fromSeed(secretFromSeed, seed); |
| 353 |
{ XSUM_U64 const Dresult = XXH3_64bits_withSecretandSeed(data, len, secretFromSeed, XXH3_SECRET_DEFAULT_SIZE, seed); |
| 354 |
checkResult64(Dresult, Nresult, testName, testNb, __LINE__); |
| 355 |
} } |
| 356 |
|
| 357 |
/* check that XXH3_64bits_withSecretandSeed() |
| 358 |
* results in exactly the same return value as XXH3_64bits_withSeed() */ |
| 359 |
if (len <= XXH3_MIDSIZE_MAX) { |
| 360 |
XSUM_U64 const Dresult = XXH3_64bits_withSecretandSeed(data, len, secret, secretSize, seed); |
| 361 |
checkResult64(Dresult, Nresult, testName, testNb, __LINE__); |
| 362 |
} |
| 363 |
|
| 364 |
/* streaming API test */ |
| 365 |
{ XXH3_state_t* const state = XXH3_createState(); |
| 366 |
assert(state != NULL); |
| 367 |
/* single ingestion */ |
| 368 |
(void)XXH3_64bits_reset_withSeed(state, seed); |
| 369 |
(void)XXH3_64bits_update(state, data, len); |
| 370 |
checkResult64(XXH3_64bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 371 |
|
| 372 |
/* random ingestion */ |
| 373 |
(void)XXH3_64bits_reset_withSeed(state, seed); |
| 374 |
SANITY_TEST_XXH3_randomUpdate(state, data, len, pRandSeed, &XXH3_64bits_update); |
| 375 |
checkResult64(XXH3_64bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 376 |
|
| 377 |
/* byte by byte ingestion */ |
| 378 |
{ size_t pos; |
| 379 |
(void)XXH3_64bits_reset_withSeed(state, seed); |
| 380 |
for (pos=0; pos<len; pos++) |
| 381 |
(void)XXH3_64bits_update(state, ((const char*)data)+pos, 1); |
| 382 |
checkResult64(XXH3_64bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 383 |
} |
| 384 |
|
| 385 |
/* check that streaming with a combination of |
| 386 |
* XXH3_generateSecret_fromSeed() and XXH3_64bits_reset_withSecretandSeed() |
| 387 |
* results in exactly the same hash generation as XXH3_64bits_reset_withSeed() */ |
| 388 |
{ char secretBuffer[XXH3_SECRET_DEFAULT_SIZE+1]; |
| 389 |
char* const secretFromSeed = secretBuffer + 1; /* intentional unalignment */ |
| 390 |
XXH3_generateSecret_fromSeed(secretFromSeed, seed); |
| 391 |
/* single ingestion */ |
| 392 |
(void)XXH3_64bits_reset_withSecretandSeed(state, secretFromSeed, XXH3_SECRET_DEFAULT_SIZE, seed); |
| 393 |
(void)XXH3_64bits_update(state, data, len); |
| 394 |
checkResult64(XXH3_64bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 395 |
} |
| 396 |
|
| 397 |
/* check that XXH3_64bits_withSecretandSeed() |
| 398 |
* results in exactly the same return value as XXH3_64bits_withSeed() */ |
| 399 |
if (len <= XXH3_MIDSIZE_MAX) { |
| 400 |
/* single ingestion */ |
| 401 |
(void)XXH3_64bits_reset_withSecretandSeed(state, secret, secretSize, seed); |
| 402 |
(void)XXH3_64bits_update(state, data, len); |
| 403 |
checkResult64(XXH3_64bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 404 |
} |
| 405 |
|
| 406 |
XXH3_freeState(state); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 412 |
/**/ |
| 413 |
static void testXXH3_withSecret( |
| 414 |
const void* data, |
| 415 |
const void* secret, |
| 416 |
size_t secretSize, |
| 417 |
const XSUM_testdata64_t* testData, |
| 418 |
XSUM_U64* pRandSeed, |
| 419 |
const char* testName, |
| 420 |
size_t testNb |
| 421 |
) |
| 422 |
{ |
| 423 |
size_t const len = (size_t)testData->len; |
| 424 |
XSUM_U64 const Nresult = testData->Nresult; |
| 425 |
|
| 426 |
if (len == 0) { |
| 427 |
data = NULL; |
| 428 |
} else { |
| 429 |
assert(data != NULL); |
| 430 |
} |
| 431 |
{ XSUM_U64 const Dresult = XXH3_64bits_withSecret(data, len, secret, secretSize); |
| 432 |
checkResult64(Dresult, Nresult, testName, testNb, __LINE__); |
| 433 |
} |
| 434 |
|
| 435 |
/* check that XXH3_64bits_withSecretandSeed() |
| 436 |
* results in exactly the same return value as XXH3_64bits_withSecret() */ |
| 437 |
if (len > XXH3_MIDSIZE_MAX) |
| 438 |
{ XSUM_U64 const Dresult = XXH3_64bits_withSecretandSeed(data, len, secret, secretSize, 0); |
| 439 |
checkResult64(Dresult, Nresult, testName, testNb, __LINE__); |
| 440 |
} |
| 441 |
|
| 442 |
/* streaming API test */ |
| 443 |
{ XXH3_state_t * const state = XXH3_createState(); |
| 444 |
assert(state != NULL); |
| 445 |
(void)XXH3_64bits_reset_withSecret(state, secret, secretSize); |
| 446 |
(void)XXH3_64bits_update(state, data, len); |
| 447 |
checkResult64(XXH3_64bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 448 |
|
| 449 |
/* random ingestion */ |
| 450 |
(void)XXH3_64bits_reset_withSecret(state, secret, secretSize); |
| 451 |
SANITY_TEST_XXH3_randomUpdate(state, data, len, pRandSeed, &XXH3_64bits_update); |
| 452 |
checkResult64(XXH3_64bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 453 |
|
| 454 |
/* byte by byte ingestion */ |
| 455 |
{ size_t pos; |
| 456 |
(void)XXH3_64bits_reset_withSecret(state, secret, secretSize); |
| 457 |
for (pos=0; pos<len; pos++) |
| 458 |
(void)XXH3_64bits_update(state, ((const char*)data)+pos, 1); |
| 459 |
checkResult64(XXH3_64bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 460 |
} |
| 461 |
|
| 462 |
/* check that XXH3_64bits_reset_withSecretandSeed() |
| 463 |
* results in exactly the same return value as XXH3_64bits_reset_withSecret() */ |
| 464 |
if (len > XXH3_MIDSIZE_MAX) { |
| 465 |
/* single ingestion */ |
| 466 |
(void)XXH3_64bits_reset_withSecretandSeed(state, secret, secretSize, 0); |
| 467 |
(void)XXH3_64bits_update(state, data, len); |
| 468 |
checkResult64(XXH3_64bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 469 |
} |
| 470 |
|
| 471 |
XXH3_freeState(state); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 477 |
/**/ |
| 478 |
static void testXXH128( |
| 479 |
const void* data, |
| 480 |
const void* secret, |
| 481 |
size_t secretSize, |
| 482 |
const XSUM_testdata128_t* testData, |
| 483 |
XSUM_U64* pRandSeed, |
| 484 |
const char* testName, |
| 485 |
size_t testNb |
| 486 |
) |
| 487 |
{ |
| 488 |
size_t const len = (size_t)testData->len; |
| 489 |
XSUM_U64 const seed = testData->seed; |
| 490 |
XXH128_hash_t const Nresult = testData->Nresult; |
| 491 |
if (len == 0) { |
| 492 |
data = NULL; |
| 493 |
} else { |
| 494 |
assert(data != NULL); |
| 495 |
} |
| 496 |
|
| 497 |
{ XXH128_hash_t const Dresult = XXH3_128bits_withSeed(data, len, seed); |
| 498 |
checkResult128(Dresult, Nresult, testName, testNb, __LINE__); |
| 499 |
} |
| 500 |
|
| 501 |
/* check that XXH128() is identical to XXH3_128bits_withSeed() */ |
| 502 |
{ XXH128_hash_t const Dresult2 = XXH128(data, len, seed); |
| 503 |
checkResult128(Dresult2, Nresult, testName, testNb, __LINE__); |
| 504 |
} |
| 505 |
|
| 506 |
/* check that the no-seed variant produces same result as seed==0 */ |
| 507 |
if (seed == 0) { |
| 508 |
XXH128_hash_t const Dresult = XXH3_128bits(data, len); |
| 509 |
checkResult128(Dresult, Nresult, testName, testNb, __LINE__); |
| 510 |
} |
| 511 |
|
| 512 |
/* check that the combination of |
| 513 |
* XXH3_generateSecret_fromSeed() and XXH3_128bits_withSecretandSeed() |
| 514 |
* results in exactly the same hash generation as XXH3_64bits_withSeed() */ |
| 515 |
{ char secretBuffer[XXH3_SECRET_DEFAULT_SIZE+1]; |
| 516 |
char* const secretFromSeed = secretBuffer + 1; /* intentional unalignment */ |
| 517 |
XXH3_generateSecret_fromSeed(secretFromSeed, seed); |
| 518 |
{ XXH128_hash_t const Dresult = XXH3_128bits_withSecretandSeed(data, len, secretFromSeed, XXH3_SECRET_DEFAULT_SIZE, seed); |
| 519 |
checkResult128(Dresult, Nresult, testName, testNb, __LINE__); |
| 520 |
} } |
| 521 |
|
| 522 |
/* check that XXH3_128bits_withSecretandSeed() |
| 523 |
* results in exactly the same return value as XXH3_128bits_withSeed() */ |
| 524 |
if (len <= XXH3_MIDSIZE_MAX) { |
| 525 |
XXH128_hash_t const Dresult = XXH3_128bits_withSecretandSeed(data, len, secret, secretSize, seed); |
| 526 |
checkResult128(Dresult, Nresult, testName, testNb, __LINE__); |
| 527 |
} |
| 528 |
|
| 529 |
/* streaming API test */ |
| 530 |
{ XXH3_state_t * const state = XXH3_createState(); |
| 531 |
assert(state != NULL); |
| 532 |
|
| 533 |
/* single ingestion */ |
| 534 |
(void)XXH3_128bits_reset_withSeed(state, seed); |
| 535 |
(void)XXH3_128bits_update(state, data, len); |
| 536 |
checkResult128(XXH3_128bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 537 |
|
| 538 |
/* random ingestion */ |
| 539 |
(void)XXH3_128bits_reset_withSeed(state, seed); |
| 540 |
SANITY_TEST_XXH3_randomUpdate(state, data, len, pRandSeed, &XXH3_128bits_update); |
| 541 |
checkResult128(XXH3_128bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 542 |
|
| 543 |
/* byte by byte ingestion */ |
| 544 |
{ size_t pos; |
| 545 |
(void)XXH3_128bits_reset_withSeed(state, seed); |
| 546 |
for (pos=0; pos<len; pos++) |
| 547 |
(void)XXH3_128bits_update(state, ((const char*)data)+pos, 1); |
| 548 |
checkResult128(XXH3_128bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 549 |
} |
| 550 |
|
| 551 |
/* check that streaming with a combination of |
| 552 |
* XXH3_generateSecret_fromSeed() and XXH3_128bits_reset_withSecretandSeed() |
| 553 |
* results in exactly the same hash generation as XXH3_128bits_reset_withSeed() */ |
| 554 |
{ char secretBuffer[XXH3_SECRET_DEFAULT_SIZE+1]; |
| 555 |
char* const secretFromSeed = secretBuffer + 1; /* intentional unalignment */ |
| 556 |
XXH3_generateSecret_fromSeed(secretFromSeed, seed); |
| 557 |
/* single ingestion */ |
| 558 |
(void)XXH3_128bits_reset_withSecretandSeed(state, secretFromSeed, XXH3_SECRET_DEFAULT_SIZE, seed); |
| 559 |
(void)XXH3_128bits_update(state, data, len); |
| 560 |
checkResult128(XXH3_128bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 561 |
} |
| 562 |
|
| 563 |
/* check that XXH3_128bits_reset_withSecretandSeed() |
| 564 |
* results in exactly the same return value as XXH3_128bits_reset_withSeed() */ |
| 565 |
if (len <= XXH3_MIDSIZE_MAX) { |
| 566 |
/* single ingestion */ |
| 567 |
(void)XXH3_128bits_reset_withSecretandSeed(state, secret, secretSize, seed); |
| 568 |
(void)XXH3_128bits_update(state, data, len); |
| 569 |
checkResult128(XXH3_128bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 570 |
} |
| 571 |
|
| 572 |
XXH3_freeState(state); |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 578 |
/**/ |
| 579 |
static void testXXH128_withSecret( |
| 580 |
const void* data, |
| 581 |
const void* secret, |
| 582 |
size_t secretSize, |
| 583 |
const XSUM_testdata128_t* testData, |
| 584 |
XSUM_U64* pRandSeed, |
| 585 |
const char* testName, |
| 586 |
size_t testNb |
| 587 |
) |
| 588 |
{ |
| 589 |
size_t const len = testData->len; |
| 590 |
XXH128_hash_t const Nresult = testData->Nresult; |
| 591 |
if (len == 0) { |
| 592 |
data = NULL; |
| 593 |
} else { |
| 594 |
assert(data != NULL); |
| 595 |
} |
| 596 |
|
| 597 |
{ XXH128_hash_t const Dresult = XXH3_128bits_withSecret(data, len, secret, secretSize); |
| 598 |
checkResult128(Dresult, Nresult, testName, testNb, __LINE__); |
| 599 |
} |
| 600 |
|
| 601 |
/* check that XXH3_128bits_withSecretandSeed() |
| 602 |
* results in exactly the same return value as XXH3_128bits_withSecret() */ |
| 603 |
if (len > XXH3_MIDSIZE_MAX) |
| 604 |
{ XXH128_hash_t const Dresult = XXH3_128bits_withSecretandSeed(data, len, secret, secretSize, 0); |
| 605 |
checkResult128(Dresult, Nresult, testName, testNb, __LINE__); |
| 606 |
} |
| 607 |
|
| 608 |
/* streaming API test */ |
| 609 |
{ XXH3_state_t* const state = XXH3_createState(); |
| 610 |
assert(state != NULL); |
| 611 |
(void)XXH3_128bits_reset_withSecret(state, secret, secretSize); |
| 612 |
(void)XXH3_128bits_update(state, data, len); |
| 613 |
checkResult128(XXH3_128bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 614 |
|
| 615 |
/* random ingestion */ |
| 616 |
(void)XXH3_128bits_reset_withSecret(state, secret, secretSize); |
| 617 |
SANITY_TEST_XXH3_randomUpdate(state, data, len, pRandSeed, &XXH3_128bits_update); |
| 618 |
checkResult128(XXH3_128bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 619 |
|
| 620 |
/* byte by byte ingestion */ |
| 621 |
{ size_t pos; |
| 622 |
(void)XXH3_128bits_reset_withSecret(state, secret, secretSize); |
| 623 |
for (pos=0; pos<len; pos++) |
| 624 |
(void)XXH3_128bits_update(state, ((const char*)data)+pos, 1); |
| 625 |
checkResult128(XXH3_128bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 626 |
} |
| 627 |
|
| 628 |
/* check that XXH3_128bits_reset_withSecretandSeed() |
| 629 |
* results in exactly the same return value as XXH3_128bits_reset_withSecret() */ |
| 630 |
if (len > XXH3_MIDSIZE_MAX) { |
| 631 |
/* single ingestion */ |
| 632 |
(void)XXH3_128bits_reset_withSecretandSeed(state, secret, secretSize, 0); |
| 633 |
(void)XXH3_128bits_update(state, data, len); |
| 634 |
checkResult128(XXH3_128bits_digest(state), Nresult, testName, testNb, __LINE__); |
| 635 |
} |
| 636 |
|
| 637 |
XXH3_freeState(state); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
/* TODO : Share this function with xsum_sanity_check.c */ |
| 643 |
/**/ |
| 644 |
static void testSecretGenerator( |
| 645 |
const void* customSeed, |
| 646 |
const XSUM_testdata_sample_t* testData, |
| 647 |
const char* testName, |
| 648 |
size_t testNb |
| 649 |
) |
| 650 |
{ |
| 651 |
/* TODO : Share this array with sanity_check.c and xsum_sanity_check.c */ |
| 652 |
static const int sampleIndex[SECRET_SAMPLE_NBBYTES] = { 0, 62, 131, 191, 241 }; /* position of sampled bytes */ |
| 653 |
XSUM_U8 secretBuffer[SECRET_SIZE_MAX] = {0}; |
| 654 |
XSUM_U8 samples[SECRET_SAMPLE_NBBYTES]; |
| 655 |
int i; |
| 656 |
|
| 657 |
assert(testData->secretLen <= SECRET_SIZE_MAX); |
| 658 |
XXH3_generateSecret(secretBuffer, testData->secretLen, customSeed, testData->seedLen); |
| 659 |
for (i=0; i<SECRET_SAMPLE_NBBYTES; i++) { |
| 660 |
samples[i] = secretBuffer[sampleIndex[i]]; |
| 661 |
} |
| 662 |
checkResultTestDataSample(samples, testData->byte, testName, testNb, __LINE__); |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
/**/ |
| 667 |
int main(int argc, const char* argv[]) |
| 668 |
{ |
| 669 |
size_t testCount = 0; |
| 670 |
size_t const sanityBufferSizeInBytes = SANITY_BUFFER_SIZE; |
| 671 |
XSUM_U8* const sanityBuffer = createSanityBuffer(sanityBufferSizeInBytes); |
| 672 |
const void* const secret = sanityBuffer + 7; |
| 673 |
size_t const secretSize = XXH3_SECRET_SIZE_MIN + 11; |
| 674 |
assert(sanityBufferSizeInBytes >= 7 + secretSize); |
| 675 |
(void) argc; |
| 676 |
(void) argv; |
| 677 |
|
| 678 |
{ |
| 679 |
/* XXH32 */ |
| 680 |
size_t const n = sizeof(XSUM_XXH32_testdata) / sizeof(XSUM_XXH32_testdata[0]); |
| 681 |
size_t i; |
| 682 |
for (i = 0; i < n; ++i, ++testCount) { |
| 683 |
testXXH32( |
| 684 |
sanityBuffer, |
| 685 |
&XSUM_XXH32_testdata[i], |
| 686 |
"XSUM_XXH32_testdata", |
| 687 |
i |
| 688 |
); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
{ |
| 693 |
/* XXH64 */ |
| 694 |
size_t const n = sizeof(XSUM_XXH64_testdata) / sizeof(XSUM_XXH64_testdata[0]); |
| 695 |
size_t i; |
| 696 |
for (i = 0; i < n; ++i, ++testCount) { |
| 697 |
testXXH64( |
| 698 |
sanityBuffer, |
| 699 |
&XSUM_XXH64_testdata[i], |
| 700 |
"XSUM_XXH64_testdata", |
| 701 |
i |
| 702 |
); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
{ |
| 707 |
/* XXH3_64bits, seeded */ |
| 708 |
size_t const randCount = 0; |
| 709 |
XSUM_U64 randSeed = SANITY_TEST_computeRandSeed(randCount); |
| 710 |
size_t const n = sizeof(XSUM_XXH3_testdata) / sizeof(XSUM_XXH3_testdata[0]); |
| 711 |
size_t i; |
| 712 |
for (i = 0; i < n; ++i, ++testCount) { |
| 713 |
testXXH3( |
| 714 |
sanityBuffer, |
| 715 |
secret, |
| 716 |
secretSize, |
| 717 |
&XSUM_XXH3_testdata[i], |
| 718 |
&randSeed, |
| 719 |
"XSUM_XXH3_testdata", |
| 720 |
i |
| 721 |
); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
{ |
| 726 |
/* XXH3_64bits, custom secret */ |
| 727 |
size_t const randCount = 22730; |
| 728 |
XSUM_U64 randSeed = SANITY_TEST_computeRandSeed(randCount); |
| 729 |
size_t const n = sizeof(XSUM_XXH3_withSecret_testdata) / sizeof(XSUM_XXH3_withSecret_testdata[0]); |
| 730 |
size_t i; |
| 731 |
for (i = 0; i < n; ++i, ++testCount) { |
| 732 |
testXXH3_withSecret( |
| 733 |
sanityBuffer, |
| 734 |
secret, |
| 735 |
secretSize, |
| 736 |
&XSUM_XXH3_withSecret_testdata[i], |
| 737 |
&randSeed, |
| 738 |
"XSUM_XXH3_withSecret_testdata", |
| 739 |
i |
| 740 |
); |
| 741 |
testCount++; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
{ |
| 746 |
/* XXH128 */ |
| 747 |
size_t const randCount = 34068; |
| 748 |
XSUM_U64 randSeed = SANITY_TEST_computeRandSeed(randCount); |
| 749 |
size_t const n = (sizeof(XSUM_XXH128_testdata)/sizeof(XSUM_XXH128_testdata[0])); |
| 750 |
size_t i; |
| 751 |
for (i = 0; i < n; ++i, ++testCount) { |
| 752 |
testXXH128( |
| 753 |
sanityBuffer, |
| 754 |
secret, |
| 755 |
secretSize, |
| 756 |
&XSUM_XXH128_testdata[i], |
| 757 |
&randSeed, |
| 758 |
"XSUM_XXH128_testdata", |
| 759 |
i |
| 760 |
); |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
{ |
| 765 |
/* XXH128 with custom Secret */ |
| 766 |
size_t const randCount = 68019; |
| 767 |
XSUM_U64 randSeed = SANITY_TEST_computeRandSeed(randCount); |
| 768 |
size_t const n = (sizeof(XSUM_XXH128_withSecret_testdata)/sizeof(XSUM_XXH128_withSecret_testdata[0])); |
| 769 |
size_t i; |
| 770 |
for (i = 0; i < n; ++i, ++testCount) { |
| 771 |
testXXH128_withSecret( |
| 772 |
sanityBuffer, |
| 773 |
secret, |
| 774 |
secretSize, |
| 775 |
&XSUM_XXH128_withSecret_testdata[i], |
| 776 |
&randSeed, |
| 777 |
"XSUM_XXH128_withSecret_testdata", |
| 778 |
i |
| 779 |
); |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
{ |
| 784 |
/* secret generator */ |
| 785 |
size_t const n = sizeof(XSUM_XXH3_generateSecret_testdata) / sizeof(XSUM_XXH3_generateSecret_testdata[0]); |
| 786 |
size_t i; |
| 787 |
for (i = 0; i < n; ++i, ++testCount) { |
| 788 |
assert(XSUM_XXH3_generateSecret_testdata[i].seedLen <= SANITY_BUFFER_SIZE); |
| 789 |
testSecretGenerator( |
| 790 |
sanityBuffer, |
| 791 |
&XSUM_XXH3_generateSecret_testdata[i], |
| 792 |
"XSUM_XXH3_generateSecret_testdata", |
| 793 |
i |
| 794 |
); |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
releaseSanityBuffer(sanityBuffer); |
| 799 |
|
| 800 |
XSUM_log("\rOK. (passes %zd tests)\n", testCount); |
| 801 |
|
| 802 |
return EXIT_SUCCESS; |
| 803 |
} |