blob: 7783fe11b1bbf80ba8b3c638c98ad5e5d2ca1007 [file] [log] [blame]
Yann Collet4ded9e52016-08-30 10:04:33 -07001/**
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 */
Yann Colletd7883a22016-08-12 16:48:02 +02009
Yann Colletd7883a22016-08-12 16:48:02 +020010
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 : 4146) /* disable: C4146: minus unsigned expression */
18#endif
19
20
21/*-************************************
22* Includes
23**************************************/
24#include <stdlib.h> /* free */
25#include <stdio.h> /* fgets, sscanf */
Yann Colletef9999f2016-09-01 16:44:48 -070026#include <time.h> /* clock_t, clock() */
Yann Colletd7883a22016-08-12 16:48:02 +020027#include <string.h> /* strcmp */
28#include "mem.h"
Yann Colletef9999f2016-09-01 16:44:48 -070029#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem */
Yann Colletd7883a22016-08-12 16:48:02 +020030#include "zstd.h" /* ZSTD_compressBound */
Yann Collete795c8a2016-12-13 16:39:36 +010031#include "zstd_errors.h" /* ZSTD_error_srcSize_wrong */
Yann Colletd7883a22016-08-12 16:48:02 +020032#include "datagen.h" /* RDG_genBuffer */
Yann Colletef9999f2016-09-01 16:44:48 -070033#define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
Yann Colletd7883a22016-08-12 16:48:02 +020034#include "xxhash.h" /* XXH64_* */
35
36
37/*-************************************
38* Constants
39**************************************/
40#define KB *(1U<<10)
41#define MB *(1U<<20)
42#define GB *(1U<<30)
43
44static const U32 nbTestsDefault = 10000;
45#define COMPRESSIBLE_NOISE_LENGTH (10 MB)
46#define FUZ_COMPRESSIBILITY_DEFAULT 50
47static const U32 prime1 = 2654435761U;
48static const U32 prime2 = 2246822519U;
49
50
Yann Colletd7883a22016-08-12 16:48:02 +020051/*-************************************
52* Display Macros
53**************************************/
54#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
55#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
56static U32 g_displayLevel = 2;
57
58#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletef9999f2016-09-01 16:44:48 -070059 if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
60 { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \
Yann Colletd7883a22016-08-12 16:48:02 +020061 if (g_displayLevel>=4) fflush(stdout); } }
Yann Colletb3060f72016-09-09 16:44:16 +020062static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6;
Yann Colletef9999f2016-09-01 16:44:48 -070063static clock_t g_displayClock = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020064
Yann Colletef9999f2016-09-01 16:44:48 -070065static clock_t g_clockTime = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020066
67
68/*-*******************************************************
69* Fuzzer functions
70*********************************************************/
71#define MAX(a,b) ((a)>(b)?(a):(b))
72
Yann Colletef9999f2016-09-01 16:44:48 -070073static clock_t FUZ_GetClockSpan(clock_t clockStart)
Yann Colletd7883a22016-08-12 16:48:02 +020074{
Yann Colletef9999f2016-09-01 16:44:48 -070075 return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */
Yann Colletd7883a22016-08-12 16:48:02 +020076}
77
78/*! FUZ_rand() :
79 @return : a 27 bits random value, from a 32-bits `seed`.
80 `seed` is also modified */
Yann Collet95162342016-10-25 16:19:52 -070081#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
Yann Colletd7883a22016-08-12 16:48:02 +020082unsigned int FUZ_rand(unsigned int* seedPtr)
83{
84 U32 rand32 = *seedPtr;
85 rand32 *= prime1;
86 rand32 += prime2;
87 rand32 = FUZ_rotl32(rand32, 13);
88 *seedPtr = rand32;
89 return rand32 >> 5;
90}
91
Yann Colletd7883a22016-08-12 16:48:02 +020092static void* allocFunction(void* opaque, size_t size)
93{
94 void* address = malloc(size);
95 (void)opaque;
96 return address;
97}
98
99static void freeFunction(void* opaque, void* address)
100{
101 (void)opaque;
102 free(address);
103}
104
Yann Colletcb327632016-08-23 00:30:31 +0200105
106/*======================================================
107* Basic Unit tests
108======================================================*/
109
Yann Colletd7883a22016-08-12 16:48:02 +0200110static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem)
111{
Yann Colletb3060f72016-09-09 16:44:16 +0200112 size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
Yann Colletd7883a22016-08-12 16:48:02 +0200113 void* CNBuffer = malloc(CNBufferSize);
114 size_t const skippableFrameSize = 11;
115 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
116 void* compressedBuffer = malloc(compressedBufferSize);
117 size_t const decodedBufferSize = CNBufferSize;
118 void* decodedBuffer = malloc(decodedBufferSize);
119 size_t cSize;
Yann Colletb3060f72016-09-09 16:44:16 +0200120 int testResult = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200121 U32 testNb=0;
122 ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem);
123 ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800124 ZSTD_inBuffer inBuff, inBuff2;
Yann Collet53e17fb2016-08-17 01:39:22 +0200125 ZSTD_outBuffer outBuff;
Yann Colletd7883a22016-08-12 16:48:02 +0200126
127 /* Create compressible test buffer */
128 if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
129 DISPLAY("Not enough memory, aborting\n");
130 goto _output_error;
131 }
132 RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
133
134 /* generate skippable frame */
135 MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
136 MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
137 cSize = skippableFrameSize + 8;
138
139 /* Basic compression test */
140 DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
141 ZSTD_initCStream_usingDict(zc, CNBuffer, 128 KB, 1);
Yann Collet53e17fb2016-08-17 01:39:22 +0200142 outBuff.dst = (char*)(compressedBuffer)+cSize;
143 outBuff.size = compressedBufferSize;
144 outBuff.pos = 0;
145 inBuff.src = CNBuffer;
146 inBuff.size = CNBufferSize;
147 inBuff.pos = 0;
148 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200149 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200150 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
151 { size_t const r = ZSTD_endStream(zc, &outBuff);
Yann Collet9a021c12016-08-26 09:05:06 +0200152 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Yann Collet53e17fb2016-08-17 01:39:22 +0200153 cSize += outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200154 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
155
Yann Colletcb327632016-08-23 00:30:31 +0200156 DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200157 { size_t const s = ZSTD_sizeof_CStream(zc);
Yann Colletcb327632016-08-23 00:30:31 +0200158 if (ZSTD_isError(s)) goto _output_error;
159 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
160 }
161
Yann Colletd7883a22016-08-12 16:48:02 +0200162 /* skippable frame test */
163 DISPLAYLEVEL(4, "test%3i : decompress skippable frame : ", testNb++);
164 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200165 inBuff.src = compressedBuffer;
166 inBuff.size = cSize;
167 inBuff.pos = 0;
168 outBuff.dst = decodedBuffer;
169 outBuff.size = CNBufferSize;
170 outBuff.pos = 0;
171 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200172 if (r != 0) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200173 if (outBuff.pos != 0) goto _output_error; /* skippable frame len is 0 */
Yann Colletd7883a22016-08-12 16:48:02 +0200174 DISPLAYLEVEL(4, "OK \n");
175
176 /* Basic decompression test */
Yann Collet9ffbeea2016-12-02 18:37:38 -0800177 inBuff2 = inBuff;
Yann Colletd7883a22016-08-12 16:48:02 +0200178 DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
179 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet17e482e2016-08-23 16:58:10 +0200180 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000000000); /* large limit */
181 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet9ffbeea2016-12-02 18:37:38 -0800182 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff);
183 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
184 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
185 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
186 DISPLAYLEVEL(4, "OK \n");
187
188 /* Re-use without init */
189 DISPLAYLEVEL(4, "test%3i : decompress again without init (re-use previous settings): ", testNb++);
190 outBuff.pos = 0;
191 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2);
192 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
Yann Collet53e17fb2016-08-17 01:39:22 +0200193 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
194 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200195 DISPLAYLEVEL(4, "OK \n");
196
197 /* check regenerated data is byte exact */
198 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
199 { size_t i;
200 for (i=0; i<CNBufferSize; i++) {
201 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
202 } }
203 DISPLAYLEVEL(4, "OK \n");
204
Yann Colletcb327632016-08-23 00:30:31 +0200205 DISPLAYLEVEL(4, "test%3i : check DStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200206 { size_t const s = ZSTD_sizeof_DStream(zd);
Yann Colletcb327632016-08-23 00:30:31 +0200207 if (ZSTD_isError(s)) goto _output_error;
208 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
209 }
210
Yann Colletd7883a22016-08-12 16:48:02 +0200211 /* Byte-by-byte decompression test */
212 DISPLAYLEVEL(4, "test%3i : decompress byte-by-byte : ", testNb++);
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200213 { /* skippable frame */
214 size_t r = 1;
Yann Colletd7883a22016-08-12 16:48:02 +0200215 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200216 inBuff.src = compressedBuffer;
217 outBuff.dst = decodedBuffer;
218 inBuff.pos = 0;
219 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200220 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200221 inBuff.size = inBuff.pos + 1;
222 outBuff.size = outBuff.pos + 1;
223 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200224 if (ZSTD_isError(r)) goto _output_error;
225 }
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200226 /* normal frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200227 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
228 r=1;
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200229 while (r) {
Yann Collet53e17fb2016-08-17 01:39:22 +0200230 inBuff.size = inBuff.pos + 1;
231 outBuff.size = outBuff.pos + 1;
232 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200233 if (ZSTD_isError(r)) goto _output_error;
234 }
235 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200236 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
237 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200238 DISPLAYLEVEL(4, "OK \n");
239
240 /* check regenerated data is byte exact */
241 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
242 { size_t i;
243 for (i=0; i<CNBufferSize; i++) {
244 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
245 } }
246 DISPLAYLEVEL(4, "OK \n");
247
Yann Collete795c8a2016-12-13 16:39:36 +0100248 /* _srcSize compression test */
249 DISPLAYLEVEL(4, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
250 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize);
251 outBuff.dst = (char*)(compressedBuffer)+cSize;
252 outBuff.size = compressedBufferSize;
253 outBuff.pos = 0;
254 inBuff.src = CNBuffer;
255 inBuff.size = CNBufferSize;
256 inBuff.pos = 0;
257 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
258 if (ZSTD_isError(r)) goto _output_error; }
259 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
260 { size_t const r = ZSTD_endStream(zc, &outBuff);
261 if (r != 0) goto _output_error; } /* error, or some data not flushed */
262 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
263
264 /* wrong _srcSize compression test */
265 DISPLAYLEVEL(4, "test%3i : wrong srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1);
266 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1);
267 outBuff.dst = (char*)(compressedBuffer)+cSize;
268 outBuff.size = compressedBufferSize;
269 outBuff.pos = 0;
270 inBuff.src = CNBuffer;
271 inBuff.size = CNBufferSize;
272 inBuff.pos = 0;
273 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
274 if (ZSTD_isError(r)) goto _output_error; }
275 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
276 { size_t const r = ZSTD_endStream(zc, &outBuff);
277 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */
278 DISPLAYLEVEL(4, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); }
279
Yann Collet12083a42016-09-06 15:01:51 +0200280 /* Complex context re-use scenario */
281 DISPLAYLEVEL(4, "test%3i : context re-use : ", testNb++);
282 ZSTD_freeCStream(zc);
283 zc = ZSTD_createCStream_advanced(customMem);
284 if (zc==NULL) goto _output_error; /* memory allocation issue */
285 /* use 1 */
286 { size_t const inSize = 513;
287 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
288 inBuff.src = CNBuffer;
289 inBuff.size = inSize;
290 inBuff.pos = 0;
291 outBuff.dst = (char*)(compressedBuffer)+cSize;
292 outBuff.size = ZSTD_compressBound(inSize);
293 outBuff.pos = 0;
294 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
295 if (ZSTD_isError(r)) goto _output_error; }
296 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
297 { size_t const r = ZSTD_endStream(zc, &outBuff);
298 if (r != 0) goto _output_error; } /* error, or some data not flushed */
299 }
300 /* use 2 */
301 { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */
302 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
303 inBuff.src = CNBuffer;
304 inBuff.size = inSize;
305 inBuff.pos = 0;
306 outBuff.dst = (char*)(compressedBuffer)+cSize;
307 outBuff.size = ZSTD_compressBound(inSize);
308 outBuff.pos = 0;
309 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
310 if (ZSTD_isError(r)) goto _output_error; }
311 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
312 { size_t const r = ZSTD_endStream(zc, &outBuff);
313 if (r != 0) goto _output_error; } /* error, or some data not flushed */
314 }
315 DISPLAYLEVEL(4, "OK \n");
316
Yann Collet95162342016-10-25 16:19:52 -0700317 /* CDict scenario */
318 DISPLAYLEVEL(4, "test%3i : digested dictionary : ", testNb++);
319 { ZSTD_CDict* const cdict = ZSTD_createCDict(CNBuffer, 128 KB, 1);
320 size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
321 if (ZSTD_isError(initError)) goto _output_error;
322 cSize = 0;
323 outBuff.dst = compressedBuffer;
324 outBuff.size = compressedBufferSize;
325 outBuff.pos = 0;
326 inBuff.src = CNBuffer;
327 inBuff.size = CNBufferSize;
328 inBuff.pos = 0;
329 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
330 if (ZSTD_isError(r)) goto _output_error; }
331 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
332 { size_t const r = ZSTD_endStream(zc, &outBuff);
333 if (r != 0) goto _output_error; } /* error, or some data not flushed */
334 cSize = outBuff.pos;
335 ZSTD_freeCDict(cdict);
336 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
337 }
338
Yann Collet12083a42016-09-06 15:01:51 +0200339 DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++);
340 { size_t const s = ZSTD_sizeof_CStream(zc);
341 if (ZSTD_isError(s)) goto _output_error;
342 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
343 }
344
Yann Collet335ad5d2016-10-25 17:47:02 -0700345 /* DDict scenario */
346 DISPLAYLEVEL(4, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize);
347 { ZSTD_DDict* const ddict = ZSTD_createDDict(CNBuffer, 128 KB);
348 size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict);
349 if (ZSTD_isError(initError)) goto _output_error;
350 inBuff.src = compressedBuffer;
351 inBuff.size = cSize;
352 inBuff.pos = 0;
353 outBuff.dst = decodedBuffer;
354 outBuff.size = CNBufferSize;
355 outBuff.pos = 0;
356 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
357 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
358 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
359 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
360 ZSTD_freeDDict(ddict);
361 DISPLAYLEVEL(4, "OK \n");
362 }
363
Yann Collet12083a42016-09-06 15:01:51 +0200364 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet17e482e2016-08-23 16:58:10 +0200365 DISPLAYLEVEL(4, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
366 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
367 if (!ZSTD_isError(r)) goto _output_error; }
368 DISPLAYLEVEL(4, "OK \n");
369
370 /* Memory restriction */
371 DISPLAYLEVEL(4, "test%3i : maxWindowSize < frame requirement : ", testNb++);
372 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
373 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000); /* too small limit */
374 if (ZSTD_isError(r)) goto _output_error; }
375 inBuff.src = compressedBuffer;
376 inBuff.size = cSize;
377 inBuff.pos = 0;
378 outBuff.dst = decodedBuffer;
379 outBuff.size = CNBufferSize;
380 outBuff.pos = 0;
381 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
382 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
383 DISPLAYLEVEL(4, "OK (%s)\n", ZSTD_getErrorName(r)); }
384
385
Yann Colletd7883a22016-08-12 16:48:02 +0200386_end:
387 ZSTD_freeCStream(zc);
388 ZSTD_freeDStream(zd);
389 free(CNBuffer);
390 free(compressedBuffer);
391 free(decodedBuffer);
392 return testResult;
393
394_output_error:
395 testResult = 1;
396 DISPLAY("Error detected in Unit tests ! \n");
397 goto _end;
398}
399
400
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200401/* ====== Fuzzer tests ====== */
402
Yann Colletd7883a22016-08-12 16:48:02 +0200403static size_t findDiff(const void* buf1, const void* buf2, size_t max)
404{
405 const BYTE* b1 = (const BYTE*)buf1;
406 const BYTE* b2 = (const BYTE*)buf2;
407 size_t u;
408 for (u=0; u<max; u++) {
409 if (b1[u] != b2[u]) break;
410 }
411 return u;
412}
413
414static size_t FUZ_rLogLength(U32* seed, U32 logLength)
415{
416 size_t const lengthMask = ((size_t)1 << logLength) - 1;
417 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
418}
419
420static size_t FUZ_randomLength(U32* seed, U32 maxLog)
421{
422 U32 const logLength = FUZ_rand(seed) % maxLog;
423 return FUZ_rLogLength(seed, logLength);
424}
425
426#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
427
428#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
429 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
430
431static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
432{
433 static const U32 maxSrcLog = 24;
434 static const U32 maxSampleLog = 19;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200435 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200436 BYTE* cNoiseBuffer[5];
Yann Collet58d5dfe2016-09-25 01:34:03 +0200437 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
438 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
439 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
440 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
441 size_t const dstBufferSize = srcBufferSize;
442 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200443 U32 result = 0;
444 U32 testNb = 0;
445 U32 coreSeed = seed;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200446 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be reset sometimes */
447 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
448 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
449 clock_t const startClock = clock();
450 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
Yann Colletcf409a72016-09-26 16:41:05 +0200451 size_t dictSize = 0;
452 U32 oldTestLog = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200453
454 /* allocations */
Yann Colletd7883a22016-08-12 16:48:02 +0200455 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
456 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
457 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
458 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
459 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200460 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
Yann Collet58d5dfe2016-09-25 01:34:03 +0200461 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
Yann Colletd7883a22016-08-12 16:48:02 +0200462 "Not enough memory, fuzzer tests cancelled");
463
464 /* Create initial samples */
465 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
466 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
467 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
468 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
469 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
470 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200471 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Colletd7883a22016-08-12 16:48:02 +0200472
473 /* catch up testNb */
474 for (testNb=1; testNb < startTest; testNb++)
475 FUZ_rand(&coreSeed);
476
477 /* test loop */
Yann Colletef9999f2016-09-01 16:44:48 -0700478 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200479 U32 lseed;
480 const BYTE* srcBuffer;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200481 size_t totalTestSize, totalGenSize, cSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200482 XXH64_state_t xxhState;
483 U64 crcOrig;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200484 U32 resetAllowed = 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200485 size_t maxTestSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200486
487 /* init */
Yann Collet4c0b44f2016-11-01 11:13:22 -0700488 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
489 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Colletd7883a22016-08-12 16:48:02 +0200490 FUZ_rand(&coreSeed);
491 lseed = coreSeed ^ prime1;
492
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200493 /* states full reset (deliberately not synchronized) */
494 /* some issues can only happen when reusing states */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200495 if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); resetAllowed=0; }
496 if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZSTD_freeDStream(zd); zd = ZSTD_createDStream(); ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ }
Yann Colletd7883a22016-08-12 16:48:02 +0200497
498 /* srcBuffer selection [0-4] */
499 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
500 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
501 else {
502 buffNb >>= 3;
503 if (buffNb & 7) {
504 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
505 buffNb = tnb[buffNb >> 3];
506 } else {
507 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
508 buffNb = tnb[buffNb >> 3];
509 } }
510 srcBuffer = cNoiseBuffer[buffNb];
511 }
512
513 /* compression init */
Yann Colletcf409a72016-09-26 16:41:05 +0200514 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
515 && oldTestLog /* at least one test happened */ && resetAllowed) {
516 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
517 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
518 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
519 size_t const resetError = ZSTD_resetCStream(zc, pledgedSrcSize);
520 CHECK(ZSTD_isError(resetError), "ZSTD_resetCStream error : %s", ZSTD_getErrorName(resetError));
521 }
Yann Collet58d5dfe2016-09-25 01:34:03 +0200522 } else {
523 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200524 U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
525 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Colletcf409a72016-09-26 16:41:05 +0200526 oldTestLog = testLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200527 /* random dictionary selection */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200528 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200529 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
530 dict = srcBuffer + dictStart;
531 }
Yann Colletcf409a72016-09-26 16:41:05 +0200532 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
533 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200534 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
535 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200536 { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize);
Yann Colletb3060f72016-09-09 16:44:16 +0200537 CHECK (ZSTD_isError(initError),"ZSTD_initCStream_advanced error : %s", ZSTD_getErrorName(initError));
Yann Colletd7883a22016-08-12 16:48:02 +0200538 } } }
539
540 /* multi-segments compression test */
541 XXH64_reset(&xxhState, 0);
Yann Collet2f263942016-09-26 14:06:08 +0200542 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200543 U32 n;
Yann Collet2f263942016-09-26 14:06:08 +0200544 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
Yann Collete795c8a2016-12-13 16:39:36 +0100545 /* compress random chunks into randomly sized dst buffers */
Yann Collet2f263942016-09-26 14:06:08 +0200546 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
547 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
548 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200549 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
550 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet2f263942016-09-26 14:06:08 +0200551 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
Yann Collet53e17fb2016-08-17 01:39:22 +0200552 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200553
Yann Collet53e17fb2016-08-17 01:39:22 +0200554 { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200555 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Colletd7883a22016-08-12 16:48:02 +0200556
Yann Collet53e17fb2016-08-17 01:39:22 +0200557 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
558 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
559 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200560 }
561
562 /* random flush operation, to mess around */
563 if ((FUZ_rand(&lseed) & 15) == 0) {
564 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200565 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
566 outBuff.size = outBuff.pos + adjustedDstSize;
567 { size_t const flushError = ZSTD_flushStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200568 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
569 } } }
570
571 /* final frame epilogue */
572 { size_t remainingToFlush = (size_t)(-1);
573 while (remainingToFlush) {
574 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
575 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
576 U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush);
Yann Collet53e17fb2016-08-17 01:39:22 +0200577 outBuff.size = outBuff.pos + adjustedDstSize;
578 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200579 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
580 CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush);
581 } }
582 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200583 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200584 }
585
586 /* multi - fragments decompression test */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200587 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Collet95162342016-10-25 16:19:52 -0700588 CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
Yann Collet9ffbeea2016-12-02 18:37:38 -0800589 } else {
Yann Collet58d5dfe2016-09-25 01:34:03 +0200590 ZSTD_initDStream_usingDict(zd, dict, dictSize);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800591 }
Yann Colletd7883a22016-08-12 16:48:02 +0200592 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200593 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
594 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
595 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200596 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
597 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
598 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200599 inBuff.size = inBuff.pos + readCSrcSize;
600 outBuff.size = inBuff.pos + dstBuffSize;
601 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200602 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200603 }
604 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200605 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
606 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200607 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
608 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
609 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
610 } }
611
612 /*===== noisy/erroneous src decompression test =====*/
613
614 /* add some noise */
615 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
616 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
617 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
618 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
619 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
620 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
621 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
622 } }
623
624 /* try decompression on noisy data */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200625 ZSTD_initDStream(zd_noise); /* note : no dictionary */
Yann Collet53e17fb2016-08-17 01:39:22 +0200626 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
627 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
628 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200629 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
630 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200631 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
632 outBuff.size = outBuff.pos + adjustedDstSize;
633 inBuff.size = inBuff.pos + randomCSrcSize;
634 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200635 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
636 } } } }
637 DISPLAY("\r%u fuzzer tests completed \n", testNb);
638
639_cleanup:
640 ZSTD_freeCStream(zc);
641 ZSTD_freeDStream(zd);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200642 ZSTD_freeDStream(zd_noise);
Yann Colletd7883a22016-08-12 16:48:02 +0200643 free(cNoiseBuffer[0]);
644 free(cNoiseBuffer[1]);
645 free(cNoiseBuffer[2]);
646 free(cNoiseBuffer[3]);
647 free(cNoiseBuffer[4]);
648 free(copyBuffer);
649 free(cBuffer);
650 free(dstBuffer);
651 return result;
652
653_output_error:
654 result = 1;
655 goto _cleanup;
656}
657
658
659/*-*******************************************************
660* Command line
661*********************************************************/
662int FUZ_usage(const char* programName)
663{
664 DISPLAY( "Usage :\n");
665 DISPLAY( " %s [args]\n", programName);
666 DISPLAY( "\n");
667 DISPLAY( "Arguments :\n");
668 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
669 DISPLAY( " -s# : Select seed (default:prompt user)\n");
670 DISPLAY( " -t# : Select starting test number (default:0)\n");
671 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
672 DISPLAY( " -v : verbose\n");
673 DISPLAY( " -p : pause at the end\n");
674 DISPLAY( " -h : display help and exit\n");
675 return 0;
676}
677
678
679int main(int argc, const char** argv)
680{
681 U32 seed=0;
682 int seedset=0;
683 int argNb;
684 int nbTests = nbTestsDefault;
685 int testNb = 0;
686 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
687 int result=0;
688 U32 mainPause = 0;
689 const char* programName = argv[0];
690 ZSTD_customMem customMem = { allocFunction, freeFunction, NULL };
691 ZSTD_customMem customNULL = { NULL, NULL, NULL };
692
693 /* Check command line */
694 for(argNb=1; argNb<argc; argNb++) {
695 const char* argument = argv[argNb];
696 if(!argument) continue; /* Protection if argument empty */
697
698 /* Parsing commands. Aggregated commands are allowed */
699 if (argument[0]=='-') {
700 argument++;
701
702 while (*argument!=0) {
703 switch(*argument)
704 {
705 case 'h':
706 return FUZ_usage(programName);
707 case 'v':
708 argument++;
709 g_displayLevel=4;
710 break;
711 case 'q':
712 argument++;
713 g_displayLevel--;
714 break;
715 case 'p': /* pause at the end */
716 argument++;
717 mainPause = 1;
718 break;
719
720 case 'i':
721 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700722 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200723 while ((*argument>='0') && (*argument<='9')) {
724 nbTests *= 10;
725 nbTests += *argument - '0';
726 argument++;
727 }
728 break;
729
730 case 'T':
731 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700732 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200733 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -0700734 g_clockTime *= 10;
735 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +0200736 argument++;
737 }
Yann Colletef9999f2016-09-01 16:44:48 -0700738 if (*argument=='m') g_clockTime *=60, argument++;
Yann Colletd7883a22016-08-12 16:48:02 +0200739 if (*argument=='n') argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700740 g_clockTime *= CLOCKS_PER_SEC;
Yann Colletd7883a22016-08-12 16:48:02 +0200741 break;
742
743 case 's':
744 argument++;
745 seed=0;
746 seedset=1;
747 while ((*argument>='0') && (*argument<='9')) {
748 seed *= 10;
749 seed += *argument - '0';
750 argument++;
751 }
752 break;
753
754 case 't':
755 argument++;
756 testNb=0;
757 while ((*argument>='0') && (*argument<='9')) {
758 testNb *= 10;
759 testNb += *argument - '0';
760 argument++;
761 }
762 break;
763
764 case 'P': /* compressibility % */
765 argument++;
766 proba=0;
767 while ((*argument>='0') && (*argument<='9')) {
768 proba *= 10;
769 proba += *argument - '0';
770 argument++;
771 }
772 if (proba<0) proba=0;
773 if (proba>100) proba=100;
774 break;
775
776 default:
777 return FUZ_usage(programName);
778 }
779 } } } /* for(argNb=1; argNb<argc; argNb++) */
780
781 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +0200782 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +0200783
Yann Colletef9999f2016-09-01 16:44:48 -0700784 if (!seedset) {
785 time_t const t = time(NULL);
786 U32 const h = XXH32(&t, sizeof(t), 1);
787 seed = h % 10000;
788 }
789
Yann Colletd7883a22016-08-12 16:48:02 +0200790 DISPLAY("Seed = %u\n", seed);
791 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
792
793 if (nbTests<=0) nbTests=1;
794
795 if (testNb==0) {
796 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
797 if (!result) {
798 DISPLAYLEVEL(4, "Unit tests using customMem :\n")
799 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
800 } }
801
802 if (!result)
803 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100);
804
805 if (mainPause) {
806 int unused;
807 DISPLAY("Press Enter \n");
808 unused = getchar();
809 (void)unused;
810 }
811 return result;
812}