]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - zbufftest.c
8cbde3f4f38e5d10e6430b041cb8033e049840b2
[FreeBSD/FreeBSD.git] / zbufftest.c
1 /*
2  * Copyright (c) 2015-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  * You may select, at your option, one of the above-listed licenses.
9  */
10
11
12 /*-************************************
13 *  Compiler specific
14 **************************************/
15 #ifdef _MSC_VER    /* Visual Studio */
16 #  define _CRT_SECURE_NO_WARNINGS     /* fgets */
17 #  pragma warning(disable : 4127)     /* disable: C4127: conditional expression is constant */
18 #  pragma warning(disable : 4146)     /* disable: C4146: minus unsigned expression */
19 #endif
20
21
22 /*-************************************
23 *  Includes
24 **************************************/
25 #include <stdlib.h>       /* free */
26 #include <stdio.h>        /* fgets, sscanf */
27 #include <string.h>       /* strcmp */
28 #include "timefn.h"       /* UTIL_time_t */
29 #include "mem.h"
30 #define ZSTD_STATIC_LINKING_ONLY   /* ZSTD_maxCLevel */
31 #include "zstd.h"         /* ZSTD_compressBound */
32 #define ZBUFF_STATIC_LINKING_ONLY  /* ZBUFF_createCCtx_advanced */
33 #include "zbuff.h"        /* ZBUFF_isError */
34 #include "datagen.h"      /* RDG_genBuffer */
35 #define XXH_STATIC_LINKING_ONLY
36 #include "xxhash.h"       /* XXH64_* */
37 #include "util.h"
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 nbTestsDefault = 10000;
48 #define COMPRESSIBLE_NOISE_LENGTH (10 MB)
49 #define FUZ_COMPRESSIBILITY_DEFAULT 50
50 static const U32 prime1 = 2654435761U;
51 static const U32 prime2 = 2246822519U;
52
53
54
55 /*-************************************
56 *  Display Macros
57 **************************************/
58 #define DISPLAY(...)          fprintf(stderr, __VA_ARGS__)
59 #define DISPLAYLEVEL(l, ...)  if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
60 static U32 g_displayLevel = 2;
61
62 static const U64 g_refreshRate = SEC_TO_MICRO / 6;
63 static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
64
65 #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
66             if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
67             { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
68             if (g_displayLevel>=4) fflush(stderr); } }
69
70 static U64 g_clockTime = 0;
71
72
73 /*-*******************************************************
74 *  Fuzzer functions
75 *********************************************************/
76 #undef MIN
77 #undef MAX
78 #define MIN(a,b) ((a)<(b)?(a):(b))
79 #define MAX(a,b) ((a)>(b)?(a):(b))
80 /*! FUZ_rand() :
81     @return : a 27 bits random value, from a 32-bits `seed`.
82     `seed` is also modified */
83 #  define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
84 unsigned int FUZ_rand(unsigned int* seedPtr)
85 {
86     U32 rand32 = *seedPtr;
87     rand32 *= prime1;
88     rand32 += prime2;
89     rand32  = FUZ_rotl32(rand32, 13);
90     *seedPtr = rand32;
91     return rand32 >> 5;
92 }
93
94
95 /*
96 static unsigned FUZ_highbit32(U32 v32)
97 {
98     unsigned nbBits = 0;
99     if (v32==0) return 0;
100     for ( ; v32 ; v32>>=1) nbBits++;
101     return nbBits;
102 }
103 */
104
105 static void* ZBUFF_allocFunction(void* opaque, size_t size)
106 {
107     void* address = malloc(size);
108     (void)opaque;
109     /* DISPLAYLEVEL(4, "alloc %p, %d opaque=%p \n", address, (int)size, opaque); */
110     return address;
111 }
112
113 static void ZBUFF_freeFunction(void* opaque, void* address)
114 {
115     (void)opaque;
116     /* if (address) DISPLAYLEVEL(4, "free %p opaque=%p \n", address, opaque); */
117     free(address);
118 }
119
120 static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem)
121 {
122     int testResult = 0;
123     size_t CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
124     void* CNBuffer = malloc(CNBufferSize);
125     size_t const skippableFrameSize = 11;
126     size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
127     void* compressedBuffer = malloc(compressedBufferSize);
128     size_t const decodedBufferSize = CNBufferSize;
129     void* decodedBuffer = malloc(decodedBufferSize);
130     size_t cSize, readSize, readSkipSize, genSize;
131     U32 testNb=0;
132     ZBUFF_CCtx* zc = ZBUFF_createCCtx_advanced(customMem);
133     ZBUFF_DCtx* zd = ZBUFF_createDCtx_advanced(customMem);
134
135     /* Create compressible test buffer */
136     if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
137         DISPLAY("Not enough memory, aborting\n");
138         goto _output_error;
139     }
140     RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
141
142     /* generate skippable frame */
143     MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
144     MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
145     cSize = skippableFrameSize + 8;
146
147     /* Basic compression test */
148     DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
149     ZBUFF_compressInitDictionary(zc, CNBuffer, 128 KB, 1);
150     readSize = CNBufferSize;
151     genSize = compressedBufferSize;
152     { size_t const r = ZBUFF_compressContinue(zc, ((char*)compressedBuffer)+cSize, &genSize, CNBuffer, &readSize);
153       if (ZBUFF_isError(r)) goto _output_error; }
154     if (readSize != CNBufferSize) goto _output_error;   /* entire input should be consumed */
155     cSize += genSize;
156     genSize = compressedBufferSize - cSize;
157     { size_t const r = ZBUFF_compressEnd(zc, ((char*)compressedBuffer)+cSize, &genSize);
158       if (r != 0) goto _output_error; }  /* error, or some data not flushed */
159     cSize += genSize;
160     DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
161
162     /* skippable frame test */
163     DISPLAYLEVEL(4, "test%3i : decompress skippable frame : ", testNb++);
164     ZBUFF_decompressInitDictionary(zd, CNBuffer, 128 KB);
165     readSkipSize = cSize;
166     genSize = CNBufferSize;
167     { size_t const r = ZBUFF_decompressContinue(zd, decodedBuffer, &genSize, compressedBuffer, &readSkipSize);
168       if (r != 0) goto _output_error; }
169     if (genSize != 0) goto _output_error;   /* skippable frame len is 0 */
170     DISPLAYLEVEL(4, "OK \n");
171
172     /* Basic decompression test */
173     DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
174     ZBUFF_decompressInitDictionary(zd, CNBuffer, 128 KB);
175     readSize = cSize - readSkipSize;
176     genSize = CNBufferSize;
177     { size_t const r = ZBUFF_decompressContinue(zd, decodedBuffer, &genSize, ((char*)compressedBuffer)+readSkipSize, &readSize);
178       if (r != 0) goto _output_error; }  /* should reach end of frame == 0; otherwise, some data left, or an error */
179     if (genSize != CNBufferSize) goto _output_error;   /* should regenerate the same amount */
180     if (readSize+readSkipSize != cSize) goto _output_error;   /* should have read the entire frame */
181     DISPLAYLEVEL(4, "OK \n");
182
183     /* check regenerated data is byte exact */
184     DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
185     {   size_t i;
186         for (i=0; i<CNBufferSize; i++) {
187             if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
188     }   }
189     DISPLAYLEVEL(4, "OK \n");
190
191     /* Byte-by-byte decompression test */
192     DISPLAYLEVEL(4, "test%3i : decompress byte-by-byte : ", testNb++);
193     {   size_t r, pIn=0, pOut=0;
194         do
195         {   ZBUFF_decompressInitDictionary(zd, CNBuffer, 128 KB);
196             r = 1;
197             while (r) {
198                 size_t inS = 1;
199                 size_t outS = 1;
200                 r = ZBUFF_decompressContinue(zd, ((BYTE*)decodedBuffer)+pOut, &outS, ((BYTE*)compressedBuffer)+pIn, &inS);
201                 pIn += inS;
202                 pOut += outS;
203             }
204             readSize = pIn;
205             genSize = pOut;
206         } while (genSize==0);
207     }
208     if (genSize != CNBufferSize) goto _output_error;   /* should regenerate the same amount */
209     if (readSize != cSize) goto _output_error;   /* should have read the entire frame */
210     DISPLAYLEVEL(4, "OK \n");
211
212     /* check regenerated data is byte exact */
213     DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
214     {   size_t i;
215         for (i=0; i<CNBufferSize; i++) {
216             if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
217     }   }
218     DISPLAYLEVEL(4, "OK \n");
219
220 _end:
221     ZBUFF_freeCCtx(zc);
222     ZBUFF_freeDCtx(zd);
223     free(CNBuffer);
224     free(compressedBuffer);
225     free(decodedBuffer);
226     return testResult;
227
228 _output_error:
229     testResult = 1;
230     DISPLAY("Error detected in Unit tests ! \n");
231     goto _end;
232 }
233
234
235 static size_t findDiff(const void* buf1, const void* buf2, size_t max)
236 {
237     const BYTE* b1 = (const BYTE*)buf1;
238     const BYTE* b2 = (const BYTE*)buf2;
239     size_t u;
240     for (u=0; u<max; u++) {
241         if (b1[u] != b2[u]) break;
242     }
243     return u;
244 }
245
246 static size_t FUZ_rLogLength(U32* seed, U32 logLength)
247 {
248     size_t const lengthMask = ((size_t)1 << logLength) - 1;
249     return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
250 }
251
252 static size_t FUZ_randomLength(U32* seed, U32 maxLog)
253 {
254     U32 const logLength = FUZ_rand(seed) % maxLog;
255     return FUZ_rLogLength(seed, logLength);
256 }
257
258 #define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
259                          DISPLAY(" (seed %u, test nb %u)  \n", seed, testNb); goto _output_error; }
260
261 static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
262 {
263     static const U32 maxSrcLog = 24;
264     static const U32 maxSampleLog = 19;
265     BYTE* cNoiseBuffer[5];
266     size_t const srcBufferSize = (size_t)1<<maxSrcLog;
267     BYTE* copyBuffer;
268     size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
269     BYTE* cBuffer;
270     size_t const cBufferSize   = ZSTD_compressBound(srcBufferSize);
271     BYTE* dstBuffer;
272     size_t dstBufferSize = srcBufferSize;
273     U32 result = 0;
274     U32 testNb = 0;
275     U32 coreSeed = seed;
276     ZBUFF_CCtx* zc;
277     ZBUFF_DCtx* zd;
278     UTIL_time_t startClock = UTIL_getTime();
279
280     /* allocations */
281     zc = ZBUFF_createCCtx();
282     zd = ZBUFF_createDCtx();
283     cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
284     cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
285     cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
286     cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
287     cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
288     copyBuffer= (BYTE*)malloc (copyBufferSize);
289     dstBuffer = (BYTE*)malloc (dstBufferSize);
290     cBuffer   = (BYTE*)malloc (cBufferSize);
291     CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
292            !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd,
293            "Not enough memory, fuzzer tests cancelled");
294
295     /* Create initial samples */
296     RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed);    /* pure noise */
297     RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed);    /* barely compressible */
298     RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
299     RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed);    /* highly compressible */
300     RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed);    /* sparse content */
301     memset(copyBuffer, 0x65, copyBufferSize);                             /* make copyBuffer considered initialized */
302
303     /* catch up testNb */
304     for (testNb=1; testNb < startTest; testNb++)
305         FUZ_rand(&coreSeed);
306
307     /* test loop */
308     for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) {
309         U32 lseed;
310         const BYTE* srcBuffer;
311         const BYTE* dict;
312         size_t maxTestSize, dictSize;
313         size_t cSize, totalTestSize, totalCSize, totalGenSize;
314         size_t errorCode;
315         U32 n, nbChunks;
316         XXH64_state_t xxhState;
317         U64 crcOrig;
318
319         /* init */
320         DISPLAYUPDATE(2, "\r%6u", testNb);
321         if (nbTests >= testNb) DISPLAYUPDATE(2, "/%6u   ", nbTests);
322         FUZ_rand(&coreSeed);
323         lseed = coreSeed ^ prime1;
324
325         /* states full reset (unsynchronized) */
326         /* some issues only happen when reusing states in a specific sequence of parameters */
327         if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZBUFF_freeCCtx(zc); zc = ZBUFF_createCCtx(); }
328         if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZBUFF_freeDCtx(zd); zd = ZBUFF_createDCtx(); }
329
330         /* srcBuffer selection [0-4] */
331         {   U32 buffNb = FUZ_rand(&lseed) & 0x7F;
332             if (buffNb & 7) buffNb=2;   /* most common : compressible (P) */
333             else {
334                 buffNb >>= 3;
335                 if (buffNb & 7) {
336                     const U32 tnb[2] = { 1, 3 };   /* barely/highly compressible */
337                     buffNb = tnb[buffNb >> 3];
338                 } else {
339                     const U32 tnb[2] = { 0, 4 };   /* not compressible / sparse */
340                     buffNb = tnb[buffNb >> 3];
341             }   }
342             srcBuffer = cNoiseBuffer[buffNb];
343         }
344
345         /* compression init */
346         {   U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
347             U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
348             maxTestSize = FUZ_rLogLength(&lseed, testLog);
349             dictSize  = (FUZ_rand(&lseed)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0;
350             /* random dictionary selection */
351             {   size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
352                 dict = srcBuffer + dictStart;
353             }
354             {   ZSTD_parameters params = ZSTD_getParams(cLevel, 0, dictSize);
355                 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
356                 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
357                 {   size_t const initError = ZBUFF_compressInit_advanced(zc, dict, dictSize, params, ZSTD_CONTENTSIZE_UNKNOWN);
358                     CHECK (ZBUFF_isError(initError),"init error : %s", ZBUFF_getErrorName(initError));
359         }   }   }
360
361         /* multi-segments compression test */
362         XXH64_reset(&xxhState, 0);
363         nbChunks    = (FUZ_rand(&lseed) & 127) + 2;
364         for (n=0, cSize=0, totalTestSize=0 ; (n<nbChunks) && (totalTestSize < maxTestSize) ; n++) {
365             /* compress random chunk into random size dst buffer */
366             {   size_t readChunkSize = FUZ_randomLength(&lseed, maxSampleLog);
367                 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
368                 size_t dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
369                 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - readChunkSize);
370
371                 size_t const compressionError = ZBUFF_compressContinue(zc, cBuffer+cSize, &dstBuffSize, srcBuffer+srcStart, &readChunkSize);
372                 CHECK (ZBUFF_isError(compressionError), "compression error : %s", ZBUFF_getErrorName(compressionError));
373
374                 XXH64_update(&xxhState, srcBuffer+srcStart, readChunkSize);
375                 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, readChunkSize);
376                 cSize += dstBuffSize;
377                 totalTestSize += readChunkSize;
378             }
379
380             /* random flush operation, to mess around */
381             if ((FUZ_rand(&lseed) & 15) == 0) {
382                 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
383                 size_t dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
384                 size_t const flushError = ZBUFF_compressFlush(zc, cBuffer+cSize, &dstBuffSize);
385                 CHECK (ZBUFF_isError(flushError), "flush error : %s", ZBUFF_getErrorName(flushError));
386                 cSize += dstBuffSize;
387         }   }
388
389         /* final frame epilogue */
390         {   size_t remainingToFlush = (size_t)(-1);
391             while (remainingToFlush) {
392                 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
393                 size_t dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
394                 U32 const enoughDstSize = dstBuffSize >= remainingToFlush;
395                 remainingToFlush = ZBUFF_compressEnd(zc, cBuffer+cSize, &dstBuffSize);
396                 CHECK (ZBUFF_isError(remainingToFlush), "flush error : %s", ZBUFF_getErrorName(remainingToFlush));
397                 CHECK (enoughDstSize && remainingToFlush, "ZBUFF_compressEnd() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush);
398                 cSize += dstBuffSize;
399         }   }
400         crcOrig = XXH64_digest(&xxhState);
401
402         /* multi - fragments decompression test */
403         ZBUFF_decompressInitDictionary(zd, dict, dictSize);
404         errorCode = 1;
405         for (totalCSize = 0, totalGenSize = 0 ; errorCode ; ) {
406             size_t readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
407             size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
408             size_t dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
409             errorCode = ZBUFF_decompressContinue(zd, dstBuffer+totalGenSize, &dstBuffSize, cBuffer+totalCSize, &readCSrcSize);
410             CHECK (ZBUFF_isError(errorCode), "decompression error : %s", ZBUFF_getErrorName(errorCode));
411             totalGenSize += dstBuffSize;
412             totalCSize += readCSrcSize;
413         }
414         CHECK (errorCode != 0, "frame not fully decoded");
415         CHECK (totalGenSize != totalTestSize, "decompressed data : wrong size")
416         CHECK (totalCSize != cSize, "compressed data should be fully read")
417         { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
418           if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
419           CHECK (crcDest!=crcOrig, "decompressed data corrupted"); }
420
421         /*=====   noisy/erroneous src decompression test   =====*/
422
423         /* add some noise */
424         {   U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
425             U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
426                 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
427                 size_t const noiseSize  = MIN((cSize/3) , randomNoiseSize);
428                 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
429                 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
430                 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
431         }   }
432
433         /* try decompression on noisy data */
434         ZBUFF_decompressInit(zd);
435         totalCSize = 0;
436         totalGenSize = 0;
437         while ( (totalCSize < cSize) && (totalGenSize < dstBufferSize) ) {
438             size_t readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
439             size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
440             size_t dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
441             size_t const decompressError = ZBUFF_decompressContinue(zd, dstBuffer+totalGenSize, &dstBuffSize, cBuffer+totalCSize, &readCSrcSize);
442             if (ZBUFF_isError(decompressError)) break;   /* error correctly detected */
443             totalGenSize += dstBuffSize;
444             totalCSize += readCSrcSize;
445     }   }
446     DISPLAY("\r%u fuzzer tests completed   \n", testNb);
447
448 _cleanup:
449     ZBUFF_freeCCtx(zc);
450     ZBUFF_freeDCtx(zd);
451     free(cNoiseBuffer[0]);
452     free(cNoiseBuffer[1]);
453     free(cNoiseBuffer[2]);
454     free(cNoiseBuffer[3]);
455     free(cNoiseBuffer[4]);
456     free(copyBuffer);
457     free(cBuffer);
458     free(dstBuffer);
459     return result;
460
461 _output_error:
462     result = 1;
463     goto _cleanup;
464 }
465
466
467 /*-*******************************************************
468 *  Command line
469 *********************************************************/
470 int FUZ_usage(const char* programName)
471 {
472     DISPLAY( "Usage :\n");
473     DISPLAY( "      %s [args]\n", programName);
474     DISPLAY( "\n");
475     DISPLAY( "Arguments :\n");
476     DISPLAY( " -i#    : Nb of tests (default:%u) \n", nbTestsDefault);
477     DISPLAY( " -s#    : Select seed (default:prompt user)\n");
478     DISPLAY( " -t#    : Select starting test number (default:0)\n");
479     DISPLAY( " -P#    : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
480     DISPLAY( " -v     : verbose\n");
481     DISPLAY( " -p     : pause at the end\n");
482     DISPLAY( " -h     : display help and exit\n");
483     return 0;
484 }
485
486
487 int main(int argc, const char** argv)
488 {
489     U32 seed=0;
490     int seedset=0;
491     int argNb;
492     int nbTests = nbTestsDefault;
493     int testNb = 0;
494     int proba = FUZ_COMPRESSIBILITY_DEFAULT;
495     int result=0;
496     U32 mainPause = 0;
497     const char* programName = argv[0];
498     ZSTD_customMem customMem = { ZBUFF_allocFunction, ZBUFF_freeFunction, NULL };
499     ZSTD_customMem customNULL = { NULL, NULL, NULL };
500
501     /* Check command line */
502     for(argNb=1; argNb<argc; argNb++) {
503         const char* argument = argv[argNb];
504         if(!argument) continue;   /* Protection if argument empty */
505
506         /* Parsing commands. Aggregated commands are allowed */
507         if (argument[0]=='-') {
508             argument++;
509
510             while (*argument!=0) {
511                 switch(*argument)
512                 {
513                 case 'h':
514                     return FUZ_usage(programName);
515                 case 'v':
516                     argument++;
517                     g_displayLevel=4;
518                     break;
519                 case 'q':
520                     argument++;
521                     g_displayLevel--;
522                     break;
523                 case 'p': /* pause at the end */
524                     argument++;
525                     mainPause = 1;
526                     break;
527
528                 case 'i':
529                     argument++;
530                     nbTests=0; g_clockTime=0;
531                     while ((*argument>='0') && (*argument<='9')) {
532                         nbTests *= 10;
533                         nbTests += *argument - '0';
534                         argument++;
535                     }
536                     break;
537
538                 case 'T':
539                     argument++;
540                     nbTests=0; g_clockTime=0;
541                     while ((*argument>='0') && (*argument<='9')) {
542                         g_clockTime *= 10;
543                         g_clockTime += *argument - '0';
544                         argument++;
545                     }
546                     if (*argument=='m') g_clockTime *=60, argument++;
547                     if (*argument=='n') argument++;
548                     g_clockTime *= SEC_TO_MICRO;
549                     break;
550
551                 case 's':
552                     argument++;
553                     seed=0;
554                     seedset=1;
555                     while ((*argument>='0') && (*argument<='9')) {
556                         seed *= 10;
557                         seed += *argument - '0';
558                         argument++;
559                     }
560                     break;
561
562                 case 't':
563                     argument++;
564                     testNb=0;
565                     while ((*argument>='0') && (*argument<='9')) {
566                         testNb *= 10;
567                         testNb += *argument - '0';
568                         argument++;
569                     }
570                     break;
571
572                 case 'P':   /* compressibility % */
573                     argument++;
574                     proba=0;
575                     while ((*argument>='0') && (*argument<='9')) {
576                         proba *= 10;
577                         proba += *argument - '0';
578                         argument++;
579                     }
580                     if (proba<0) proba=0;
581                     if (proba>100) proba=100;
582                     break;
583
584                 default:
585                     return FUZ_usage(programName);
586                 }
587     }   }   }   /* for(argNb=1; argNb<argc; argNb++) */
588
589     /* Get Seed */
590     DISPLAY("Starting zstd_buffered tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
591
592     if (!seedset) {
593         time_t const t = time(NULL);
594         U32 const h = XXH32(&t, sizeof(t), 1);
595         seed = h % 10000;
596     }
597     DISPLAY("Seed = %u\n", seed);
598     if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
599
600     if (nbTests<=0) nbTests=1;
601
602     if (testNb==0) {
603         result = basicUnitTests(0, ((double)proba) / 100, customNULL);  /* constant seed for predictability */
604         if (!result) {
605             DISPLAYLEVEL(4, "Unit tests using customMem :\n")
606             result = basicUnitTests(0, ((double)proba) / 100, customMem);  /* use custom memory allocation functions */
607     }   }
608
609     if (!result)
610         result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100);
611
612     if (mainPause) {
613         int unused;
614         DISPLAY("Press Enter \n");
615         unused = getchar();
616         (void)unused;
617     }
618     return result;
619 }