sanity_test.c 25 KB

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