blob: ce619308572cc40af5410b430fa5978691f2ab58 [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 */
Przemyslaw Skibinski97a258d2016-12-21 14:00:41 +010015# define _CRT_SECURE_NO_WARNINGS /* fgets */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010016# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
Yann Colletd7883a22016-08-12 16:48:02 +020017# 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 */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010028#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);
Yann Colletd564faa2016-12-18 21:39:15 +0100251 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100252 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 */
Yann Colletd564faa2016-12-18 21:39:15 +0100262 { unsigned long long origSize = ZSTD_getDecompressedSize(outBuff.dst, outBuff.pos);
263 DISPLAY("outBuff.pos : %u \n", (U32)outBuff.pos);
264 DISPLAY("origSize = %u \n", (U32)origSize);
265 if ((size_t)origSize != CNBufferSize) goto _output_error; } /* exact original size must be present */
Yann Collete795c8a2016-12-13 16:39:36 +0100266 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
267
268 /* wrong _srcSize compression test */
269 DISPLAYLEVEL(4, "test%3i : wrong srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1);
270 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1);
Yann Colletd564faa2016-12-18 21:39:15 +0100271 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100272 outBuff.size = compressedBufferSize;
273 outBuff.pos = 0;
274 inBuff.src = CNBuffer;
275 inBuff.size = CNBufferSize;
276 inBuff.pos = 0;
277 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
278 if (ZSTD_isError(r)) goto _output_error; }
279 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
280 { size_t const r = ZSTD_endStream(zc, &outBuff);
281 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */
282 DISPLAYLEVEL(4, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); }
283
Yann Collet12083a42016-09-06 15:01:51 +0200284 /* Complex context re-use scenario */
285 DISPLAYLEVEL(4, "test%3i : context re-use : ", testNb++);
286 ZSTD_freeCStream(zc);
287 zc = ZSTD_createCStream_advanced(customMem);
288 if (zc==NULL) goto _output_error; /* memory allocation issue */
289 /* use 1 */
290 { size_t const inSize = 513;
291 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
292 inBuff.src = CNBuffer;
293 inBuff.size = inSize;
294 inBuff.pos = 0;
295 outBuff.dst = (char*)(compressedBuffer)+cSize;
296 outBuff.size = ZSTD_compressBound(inSize);
297 outBuff.pos = 0;
298 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
299 if (ZSTD_isError(r)) goto _output_error; }
300 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
301 { size_t const r = ZSTD_endStream(zc, &outBuff);
302 if (r != 0) goto _output_error; } /* error, or some data not flushed */
303 }
304 /* use 2 */
305 { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */
306 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
307 inBuff.src = CNBuffer;
308 inBuff.size = inSize;
309 inBuff.pos = 0;
310 outBuff.dst = (char*)(compressedBuffer)+cSize;
311 outBuff.size = ZSTD_compressBound(inSize);
312 outBuff.pos = 0;
313 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
314 if (ZSTD_isError(r)) goto _output_error; }
315 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
316 { size_t const r = ZSTD_endStream(zc, &outBuff);
317 if (r != 0) goto _output_error; } /* error, or some data not flushed */
318 }
319 DISPLAYLEVEL(4, "OK \n");
320
Yann Collet95162342016-10-25 16:19:52 -0700321 /* CDict scenario */
322 DISPLAYLEVEL(4, "test%3i : digested dictionary : ", testNb++);
323 { ZSTD_CDict* const cdict = ZSTD_createCDict(CNBuffer, 128 KB, 1);
324 size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
325 if (ZSTD_isError(initError)) goto _output_error;
326 cSize = 0;
327 outBuff.dst = compressedBuffer;
328 outBuff.size = compressedBufferSize;
329 outBuff.pos = 0;
330 inBuff.src = CNBuffer;
331 inBuff.size = CNBufferSize;
332 inBuff.pos = 0;
333 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
334 if (ZSTD_isError(r)) goto _output_error; }
335 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
336 { size_t const r = ZSTD_endStream(zc, &outBuff);
337 if (r != 0) goto _output_error; } /* error, or some data not flushed */
338 cSize = outBuff.pos;
339 ZSTD_freeCDict(cdict);
340 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
341 }
342
Yann Collet12083a42016-09-06 15:01:51 +0200343 DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++);
344 { size_t const s = ZSTD_sizeof_CStream(zc);
345 if (ZSTD_isError(s)) goto _output_error;
346 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
347 }
348
Yann Collet335ad5d2016-10-25 17:47:02 -0700349 /* DDict scenario */
350 DISPLAYLEVEL(4, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize);
351 { ZSTD_DDict* const ddict = ZSTD_createDDict(CNBuffer, 128 KB);
352 size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict);
353 if (ZSTD_isError(initError)) goto _output_error;
354 inBuff.src = compressedBuffer;
355 inBuff.size = cSize;
356 inBuff.pos = 0;
357 outBuff.dst = decodedBuffer;
358 outBuff.size = CNBufferSize;
359 outBuff.pos = 0;
360 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
361 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
362 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
363 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
364 ZSTD_freeDDict(ddict);
365 DISPLAYLEVEL(4, "OK \n");
366 }
367
Yann Collet12083a42016-09-06 15:01:51 +0200368 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet17e482e2016-08-23 16:58:10 +0200369 DISPLAYLEVEL(4, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
370 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
371 if (!ZSTD_isError(r)) goto _output_error; }
372 DISPLAYLEVEL(4, "OK \n");
373
374 /* Memory restriction */
375 DISPLAYLEVEL(4, "test%3i : maxWindowSize < frame requirement : ", testNb++);
376 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
377 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000); /* too small limit */
378 if (ZSTD_isError(r)) goto _output_error; }
379 inBuff.src = compressedBuffer;
380 inBuff.size = cSize;
381 inBuff.pos = 0;
382 outBuff.dst = decodedBuffer;
383 outBuff.size = CNBufferSize;
384 outBuff.pos = 0;
385 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
386 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
387 DISPLAYLEVEL(4, "OK (%s)\n", ZSTD_getErrorName(r)); }
388
389
Yann Colletd7883a22016-08-12 16:48:02 +0200390_end:
391 ZSTD_freeCStream(zc);
392 ZSTD_freeDStream(zd);
393 free(CNBuffer);
394 free(compressedBuffer);
395 free(decodedBuffer);
396 return testResult;
397
398_output_error:
399 testResult = 1;
400 DISPLAY("Error detected in Unit tests ! \n");
401 goto _end;
402}
403
404
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200405/* ====== Fuzzer tests ====== */
406
Yann Colletd7883a22016-08-12 16:48:02 +0200407static size_t findDiff(const void* buf1, const void* buf2, size_t max)
408{
409 const BYTE* b1 = (const BYTE*)buf1;
410 const BYTE* b2 = (const BYTE*)buf2;
411 size_t u;
412 for (u=0; u<max; u++) {
413 if (b1[u] != b2[u]) break;
414 }
415 return u;
416}
417
418static size_t FUZ_rLogLength(U32* seed, U32 logLength)
419{
420 size_t const lengthMask = ((size_t)1 << logLength) - 1;
421 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
422}
423
424static size_t FUZ_randomLength(U32* seed, U32 maxLog)
425{
426 U32 const logLength = FUZ_rand(seed) % maxLog;
427 return FUZ_rLogLength(seed, logLength);
428}
429
430#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
431
432#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
433 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
434
435static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
436{
437 static const U32 maxSrcLog = 24;
438 static const U32 maxSampleLog = 19;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200439 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200440 BYTE* cNoiseBuffer[5];
Yann Collet58d5dfe2016-09-25 01:34:03 +0200441 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
442 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
443 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
444 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
445 size_t const dstBufferSize = srcBufferSize;
446 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200447 U32 result = 0;
448 U32 testNb = 0;
449 U32 coreSeed = seed;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200450 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be reset sometimes */
451 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
452 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
453 clock_t const startClock = clock();
454 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
Yann Colletcf409a72016-09-26 16:41:05 +0200455 size_t dictSize = 0;
456 U32 oldTestLog = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200457
458 /* allocations */
Yann Colletd7883a22016-08-12 16:48:02 +0200459 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
460 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
461 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
462 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
463 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200464 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
Yann Collet58d5dfe2016-09-25 01:34:03 +0200465 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
Yann Colletd7883a22016-08-12 16:48:02 +0200466 "Not enough memory, fuzzer tests cancelled");
467
468 /* Create initial samples */
469 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
470 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
471 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
472 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
473 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
474 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200475 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Colletd7883a22016-08-12 16:48:02 +0200476
477 /* catch up testNb */
478 for (testNb=1; testNb < startTest; testNb++)
479 FUZ_rand(&coreSeed);
480
481 /* test loop */
Yann Colletef9999f2016-09-01 16:44:48 -0700482 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200483 U32 lseed;
484 const BYTE* srcBuffer;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200485 size_t totalTestSize, totalGenSize, cSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200486 XXH64_state_t xxhState;
487 U64 crcOrig;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200488 U32 resetAllowed = 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200489 size_t maxTestSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200490
491 /* init */
Yann Collet4c0b44f2016-11-01 11:13:22 -0700492 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
493 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Colletd7883a22016-08-12 16:48:02 +0200494 FUZ_rand(&coreSeed);
495 lseed = coreSeed ^ prime1;
496
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200497 /* states full reset (deliberately not synchronized) */
498 /* some issues can only happen when reusing states */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200499 if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); resetAllowed=0; }
500 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 +0200501
502 /* srcBuffer selection [0-4] */
503 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
504 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
505 else {
506 buffNb >>= 3;
507 if (buffNb & 7) {
508 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
509 buffNb = tnb[buffNb >> 3];
510 } else {
511 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
512 buffNb = tnb[buffNb >> 3];
513 } }
514 srcBuffer = cNoiseBuffer[buffNb];
515 }
516
517 /* compression init */
Yann Colletcf409a72016-09-26 16:41:05 +0200518 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
519 && oldTestLog /* at least one test happened */ && resetAllowed) {
520 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
521 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
522 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
523 size_t const resetError = ZSTD_resetCStream(zc, pledgedSrcSize);
524 CHECK(ZSTD_isError(resetError), "ZSTD_resetCStream error : %s", ZSTD_getErrorName(resetError));
525 }
Yann Collet58d5dfe2016-09-25 01:34:03 +0200526 } else {
527 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200528 U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
529 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Colletcf409a72016-09-26 16:41:05 +0200530 oldTestLog = testLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200531 /* random dictionary selection */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200532 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200533 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
534 dict = srcBuffer + dictStart;
535 }
Yann Colletcf409a72016-09-26 16:41:05 +0200536 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
537 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200538 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
539 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200540 { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize);
Yann Colletb3060f72016-09-09 16:44:16 +0200541 CHECK (ZSTD_isError(initError),"ZSTD_initCStream_advanced error : %s", ZSTD_getErrorName(initError));
Yann Colletd7883a22016-08-12 16:48:02 +0200542 } } }
543
544 /* multi-segments compression test */
545 XXH64_reset(&xxhState, 0);
Yann Collet2f263942016-09-26 14:06:08 +0200546 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200547 U32 n;
Yann Collet2f263942016-09-26 14:06:08 +0200548 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
Yann Collete795c8a2016-12-13 16:39:36 +0100549 /* compress random chunks into randomly sized dst buffers */
Yann Collet2f263942016-09-26 14:06:08 +0200550 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
551 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
552 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200553 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
554 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet2f263942016-09-26 14:06:08 +0200555 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
Yann Collet53e17fb2016-08-17 01:39:22 +0200556 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200557
Yann Collet53e17fb2016-08-17 01:39:22 +0200558 { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200559 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Colletd7883a22016-08-12 16:48:02 +0200560
Yann Collet53e17fb2016-08-17 01:39:22 +0200561 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
562 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
563 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200564 }
565
566 /* random flush operation, to mess around */
567 if ((FUZ_rand(&lseed) & 15) == 0) {
568 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200569 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
570 outBuff.size = outBuff.pos + adjustedDstSize;
571 { size_t const flushError = ZSTD_flushStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200572 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
573 } } }
574
575 /* final frame epilogue */
576 { size_t remainingToFlush = (size_t)(-1);
577 while (remainingToFlush) {
578 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
579 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
580 U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush);
Yann Collet53e17fb2016-08-17 01:39:22 +0200581 outBuff.size = outBuff.pos + adjustedDstSize;
582 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200583 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
584 CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush);
585 } }
586 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200587 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200588 }
589
590 /* multi - fragments decompression test */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200591 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Collet95162342016-10-25 16:19:52 -0700592 CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
Yann Collet9ffbeea2016-12-02 18:37:38 -0800593 } else {
Yann Collet58d5dfe2016-09-25 01:34:03 +0200594 ZSTD_initDStream_usingDict(zd, dict, dictSize);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800595 }
Yann Colletd7883a22016-08-12 16:48:02 +0200596 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200597 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
598 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
599 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200600 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
601 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
602 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200603 inBuff.size = inBuff.pos + readCSrcSize;
604 outBuff.size = inBuff.pos + dstBuffSize;
605 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200606 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200607 }
608 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200609 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
610 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200611 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
612 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
613 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
614 } }
615
616 /*===== noisy/erroneous src decompression test =====*/
617
618 /* add some noise */
619 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
620 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
621 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
622 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
623 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
624 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
625 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
626 } }
627
628 /* try decompression on noisy data */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200629 ZSTD_initDStream(zd_noise); /* note : no dictionary */
Yann Collet53e17fb2016-08-17 01:39:22 +0200630 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
631 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
632 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200633 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
634 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200635 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
636 outBuff.size = outBuff.pos + adjustedDstSize;
637 inBuff.size = inBuff.pos + randomCSrcSize;
638 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200639 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
640 } } } }
641 DISPLAY("\r%u fuzzer tests completed \n", testNb);
642
643_cleanup:
644 ZSTD_freeCStream(zc);
645 ZSTD_freeDStream(zd);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200646 ZSTD_freeDStream(zd_noise);
Yann Colletd7883a22016-08-12 16:48:02 +0200647 free(cNoiseBuffer[0]);
648 free(cNoiseBuffer[1]);
649 free(cNoiseBuffer[2]);
650 free(cNoiseBuffer[3]);
651 free(cNoiseBuffer[4]);
652 free(copyBuffer);
653 free(cBuffer);
654 free(dstBuffer);
655 return result;
656
657_output_error:
658 result = 1;
659 goto _cleanup;
660}
661
662
663/*-*******************************************************
664* Command line
665*********************************************************/
666int FUZ_usage(const char* programName)
667{
668 DISPLAY( "Usage :\n");
669 DISPLAY( " %s [args]\n", programName);
670 DISPLAY( "\n");
671 DISPLAY( "Arguments :\n");
672 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
673 DISPLAY( " -s# : Select seed (default:prompt user)\n");
674 DISPLAY( " -t# : Select starting test number (default:0)\n");
675 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
676 DISPLAY( " -v : verbose\n");
677 DISPLAY( " -p : pause at the end\n");
678 DISPLAY( " -h : display help and exit\n");
679 return 0;
680}
681
682
683int main(int argc, const char** argv)
684{
685 U32 seed=0;
686 int seedset=0;
687 int argNb;
688 int nbTests = nbTestsDefault;
689 int testNb = 0;
690 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
691 int result=0;
692 U32 mainPause = 0;
693 const char* programName = argv[0];
694 ZSTD_customMem customMem = { allocFunction, freeFunction, NULL };
695 ZSTD_customMem customNULL = { NULL, NULL, NULL };
696
697 /* Check command line */
698 for(argNb=1; argNb<argc; argNb++) {
699 const char* argument = argv[argNb];
700 if(!argument) continue; /* Protection if argument empty */
701
702 /* Parsing commands. Aggregated commands are allowed */
703 if (argument[0]=='-') {
704 argument++;
705
706 while (*argument!=0) {
707 switch(*argument)
708 {
709 case 'h':
710 return FUZ_usage(programName);
711 case 'v':
712 argument++;
713 g_displayLevel=4;
714 break;
715 case 'q':
716 argument++;
717 g_displayLevel--;
718 break;
719 case 'p': /* pause at the end */
720 argument++;
721 mainPause = 1;
722 break;
723
724 case 'i':
725 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700726 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200727 while ((*argument>='0') && (*argument<='9')) {
728 nbTests *= 10;
729 nbTests += *argument - '0';
730 argument++;
731 }
732 break;
733
734 case 'T':
735 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700736 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200737 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -0700738 g_clockTime *= 10;
739 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +0200740 argument++;
741 }
Yann Colletef9999f2016-09-01 16:44:48 -0700742 if (*argument=='m') g_clockTime *=60, argument++;
Yann Colletd7883a22016-08-12 16:48:02 +0200743 if (*argument=='n') argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700744 g_clockTime *= CLOCKS_PER_SEC;
Yann Colletd7883a22016-08-12 16:48:02 +0200745 break;
746
747 case 's':
748 argument++;
749 seed=0;
750 seedset=1;
751 while ((*argument>='0') && (*argument<='9')) {
752 seed *= 10;
753 seed += *argument - '0';
754 argument++;
755 }
756 break;
757
758 case 't':
759 argument++;
760 testNb=0;
761 while ((*argument>='0') && (*argument<='9')) {
762 testNb *= 10;
763 testNb += *argument - '0';
764 argument++;
765 }
766 break;
767
768 case 'P': /* compressibility % */
769 argument++;
770 proba=0;
771 while ((*argument>='0') && (*argument<='9')) {
772 proba *= 10;
773 proba += *argument - '0';
774 argument++;
775 }
776 if (proba<0) proba=0;
777 if (proba>100) proba=100;
778 break;
779
780 default:
781 return FUZ_usage(programName);
782 }
783 } } } /* for(argNb=1; argNb<argc; argNb++) */
784
785 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +0200786 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +0200787
Yann Colletef9999f2016-09-01 16:44:48 -0700788 if (!seedset) {
789 time_t const t = time(NULL);
790 U32 const h = XXH32(&t, sizeof(t), 1);
791 seed = h % 10000;
792 }
793
Yann Colletd7883a22016-08-12 16:48:02 +0200794 DISPLAY("Seed = %u\n", seed);
795 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
796
797 if (nbTests<=0) nbTests=1;
798
799 if (testNb==0) {
800 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
801 if (!result) {
802 DISPLAYLEVEL(4, "Unit tests using customMem :\n")
803 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
804 } }
805
806 if (!result)
807 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100);
808
809 if (mainPause) {
810 int unused;
811 DISPLAY("Press Enter \n");
812 unused = getchar();
813 (void)unused;
814 }
815 return result;
816}