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