]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/fuzzer.c
Import zstd 1.3.0
[FreeBSD/FreeBSD.git] / tests / fuzzer.c
1 /**
2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  */
9
10
11 /*-************************************
12 *  Compiler specific
13 **************************************/
14 #ifdef _MSC_VER    /* Visual Studio */
15 #  define _CRT_SECURE_NO_WARNINGS   /* fgets */
16 #  pragma warning(disable : 4127)   /* disable: C4127: conditional expression is constant */
17 #  pragma warning(disable : 4204)   /* disable: C4204: non-constant aggregate initializer */
18 #endif
19
20
21 /*-************************************
22 *  Includes
23 **************************************/
24 #include <stdlib.h>       /* free */
25 #include <stdio.h>        /* fgets, sscanf */
26 #include <string.h>       /* strcmp */
27 #include <time.h>         /* clock_t */
28 #define ZSTD_STATIC_LINKING_ONLY  /* ZSTD_compressContinue, ZSTD_compressBlock */
29 #include "zstd.h"         /* ZSTD_VERSION_STRING */
30 #include "zstd_errors.h"  /* ZSTD_getErrorCode */
31 #include "zstdmt_compress.h"
32 #define ZDICT_STATIC_LINKING_ONLY
33 #include "zdict.h"        /* ZDICT_trainFromBuffer */
34 #include "datagen.h"      /* RDG_genBuffer */
35 #include "mem.h"
36 #define XXH_STATIC_LINKING_ONLY
37 #include "xxhash.h"       /* XXH64 */
38
39
40 /*-************************************
41 *  Constants
42 **************************************/
43 #define KB *(1U<<10)
44 #define MB *(1U<<20)
45 #define GB *(1U<<30)
46
47 static const U32 FUZ_compressibility_default = 50;
48 static const U32 nbTestsDefault = 30000;
49
50
51 /*-************************************
52 *  Display Macros
53 **************************************/
54 #define DISPLAY(...)          fprintf(stderr, __VA_ARGS__)
55 #define DISPLAYLEVEL(l, ...)  if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
56 static U32 g_displayLevel = 2;
57
58 #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
59             if ((FUZ_clockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
60             { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \
61             if (g_displayLevel>=4) fflush(stderr); } }
62 static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6;
63 static clock_t g_displayClock = 0;
64
65
66 /*-*******************************************************
67 *  Fuzzer functions
68 *********************************************************/
69 #define MIN(a,b) ((a)<(b)?(a):(b))
70 #define MAX(a,b) ((a)>(b)?(a):(b))
71
72 static clock_t FUZ_clockSpan(clock_t cStart)
73 {
74     return clock() - cStart;   /* works even when overflow; max span ~ 30mn */
75 }
76
77 #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
78 static unsigned FUZ_rand(unsigned* src)
79 {
80     static const U32 prime1 = 2654435761U;
81     static const U32 prime2 = 2246822519U;
82     U32 rand32 = *src;
83     rand32 *= prime1;
84     rand32 += prime2;
85     rand32  = FUZ_rotl32(rand32, 13);
86     *src = rand32;
87     return rand32 >> 5;
88 }
89
90 static unsigned FUZ_highbit32(U32 v32)
91 {
92     unsigned nbBits = 0;
93     if (v32==0) return 0;
94     while (v32) v32 >>= 1, nbBits++;
95     return nbBits;
96 }
97
98
99 /*=============================================
100 *   Basic Unit tests
101 =============================================*/
102
103 #define CHECK_V(var, fn)  size_t const var = fn; if (ZSTD_isError(var)) goto _output_error
104 #define CHECK(fn)  { CHECK_V(err, fn); }
105 #define CHECKPLUS(var, fn, more)  { CHECK_V(var, fn); more; }
106
107 static int basicUnitTests(U32 seed, double compressibility)
108 {
109     size_t const CNBuffSize = 5 MB;
110     void* const CNBuffer = malloc(CNBuffSize);
111     void* const compressedBuffer = malloc(ZSTD_compressBound(CNBuffSize));
112     void* const decodedBuffer = malloc(CNBuffSize);
113     ZSTD_DCtx* dctx = ZSTD_createDCtx();
114     int testResult = 0;
115     U32 testNb=0;
116     size_t cSize;
117
118     /* Create compressible noise */
119     if (!CNBuffer || !compressedBuffer || !decodedBuffer) {
120         DISPLAY("Not enough memory, aborting\n");
121         testResult = 1;
122         goto _end;
123     }
124     RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed);
125
126     /* Basic tests */
127     DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName : ", testNb++);
128     {   const char* errorString = ZSTD_getErrorName(0);
129         DISPLAYLEVEL(4, "OK : %s \n", errorString);
130     }
131
132     DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName with wrong value : ", testNb++);
133     {   const char* errorString = ZSTD_getErrorName(499);
134         DISPLAYLEVEL(4, "OK : %s \n", errorString);
135     }
136
137
138     DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, (U32)CNBuffSize);
139     CHECKPLUS(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(CNBuffSize),
140                                CNBuffer, CNBuffSize, 1),
141               cSize=r );
142     DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
143
144
145     DISPLAYLEVEL(4, "test%3i : ZSTD_getFrameContentSize test : ", testNb++);
146     {   unsigned long long const rSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
147         if (rSize != CNBuffSize) goto _output_error;
148     }
149     DISPLAYLEVEL(4, "OK \n");
150
151     DISPLAYLEVEL(4, "test%3i : ZSTD_findDecompressedSize test : ", testNb++);
152     {   unsigned long long const rSize = ZSTD_findDecompressedSize(compressedBuffer, cSize);
153         if (rSize != CNBuffSize) goto _output_error;
154     }
155     DISPLAYLEVEL(4, "OK \n");
156
157     DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)CNBuffSize);
158     { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
159       if (r != CNBuffSize) goto _output_error; }
160     DISPLAYLEVEL(4, "OK \n");
161
162     DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
163     {   size_t u;
164         for (u=0; u<CNBuffSize; u++) {
165             if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;;
166     }   }
167     DISPLAYLEVEL(4, "OK \n");
168
169
170     DISPLAYLEVEL(4, "test%3i : decompress with null dict : ", testNb++);
171     { size_t const r = ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, NULL, 0);
172       if (r != CNBuffSize) goto _output_error; }
173     DISPLAYLEVEL(4, "OK \n");
174
175     DISPLAYLEVEL(4, "test%3i : decompress with null DDict : ", testNb++);
176     { size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, NULL);
177       if (r != CNBuffSize) goto _output_error; }
178     DISPLAYLEVEL(4, "OK \n");
179
180     DISPLAYLEVEL(4, "test%3i : decompress with 1 missing byte : ", testNb++);
181     { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize-1);
182       if (!ZSTD_isError(r)) goto _output_error;
183       if (ZSTD_getErrorCode((size_t)r) != ZSTD_error_srcSize_wrong) goto _output_error; }
184     DISPLAYLEVEL(4, "OK \n");
185
186     DISPLAYLEVEL(4, "test%3i : decompress with 1 too much byte : ", testNb++);
187     { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize+1);
188       if (!ZSTD_isError(r)) goto _output_error;
189       if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
190     DISPLAYLEVEL(4, "OK \n");
191
192
193     /* Static CCtx tests */
194 #define STATIC_CCTX_LEVEL 3
195     DISPLAYLEVEL(4, "test%3i : create static CCtx for level %u :", testNb++, STATIC_CCTX_LEVEL);
196     {   size_t const staticCCtxSize = ZSTD_estimateCStreamSize(STATIC_CCTX_LEVEL);
197         void* const staticCCtxBuffer = malloc(staticCCtxSize);
198         size_t const staticDCtxSize = ZSTD_estimateDCtxSize();
199         void* const staticDCtxBuffer = malloc(staticDCtxSize);
200         if (staticCCtxBuffer==NULL || staticDCtxBuffer==NULL) {
201             free(staticCCtxBuffer);
202             free(staticDCtxBuffer);
203             DISPLAY("Not enough memory, aborting\n");
204             testResult = 1;
205             goto _end;
206         }
207         {   ZSTD_CCtx* staticCCtx = ZSTD_initStaticCCtx(staticCCtxBuffer, staticCCtxSize);
208             ZSTD_DCtx* staticDCtx = ZSTD_initStaticDCtx(staticDCtxBuffer, staticDCtxSize);
209             if ((staticCCtx==NULL) || (staticDCtx==NULL)) goto _output_error;
210             DISPLAYLEVEL(4, "OK \n");
211
212             DISPLAYLEVEL(4, "test%3i : init CCtx for level %u : ", testNb++, STATIC_CCTX_LEVEL);
213             { size_t const r = ZSTD_compressBegin(staticCCtx, STATIC_CCTX_LEVEL);
214               if (ZSTD_isError(r)) goto _output_error; }
215             DISPLAYLEVEL(4, "OK \n");
216
217             DISPLAYLEVEL(4, "test%3i : simple compression test with static CCtx : ", testNb++);
218             CHECKPLUS(r, ZSTD_compressCCtx(staticCCtx,
219                             compressedBuffer, ZSTD_compressBound(CNBuffSize),
220                             CNBuffer, CNBuffSize, STATIC_CCTX_LEVEL),
221                       cSize=r );
222             DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n",
223                             (U32)cSize, (double)cSize/CNBuffSize*100);
224
225             DISPLAYLEVEL(4, "test%3i : simple decompression test with static DCtx : ", testNb++);
226             { size_t const r = ZSTD_decompressDCtx(staticDCtx,
227                                                 decodedBuffer, CNBuffSize,
228                                                 compressedBuffer, cSize);
229               if (r != CNBuffSize) goto _output_error; }
230             DISPLAYLEVEL(4, "OK \n");
231
232             DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
233             {   size_t u;
234                 for (u=0; u<CNBuffSize; u++) {
235                     if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u])
236                         goto _output_error;;
237             }   }
238             DISPLAYLEVEL(4, "OK \n");
239
240             DISPLAYLEVEL(4, "test%3i : init CCtx for too large level (must fail) : ", testNb++);
241             { size_t const r = ZSTD_compressBegin(staticCCtx, ZSTD_maxCLevel());
242               if (!ZSTD_isError(r)) goto _output_error; }
243             DISPLAYLEVEL(4, "OK \n");
244
245             DISPLAYLEVEL(4, "test%3i : init CCtx for small level %u (should work again) : ", testNb++, 1);
246             { size_t const r = ZSTD_compressBegin(staticCCtx, 1);
247               if (ZSTD_isError(r)) goto _output_error; }
248             DISPLAYLEVEL(4, "OK \n");
249
250             DISPLAYLEVEL(4, "test%3i : init CStream for small level %u : ", testNb++, 1);
251             { size_t const r = ZSTD_initCStream(staticCCtx, 1);
252               if (ZSTD_isError(r)) goto _output_error; }
253             DISPLAYLEVEL(4, "OK \n");
254
255             DISPLAYLEVEL(4, "test%3i : init CStream with dictionary (should fail) : ", testNb++);
256             { size_t const r = ZSTD_initCStream_usingDict(staticCCtx, CNBuffer, 64 KB, 1);
257               if (!ZSTD_isError(r)) goto _output_error; }
258             DISPLAYLEVEL(4, "OK \n");
259
260             DISPLAYLEVEL(4, "test%3i : init DStream (should fail) : ", testNb++);
261             { size_t const r = ZSTD_initDStream(staticDCtx);
262               if (ZSTD_isError(r)) goto _output_error; }
263             {   ZSTD_outBuffer output = { decodedBuffer, CNBuffSize, 0 };
264                 ZSTD_inBuffer input = { compressedBuffer, ZSTD_FRAMEHEADERSIZE_MAX+1, 0 };
265                 size_t const r = ZSTD_decompressStream(staticDCtx, &output, &input);
266                 if (!ZSTD_isError(r)) goto _output_error;
267             }
268             DISPLAYLEVEL(4, "OK \n");
269         }
270         free(staticCCtxBuffer);
271         free(staticDCtxBuffer);
272     }
273
274
275
276     /* ZSTDMT simple MT compression test */
277     DISPLAYLEVEL(4, "test%3i : create ZSTDMT CCtx : ", testNb++);
278     {   ZSTDMT_CCtx* mtctx = ZSTDMT_createCCtx(2);
279         if (mtctx==NULL) {
280             DISPLAY("mtctx : mot enough memory, aborting \n");
281             testResult = 1;
282             goto _end;
283         }
284         DISPLAYLEVEL(4, "OK \n");
285
286         DISPLAYLEVEL(4, "test%3i : compress %u bytes with 2 threads : ", testNb++, (U32)CNBuffSize);
287         CHECKPLUS(r, ZSTDMT_compressCCtx(mtctx,
288                                 compressedBuffer, ZSTD_compressBound(CNBuffSize),
289                                 CNBuffer, CNBuffSize,
290                                 1),
291                   cSize=r );
292         DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
293
294         DISPLAYLEVEL(4, "test%3i : decompressed size test : ", testNb++);
295         {   unsigned long long const rSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
296             if (rSize != CNBuffSize)  {
297                 DISPLAY("ZSTD_getFrameContentSize incorrect : %u != %u \n", (U32)rSize, (U32)CNBuffSize);
298                 goto _output_error;
299         }   }
300         DISPLAYLEVEL(4, "OK \n");
301
302         DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)CNBuffSize);
303         { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
304           if (r != CNBuffSize) goto _output_error; }
305         DISPLAYLEVEL(4, "OK \n");
306
307         DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
308         {   size_t u;
309             for (u=0; u<CNBuffSize; u++) {
310                 if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;;
311         }   }
312         DISPLAYLEVEL(4, "OK \n");
313
314         ZSTDMT_freeCCtx(mtctx);
315     }
316
317
318     /* Simple API multiframe test */
319     DISPLAYLEVEL(4, "test%3i : compress multiple frames : ", testNb++);
320     {   size_t off = 0;
321         int i;
322         int const segs = 4;
323         /* only use the first half so we don't push against size limit of compressedBuffer */
324         size_t const segSize = (CNBuffSize / 2) / segs;
325         for (i = 0; i < segs; i++) {
326             CHECK_V(r,
327                     ZSTD_compress(
328                             (BYTE *)compressedBuffer + off, CNBuffSize - off,
329                             (BYTE *)CNBuffer + segSize * i,
330                             segSize, 5));
331             off += r;
332             if (i == segs/2) {
333                 /* insert skippable frame */
334                 const U32 skipLen = 128 KB;
335                 MEM_writeLE32((BYTE*)compressedBuffer + off, ZSTD_MAGIC_SKIPPABLE_START);
336                 MEM_writeLE32((BYTE*)compressedBuffer + off + 4, skipLen);
337                 off += skipLen + ZSTD_skippableHeaderSize;
338             }
339         }
340         cSize = off;
341     }
342     DISPLAYLEVEL(4, "OK \n");
343
344     DISPLAYLEVEL(4, "test%3i : get decompressed size of multiple frames : ", testNb++);
345     {   unsigned long long const r = ZSTD_findDecompressedSize(compressedBuffer, cSize);
346         if (r != CNBuffSize / 2) goto _output_error; }
347     DISPLAYLEVEL(4, "OK \n");
348
349     DISPLAYLEVEL(4, "test%3i : decompress multiple frames : ", testNb++);
350     {   CHECK_V(r, ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize));
351         if (r != CNBuffSize / 2) goto _output_error; }
352     DISPLAYLEVEL(4, "OK \n");
353
354     DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
355     if (memcmp(decodedBuffer, CNBuffer, CNBuffSize / 2) != 0) goto _output_error;
356     DISPLAYLEVEL(4, "OK \n");
357
358     /* Dictionary and CCtx Duplication tests */
359     {   ZSTD_CCtx* const ctxOrig = ZSTD_createCCtx();
360         ZSTD_CCtx* const ctxDuplicated = ZSTD_createCCtx();
361         static const size_t dictSize = 551;
362
363         DISPLAYLEVEL(4, "test%3i : copy context too soon : ", testNb++);
364         { size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0);
365           if (!ZSTD_isError(copyResult)) goto _output_error; }   /* error must be detected */
366         DISPLAYLEVEL(4, "OK \n");
367
368         DISPLAYLEVEL(4, "test%3i : load dictionary into context : ", testNb++);
369         CHECK( ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2) );
370         CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0) ); /* Begin_usingDict implies unknown srcSize, so match that */
371         DISPLAYLEVEL(4, "OK \n");
372
373         DISPLAYLEVEL(4, "test%3i : compress with flat dictionary : ", testNb++);
374         cSize = 0;
375         CHECKPLUS(r, ZSTD_compressEnd(ctxOrig, compressedBuffer, ZSTD_compressBound(CNBuffSize),
376                                            (const char*)CNBuffer + dictSize, CNBuffSize - dictSize),
377                   cSize += r);
378         DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
379
380         DISPLAYLEVEL(4, "test%3i : frame built with flat dictionary should be decompressible : ", testNb++);
381         CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
382                                        decodedBuffer, CNBuffSize,
383                                        compressedBuffer, cSize,
384                                        CNBuffer, dictSize),
385                   if (r != CNBuffSize - dictSize) goto _output_error);
386         DISPLAYLEVEL(4, "OK \n");
387
388         DISPLAYLEVEL(4, "test%3i : compress with duplicated context : ", testNb++);
389         {   size_t const cSizeOrig = cSize;
390             cSize = 0;
391             CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(CNBuffSize),
392                                                (const char*)CNBuffer + dictSize, CNBuffSize - dictSize),
393                       cSize += r);
394             if (cSize != cSizeOrig) goto _output_error;   /* should be identical ==> same size */
395         }
396         DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
397
398         DISPLAYLEVEL(4, "test%3i : frame built with duplicated context should be decompressible : ", testNb++);
399         CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
400                                            decodedBuffer, CNBuffSize,
401                                            compressedBuffer, cSize,
402                                            CNBuffer, dictSize),
403                   if (r != CNBuffSize - dictSize) goto _output_error);
404         DISPLAYLEVEL(4, "OK \n");
405
406         DISPLAYLEVEL(4, "test%3i : decompress with DDict : ", testNb++);
407         {   ZSTD_DDict* const ddict = ZSTD_createDDict(CNBuffer, dictSize);
408             size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict);
409             if (r != CNBuffSize - dictSize) goto _output_error;
410             DISPLAYLEVEL(4, "OK (size of DDict : %u) \n", (U32)ZSTD_sizeof_DDict(ddict));
411             ZSTD_freeDDict(ddict);
412         }
413
414         DISPLAYLEVEL(4, "test%3i : decompress with static DDict : ", testNb++);
415         {   size_t const ddictBufferSize = ZSTD_estimateDDictSize(dictSize, 0);
416             void* ddictBuffer = malloc(ddictBufferSize);
417             if (ddictBuffer == NULL) goto _output_error;
418             {   ZSTD_DDict* const ddict = ZSTD_initStaticDDict(ddictBuffer, ddictBufferSize, CNBuffer, dictSize, 0);
419                 size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict);
420                 if (r != CNBuffSize - dictSize) goto _output_error;
421             }
422             free(ddictBuffer);
423             DISPLAYLEVEL(4, "OK (size of static DDict : %u) \n", (U32)ddictBufferSize);
424         }
425
426         DISPLAYLEVEL(4, "test%3i : check content size on duplicated context : ", testNb++);
427         {   size_t const testSize = CNBuffSize / 3;
428             {   ZSTD_parameters p = ZSTD_getParams(2, testSize, dictSize);
429                 p.fParams.contentSizeFlag = 1;
430                 CHECK( ZSTD_compressBegin_advanced(ctxOrig, CNBuffer, dictSize, p, testSize-1) );
431             }
432             CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, testSize) );
433
434             CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(testSize),
435                                           (const char*)CNBuffer + dictSize, testSize),
436                       cSize = r);
437             {   ZSTD_frameHeader fp;
438                 if (ZSTD_getFrameHeader(&fp, compressedBuffer, cSize)) goto _output_error;
439                 if ((fp.frameContentSize != testSize) && (fp.frameContentSize != 0)) goto _output_error;
440         }   }
441         DISPLAYLEVEL(4, "OK \n");
442
443         ZSTD_freeCCtx(ctxOrig);
444         ZSTD_freeCCtx(ctxDuplicated);
445     }
446
447     /* Dictionary and dictBuilder tests */
448     {   ZSTD_CCtx* const cctx = ZSTD_createCCtx();
449         size_t dictSize = 16 KB;
450         void* dictBuffer = malloc(dictSize);
451         size_t const totalSampleSize = 1 MB;
452         size_t const sampleUnitSize = 8 KB;
453         U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
454         size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
455         U32 dictID;
456
457         if (dictBuffer==NULL || samplesSizes==NULL) {
458             free(dictBuffer);
459             free(samplesSizes);
460             goto _output_error;
461         }
462
463         DISPLAYLEVEL(4, "test%3i : dictBuilder : ", testNb++);
464         { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
465         dictSize = ZDICT_trainFromBuffer(dictBuffer, dictSize,
466                                          CNBuffer, samplesSizes, nbSamples);
467         if (ZDICT_isError(dictSize)) goto _output_error;
468         DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize);
469
470         DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++);
471         dictID = ZDICT_getDictID(dictBuffer, dictSize);
472         if (dictID==0) goto _output_error;
473         DISPLAYLEVEL(4, "OK : %u \n", dictID);
474
475         DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++);
476         cSize = ZSTD_compress_usingDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
477                                         CNBuffer, CNBuffSize,
478                                         dictBuffer, dictSize, 4);
479         if (ZSTD_isError(cSize)) goto _output_error;
480         DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
481
482         DISPLAYLEVEL(4, "test%3i : retrieve dictID from dictionary : ", testNb++);
483         {   U32 const did = ZSTD_getDictID_fromDict(dictBuffer, dictSize);
484             if (did != dictID) goto _output_error;   /* non-conformant (content-only) dictionary */
485         }
486         DISPLAYLEVEL(4, "OK \n");
487
488         DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++);
489         {   U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
490             if (did != dictID) goto _output_error;   /* non-conformant (content-only) dictionary */
491         }
492         DISPLAYLEVEL(4, "OK \n");
493
494         DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++);
495         CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
496                                        decodedBuffer, CNBuffSize,
497                                        compressedBuffer, cSize,
498                                        dictBuffer, dictSize),
499                   if (r != CNBuffSize) goto _output_error);
500         DISPLAYLEVEL(4, "OK \n");
501
502         DISPLAYLEVEL(4, "test%3i : estimate CDict size : ", testNb++);
503         {   ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
504             size_t const estimatedSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, 1 /*byReference*/);
505             DISPLAYLEVEL(4, "OK : %u \n", (U32)estimatedSize);
506         }
507
508         DISPLAYLEVEL(4, "test%3i : compress with CDict ", testNb++);
509         {   ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
510             ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize,
511                                             1 /* byReference */, ZSTD_dm_auto,
512                                             cParams, ZSTD_defaultCMem);
513             DISPLAYLEVEL(4, "(size : %u) : ", (U32)ZSTD_sizeof_CDict(cdict));
514             cSize = ZSTD_compress_usingCDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
515                                                  CNBuffer, CNBuffSize, cdict);
516             ZSTD_freeCDict(cdict);
517             if (ZSTD_isError(cSize)) goto _output_error;
518         }
519         DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
520
521         DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++);
522         {   U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
523             if (did != dictID) goto _output_error;   /* non-conformant (content-only) dictionary */
524         }
525         DISPLAYLEVEL(4, "OK \n");
526
527         DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++);
528         CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
529                                        decodedBuffer, CNBuffSize,
530                                        compressedBuffer, cSize,
531                                        dictBuffer, dictSize),
532                   if (r != CNBuffSize) goto _output_error);
533         DISPLAYLEVEL(4, "OK \n");
534
535         DISPLAYLEVEL(4, "test%3i : compress with static CDict : ", testNb++);
536         {   ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
537             size_t const cdictSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, 0);
538             void* const cdictBuffer = malloc(cdictSize);
539             if (cdictBuffer==NULL) goto _output_error;
540             {   ZSTD_CDict* const cdict = ZSTD_initStaticCDict(cdictBuffer, cdictSize,
541                                             dictBuffer, dictSize,
542                                             0 /* by Reference */, ZSTD_dm_auto,
543                                             cParams);
544                 if (cdict == NULL) {
545                     DISPLAY("ZSTD_initStaticCDict failed ");
546                     goto _output_error;
547                 }
548                 cSize = ZSTD_compress_usingCDict(cctx,
549                                 compressedBuffer, ZSTD_compressBound(CNBuffSize),
550                                 CNBuffer, CNBuffSize, cdict);
551                 if (ZSTD_isError(cSize)) {
552                     DISPLAY("ZSTD_compress_usingCDict failed ");
553                     goto _output_error;
554             }   }
555             free(cdictBuffer);
556         }
557         DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
558
559         DISPLAYLEVEL(4, "test%3i : ZSTD_compress_usingCDict_advanced, no contentSize, no dictID : ", testNb++);
560         {   ZSTD_frameParameters const fParams = { 0 /* frameSize */, 1 /* checksum */, 1 /* noDictID*/ };
561             ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
562             ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, 1 /*byRef*/, ZSTD_dm_auto, cParams, ZSTD_defaultCMem);
563             cSize = ZSTD_compress_usingCDict_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
564                                                  CNBuffer, CNBuffSize, cdict, fParams);
565             ZSTD_freeCDict(cdict);
566             if (ZSTD_isError(cSize)) goto _output_error;
567         }
568         DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
569
570         DISPLAYLEVEL(4, "test%3i : try retrieving contentSize from frame : ", testNb++);
571         {   U64 const contentSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
572             if (contentSize != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
573         }
574         DISPLAYLEVEL(4, "OK (unknown)\n");
575
576         DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++);
577         CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
578                                        decodedBuffer, CNBuffSize,
579                                        compressedBuffer, cSize,
580                                        dictBuffer, dictSize),
581                   if (r != CNBuffSize) goto _output_error);
582         DISPLAYLEVEL(4, "OK \n");
583
584         DISPLAYLEVEL(4, "test%3i : ZSTD_compress_advanced, no dictID : ", testNb++);
585         {   ZSTD_parameters p = ZSTD_getParams(3, CNBuffSize, dictSize);
586             p.fParams.noDictIDFlag = 1;
587             cSize = ZSTD_compress_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
588                                            CNBuffer, CNBuffSize,
589                                            dictBuffer, dictSize, p);
590             if (ZSTD_isError(cSize)) goto _output_error;
591         }
592         DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
593
594         DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++);
595         CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
596                                        decodedBuffer, CNBuffSize,
597                                        compressedBuffer, cSize,
598                                        dictBuffer, dictSize),
599                   if (r != CNBuffSize) goto _output_error);
600         DISPLAYLEVEL(4, "OK \n");
601
602         DISPLAYLEVEL(4, "test%3i : dictionary containing only header should return error : ", testNb++);
603         {
604           const size_t ret = ZSTD_decompress_usingDict(
605               dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize,
606               "\x37\xa4\x30\xec\x11\x22\x33\x44", 8);
607           if (ZSTD_getErrorCode(ret) != ZSTD_error_dictionary_corrupted) goto _output_error;
608         }
609         DISPLAYLEVEL(4, "OK \n");
610
611         DISPLAYLEVEL(4, "test%3i : Building cdict w/ ZSTD_dm_fullDict on a good dictionary : ", testNb++);
612         {   ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
613             ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, 1 /*byRef*/, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem);
614             if (cdict==NULL) goto _output_error;
615             ZSTD_freeCDict(cdict);
616         }
617         DISPLAYLEVEL(4, "OK \n");
618
619         DISPLAYLEVEL(4, "test%3i : Building cdict w/ ZSTD_dm_fullDict on a rawContent (must fail) : ", testNb++);
620         {   ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
621             ZSTD_CDict* const cdict = ZSTD_createCDict_advanced((const char*)dictBuffer+1, dictSize-1, 1 /*byRef*/, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem);
622             if (cdict!=NULL) goto _output_error;
623             ZSTD_freeCDict(cdict);
624         }
625         DISPLAYLEVEL(4, "OK \n");
626
627         ZSTD_freeCCtx(cctx);
628         free(dictBuffer);
629         free(samplesSizes);
630     }
631
632     /* COVER dictionary builder tests */
633     {   ZSTD_CCtx* const cctx = ZSTD_createCCtx();
634         size_t dictSize = 16 KB;
635         size_t optDictSize = dictSize;
636         void* dictBuffer = malloc(dictSize);
637         size_t const totalSampleSize = 1 MB;
638         size_t const sampleUnitSize = 8 KB;
639         U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
640         size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
641         ZDICT_cover_params_t params;
642         U32 dictID;
643
644         if (dictBuffer==NULL || samplesSizes==NULL) {
645             free(dictBuffer);
646             free(samplesSizes);
647             goto _output_error;
648         }
649
650         DISPLAYLEVEL(4, "test%3i : ZDICT_trainFromBuffer_cover : ", testNb++);
651         { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
652         memset(&params, 0, sizeof(params));
653         params.d = 1 + (FUZ_rand(&seed) % 16);
654         params.k = params.d + (FUZ_rand(&seed) % 256);
655         dictSize = ZDICT_trainFromBuffer_cover(dictBuffer, dictSize,
656                                                CNBuffer, samplesSizes, nbSamples,
657                                                params);
658         if (ZDICT_isError(dictSize)) goto _output_error;
659         DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize);
660
661         DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++);
662         dictID = ZDICT_getDictID(dictBuffer, dictSize);
663         if (dictID==0) goto _output_error;
664         DISPLAYLEVEL(4, "OK : %u \n", dictID);
665
666         DISPLAYLEVEL(4, "test%3i : ZDICT_optimizeTrainFromBuffer_cover : ", testNb++);
667         memset(&params, 0, sizeof(params));
668         params.steps = 4;
669         optDictSize = ZDICT_optimizeTrainFromBuffer_cover(dictBuffer, optDictSize,
670                                                           CNBuffer, samplesSizes,
671                                                           nbSamples / 4, &params);
672         if (ZDICT_isError(optDictSize)) goto _output_error;
673         DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)optDictSize);
674
675         DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++);
676         dictID = ZDICT_getDictID(dictBuffer, optDictSize);
677         if (dictID==0) goto _output_error;
678         DISPLAYLEVEL(4, "OK : %u \n", dictID);
679
680         ZSTD_freeCCtx(cctx);
681         free(dictBuffer);
682         free(samplesSizes);
683     }
684
685     /* Decompression defense tests */
686     DISPLAYLEVEL(4, "test%3i : Check input length for magic number : ", testNb++);
687     { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 3);   /* too small input */
688       if (!ZSTD_isError(r)) goto _output_error;
689       if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
690     DISPLAYLEVEL(4, "OK \n");
691
692     DISPLAYLEVEL(4, "test%3i : Check magic Number : ", testNb++);
693     ((char*)(CNBuffer))[0] = 1;
694     { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 4);
695       if (!ZSTD_isError(r)) goto _output_error; }
696     DISPLAYLEVEL(4, "OK \n");
697
698     /* content size verification test */
699     DISPLAYLEVEL(4, "test%3i : Content size verification : ", testNb++);
700     {   ZSTD_CCtx* const cctx = ZSTD_createCCtx();
701         size_t const srcSize = 5000;
702         size_t const wrongSrcSize = (srcSize + 1000);
703         ZSTD_parameters params = ZSTD_getParams(1, wrongSrcSize, 0);
704         params.fParams.contentSizeFlag = 1;
705         CHECK( ZSTD_compressBegin_advanced(cctx, NULL, 0, params, wrongSrcSize) );
706         {   size_t const result = ZSTD_compressEnd(cctx, decodedBuffer, CNBuffSize, CNBuffer, srcSize);
707             if (!ZSTD_isError(result)) goto _output_error;
708             if (ZSTD_getErrorCode(result) != ZSTD_error_srcSize_wrong) goto _output_error;
709             DISPLAYLEVEL(4, "OK : %s \n", ZSTD_getErrorName(result));
710         }
711         ZSTD_freeCCtx(cctx);
712     }
713
714     /* block API tests */
715     {   ZSTD_CCtx* const cctx = ZSTD_createCCtx();
716         static const size_t dictSize = 65 KB;
717         static const size_t blockSize = 100 KB;   /* won't cause pb with small dict size */
718         size_t cSize2;
719
720         /* basic block compression */
721         DISPLAYLEVEL(4, "test%3i : Block compression test : ", testNb++);
722         CHECK( ZSTD_compressBegin(cctx, 5) );
723         CHECK( ZSTD_getBlockSize(cctx) >= blockSize);
724         cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize);
725         if (ZSTD_isError(cSize)) goto _output_error;
726         DISPLAYLEVEL(4, "OK \n");
727
728         DISPLAYLEVEL(4, "test%3i : Block decompression test : ", testNb++);
729         CHECK( ZSTD_decompressBegin(dctx) );
730         { CHECK_V(r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
731           if (r != blockSize) goto _output_error; }
732         DISPLAYLEVEL(4, "OK \n");
733
734         /* dictionary block compression */
735         DISPLAYLEVEL(4, "test%3i : Dictionary Block compression test : ", testNb++);
736         CHECK( ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5) );
737         cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize);
738         if (ZSTD_isError(cSize)) goto _output_error;
739         cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize+blockSize, blockSize);
740         if (ZSTD_isError(cSize2)) goto _output_error;
741         memcpy((char*)compressedBuffer+cSize, (char*)CNBuffer+dictSize+blockSize, blockSize);   /* fake non-compressed block */
742         cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize+blockSize, ZSTD_compressBound(blockSize),
743                                           (char*)CNBuffer+dictSize+2*blockSize, blockSize);
744         if (ZSTD_isError(cSize2)) goto _output_error;
745         DISPLAYLEVEL(4, "OK \n");
746
747         DISPLAYLEVEL(4, "test%3i : Dictionary Block decompression test : ", testNb++);
748         CHECK( ZSTD_decompressBegin_usingDict(dctx, CNBuffer, dictSize) );
749         { CHECK_V( r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
750           if (r != blockSize) goto _output_error; }
751         ZSTD_insertBlock(dctx, (char*)decodedBuffer+blockSize, blockSize);   /* insert non-compressed block into dctx history */
752         { CHECK_V( r, ZSTD_decompressBlock(dctx, (char*)decodedBuffer+2*blockSize, CNBuffSize, (char*)compressedBuffer+cSize+blockSize, cSize2) );
753           if (r != blockSize) goto _output_error; }
754         DISPLAYLEVEL(4, "OK \n");
755
756         ZSTD_freeCCtx(cctx);
757         ZSTD_freeDCtx(dctx);
758     }
759
760     /* long rle test */
761     {   size_t sampleSize = 0;
762         DISPLAYLEVEL(4, "test%3i : Long RLE test : ", testNb++);
763         RDG_genBuffer(CNBuffer, sampleSize, compressibility, 0., seed+1);
764         memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1);
765         sampleSize += 256 KB - 1;
766         RDG_genBuffer((char*)CNBuffer+sampleSize, 96 KB, compressibility, 0., seed+2);
767         sampleSize += 96 KB;
768         cSize = ZSTD_compress(compressedBuffer, ZSTD_compressBound(sampleSize), CNBuffer, sampleSize, 1);
769         if (ZSTD_isError(cSize)) goto _output_error;
770         { CHECK_V(regenSize, ZSTD_decompress(decodedBuffer, sampleSize, compressedBuffer, cSize));
771           if (regenSize!=sampleSize) goto _output_error; }
772         DISPLAYLEVEL(4, "OK \n");
773     }
774
775     /* All zeroes test (test bug #137) */
776     #define ZEROESLENGTH 100
777     DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH);
778     memset(CNBuffer, 0, ZEROESLENGTH);
779     { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1) );
780       cSize = r; }
781     DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100);
782
783     DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH);
784     { CHECK_V(r, ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize) );
785       if (r != ZEROESLENGTH) goto _output_error; }
786     DISPLAYLEVEL(4, "OK \n");
787
788     /* nbSeq limit test */
789     #define _3BYTESTESTLENGTH 131000
790     #define NB3BYTESSEQLOG   9
791     #define NB3BYTESSEQ     (1 << NB3BYTESSEQLOG)
792     #define NB3BYTESSEQMASK (NB3BYTESSEQ-1)
793     /* creates a buffer full of 3-bytes sequences */
794     {   BYTE _3BytesSeqs[NB3BYTESSEQ][3];
795         U32 rSeed = 1;
796
797         /* create batch of 3-bytes sequences */
798         {   int i;
799             for (i=0; i < NB3BYTESSEQ; i++) {
800                 _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&rSeed) & 255);
801                 _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&rSeed) & 255);
802                 _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&rSeed) & 255);
803         }   }
804
805         /* randomly fills CNBuffer with prepared 3-bytes sequences */
806         {   int i;
807             for (i=0; i < _3BYTESTESTLENGTH; i += 3) {   /* note : CNBuffer size > _3BYTESTESTLENGTH+3 */
808                 U32 const id = FUZ_rand(&rSeed) & NB3BYTESSEQMASK;
809                 ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0];
810                 ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1];
811                 ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2];
812     }   }   }
813     DISPLAYLEVEL(4, "test%3i : compress lots 3-bytes sequences : ", testNb++);
814     { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH),
815                                  CNBuffer, _3BYTESTESTLENGTH, 19) );
816       cSize = r; }
817     DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/_3BYTESTESTLENGTH*100);
818
819     DISPLAYLEVEL(4, "test%3i : decompress lots 3-bytes sequence : ", testNb++);
820     { CHECK_V(r, ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize) );
821       if (r != _3BYTESTESTLENGTH) goto _output_error; }
822     DISPLAYLEVEL(4, "OK \n");
823
824     /* findFrameCompressedSize on skippable frames */
825     DISPLAYLEVEL(4, "test%3i : frame compressed size of skippable frame : ", testNb++);
826     {   const char* frame = "\x50\x2a\x4d\x18\x05\x0\x0\0abcde";
827         size_t const frameSrcSize = 13;
828         if (ZSTD_findFrameCompressedSize(frame, frameSrcSize) != frameSrcSize) goto _output_error; }
829     DISPLAYLEVEL(4, "OK \n");
830
831     /* error string tests */
832     DISPLAYLEVEL(4, "test%3i : testing ZSTD error code strings : ", testNb++);
833     if (strcmp("No error detected", ZSTD_getErrorName((ZSTD_ErrorCode)(0-ZSTD_error_no_error))) != 0) goto _output_error;
834     if (strcmp("No error detected", ZSTD_getErrorString(ZSTD_error_no_error)) != 0) goto _output_error;
835     if (strcmp("Unspecified error code", ZSTD_getErrorString((ZSTD_ErrorCode)(0-ZSTD_error_GENERIC))) != 0) goto _output_error;
836     if (strcmp("Error (generic)", ZSTD_getErrorName((size_t)0-ZSTD_error_GENERIC)) != 0) goto _output_error;
837     if (strcmp("Error (generic)", ZSTD_getErrorString(ZSTD_error_GENERIC)) != 0) goto _output_error;
838     if (strcmp("No error detected", ZSTD_getErrorName(ZSTD_error_GENERIC)) != 0) goto _output_error;
839     DISPLAYLEVEL(4, "OK \n");
840
841 _end:
842     free(CNBuffer);
843     free(compressedBuffer);
844     free(decodedBuffer);
845     return testResult;
846
847 _output_error:
848     testResult = 1;
849     DISPLAY("Error detected in Unit tests ! \n");
850     goto _end;
851 }
852
853
854 static size_t findDiff(const void* buf1, const void* buf2, size_t max)
855 {
856     const BYTE* b1 = (const BYTE*)buf1;
857     const BYTE* b2 = (const BYTE*)buf2;
858     size_t u;
859     for (u=0; u<max; u++) {
860         if (b1[u] != b2[u]) break;
861     }
862     return u;
863 }
864
865
866 static ZSTD_parameters FUZ_makeParams(ZSTD_compressionParameters cParams, ZSTD_frameParameters fParams)
867 {
868     ZSTD_parameters params;
869     params.cParams = cParams;
870     params.fParams = fParams;
871     return params;
872 }
873
874 static size_t FUZ_rLogLength(U32* seed, U32 logLength)
875 {
876     size_t const lengthMask = ((size_t)1 << logLength) - 1;
877     return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
878 }
879
880 static size_t FUZ_randomLength(U32* seed, U32 maxLog)
881 {
882     U32 const logLength = FUZ_rand(seed) % maxLog;
883     return FUZ_rLogLength(seed, logLength);
884 }
885
886 #undef CHECK
887 #define CHECK(cond, ...) {                                    \
888     if (cond) {                                               \
889         DISPLAY("Error => ");                                 \
890         DISPLAY(__VA_ARGS__);                                 \
891         DISPLAY(" (seed %u, test nb %u)  \n", seed, testNb);  \
892         goto _output_error;                                   \
893 }   }
894
895 #define CHECK_Z(f) {                                          \
896     size_t const err = f;                                     \
897     if (ZSTD_isError(err)) {                                  \
898         DISPLAY("Error => %s : %s ",                          \
899                 #f, ZSTD_getErrorName(err));                  \
900         DISPLAY(" (seed %u, test nb %u)  \n", seed, testNb);  \
901         goto _output_error;                                   \
902 }   }
903
904
905 static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxDurationS, double compressibility, int bigTests)
906 {
907     static const U32 maxSrcLog = 23;
908     static const U32 maxSampleLog = 22;
909     size_t const srcBufferSize = (size_t)1<<maxSrcLog;
910     size_t const dstBufferSize = (size_t)1<<maxSampleLog;
911     size_t const cBufferSize   = ZSTD_compressBound(dstBufferSize);
912     BYTE* cNoiseBuffer[5];
913     BYTE* srcBuffer;   /* jumping pointer */
914     BYTE* const cBuffer = (BYTE*) malloc (cBufferSize);
915     BYTE* const dstBuffer = (BYTE*) malloc (dstBufferSize);
916     BYTE* const mirrorBuffer = (BYTE*) malloc (dstBufferSize);
917     ZSTD_CCtx* const refCtx = ZSTD_createCCtx();
918     ZSTD_CCtx* const ctx = ZSTD_createCCtx();
919     ZSTD_DCtx* const dctx = ZSTD_createDCtx();
920     U32 result = 0;
921     U32 testNb = 0;
922     U32 coreSeed = seed, lseed = 0;
923     clock_t const startClock = clock();
924     clock_t const maxClockSpan = maxDurationS * CLOCKS_PER_SEC;
925     int const cLevelLimiter = bigTests ? 3 : 2;
926
927     /* allocation */
928     cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
929     cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
930     cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
931     cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
932     cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
933     CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4]
934            || !dstBuffer || !mirrorBuffer || !cBuffer || !refCtx || !ctx || !dctx,
935            "Not enough memory, fuzzer tests cancelled");
936
937     /* Create initial samples */
938     RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed);    /* pure noise */
939     RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed);    /* barely compressible */
940     RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
941     RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed);    /* highly compressible */
942     RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed);    /* sparse content */
943     srcBuffer = cNoiseBuffer[2];
944
945     /* catch up testNb */
946     for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed);
947
948     /* main test loop */
949     for ( ; (testNb <= nbTests) || (FUZ_clockSpan(startClock) < maxClockSpan); testNb++ ) {
950         size_t sampleSize, maxTestSize, totalTestSize;
951         size_t cSize, totalCSize, totalGenSize;
952         U64 crcOrig;
953         BYTE* sampleBuffer;
954         const BYTE* dict;
955         size_t dictSize;
956
957         /* notification */
958         if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u    ", testNb, nbTests); }
959         else { DISPLAYUPDATE(2, "\r%6u          ", testNb); }
960
961         FUZ_rand(&coreSeed);
962         { U32 const prime1 = 2654435761U; lseed = coreSeed ^ prime1; }
963
964         /* srcBuffer selection [0-4] */
965         {   U32 buffNb = FUZ_rand(&lseed) & 0x7F;
966             if (buffNb & 7) buffNb=2;   /* most common : compressible (P) */
967             else {
968                 buffNb >>= 3;
969                 if (buffNb & 7) {
970                     const U32 tnb[2] = { 1, 3 };   /* barely/highly compressible */
971                     buffNb = tnb[buffNb >> 3];
972                 } else {
973                     const U32 tnb[2] = { 0, 4 };   /* not compressible / sparse */
974                     buffNb = tnb[buffNb >> 3];
975             }   }
976             srcBuffer = cNoiseBuffer[buffNb];
977         }
978
979         /* select src segment */
980         sampleSize = FUZ_randomLength(&lseed, maxSampleLog);
981
982         /* create sample buffer (to catch read error with valgrind & sanitizers)  */
983         sampleBuffer = (BYTE*)malloc(sampleSize);
984         CHECK(sampleBuffer==NULL, "not enough memory for sample buffer");
985         { size_t const sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
986           memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); }
987         crcOrig = XXH64(sampleBuffer, sampleSize, 0);
988
989         /* compression tests */
990         {   unsigned const cLevel =
991                     ( FUZ_rand(&lseed) %
992                      (ZSTD_maxCLevel() - (FUZ_highbit32((U32)sampleSize) / cLevelLimiter)) )
993                      + 1;
994             cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel);
995             CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed : %s", ZSTD_getErrorName(cSize));
996
997             /* compression failure test : too small dest buffer */
998             if (cSize > 3) {
999                 const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1;   /* no problem, as cSize > 4 (frameHeaderSizer) */
1000                 const size_t tooSmallSize = cSize - missing;
1001                 const U32 endMark = 0x4DC2B1A9;
1002                 memcpy(dstBuffer+tooSmallSize, &endMark, 4);
1003                 { size_t const errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel);
1004                   CHECK(!ZSTD_isError(errorCode), "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (U32)tooSmallSize, (U32)cSize); }
1005                 { U32 endCheck; memcpy(&endCheck, dstBuffer+tooSmallSize, 4);
1006                   CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow"); }
1007         }   }
1008
1009         /* Decompressed size test */
1010         {   unsigned long long const rSize = ZSTD_findDecompressedSize(cBuffer, cSize);
1011             CHECK(rSize != sampleSize, "decompressed size incorrect");
1012         }
1013
1014         /* frame header decompression test */
1015         {   ZSTD_frameHeader dParams;
1016             CHECK_Z( ZSTD_getFrameHeader(&dParams, cBuffer, cSize) );
1017             CHECK(dParams.frameContentSize != sampleSize, "Frame content size incorrect");
1018         }
1019
1020         /* successful decompression test */
1021         {   size_t const margin = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1;
1022             size_t const dSize = ZSTD_decompress(dstBuffer, sampleSize + margin, cBuffer, cSize);
1023             CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s) (srcSize : %u ; cSize : %u)", ZSTD_getErrorName(dSize), (U32)sampleSize, (U32)cSize);
1024             {   U64 const crcDest = XXH64(dstBuffer, sampleSize, 0);
1025                 CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (U32)findDiff(sampleBuffer, dstBuffer, sampleSize), (U32)sampleSize);
1026         }   }
1027
1028         free(sampleBuffer);   /* no longer useful after this point */
1029
1030         /* truncated src decompression test */
1031         {   size_t const missing = (FUZ_rand(&lseed) % (cSize-2)) + 1;   /* no problem, as cSize > 4 (frameHeaderSizer) */
1032             size_t const tooSmallSize = cSize - missing;
1033             void* cBufferTooSmall = malloc(tooSmallSize);   /* valgrind will catch read overflows */
1034             CHECK(cBufferTooSmall == NULL, "not enough memory !");
1035             memcpy(cBufferTooSmall, cBuffer, tooSmallSize);
1036             { size_t const errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize);
1037               CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); }
1038             free(cBufferTooSmall);
1039         }
1040
1041         /* too small dst decompression test */
1042         if (sampleSize > 3) {
1043             size_t const missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1;   /* no problem, as cSize > 4 (frameHeaderSizer) */
1044             size_t const tooSmallSize = sampleSize - missing;
1045             static const BYTE token = 0xA9;
1046             dstBuffer[tooSmallSize] = token;
1047             { size_t const errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize);
1048               CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (U32)errorCode, (U32)tooSmallSize); }
1049             CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow");
1050         }
1051
1052         /* noisy src decompression test */
1053         if (cSize > 6) {
1054             /* insert noise into src */
1055             {   U32 const maxNbBits = FUZ_highbit32((U32)(cSize-4));
1056                 size_t pos = 4;   /* preserve magic number (too easy to detect) */
1057                 for (;;) {
1058                     /* keep some original src */
1059                     {   U32 const nbBits = FUZ_rand(&lseed) % maxNbBits;
1060                         size_t const mask = (1<<nbBits) - 1;
1061                         size_t const skipLength = FUZ_rand(&lseed) & mask;
1062                         pos += skipLength;
1063                     }
1064                     if (pos <= cSize) break;
1065                     /* add noise */
1066                     {   U32 const nbBitsCodes = FUZ_rand(&lseed) % maxNbBits;
1067                         U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0;
1068                         size_t const mask = (1<<nbBits) - 1;
1069                         size_t const rNoiseLength = (FUZ_rand(&lseed) & mask) + 1;
1070                         size_t const noiseLength = MIN(rNoiseLength, cSize-pos);
1071                         size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength);
1072                         memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength);
1073                         pos += noiseLength;
1074             }   }   }
1075
1076             /* decompress noisy source */
1077             {   U32 const endMark = 0xA9B1C3D6;
1078                 memcpy(dstBuffer+sampleSize, &endMark, 4);
1079                 {   size_t const decompressResult = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize);
1080                     /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */
1081                     CHECK((!ZSTD_isError(decompressResult)) && (decompressResult>sampleSize),
1082                           "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (U32)decompressResult, (U32)sampleSize);
1083                 }
1084                 {   U32 endCheck; memcpy(&endCheck, dstBuffer+sampleSize, 4);
1085                     CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow");
1086         }   }   }   /* noisy src decompression test */
1087
1088         /*=====   Streaming compression test, scattered segments and dictionary   =====*/
1089
1090         {   U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
1091             U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
1092             int const cLevel = (FUZ_rand(&lseed) %
1093                                 (ZSTD_maxCLevel() -
1094                                  (MAX(testLog, dictLog) / cLevelLimiter))) +
1095                                1;
1096             maxTestSize = FUZ_rLogLength(&lseed, testLog);
1097             if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1;
1098
1099             dictSize = FUZ_rLogLength(&lseed, dictLog);   /* needed also for decompression */
1100             dict = srcBuffer + (FUZ_rand(&lseed) % (srcBufferSize - dictSize));
1101
1102             if (FUZ_rand(&lseed) & 0xF) {
1103                 CHECK_Z ( ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, cLevel) );
1104             } else {
1105                 ZSTD_compressionParameters const cPar = ZSTD_getCParams(cLevel, 0, dictSize);
1106                 ZSTD_frameParameters const fPar = { FUZ_rand(&lseed)&1 /* contentSizeFlag */,
1107                                                     !(FUZ_rand(&lseed)&3) /* contentChecksumFlag*/,
1108                                                     0 /*NodictID*/ };   /* note : since dictionary is fake, dictIDflag has no impact */
1109                 ZSTD_parameters const p = FUZ_makeParams(cPar, fPar);
1110                 CHECK_Z ( ZSTD_compressBegin_advanced(refCtx, dict, dictSize, p, 0) );
1111             }
1112             CHECK_Z( ZSTD_copyCCtx(ctx, refCtx, 0) );
1113         }
1114         ZSTD_setCCtxParameter(ctx, ZSTD_p_forceWindow, FUZ_rand(&lseed) & 1);
1115
1116         {   U32 const nbChunks = (FUZ_rand(&lseed) & 127) + 2;
1117             U32 n;
1118             XXH64_state_t xxhState;
1119             XXH64_reset(&xxhState, 0);
1120             for (totalTestSize=0, cSize=0, n=0 ; n<nbChunks ; n++) {
1121                 size_t const segmentSize = FUZ_randomLength(&lseed, maxSampleLog);
1122                 size_t const segmentStart = FUZ_rand(&lseed) % (srcBufferSize - segmentSize);
1123
1124                 if (cBufferSize-cSize < ZSTD_compressBound(segmentSize)) break;   /* avoid invalid dstBufferTooSmall */
1125                 if (totalTestSize+segmentSize > maxTestSize) break;
1126
1127                 {   size_t const compressResult = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+segmentStart, segmentSize);
1128                     CHECK (ZSTD_isError(compressResult), "multi-segments compression error : %s", ZSTD_getErrorName(compressResult));
1129                     cSize += compressResult;
1130                 }
1131                 XXH64_update(&xxhState, srcBuffer+segmentStart, segmentSize);
1132                 memcpy(mirrorBuffer + totalTestSize, srcBuffer+segmentStart, segmentSize);
1133                 totalTestSize += segmentSize;
1134             }
1135
1136             {   size_t const flushResult = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize, NULL, 0);
1137                 CHECK (ZSTD_isError(flushResult), "multi-segments epilogue error : %s", ZSTD_getErrorName(flushResult));
1138                 cSize += flushResult;
1139             }
1140             crcOrig = XXH64_digest(&xxhState);
1141         }
1142
1143         /* streaming decompression test */
1144         if (dictSize<8) dictSize=0, dict=NULL;   /* disable dictionary */
1145         CHECK_Z( ZSTD_decompressBegin_usingDict(dctx, dict, dictSize) );
1146         totalCSize = 0;
1147         totalGenSize = 0;
1148         while (totalCSize < cSize) {
1149             size_t const inSize = ZSTD_nextSrcSizeToDecompress(dctx);
1150             size_t const genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize);
1151             CHECK (ZSTD_isError(genSize), "ZSTD_decompressContinue error : %s", ZSTD_getErrorName(genSize));
1152             totalGenSize += genSize;
1153             totalCSize += inSize;
1154         }
1155         CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded");
1156         CHECK (totalGenSize != totalTestSize, "streaming decompressed data : wrong size")
1157         CHECK (totalCSize != cSize, "compressed data should be fully read")
1158         {   U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1159             if (crcDest!=crcOrig) {
1160                 size_t const errorPos = findDiff(mirrorBuffer, dstBuffer, totalTestSize);
1161                 CHECK (1, "streaming decompressed data corrupted : byte %u / %u  (%02X!=%02X)",
1162                    (U32)errorPos, (U32)totalTestSize, dstBuffer[errorPos], mirrorBuffer[errorPos]);
1163         }   }
1164     }   /* for ( ; (testNb <= nbTests) */
1165     DISPLAY("\r%u fuzzer tests completed   \n", testNb-1);
1166
1167 _cleanup:
1168     ZSTD_freeCCtx(refCtx);
1169     ZSTD_freeCCtx(ctx);
1170     ZSTD_freeDCtx(dctx);
1171     free(cNoiseBuffer[0]);
1172     free(cNoiseBuffer[1]);
1173     free(cNoiseBuffer[2]);
1174     free(cNoiseBuffer[3]);
1175     free(cNoiseBuffer[4]);
1176     free(cBuffer);
1177     free(dstBuffer);
1178     free(mirrorBuffer);
1179     return result;
1180
1181 _output_error:
1182     result = 1;
1183     goto _cleanup;
1184 }
1185
1186
1187 /*_*******************************************************
1188 *  Command line
1189 *********************************************************/
1190 static int FUZ_usage(const char* programName)
1191 {
1192     DISPLAY( "Usage :\n");
1193     DISPLAY( "      %s [args]\n", programName);
1194     DISPLAY( "\n");
1195     DISPLAY( "Arguments :\n");
1196     DISPLAY( " -i#    : Nb of tests (default:%u) \n", nbTestsDefault);
1197     DISPLAY( " -s#    : Select seed (default:prompt user)\n");
1198     DISPLAY( " -t#    : Select starting test number (default:0)\n");
1199     DISPLAY( " -P#    : Select compressibility in %% (default:%u%%)\n", FUZ_compressibility_default);
1200     DISPLAY( " -v     : verbose\n");
1201     DISPLAY( " -p     : pause at the end\n");
1202     DISPLAY( " -h     : display help and exit\n");
1203     return 0;
1204 }
1205
1206 /*! readU32FromChar() :
1207     @return : unsigned integer value read from input in `char` format
1208     allows and interprets K, KB, KiB, M, MB and MiB suffix.
1209     Will also modify `*stringPtr`, advancing it to position where it stopped reading.
1210     Note : function result can overflow if digit string > MAX_UINT */
1211 static unsigned readU32FromChar(const char** stringPtr)
1212 {
1213     unsigned result = 0;
1214     while ((**stringPtr >='0') && (**stringPtr <='9'))
1215         result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
1216     if ((**stringPtr=='K') || (**stringPtr=='M')) {
1217         result <<= 10;
1218         if (**stringPtr=='M') result <<= 10;
1219         (*stringPtr)++ ;
1220         if (**stringPtr=='i') (*stringPtr)++;
1221         if (**stringPtr=='B') (*stringPtr)++;
1222     }
1223     return result;
1224 }
1225
1226 int main(int argc, const char** argv)
1227 {
1228     U32 seed = 0;
1229     int seedset = 0;
1230     int argNb;
1231     int nbTests = nbTestsDefault;
1232     int testNb = 0;
1233     U32 proba = FUZ_compressibility_default;
1234     int result = 0;
1235     U32 mainPause = 0;
1236     U32 maxDuration = 0;
1237     int bigTests = 1;
1238     const char* const programName = argv[0];
1239
1240     /* Check command line */
1241     for (argNb=1; argNb<argc; argNb++) {
1242         const char* argument = argv[argNb];
1243         if(!argument) continue;   /* Protection if argument empty */
1244
1245         /* Handle commands. Aggregated commands are allowed */
1246         if (argument[0]=='-') {
1247
1248             if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
1249
1250             argument++;
1251             while (*argument!=0) {
1252                 switch(*argument)
1253                 {
1254                 case 'h':
1255                     return FUZ_usage(programName);
1256
1257                 case 'v':
1258                     argument++;
1259                     g_displayLevel = 4;
1260                     break;
1261
1262                 case 'q':
1263                     argument++;
1264                     g_displayLevel--;
1265                     break;
1266
1267                 case 'p': /* pause at the end */
1268                     argument++;
1269                     mainPause = 1;
1270                     break;
1271
1272                 case 'i':
1273                     argument++; maxDuration = 0;
1274                     nbTests = readU32FromChar(&argument);
1275                     break;
1276
1277                 case 'T':
1278                     argument++;
1279                     nbTests = 0;
1280                     maxDuration = readU32FromChar(&argument);
1281                     if (*argument=='s') argument++;   /* seconds */
1282                     if (*argument=='m') maxDuration *= 60, argument++;   /* minutes */
1283                     if (*argument=='n') argument++;
1284                     break;
1285
1286                 case 's':
1287                     argument++;
1288                     seedset = 1;
1289                     seed = readU32FromChar(&argument);
1290                     break;
1291
1292                 case 't':
1293                     argument++;
1294                     testNb = readU32FromChar(&argument);
1295                     break;
1296
1297                 case 'P':   /* compressibility % */
1298                     argument++;
1299                     proba = readU32FromChar(&argument);
1300                     if (proba>100) proba = 100;
1301                     break;
1302
1303                 default:
1304                     return (FUZ_usage(programName), 1);
1305     }   }   }   }   /* for (argNb=1; argNb<argc; argNb++) */
1306
1307     /* Get Seed */
1308     DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
1309
1310     if (!seedset) {
1311         time_t const t = time(NULL);
1312         U32 const h = XXH32(&t, sizeof(t), 1);
1313         seed = h % 10000;
1314     }
1315
1316     DISPLAY("Seed = %u\n", seed);
1317     if (proba!=FUZ_compressibility_default) DISPLAY("Compressibility : %u%%\n", proba);
1318
1319     if (nbTests < testNb) nbTests = testNb;
1320
1321     if (testNb==0)
1322         result = basicUnitTests(0, ((double)proba) / 100);  /* constant seed for predictability */
1323     if (!result)
1324         result = fuzzerTests(seed, nbTests, testNb, maxDuration, ((double)proba) / 100, bigTests);
1325     if (mainPause) {
1326         int unused;
1327         DISPLAY("Press Enter \n");
1328         unused = getchar();
1329         (void)unused;
1330     }
1331     return result;
1332 }