blob: 83b95974f1800b3d490f0ce2e6e43dab51099960 [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 Collet4856a002015-01-24 01:58:16 +01009
Yann Collet4856a002015-01-24 01:58:16 +010010
Yann Collet0d9ce042016-03-19 13:21:08 +010011/*-************************************
Yann Collet4856a002015-01-24 01:58:16 +010012* Compiler specific
13**************************************/
14#ifdef _MSC_VER /* Visual Studio */
Yann Colleta5ffe3d2017-05-12 16:29:19 -070015# define _CRT_SECURE_NO_WARNINGS /* fgets */
16# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
17# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
Yann Collet4856a002015-01-24 01:58:16 +010018#endif
19
Yann Collet4856a002015-01-24 01:58:16 +010020
Yann Collet0d9ce042016-03-19 13:21:08 +010021/*-************************************
Yann Collet4856a002015-01-24 01:58:16 +010022* Includes
23**************************************/
Yann Collet6fa05a22016-07-20 14:58:49 +020024#include <stdlib.h> /* free */
25#include <stdio.h> /* fgets, sscanf */
Yann Collet6fa05a22016-07-20 14:58:49 +020026#include <string.h> /* strcmp */
27#include <time.h> /* clock_t */
Yann Colleta5ffe3d2017-05-12 16:29:19 -070028#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressContinue, ZSTD_compressBlock */
Yann Collet6fa05a22016-07-20 14:58:49 +020029#include "zstd.h" /* ZSTD_VERSION_STRING */
Yann Collete7a41a52016-12-05 16:21:06 -080030#include "zstd_errors.h" /* ZSTD_getErrorCode */
Yann Collet0ef68032017-03-29 16:58:57 -070031#include "zstdmt_compress.h"
Nick Terrella8b4fe02017-01-02 18:45:19 -080032#define ZDICT_STATIC_LINKING_ONLY
Yann Collet6fa05a22016-07-20 14:58:49 +020033#include "zdict.h" /* ZDICT_trainFromBuffer */
34#include "datagen.h" /* RDG_genBuffer */
Yann Collet353c5d22015-10-21 14:39:26 +010035#include "mem.h"
Yann Collet6c903a82016-05-28 13:34:07 +020036#define XXH_STATIC_LINKING_ONLY
Yann Collet6fa05a22016-07-20 14:58:49 +020037#include "xxhash.h" /* XXH64 */
Yann Collet4856a002015-01-24 01:58:16 +010038
39
Yann Collet0d9ce042016-03-19 13:21:08 +010040/*-************************************
41* Constants
Yann Collet4856a002015-01-24 01:58:16 +010042**************************************/
Yann Collet4856a002015-01-24 01:58:16 +010043#define KB *(1U<<10)
44#define MB *(1U<<20)
45#define GB *(1U<<30)
46
Yann Collet0d9ce042016-03-19 13:21:08 +010047static const U32 FUZ_compressibility_default = 50;
Yann Collet110cc142015-11-19 12:02:28 +010048static const U32 nbTestsDefault = 30000;
Yann Collet4856a002015-01-24 01:58:16 +010049
50
Yann Collet0d9ce042016-03-19 13:21:08 +010051/*-************************************
Yann Collet4856a002015-01-24 01:58:16 +010052* 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 Colletea63bb72016-04-08 15:25:32 +020059 if ((FUZ_clockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
60 { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \
Sean Purcell042ba122017-03-23 11:13:52 -070061 if (g_displayLevel>=4) fflush(stderr); } }
Yann Collet64deef32016-09-14 00:16:07 +020062static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6;
Yann Colletea63bb72016-04-08 15:25:32 +020063static clock_t g_displayClock = 0;
Yann Collet553cf6a2015-12-04 17:25:26 +010064
Yann Collet4856a002015-01-24 01:58:16 +010065
Yann Collet0d9ce042016-03-19 13:21:08 +010066/*-*******************************************************
Yann Collet4856a002015-01-24 01:58:16 +010067* Fuzzer functions
68*********************************************************/
Yann Collete93add02016-02-15 17:44:14 +010069#define MIN(a,b) ((a)<(b)?(a):(b))
Sean Purcellf5e50512017-03-15 15:04:54 -070070#define MAX(a,b) ((a)>(b)?(a):(b))
Yann Collet110cc142015-11-19 12:02:28 +010071
Yann Colletea63bb72016-04-08 15:25:32 +020072static clock_t FUZ_clockSpan(clock_t cStart)
Yann Collet4856a002015-01-24 01:58:16 +010073{
Yann Colletea63bb72016-04-08 15:25:32 +020074 return clock() - cStart; /* works even when overflow; max span ~ 30mn */
Yann Collet4856a002015-01-24 01:58:16 +010075}
76
Yann Colletc0a9bf32016-05-30 01:56:08 +020077#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
78static unsigned FUZ_rand(unsigned* src)
Yann Collet4856a002015-01-24 01:58:16 +010079{
Yann Collet0d9ce042016-03-19 13:21:08 +010080 static const U32 prime1 = 2654435761U;
81 static const U32 prime2 = 2246822519U;
Yann Collet4856a002015-01-24 01:58:16 +010082 U32 rand32 = *src;
83 rand32 *= prime1;
84 rand32 += prime2;
85 rand32 = FUZ_rotl32(rand32, 13);
86 *src = rand32;
87 return rand32 >> 5;
88}
89
Yann Collet997f9ee2015-08-21 02:44:20 +010090static unsigned FUZ_highbit32(U32 v32)
Yann Collet4856a002015-01-24 01:58:16 +010091{
92 unsigned nbBits = 0;
93 if (v32==0) return 0;
Yann Colletc0a9bf32016-05-30 01:56:08 +020094 while (v32) v32 >>= 1, nbBits++;
Yann Collet4856a002015-01-24 01:58:16 +010095 return nbBits;
96}
Yann Collet4856a002015-01-24 01:58:16 +010097
98
Yann Colletcb327632016-08-23 00:30:31 +020099/*=============================================
100* Basic Unit tests
101=============================================*/
102
Yann Colletd4f4e582016-06-27 01:31:35 +0200103#define CHECK_V(var, fn) size_t const var = fn; if (ZSTD_isError(var)) goto _output_error
104#define CHECK(fn) { CHECK_V(err, fn); }
105#define CHECKPLUS(var, fn, more) { CHECK_V(var, fn); more; }
Yann Collet0fdc71c2017-05-24 17:41:41 -0700106
Yann Collet4856a002015-01-24 01:58:16 +0100107static int basicUnitTests(U32 seed, double compressibility)
108{
Yann Collet30009522016-05-30 16:17:33 +0200109 size_t const CNBuffSize = 5 MB;
Yann Colletd2858e92016-05-30 15:10:09 +0200110 void* const CNBuffer = malloc(CNBuffSize);
111 void* const compressedBuffer = malloc(ZSTD_compressBound(CNBuffSize));
112 void* const decodedBuffer = malloc(CNBuffSize);
Yann Colletc0b17312017-02-28 01:02:46 -0800113 ZSTD_DCtx* dctx = ZSTD_createDCtx();
Yann Colletc0a9bf32016-05-30 01:56:08 +0200114 int testResult = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100115 U32 testNb=0;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200116 size_t cSize;
Yann Collet4856a002015-01-24 01:58:16 +0100117
Yann Colletc0a9bf32016-05-30 01:56:08 +0200118 /* Create compressible noise */
Yann Colletd1d210f2016-03-19 12:12:07 +0100119 if (!CNBuffer || !compressedBuffer || !decodedBuffer) {
Yann Collet94f998b2015-07-04 23:10:40 -0800120 DISPLAY("Not enough memory, aborting\n");
121 testResult = 1;
122 goto _end;
123 }
Yann Colletd2858e92016-05-30 15:10:09 +0200124 RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed);
Yann Collet4856a002015-01-24 01:58:16 +0100125
Yann Colletd5d9bc32015-08-23 23:13:49 +0100126 /* Basic tests */
Yann Collet9a69ec42016-08-01 16:25:58 +0200127 DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName : ", testNb++);
128 { const char* errorString = ZSTD_getErrorName(0);
129 DISPLAYLEVEL(4, "OK : %s \n", errorString);
130 }
131
132 DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName with wrong value : ", testNb++);
133 { const char* errorString = ZSTD_getErrorName(499);
134 DISPLAYLEVEL(4, "OK : %s \n", errorString);
135 }
136
Yann Collet0ef68032017-03-29 16:58:57 -0700137
Yann Colletd2858e92016-05-30 15:10:09 +0200138 DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, (U32)CNBuffSize);
139 CHECKPLUS(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(CNBuffSize),
140 CNBuffer, CNBuffSize, 1),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200141 cSize=r );
Yann Colletd2858e92016-05-30 15:10:09 +0200142 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
Yann Collet4856a002015-01-24 01:58:16 +0100143
Yann Collet0ef68032017-03-29 16:58:57 -0700144
145 DISPLAYLEVEL(4, "test%3i : ZSTD_getFrameContentSize test : ", testNb++);
146 { unsigned long long const rSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
147 if (rSize != CNBuffSize) goto _output_error;
148 }
149 DISPLAYLEVEL(4, "OK \n");
150
151 DISPLAYLEVEL(4, "test%3i : ZSTD_findDecompressedSize test : ", testNb++);
Sean Purcell4e709712017-02-07 13:50:09 -0800152 { unsigned long long const rSize = ZSTD_findDecompressedSize(compressedBuffer, cSize);
Yann Colletf323bf72016-07-07 13:14:21 +0200153 if (rSize != CNBuffSize) goto _output_error;
154 }
155 DISPLAYLEVEL(4, "OK \n");
156
Yann Colletd2858e92016-05-30 15:10:09 +0200157 DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)CNBuffSize);
Yann Colletc991cc12016-07-28 00:55:43 +0200158 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
159 if (r != CNBuffSize) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100160 DISPLAYLEVEL(4, "OK \n");
161
Yann Colletc0a9bf32016-05-30 01:56:08 +0200162 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
163 { size_t u;
Yann Colletd2858e92016-05-30 15:10:09 +0200164 for (u=0; u<CNBuffSize; u++) {
Yann Colletc0a9bf32016-05-30 01:56:08 +0200165 if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;;
166 } }
167 DISPLAYLEVEL(4, "OK \n");
Yann Collet4856a002015-01-24 01:58:16 +0100168
Yann Collet0ef68032017-03-29 16:58:57 -0700169
Yann Colletc0b17312017-02-28 01:02:46 -0800170 DISPLAYLEVEL(4, "test%3i : decompress with null dict : ", testNb++);
171 { size_t const r = ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, NULL, 0);
172 if (r != CNBuffSize) goto _output_error; }
173 DISPLAYLEVEL(4, "OK \n");
174
175 DISPLAYLEVEL(4, "test%3i : decompress with null DDict : ", testNb++);
176 { size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, NULL);
177 if (r != CNBuffSize) goto _output_error; }
178 DISPLAYLEVEL(4, "OK \n");
179
Yann Collet4856a002015-01-24 01:58:16 +0100180 DISPLAYLEVEL(4, "test%3i : decompress with 1 missing byte : ", testNb++);
Yann Colletd2858e92016-05-30 15:10:09 +0200181 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize-1);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200182 if (!ZSTD_isError(r)) goto _output_error;
183 if (ZSTD_getErrorCode((size_t)r) != ZSTD_error_srcSize_wrong) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100184 DISPLAYLEVEL(4, "OK \n");
185
186 DISPLAYLEVEL(4, "test%3i : decompress with 1 too much byte : ", testNb++);
Yann Colletd2858e92016-05-30 15:10:09 +0200187 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize+1);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200188 if (!ZSTD_isError(r)) goto _output_error;
189 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100190 DISPLAYLEVEL(4, "OK \n");
191
Yann Collet0ef68032017-03-29 16:58:57 -0700192
Yann Colletc7fe2622017-05-23 13:16:00 -0700193 /* Static CCtx tests */
194#define STATIC_CCTX_LEVEL 3
195 DISPLAYLEVEL(4, "test%3i : create static CCtx for level %u :", testNb++, STATIC_CCTX_LEVEL);
196 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(STATIC_CCTX_LEVEL, 0, 0);
197 size_t const staticCCtxSize = ZSTD_estimateCStreamSize(cParams);
Yann Collet0fdc71c2017-05-24 17:41:41 -0700198 void* const staticCCtxBuffer = malloc(staticCCtxSize);
199 size_t const staticDCtxSize = ZSTD_estimateDCtxSize();
200 void* const staticDCtxBuffer = malloc(staticDCtxSize);
201 if (staticCCtxBuffer==NULL || staticDCtxBuffer==NULL) {
202 free(staticCCtxBuffer);
203 free(staticDCtxBuffer);
Yann Colletc7fe2622017-05-23 13:16:00 -0700204 DISPLAY("Not enough memory, aborting\n");
205 testResult = 1;
206 goto _end;
207 }
208 { ZSTD_CCtx* staticCCtx = ZSTD_initStaticCCtx(staticCCtxBuffer, staticCCtxSize);
Yann Collet0fdc71c2017-05-24 17:41:41 -0700209 ZSTD_DCtx* staticDCtx = ZSTD_initStaticDCtx(staticDCtxBuffer, staticDCtxSize);
210 if ((staticCCtx==NULL) || (staticDCtx==NULL)) goto _output_error;
Yann Colletc7fe2622017-05-23 13:16:00 -0700211 DISPLAYLEVEL(4, "OK \n");
212
213 DISPLAYLEVEL(4, "test%3i : init CCtx for level %u : ", testNb++, STATIC_CCTX_LEVEL);
214 { size_t const r = ZSTD_compressBegin(staticCCtx, STATIC_CCTX_LEVEL);
215 if (ZSTD_isError(r)) goto _output_error; }
216 DISPLAYLEVEL(4, "OK \n");
217
218 DISPLAYLEVEL(4, "test%3i : simple compression test with static CCtx : ", testNb++);
219 CHECKPLUS(r, ZSTD_compressCCtx(staticCCtx,
220 compressedBuffer, ZSTD_compressBound(CNBuffSize),
221 CNBuffer, CNBuffSize, STATIC_CCTX_LEVEL),
222 cSize=r );
Yann Collet0fdc71c2017-05-24 17:41:41 -0700223 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n",
224 (U32)cSize, (double)cSize/CNBuffSize*100);
Yann Colletc7fe2622017-05-23 13:16:00 -0700225
Yann Collet0fdc71c2017-05-24 17:41:41 -0700226 DISPLAYLEVEL(4, "test%3i : simple decompression test with static DCtx : ", testNb++);
227 { size_t const r = ZSTD_decompressDCtx(staticDCtx,
228 decodedBuffer, CNBuffSize,
229 compressedBuffer, cSize);
Yann Colletc7fe2622017-05-23 13:16:00 -0700230 if (r != CNBuffSize) goto _output_error; }
231 DISPLAYLEVEL(4, "OK \n");
232
Yann Collet0fdc71c2017-05-24 17:41:41 -0700233 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
234 { size_t u;
235 for (u=0; u<CNBuffSize; u++) {
236 if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u])
237 goto _output_error;;
238 } }
239 DISPLAYLEVEL(4, "OK \n");
240
Yann Colletc7fe2622017-05-23 13:16:00 -0700241 DISPLAYLEVEL(4, "test%3i : init CCtx for too large level (must fail) : ", testNb++);
242 { size_t const r = ZSTD_compressBegin(staticCCtx, ZSTD_maxCLevel());
243 if (!ZSTD_isError(r)) goto _output_error; }
244 DISPLAYLEVEL(4, "OK \n");
245
246 DISPLAYLEVEL(4, "test%3i : init CCtx for small level %u (should work again) : ", testNb++, 1);
247 { size_t const r = ZSTD_compressBegin(staticCCtx, 1);
248 if (ZSTD_isError(r)) goto _output_error; }
249 DISPLAYLEVEL(4, "OK \n");
250
251 DISPLAYLEVEL(4, "test%3i : init CStream for small level %u : ", testNb++, 1);
252 { size_t const r = ZSTD_initCStream(staticCCtx, 1);
253 if (ZSTD_isError(r)) goto _output_error; }
254 DISPLAYLEVEL(4, "OK \n");
255
256 DISPLAYLEVEL(4, "test%3i : init CStream with dictionary (should fail) : ", testNb++);
257 { size_t const r = ZSTD_initCStream_usingDict(staticCCtx, CNBuffer, 64 KB, 1);
258 if (!ZSTD_isError(r)) goto _output_error; }
259 DISPLAYLEVEL(4, "OK \n");
Yann Collet0fdc71c2017-05-24 17:41:41 -0700260
261 DISPLAYLEVEL(4, "test%3i : init DStream (should fail) : ", testNb++);
262 { size_t const r = ZSTD_initDStream(staticDCtx);
263 if (ZSTD_isError(r)) goto _output_error; }
264 { ZSTD_outBuffer output = { decodedBuffer, CNBuffSize, 0 };
265 ZSTD_inBuffer input = { compressedBuffer, ZSTD_FRAMEHEADERSIZE_MAX+1, 0 };
266 size_t const r = ZSTD_decompressStream(staticDCtx, &output, &input);
267 if (!ZSTD_isError(r)) goto _output_error;
268 }
269 DISPLAYLEVEL(4, "OK \n");
Yann Colletc7fe2622017-05-23 13:16:00 -0700270 }
271 free(staticCCtxBuffer);
Yann Collet0fdc71c2017-05-24 17:41:41 -0700272 free(staticDCtxBuffer);
Yann Colletc7fe2622017-05-23 13:16:00 -0700273 }
274
275
276
Yann Collet1e1e26f2017-03-29 17:09:59 -0700277 /* ZSTDMT simple MT compression test */
Yann Collet0ef68032017-03-29 16:58:57 -0700278 DISPLAYLEVEL(4, "test%3i : create ZSTDMT CCtx : ", testNb++);
279 { ZSTDMT_CCtx* mtctx = ZSTDMT_createCCtx(2);
280 if (mtctx==NULL) {
281 DISPLAY("mtctx : mot enough memory, aborting \n");
282 testResult = 1;
283 goto _end;
284 }
285 DISPLAYLEVEL(4, "OK \n");
286
287 DISPLAYLEVEL(4, "test%3i : compress %u bytes with 2 threads : ", testNb++, (U32)CNBuffSize);
288 CHECKPLUS(r, ZSTDMT_compressCCtx(mtctx,
289 compressedBuffer, ZSTD_compressBound(CNBuffSize),
290 CNBuffer, CNBuffSize,
291 1),
292 cSize=r );
293 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
294
295 DISPLAYLEVEL(4, "test%3i : decompressed size test : ", testNb++);
296 { unsigned long long const rSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
297 if (rSize != CNBuffSize) {
298 DISPLAY("ZSTD_getFrameContentSize incorrect : %u != %u \n", (U32)rSize, (U32)CNBuffSize);
299 goto _output_error;
300 } }
301 DISPLAYLEVEL(4, "OK \n");
302
303 DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)CNBuffSize);
304 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
305 if (r != CNBuffSize) goto _output_error; }
306 DISPLAYLEVEL(4, "OK \n");
307
308 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
309 { size_t u;
310 for (u=0; u<CNBuffSize; u++) {
311 if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;;
312 } }
313 DISPLAYLEVEL(4, "OK \n");
314
315 ZSTDMT_freeCCtx(mtctx);
316 }
317
318
Sean Purcellba2ad9f2017-02-07 16:16:55 -0800319 /* Simple API multiframe test */
320 DISPLAYLEVEL(4, "test%3i : compress multiple frames : ", testNb++);
321 { size_t off = 0;
322 int i;
323 int const segs = 4;
324 /* only use the first half so we don't push against size limit of compressedBuffer */
325 size_t const segSize = (CNBuffSize / 2) / segs;
326 for (i = 0; i < segs; i++) {
327 CHECK_V(r,
328 ZSTD_compress(
329 (BYTE *)compressedBuffer + off, CNBuffSize - off,
330 (BYTE *)CNBuffer + segSize * i,
331 segSize, 5));
332 off += r;
333 if (i == segs/2) {
334 /* insert skippable frame */
335 const U32 skipLen = 128 KB;
336 MEM_writeLE32((BYTE*)compressedBuffer + off, ZSTD_MAGIC_SKIPPABLE_START);
337 MEM_writeLE32((BYTE*)compressedBuffer + off + 4, skipLen);
338 off += skipLen + ZSTD_skippableHeaderSize;
339 }
340 }
341 cSize = off;
342 }
343 DISPLAYLEVEL(4, "OK \n");
344
345 DISPLAYLEVEL(4, "test%3i : get decompressed size of multiple frames : ", testNb++);
346 { unsigned long long const r = ZSTD_findDecompressedSize(compressedBuffer, cSize);
347 if (r != CNBuffSize / 2) goto _output_error; }
348 DISPLAYLEVEL(4, "OK \n");
349
350 DISPLAYLEVEL(4, "test%3i : decompress multiple frames : ", testNb++);
351 { CHECK_V(r, ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize));
352 if (r != CNBuffSize / 2) goto _output_error; }
353 DISPLAYLEVEL(4, "OK \n");
354
355 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
356 if (memcmp(decodedBuffer, CNBuffer, CNBuffSize / 2) != 0) goto _output_error;
357 DISPLAYLEVEL(4, "OK \n");
358
Yann Collet389648c2016-04-12 19:13:08 +0200359 /* Dictionary and CCtx Duplication tests */
Yann Colletc0a9bf32016-05-30 01:56:08 +0200360 { ZSTD_CCtx* const ctxOrig = ZSTD_createCCtx();
361 ZSTD_CCtx* const ctxDuplicated = ZSTD_createCCtx();
Yann Colletc0a9bf32016-05-30 01:56:08 +0200362 static const size_t dictSize = 551;
Yann Collet60096272016-01-08 17:27:50 +0100363
Yann Collet887e7da2016-04-11 20:12:27 +0200364 DISPLAYLEVEL(4, "test%3i : copy context too soon : ", testNb++);
Yann Collet97b378a2016-09-21 17:20:19 +0200365 { size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200366 if (!ZSTD_isError(copyResult)) goto _output_error; } /* error must be detected */
Yann Collet887e7da2016-04-11 20:12:27 +0200367 DISPLAYLEVEL(4, "OK \n");
368
Yann Collet60096272016-01-08 17:27:50 +0100369 DISPLAYLEVEL(4, "test%3i : load dictionary into context : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200370 CHECK( ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2) );
Sean Purcell84b37cc2017-02-09 12:27:32 -0800371 CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0) ); /* Begin_usingDict implies unknown srcSize, so match that */
Yann Collet60096272016-01-08 17:27:50 +0100372 DISPLAYLEVEL(4, "OK \n");
373
Yann Collet30009522016-05-30 16:17:33 +0200374 DISPLAYLEVEL(4, "test%3i : compress with flat dictionary : ", testNb++);
Yann Collet60096272016-01-08 17:27:50 +0100375 cSize = 0;
Yann Collet62470b42016-07-28 15:29:08 +0200376 CHECKPLUS(r, ZSTD_compressEnd(ctxOrig, compressedBuffer, ZSTD_compressBound(CNBuffSize),
Yann Colletd2858e92016-05-30 15:10:09 +0200377 (const char*)CNBuffer + dictSize, CNBuffSize - dictSize),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200378 cSize += r);
Yann Colletd2858e92016-05-30 15:10:09 +0200379 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
Yann Collet60096272016-01-08 17:27:50 +0100380
Yann Collet30009522016-05-30 16:17:33 +0200381 DISPLAYLEVEL(4, "test%3i : frame built with flat dictionary should be decompressible : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200382 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
Yann Colletd2858e92016-05-30 15:10:09 +0200383 decodedBuffer, CNBuffSize,
Yann Colletc0a9bf32016-05-30 01:56:08 +0200384 compressedBuffer, cSize,
385 CNBuffer, dictSize),
Yann Colletd2858e92016-05-30 15:10:09 +0200386 if (r != CNBuffSize - dictSize) goto _output_error);
Yann Collet60096272016-01-08 17:27:50 +0100387 DISPLAYLEVEL(4, "OK \n");
388
389 DISPLAYLEVEL(4, "test%3i : compress with duplicated context : ", testNb++);
Yann Collet389648c2016-04-12 19:13:08 +0200390 { size_t const cSizeOrig = cSize;
391 cSize = 0;
Yann Collet62470b42016-07-28 15:29:08 +0200392 CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(CNBuffSize),
Yann Colletd2858e92016-05-30 15:10:09 +0200393 (const char*)CNBuffer + dictSize, CNBuffSize - dictSize),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200394 cSize += r);
Yann Collet227cc392016-07-15 11:27:09 +0200395 if (cSize != cSizeOrig) goto _output_error; /* should be identical ==> same size */
Yann Collet389648c2016-04-12 19:13:08 +0200396 }
Yann Colletd2858e92016-05-30 15:10:09 +0200397 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
Yann Collet60096272016-01-08 17:27:50 +0100398
399 DISPLAYLEVEL(4, "test%3i : frame built with duplicated context should be decompressible : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200400 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
Yann Colletd2858e92016-05-30 15:10:09 +0200401 decodedBuffer, CNBuffSize,
Yann Collet60096272016-01-08 17:27:50 +0100402 compressedBuffer, cSize,
Yann Colletc0a9bf32016-05-30 01:56:08 +0200403 CNBuffer, dictSize),
Yann Colletd2858e92016-05-30 15:10:09 +0200404 if (r != CNBuffSize - dictSize) goto _output_error);
Yann Collet60096272016-01-08 17:27:50 +0100405 DISPLAYLEVEL(4, "OK \n");
Yann Collet541dc7c2016-04-12 18:00:20 +0200406
Yann Collet8dff9562017-02-25 10:11:15 -0800407 DISPLAYLEVEL(4, "test%3i : decompress with DDict : ", testNb++);
Yann Colletbd7fa212017-02-26 14:43:07 -0800408 { ZSTD_DDict* const ddict = ZSTD_createDDict_byReference(CNBuffer, dictSize);
Yann Collet8dff9562017-02-25 10:11:15 -0800409 size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict);
410 if (r != CNBuffSize - dictSize) goto _output_error;
411 DISPLAYLEVEL(4, "OK (size of DDict : %u) \n", (U32)ZSTD_sizeof_DDict(ddict));
412 ZSTD_freeDDict(ddict);
413 }
414
Yann Collet541dc7c2016-04-12 18:00:20 +0200415 DISPLAYLEVEL(4, "test%3i : check content size on duplicated context : ", testNb++);
Yann Colletd2858e92016-05-30 15:10:09 +0200416 { size_t const testSize = CNBuffSize / 3;
Yann Collet6c6e1752016-06-27 15:28:45 +0200417 { ZSTD_parameters p = ZSTD_getParams(2, testSize, dictSize);
418 p.fParams.contentSizeFlag = 1;
Yann Colletd2858e92016-05-30 15:10:09 +0200419 CHECK( ZSTD_compressBegin_advanced(ctxOrig, CNBuffer, dictSize, p, testSize-1) );
Yann Collet33341de2016-05-29 23:09:51 +0200420 }
Yann Collet97b378a2016-09-21 17:20:19 +0200421 CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, testSize) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200422
Yann Collet97b378a2016-09-21 17:20:19 +0200423 CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(testSize),
424 (const char*)CNBuffer + dictSize, testSize),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200425 cSize = r);
Yann Collet542c9df2017-05-09 15:46:07 -0700426 { ZSTD_frameHeader fp;
427 if (ZSTD_getFrameHeader(&fp, compressedBuffer, cSize)) goto _output_error;
Yann Collet541dc7c2016-04-12 18:00:20 +0200428 if ((fp.frameContentSize != testSize) && (fp.frameContentSize != 0)) goto _output_error;
429 } }
430 DISPLAYLEVEL(4, "OK \n");
431
432 ZSTD_freeCCtx(ctxOrig);
433 ZSTD_freeCCtx(ctxDuplicated);
Yann Collet60096272016-01-08 17:27:50 +0100434 }
435
Yann Collet30009522016-05-30 16:17:33 +0200436 /* Dictionary and dictBuilder tests */
437 { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
Yann Collet30009522016-05-30 16:17:33 +0200438 size_t dictSize = 16 KB;
439 void* dictBuffer = malloc(dictSize);
440 size_t const totalSampleSize = 1 MB;
441 size_t const sampleUnitSize = 8 KB;
Yann Colletb81cbba2016-05-30 22:29:45 +0200442 U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
Yann Collet30009522016-05-30 16:17:33 +0200443 size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
Yann Colletf586bdf2016-12-06 06:11:46 +0100444 U32 dictID;
Yann Collet30009522016-05-30 16:17:33 +0200445
446 if (dictBuffer==NULL || samplesSizes==NULL) {
447 free(dictBuffer);
448 free(samplesSizes);
449 goto _output_error;
450 }
451
452 DISPLAYLEVEL(4, "test%3i : dictBuilder : ", testNb++);
453 { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
454 dictSize = ZDICT_trainFromBuffer(dictBuffer, dictSize,
455 CNBuffer, samplesSizes, nbSamples);
456 if (ZDICT_isError(dictSize)) goto _output_error;
457 DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize);
458
Yann Colletda3fbcb2016-08-19 14:23:58 +0200459 DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++);
Yann Colletf586bdf2016-12-06 06:11:46 +0100460 dictID = ZDICT_getDictID(dictBuffer, dictSize);
461 if (dictID==0) goto _output_error;
462 DISPLAYLEVEL(4, "OK : %u \n", dictID);
Yann Colletda3fbcb2016-08-19 14:23:58 +0200463
Yann Collet30009522016-05-30 16:17:33 +0200464 DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++);
465 cSize = ZSTD_compress_usingDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
466 CNBuffer, CNBuffSize,
467 dictBuffer, dictSize, 4);
468 if (ZSTD_isError(cSize)) goto _output_error;
469 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
470
Yann Collete7a41a52016-12-05 16:21:06 -0800471 DISPLAYLEVEL(4, "test%3i : retrieve dictID from dictionary : ", testNb++);
Yann Colletf586bdf2016-12-06 06:11:46 +0100472 { U32 const did = ZSTD_getDictID_fromDict(dictBuffer, dictSize);
473 if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
Yann Collete7a41a52016-12-05 16:21:06 -0800474 }
475 DISPLAYLEVEL(4, "OK \n");
476
477 DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++);
Yann Colletf586bdf2016-12-06 06:11:46 +0100478 { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
479 if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
Yann Collete7a41a52016-12-05 16:21:06 -0800480 }
481 DISPLAYLEVEL(4, "OK \n");
482
Yann Collet30009522016-05-30 16:17:33 +0200483 DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++);
484 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
485 decodedBuffer, CNBuffSize,
486 compressedBuffer, cSize,
487 dictBuffer, dictSize),
488 if (r != CNBuffSize) goto _output_error);
489 DISPLAYLEVEL(4, "OK \n");
490
Yann Colleta1d67042017-05-08 17:51:49 -0700491 DISPLAYLEVEL(4, "test%3i : estimate CDict size : ", testNb++);
492 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
Yann Collet25989e32017-05-25 15:07:37 -0700493 size_t const estimatedSize = ZSTD_estimateCDictSize(cParams, dictSize, 1);
Yann Colleta1d67042017-05-08 17:51:49 -0700494 DISPLAYLEVEL(4, "OK : %u \n", (U32)estimatedSize);
495 }
496
Nick Terrell39a6cc52017-04-03 21:00:44 -0700497 DISPLAYLEVEL(4, "test%3i : compress with preprocessed dictionary : ", testNb++);
Yann Collet2f734272017-04-27 14:39:39 -0700498 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
Yann Collet25989e32017-05-25 15:07:37 -0700499 ZSTD_customMem const customMem = { NULL, NULL, NULL };
500 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize,
501 1 /* by Referece */, cParams, customMem);
Yann Collet2f734272017-04-27 14:39:39 -0700502 cSize = ZSTD_compress_usingCDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
Nick Terrell39a6cc52017-04-03 21:00:44 -0700503 CNBuffer, CNBuffSize, cdict);
Yann Collet2f734272017-04-27 14:39:39 -0700504 ZSTD_freeCDict(cdict);
505 if (ZSTD_isError(cSize)) goto _output_error;
Nick Terrell39a6cc52017-04-03 21:00:44 -0700506 }
507 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
508
509 DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++);
510 { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
511 if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
512 }
513 DISPLAYLEVEL(4, "OK \n");
514
Nick Terrell39a6cc52017-04-03 21:00:44 -0700515 DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++);
516 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
517 decodedBuffer, CNBuffSize,
518 compressedBuffer, cSize,
519 dictBuffer, dictSize),
520 if (r != CNBuffSize) goto _output_error);
521 DISPLAYLEVEL(4, "OK \n");
522
Yann Collet2f734272017-04-27 14:39:39 -0700523 DISPLAYLEVEL(4, "test%3i : ZSTD_compress_usingCDict_advanced, no contentSize, no dictID : ", testNb++);
524 { ZSTD_frameParameters const fParams = { 0 /* frameSize */, 1 /* checksum */, 1 /* noDictID*/ };
525 ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
526 ZSTD_customMem const customMem = { NULL, NULL, NULL };
527 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, 1, cParams, customMem);
528 cSize = ZSTD_compress_usingCDict_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
529 CNBuffer, CNBuffSize, cdict, fParams);
530 ZSTD_freeCDict(cdict);
531 if (ZSTD_isError(cSize)) goto _output_error;
532 }
533 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
534
535 DISPLAYLEVEL(4, "test%3i : try retrieving contentSize from frame : ", testNb++);
536 { U64 const contentSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
537 if (contentSize != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
538 }
539 DISPLAYLEVEL(4, "OK (unknown)\n");
540
541 DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++);
542 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
543 decodedBuffer, CNBuffSize,
544 compressedBuffer, cSize,
545 dictBuffer, dictSize),
546 if (r != CNBuffSize) goto _output_error);
547 DISPLAYLEVEL(4, "OK \n");
548
549 DISPLAYLEVEL(4, "test%3i : ZSTD_compress_advanced, no dictID : ", testNb++);
Yann Collet6c6e1752016-06-27 15:28:45 +0200550 { ZSTD_parameters p = ZSTD_getParams(3, CNBuffSize, dictSize);
551 p.fParams.noDictIDFlag = 1;
Yann Collet30009522016-05-30 16:17:33 +0200552 cSize = ZSTD_compress_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
553 CNBuffer, CNBuffSize,
554 dictBuffer, dictSize, p);
555 if (ZSTD_isError(cSize)) goto _output_error;
556 }
557 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
558
559 DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++);
560 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
561 decodedBuffer, CNBuffSize,
562 compressedBuffer, cSize,
563 dictBuffer, dictSize),
564 if (r != CNBuffSize) goto _output_error);
565 DISPLAYLEVEL(4, "OK \n");
566
Sean Purcell834ab502017-01-11 17:31:06 -0800567 DISPLAYLEVEL(4, "test%3i : dictionary containing only header should return error : ", testNb++);
568 {
569 const size_t ret = ZSTD_decompress_usingDict(
570 dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize,
571 "\x37\xa4\x30\xec\x11\x22\x33\x44", 8);
572 if (ZSTD_getErrorCode(ret) != ZSTD_error_dictionary_corrupted) goto _output_error;
573 }
Sean Purcellc44c4d52017-01-12 09:38:29 -0800574 DISPLAYLEVEL(4, "OK \n");
Sean Purcell834ab502017-01-11 17:31:06 -0800575
Yann Collet30009522016-05-30 16:17:33 +0200576 ZSTD_freeCCtx(cctx);
Yann Collet30009522016-05-30 16:17:33 +0200577 free(dictBuffer);
578 free(samplesSizes);
579 }
580
Nick Terrella8b4fe02017-01-02 18:45:19 -0800581 /* COVER dictionary builder tests */
582 { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
Nick Terrella8b4fe02017-01-02 18:45:19 -0800583 size_t dictSize = 16 KB;
584 size_t optDictSize = dictSize;
585 void* dictBuffer = malloc(dictSize);
586 size_t const totalSampleSize = 1 MB;
587 size_t const sampleUnitSize = 8 KB;
588 U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
589 size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
590 COVER_params_t params;
591 U32 dictID;
592
593 if (dictBuffer==NULL || samplesSizes==NULL) {
594 free(dictBuffer);
595 free(samplesSizes);
596 goto _output_error;
597 }
598
599 DISPLAYLEVEL(4, "test%3i : COVER_trainFromBuffer : ", testNb++);
600 { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
601 memset(&params, 0, sizeof(params));
602 params.d = 1 + (FUZ_rand(&seed) % 16);
603 params.k = params.d + (FUZ_rand(&seed) % 256);
604 dictSize = COVER_trainFromBuffer(dictBuffer, dictSize,
605 CNBuffer, samplesSizes, nbSamples,
606 params);
607 if (ZDICT_isError(dictSize)) goto _output_error;
608 DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize);
609
610 DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++);
611 dictID = ZDICT_getDictID(dictBuffer, dictSize);
612 if (dictID==0) goto _output_error;
613 DISPLAYLEVEL(4, "OK : %u \n", dictID);
614
615 DISPLAYLEVEL(4, "test%3i : COVER_optimizeTrainFromBuffer : ", testNb++);
616 memset(&params, 0, sizeof(params));
617 params.steps = 4;
618 optDictSize = COVER_optimizeTrainFromBuffer(dictBuffer, optDictSize,
Yann Colletc0b17312017-02-28 01:02:46 -0800619 CNBuffer, samplesSizes, nbSamples / 4,
Nick Terrella8b4fe02017-01-02 18:45:19 -0800620 &params);
621 if (ZDICT_isError(optDictSize)) goto _output_error;
622 DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)optDictSize);
623
624 DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++);
625 dictID = ZDICT_getDictID(dictBuffer, optDictSize);
626 if (dictID==0) goto _output_error;
627 DISPLAYLEVEL(4, "OK : %u \n", dictID);
628
629 ZSTD_freeCCtx(cctx);
Nick Terrella8b4fe02017-01-02 18:45:19 -0800630 free(dictBuffer);
631 free(samplesSizes);
632 }
633
Yann Collet4856a002015-01-24 01:58:16 +0100634 /* Decompression defense tests */
635 DISPLAYLEVEL(4, "test%3i : Check input length for magic number : ", testNb++);
Yann Collet20d5e032017-04-11 18:34:02 -0700636 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 3); /* too small input */
Yann Colletc0a9bf32016-05-30 01:56:08 +0200637 if (!ZSTD_isError(r)) goto _output_error;
Yann Collet20d5e032017-04-11 18:34:02 -0700638 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100639 DISPLAYLEVEL(4, "OK \n");
640
641 DISPLAYLEVEL(4, "test%3i : Check magic Number : ", testNb++);
642 ((char*)(CNBuffer))[0] = 1;
Yann Colletd2858e92016-05-30 15:10:09 +0200643 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 4);
Yann Collet33341de2016-05-29 23:09:51 +0200644 if (!ZSTD_isError(r)) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100645 DISPLAYLEVEL(4, "OK \n");
646
Yann Collet20d5e032017-04-11 18:34:02 -0700647 /* content size verification test */
648 DISPLAYLEVEL(4, "test%3i : Content size verification : ", testNb++);
649 { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
650 size_t const srcSize = 5000;
651 size_t const wrongSrcSize = (srcSize + 1000);
652 ZSTD_parameters params = ZSTD_getParams(1, wrongSrcSize, 0);
653 params.fParams.contentSizeFlag = 1;
654 { size_t const result = ZSTD_compressBegin_advanced(cctx, NULL, 0, params, wrongSrcSize);
655 if (ZSTD_isError(result)) goto _output_error;
656 }
657 { size_t const result = ZSTD_compressEnd(cctx, decodedBuffer, CNBuffSize, CNBuffer, srcSize);
658 if (!ZSTD_isError(result)) goto _output_error;
659 if (ZSTD_getErrorCode(result) != ZSTD_error_srcSize_wrong) goto _output_error;
660 DISPLAYLEVEL(4, "OK : %s \n", ZSTD_getErrorName(result));
Yann Colletf913cbe2017-04-13 22:46:41 -0700661 }
662 ZSTD_freeCCtx(cctx);
663 }
Yann Collet20d5e032017-04-11 18:34:02 -0700664
Yann Colletbf42c8e2016-01-09 01:08:23 +0100665 /* block API tests */
Yann Colletd1d210f2016-03-19 12:12:07 +0100666 { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
Yann Colletd3d2db52016-07-15 12:20:26 +0200667 static const size_t dictSize = 65 KB;
668 static const size_t blockSize = 100 KB; /* won't cause pb with small dict size */
Yann Colletd4f4e582016-06-27 01:31:35 +0200669 size_t cSize2;
Yann Colletbf42c8e2016-01-09 01:08:23 +0100670
671 /* basic block compression */
672 DISPLAYLEVEL(4, "test%3i : Block compression test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200673 CHECK( ZSTD_compressBegin(cctx, 5) );
Yann Colletfa3671e2017-05-19 10:51:30 -0700674 CHECK( ZSTD_getBlockSize(cctx) >= blockSize);
Yann Colletbf42c8e2016-01-09 01:08:23 +0100675 cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize);
676 if (ZSTD_isError(cSize)) goto _output_error;
677 DISPLAYLEVEL(4, "OK \n");
678
679 DISPLAYLEVEL(4, "test%3i : Block decompression test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200680 CHECK( ZSTD_decompressBegin(dctx) );
Yann Colletd4f4e582016-06-27 01:31:35 +0200681 { CHECK_V(r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
Yann Colletd2858e92016-05-30 15:10:09 +0200682 if (r != blockSize) goto _output_error; }
Yann Colletbf42c8e2016-01-09 01:08:23 +0100683 DISPLAYLEVEL(4, "OK \n");
684
Yann Colletb0125102016-01-09 02:00:10 +0100685 /* dictionary block compression */
686 DISPLAYLEVEL(4, "test%3i : Dictionary Block compression test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200687 CHECK( ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5) );
Yann Colletb0125102016-01-09 02:00:10 +0100688 cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize);
689 if (ZSTD_isError(cSize)) goto _output_error;
Yann Colletd4f4e582016-06-27 01:31:35 +0200690 cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize+blockSize, blockSize);
691 if (ZSTD_isError(cSize2)) goto _output_error;
Yann Colletaa2628d2016-07-07 15:28:41 +0200692 memcpy((char*)compressedBuffer+cSize, (char*)CNBuffer+dictSize+blockSize, blockSize); /* fake non-compressed block */
693 cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize+blockSize, ZSTD_compressBound(blockSize),
694 (char*)CNBuffer+dictSize+2*blockSize, blockSize);
695 if (ZSTD_isError(cSize2)) goto _output_error;
Yann Colletb0125102016-01-09 02:00:10 +0100696 DISPLAYLEVEL(4, "OK \n");
697
698 DISPLAYLEVEL(4, "test%3i : Dictionary Block decompression test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200699 CHECK( ZSTD_decompressBegin_usingDict(dctx, CNBuffer, dictSize) );
Yann Colletd4f4e582016-06-27 01:31:35 +0200700 { CHECK_V( r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
701 if (r != blockSize) goto _output_error; }
Yann Colletaa2628d2016-07-07 15:28:41 +0200702 ZSTD_insertBlock(dctx, (char*)decodedBuffer+blockSize, blockSize); /* insert non-compressed block into dctx history */
703 { CHECK_V( r, ZSTD_decompressBlock(dctx, (char*)decodedBuffer+2*blockSize, CNBuffSize, (char*)compressedBuffer+cSize+blockSize, cSize2) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200704 if (r != blockSize) goto _output_error; }
Yann Colletb0125102016-01-09 02:00:10 +0100705 DISPLAYLEVEL(4, "OK \n");
706
Yann Colletbf42c8e2016-01-09 01:08:23 +0100707 ZSTD_freeCCtx(cctx);
708 ZSTD_freeDCtx(dctx);
709 }
710
Yann Collet213089c2015-06-18 07:43:16 -0800711 /* long rle test */
Yann Colletd1d210f2016-03-19 12:12:07 +0100712 { size_t sampleSize = 0;
Yann Collet213089c2015-06-18 07:43:16 -0800713 DISPLAYLEVEL(4, "test%3i : Long RLE test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200714 RDG_genBuffer(CNBuffer, sampleSize, compressibility, 0., seed+1);
Yann Collet213089c2015-06-18 07:43:16 -0800715 memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1);
716 sampleSize += 256 KB - 1;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200717 RDG_genBuffer((char*)CNBuffer+sampleSize, 96 KB, compressibility, 0., seed+2);
Yann Collet213089c2015-06-18 07:43:16 -0800718 sampleSize += 96 KB;
Yann Collet5be2dd22015-11-11 13:43:58 +0100719 cSize = ZSTD_compress(compressedBuffer, ZSTD_compressBound(sampleSize), CNBuffer, sampleSize, 1);
Yann Collet213089c2015-06-18 07:43:16 -0800720 if (ZSTD_isError(cSize)) goto _output_error;
Yann Colletd4f4e582016-06-27 01:31:35 +0200721 { CHECK_V(regenSize, ZSTD_decompress(decodedBuffer, sampleSize, compressedBuffer, cSize));
Yann Colletc0a9bf32016-05-30 01:56:08 +0200722 if (regenSize!=sampleSize) goto _output_error; }
Yann Collet213089c2015-06-18 07:43:16 -0800723 DISPLAYLEVEL(4, "OK \n");
724 }
725
Yann Colletc0a9bf32016-05-30 01:56:08 +0200726 /* All zeroes test (test bug #137) */
Yann Collet4ba85342016-03-07 20:01:45 +0100727 #define ZEROESLENGTH 100
728 DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH);
729 memset(CNBuffer, 0, ZEROESLENGTH);
Yann Colletd4f4e582016-06-27 01:31:35 +0200730 { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200731 cSize = r; }
Yann Collet4ba85342016-03-07 20:01:45 +0100732 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100);
733
734 DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH);
Yann Colletd4f4e582016-06-27 01:31:35 +0200735 { CHECK_V(r, ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200736 if (r != ZEROESLENGTH) goto _output_error; }
Yann Collet4ba85342016-03-07 20:01:45 +0100737 DISPLAYLEVEL(4, "OK \n");
738
739 /* nbSeq limit test */
Yann Colletd1d210f2016-03-19 12:12:07 +0100740 #define _3BYTESTESTLENGTH 131000
741 #define NB3BYTESSEQLOG 9
742 #define NB3BYTESSEQ (1 << NB3BYTESSEQLOG)
743 #define NB3BYTESSEQMASK (NB3BYTESSEQ-1)
Yann Collet0d9ce042016-03-19 13:21:08 +0100744 /* creates a buffer full of 3-bytes sequences */
Yann Colletd1d210f2016-03-19 12:12:07 +0100745 { BYTE _3BytesSeqs[NB3BYTESSEQ][3];
Yann Collet0d9ce042016-03-19 13:21:08 +0100746 U32 rSeed = 1;
Yann Collet4ba85342016-03-07 20:01:45 +0100747
Yann Collet0d9ce042016-03-19 13:21:08 +0100748 /* create batch of 3-bytes sequences */
Yann Colletf323bf72016-07-07 13:14:21 +0200749 { int i;
750 for (i=0; i < NB3BYTESSEQ; i++) {
751 _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&rSeed) & 255);
752 _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&rSeed) & 255);
753 _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&rSeed) & 255);
754 } }
Yann Collet4ba85342016-03-07 20:01:45 +0100755
Yann Collet0d9ce042016-03-19 13:21:08 +0100756 /* randomly fills CNBuffer with prepared 3-bytes sequences */
Yann Colletf323bf72016-07-07 13:14:21 +0200757 { int i;
758 for (i=0; i < _3BYTESTESTLENGTH; i += 3) { /* note : CNBuffer size > _3BYTESTESTLENGTH+3 */
759 U32 const id = FUZ_rand(&rSeed) & NB3BYTESSEQMASK;
760 ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0];
761 ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1];
762 ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2];
763 } } }
Yann Collet0d9ce042016-03-19 13:21:08 +0100764 DISPLAYLEVEL(4, "test%3i : compress lots 3-bytes sequences : ", testNb++);
Yann Colletd4f4e582016-06-27 01:31:35 +0200765 { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200766 CNBuffer, _3BYTESTESTLENGTH, 19) );
767 cSize = r; }
Yann Collet0d9ce042016-03-19 13:21:08 +0100768 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/_3BYTESTESTLENGTH*100);
Yann Collet4ba85342016-03-07 20:01:45 +0100769
Yann Collet0d9ce042016-03-19 13:21:08 +0100770 DISPLAYLEVEL(4, "test%3i : decompress lots 3-bytes sequence : ", testNb++);
Yann Colletd4f4e582016-06-27 01:31:35 +0200771 { CHECK_V(r, ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200772 if (r != _3BYTESTESTLENGTH) goto _output_error; }
Yann Collet0d9ce042016-03-19 13:21:08 +0100773 DISPLAYLEVEL(4, "OK \n");
Yann Collet4ba85342016-03-07 20:01:45 +0100774
Sean Purcell9050e192017-02-22 12:12:32 -0800775 /* findFrameCompressedSize on skippable frames */
776 DISPLAYLEVEL(4, "test%3i : frame compressed size of skippable frame : ", testNb++);
777 { const char* frame = "\x50\x2a\x4d\x18\x05\x0\x0\0abcde";
778 size_t const frameSrcSize = 13;
779 if (ZSTD_findFrameCompressedSize(frame, frameSrcSize) != frameSrcSize) goto _output_error; }
780 DISPLAYLEVEL(4, "OK \n");
781
Sean Purcelle0b32652017-02-08 15:31:47 -0800782 /* error string tests */
783 DISPLAYLEVEL(4, "test%3i : testing ZSTD error code strings : ", testNb++);
784 if (strcmp("No error detected", ZSTD_getErrorName((ZSTD_ErrorCode)(0-ZSTD_error_no_error))) != 0) goto _output_error;
785 if (strcmp("No error detected", ZSTD_getErrorString(ZSTD_error_no_error)) != 0) goto _output_error;
786 if (strcmp("Unspecified error code", ZSTD_getErrorString((ZSTD_ErrorCode)(0-ZSTD_error_GENERIC))) != 0) goto _output_error;
787 if (strcmp("Error (generic)", ZSTD_getErrorName((size_t)0-ZSTD_error_GENERIC)) != 0) goto _output_error;
788 if (strcmp("Error (generic)", ZSTD_getErrorString(ZSTD_error_GENERIC)) != 0) goto _output_error;
789 if (strcmp("No error detected", ZSTD_getErrorName(ZSTD_error_GENERIC)) != 0) goto _output_error;
790 DISPLAYLEVEL(4, "OK \n");
791
Yann Collet4856a002015-01-24 01:58:16 +0100792_end:
793 free(CNBuffer);
794 free(compressedBuffer);
795 free(decodedBuffer);
796 return testResult;
797
798_output_error:
799 testResult = 1;
800 DISPLAY("Error detected in Unit tests ! \n");
801 goto _end;
802}
803
804
805static size_t findDiff(const void* buf1, const void* buf2, size_t max)
806{
Yann Collet213089c2015-06-18 07:43:16 -0800807 const BYTE* b1 = (const BYTE*)buf1;
808 const BYTE* b2 = (const BYTE*)buf2;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200809 size_t u;
810 for (u=0; u<max; u++) {
811 if (b1[u] != b2[u]) break;
Yann Collet4856a002015-01-24 01:58:16 +0100812 }
Yann Colletc0a9bf32016-05-30 01:56:08 +0200813 return u;
Yann Collet4856a002015-01-24 01:58:16 +0100814}
815
Yann Collet1fce6e02016-04-08 20:26:33 +0200816
Yann Collet73213452017-04-27 14:19:34 -0700817static ZSTD_parameters FUZ_makeParams(ZSTD_compressionParameters cParams, ZSTD_frameParameters fParams)
818{
819 ZSTD_parameters params;
820 params.cParams = cParams;
821 params.fParams = fParams;
822 return params;
823}
824
Yann Collet1fce6e02016-04-08 20:26:33 +0200825static size_t FUZ_rLogLength(U32* seed, U32 logLength)
826{
827 size_t const lengthMask = ((size_t)1 << logLength) - 1;
828 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
829}
830
831static size_t FUZ_randomLength(U32* seed, U32 maxLog)
832{
833 U32 const logLength = FUZ_rand(seed) % maxLog;
834 return FUZ_rLogLength(seed, logLength);
835}
836
Yann Colletc0a9bf32016-05-30 01:56:08 +0200837#undef CHECK
Yann Collet0d9ce042016-03-19 13:21:08 +0100838#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
839 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100840
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700841static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxDurationS, double compressibility, int bigTests)
Yann Collet4856a002015-01-24 01:58:16 +0100842{
Yann Collet1fce6e02016-04-08 20:26:33 +0200843 static const U32 maxSrcLog = 23;
844 static const U32 maxSampleLog = 22;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200845 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
846 size_t const dstBufferSize = (size_t)1<<maxSampleLog;
847 size_t const cBufferSize = ZSTD_compressBound(dstBufferSize);
Yann Colletd5d9bc32015-08-23 23:13:49 +0100848 BYTE* cNoiseBuffer[5];
Yann Colletc0a9bf32016-05-30 01:56:08 +0200849 BYTE* srcBuffer; /* jumping pointer */
850 BYTE* const cBuffer = (BYTE*) malloc (cBufferSize);
851 BYTE* const dstBuffer = (BYTE*) malloc (dstBufferSize);
852 BYTE* const mirrorBuffer = (BYTE*) malloc (dstBufferSize);
Yann Colletd2858e92016-05-30 15:10:09 +0200853 ZSTD_CCtx* const refCtx = ZSTD_createCCtx();
854 ZSTD_CCtx* const ctx = ZSTD_createCCtx();
855 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
Yann Collet30009522016-05-30 16:17:33 +0200856 U32 result = 0;
857 U32 testNb = 0;
858 U32 coreSeed = seed, lseed = 0;
Yann Colletd2858e92016-05-30 15:10:09 +0200859 clock_t const startClock = clock();
Yann Colletea63bb72016-04-08 15:25:32 +0200860 clock_t const maxClockSpan = maxDurationS * CLOCKS_PER_SEC;
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700861 int const cLevelLimiter = bigTests ? 3 : 2;
Yann Collet4856a002015-01-24 01:58:16 +0100862
863 /* allocation */
Yann Colletd5d9bc32015-08-23 23:13:49 +0100864 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
865 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
866 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
867 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
868 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Collete47c4e52015-12-05 09:23:53 +0100869 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4]
Yann Colletecd651b2016-01-07 15:35:18 +0100870 || !dstBuffer || !mirrorBuffer || !cBuffer || !refCtx || !ctx || !dctx,
Yann Colletd5d9bc32015-08-23 23:13:49 +0100871 "Not enough memory, fuzzer tests cancelled");
Yann Collet4856a002015-01-24 01:58:16 +0100872
Yann Colletd5d9bc32015-08-23 23:13:49 +0100873 /* Create initial samples */
874 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
875 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
876 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
877 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
878 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
879 srcBuffer = cNoiseBuffer[2];
Yann Collet4856a002015-01-24 01:58:16 +0100880
881 /* catch up testNb */
Yann Collet546c9b12016-03-19 12:47:52 +0100882 for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed);
Yann Collet4856a002015-01-24 01:58:16 +0100883
Yann Collet546c9b12016-03-19 12:47:52 +0100884 /* main test loop */
Yann Colletea63bb72016-04-08 15:25:32 +0200885 for ( ; (testNb <= nbTests) || (FUZ_clockSpan(startClock) < maxClockSpan); testNb++ ) {
Yann Colletc0a9bf32016-05-30 01:56:08 +0200886 size_t sampleSize, maxTestSize, totalTestSize;
887 size_t cSize, totalCSize, totalGenSize;
Yann Collet1fce6e02016-04-08 20:26:33 +0200888 U64 crcOrig;
Yann Collet110cc142015-11-19 12:02:28 +0100889 BYTE* sampleBuffer;
Yann Collet4bfe4152015-12-06 13:18:37 +0100890 const BYTE* dict;
891 size_t dictSize;
Yann Collet4856a002015-01-24 01:58:16 +0100892
Yann Collet546c9b12016-03-19 12:47:52 +0100893 /* notification */
894 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
Yann Collet4c0b44f2016-11-01 11:13:22 -0700895 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Collet1c2ddba2015-12-04 17:45:35 +0100896
Yann Collet4856a002015-01-24 01:58:16 +0100897 FUZ_rand(&coreSeed);
Yann Collet0d9ce042016-03-19 13:21:08 +0100898 { U32 const prime1 = 2654435761U; lseed = coreSeed ^ prime1; }
Yann Collet1fce6e02016-04-08 20:26:33 +0200899
900 /* srcBuffer selection [0-4] */
901 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
902 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
903 else {
904 buffNb >>= 3;
905 if (buffNb & 7) {
906 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
907 buffNb = tnb[buffNb >> 3];
908 } else {
909 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
910 buffNb = tnb[buffNb >> 3];
911 } }
912 srcBuffer = cNoiseBuffer[buffNb];
913 }
914
915 /* select src segment */
Yann Colletd2858e92016-05-30 15:10:09 +0200916 sampleSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet4856a002015-01-24 01:58:16 +0100917
Yann Collet110cc142015-11-19 12:02:28 +0100918 /* create sample buffer (to catch read error with valgrind & sanitizers) */
919 sampleBuffer = (BYTE*)malloc(sampleSize);
Yann Colletd2858e92016-05-30 15:10:09 +0200920 CHECK(sampleBuffer==NULL, "not enough memory for sample buffer");
Yann Colletc0a9bf32016-05-30 01:56:08 +0200921 { size_t const sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
922 memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); }
Yann Collet110cc142015-11-19 12:02:28 +0100923 crcOrig = XXH64(sampleBuffer, sampleSize, 0);
924
Yann Collet1fce6e02016-04-08 20:26:33 +0200925 /* compression tests */
Yann Collet20d5e032017-04-11 18:34:02 -0700926 { unsigned const cLevel =
927 ( FUZ_rand(&lseed) %
928 (ZSTD_maxCLevel() - (FUZ_highbit32((U32)sampleSize) / cLevelLimiter)) )
929 + 1;
Yann Collet1fce6e02016-04-08 20:26:33 +0200930 cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel);
Yann Colleta7737f62016-09-06 09:44:59 +0200931 CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed : %s", ZSTD_getErrorName(cSize));
Yann Collet4856a002015-01-24 01:58:16 +0100932
Yann Collet1fce6e02016-04-08 20:26:33 +0200933 /* compression failure test : too small dest buffer */
934 if (cSize > 3) {
935 const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
936 const size_t tooSmallSize = cSize - missing;
937 const U32 endMark = 0x4DC2B1A9;
938 memcpy(dstBuffer+tooSmallSize, &endMark, 4);
939 { size_t const errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel);
940 CHECK(!ZSTD_isError(errorCode), "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (U32)tooSmallSize, (U32)cSize); }
941 { U32 endCheck; memcpy(&endCheck, dstBuffer+tooSmallSize, 4);
942 CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow"); }
Yann Colletd2858e92016-05-30 15:10:09 +0200943 } }
Yann Collet1fce6e02016-04-08 20:26:33 +0200944
Yann Colletf323bf72016-07-07 13:14:21 +0200945 /* Decompressed size test */
Sean Purcell4e709712017-02-07 13:50:09 -0800946 { unsigned long long const rSize = ZSTD_findDecompressedSize(cBuffer, cSize);
Yann Colletf323bf72016-07-07 13:14:21 +0200947 CHECK(rSize != sampleSize, "decompressed size incorrect");
948 }
949
Yann Collet546c9b12016-03-19 12:47:52 +0100950 /* frame header decompression test */
Yann Collet542c9df2017-05-09 15:46:07 -0700951 { ZSTD_frameHeader dParams;
952 size_t const check = ZSTD_getFrameHeader(&dParams, cBuffer, cSize);
Yann Collet346bffb2016-03-15 15:24:52 +0100953 CHECK(ZSTD_isError(check), "Frame Parameters extraction failed");
954 CHECK(dParams.frameContentSize != sampleSize, "Frame content size incorrect");
955 }
956
Yann Collet546c9b12016-03-19 12:47:52 +0100957 /* successful decompression test */
Yann Collet1fce6e02016-04-08 20:26:33 +0200958 { size_t const margin = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200959 size_t const dSize = ZSTD_decompress(dstBuffer, sampleSize + margin, cBuffer, cSize);
Yann Collet546c9b12016-03-19 12:47:52 +0100960 CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s) (srcSize : %u ; cSize : %u)", ZSTD_getErrorName(dSize), (U32)sampleSize, (U32)cSize);
Yann Collet1fce6e02016-04-08 20:26:33 +0200961 { U64 const crcDest = XXH64(dstBuffer, sampleSize, 0);
962 CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (U32)findDiff(sampleBuffer, dstBuffer, sampleSize), (U32)sampleSize);
963 } }
Yann Collet110cc142015-11-19 12:02:28 +0100964
965 free(sampleBuffer); /* no longer useful after this point */
Yann Colletf3cb79b2015-08-20 00:02:43 +0100966
967 /* truncated src decompression test */
Yann Collet1fce6e02016-04-08 20:26:33 +0200968 { size_t const missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
969 size_t const tooSmallSize = cSize - missing;
Yann Colletd2858e92016-05-30 15:10:09 +0200970 void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch read overflows */
Yann Colletf3cb79b2015-08-20 00:02:43 +0100971 CHECK(cBufferTooSmall == NULL, "not enough memory !");
Yann Colletd5d9bc32015-08-23 23:13:49 +0100972 memcpy(cBufferTooSmall, cBuffer, tooSmallSize);
Yann Collet1fce6e02016-04-08 20:26:33 +0200973 { size_t const errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize);
974 CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); }
Yann Colletf3cb79b2015-08-20 00:02:43 +0100975 free(cBufferTooSmall);
976 }
977
978 /* too small dst decompression test */
Yann Colletd1d210f2016-03-19 12:12:07 +0100979 if (sampleSize > 3) {
Yann Colletea63bb72016-04-08 15:25:32 +0200980 size_t const missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
981 size_t const tooSmallSize = sampleSize - missing;
Yann Colletf3cb79b2015-08-20 00:02:43 +0100982 static const BYTE token = 0xA9;
983 dstBuffer[tooSmallSize] = token;
Yann Collet1fce6e02016-04-08 20:26:33 +0200984 { size_t const errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize);
985 CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (U32)errorCode, (U32)tooSmallSize); }
Yann Colletf3cb79b2015-08-20 00:02:43 +0100986 CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow");
987 }
Yann Collet997f9ee2015-08-21 02:44:20 +0100988
989 /* noisy src decompression test */
Yann Colletd1d210f2016-03-19 12:12:07 +0100990 if (cSize > 6) {
991 /* insert noise into src */
992 { U32 const maxNbBits = FUZ_highbit32((U32)(cSize-4));
993 size_t pos = 4; /* preserve magic number (too easy to detect) */
994 for (;;) {
995 /* keep some original src */
996 { U32 const nbBits = FUZ_rand(&lseed) % maxNbBits;
997 size_t const mask = (1<<nbBits) - 1;
998 size_t const skipLength = FUZ_rand(&lseed) & mask;
999 pos += skipLength;
1000 }
1001 if (pos <= cSize) break;
1002 /* add noise */
Yann Collet33341de2016-05-29 23:09:51 +02001003 { U32 const nbBitsCodes = FUZ_rand(&lseed) % maxNbBits;
1004 U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0;
1005 size_t const mask = (1<<nbBits) - 1;
1006 size_t const rNoiseLength = (FUZ_rand(&lseed) & mask) + 1;
1007 size_t const noiseLength = MIN(rNoiseLength, cSize-pos);
1008 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength);
Yann Colletd1d210f2016-03-19 12:12:07 +01001009 memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength);
1010 pos += noiseLength;
1011 } } }
Yann Collet997f9ee2015-08-21 02:44:20 +01001012
1013 /* decompress noisy source */
Yann Colletd1d210f2016-03-19 12:12:07 +01001014 { U32 const endMark = 0xA9B1C3D6;
Yann Collet997f9ee2015-08-21 02:44:20 +01001015 memcpy(dstBuffer+sampleSize, &endMark, 4);
Yann Collet1fce6e02016-04-08 20:26:33 +02001016 { size_t const decompressResult = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize);
1017 /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */
1018 CHECK((!ZSTD_isError(decompressResult)) && (decompressResult>sampleSize),
1019 "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (U32)decompressResult, (U32)sampleSize);
1020 }
1021 { U32 endCheck; memcpy(&endCheck, dstBuffer+sampleSize, 4);
1022 CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow");
1023 } } } /* noisy src decompression test */
Yann Collet417890c2015-12-04 17:16:37 +01001024
Yann Collet1fce6e02016-04-08 20:26:33 +02001025 /*===== Streaming compression test, scattered segments and dictionary =====*/
1026
1027 { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -07001028 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001029 int const cLevel = (FUZ_rand(&lseed) %
1030 (ZSTD_maxCLevel() -
1031 (MAX(testLog, dictLog) / cLevelLimiter))) +
1032 1;
Yann Collet1fce6e02016-04-08 20:26:33 +02001033 maxTestSize = FUZ_rLogLength(&lseed, testLog);
1034 if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1;
1035
Sean Purcellf5e50512017-03-15 15:04:54 -07001036 dictSize = FUZ_rLogLength(&lseed, dictLog); /* needed also for decompression */
Yann Colletc0a9bf32016-05-30 01:56:08 +02001037 dict = srcBuffer + (FUZ_rand(&lseed) % (srcBufferSize - dictSize));
Yann Collet1fce6e02016-04-08 20:26:33 +02001038
Yann Colletf2a3b6e2016-05-31 18:13:56 +02001039 if (FUZ_rand(&lseed) & 0xF) {
Yann Collet33341de2016-05-29 23:09:51 +02001040 size_t const errorCode = ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, cLevel);
1041 CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode));
1042 } else {
cyanb8806312016-05-30 18:20:46 +02001043 ZSTD_compressionParameters const cPar = ZSTD_getCParams(cLevel, 0, dictSize);
Yann Colletf4bd8572017-04-27 11:31:55 -07001044 ZSTD_frameParameters const fPar = { FUZ_rand(&lseed)&1 /* contentSizeFlag */,
Yann Colletf2a3b6e2016-05-31 18:13:56 +02001045 !(FUZ_rand(&lseed)&3) /* contentChecksumFlag*/,
1046 0 /*NodictID*/ }; /* note : since dictionary is fake, dictIDflag has no impact */
Yann Collet73213452017-04-27 14:19:34 -07001047 ZSTD_parameters const p = FUZ_makeParams(cPar, fPar);
Yann Colletf4bd8572017-04-27 11:31:55 -07001048 size_t const errorCode = ZSTD_compressBegin_advanced(refCtx, dict, dictSize, p, 0);
Yann Collet33341de2016-05-29 23:09:51 +02001049 CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_advanced error : %s", ZSTD_getErrorName(errorCode));
1050 }
Yann Collet97b378a2016-09-21 17:20:19 +02001051 { size_t const errorCode = ZSTD_copyCCtx(ctx, refCtx, 0);
1052 CHECK (ZSTD_isError(errorCode), "ZSTD_copyCCtx error : %s", ZSTD_getErrorName(errorCode));
1053 } }
Yann Collet06e76972017-01-25 16:39:03 -08001054 ZSTD_setCCtxParameter(ctx, ZSTD_p_forceWindow, FUZ_rand(&lseed) & 1);
Yann Colletf4bd8572017-04-27 11:31:55 -07001055
Yann Colletd2858e92016-05-30 15:10:09 +02001056 { U32 const nbChunks = (FUZ_rand(&lseed) & 127) + 2;
1057 U32 n;
Yann Colletf4bd8572017-04-27 11:31:55 -07001058 XXH64_state_t xxhState;
1059 XXH64_reset(&xxhState, 0);
Yann Colletd2858e92016-05-30 15:10:09 +02001060 for (totalTestSize=0, cSize=0, n=0 ; n<nbChunks ; n++) {
1061 size_t const segmentSize = FUZ_randomLength(&lseed, maxSampleLog);
1062 size_t const segmentStart = FUZ_rand(&lseed) % (srcBufferSize - segmentSize);
Yann Collet417890c2015-12-04 17:16:37 +01001063
Yann Colletd2858e92016-05-30 15:10:09 +02001064 if (cBufferSize-cSize < ZSTD_compressBound(segmentSize)) break; /* avoid invalid dstBufferTooSmall */
1065 if (totalTestSize+segmentSize > maxTestSize) break;
Yann Collet417890c2015-12-04 17:16:37 +01001066
Yann Colletd2858e92016-05-30 15:10:09 +02001067 { size_t const compressResult = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+segmentStart, segmentSize);
1068 CHECK (ZSTD_isError(compressResult), "multi-segments compression error : %s", ZSTD_getErrorName(compressResult));
1069 cSize += compressResult;
1070 }
1071 XXH64_update(&xxhState, srcBuffer+segmentStart, segmentSize);
1072 memcpy(mirrorBuffer + totalTestSize, srcBuffer+segmentStart, segmentSize);
1073 totalTestSize += segmentSize;
Yann Colletf4bd8572017-04-27 11:31:55 -07001074 }
Yann Colletd2858e92016-05-30 15:10:09 +02001075
Yann Colletf4bd8572017-04-27 11:31:55 -07001076 { size_t const flushResult = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize, NULL, 0);
1077 CHECK (ZSTD_isError(flushResult), "multi-segments epilogue error : %s", ZSTD_getErrorName(flushResult));
1078 cSize += flushResult;
1079 }
1080 crcOrig = XXH64_digest(&xxhState);
Yann Collet1fce6e02016-04-08 20:26:33 +02001081 }
Yann Collete47c4e52015-12-05 09:23:53 +01001082
1083 /* streaming decompression test */
Yann Collet33341de2016-05-29 23:09:51 +02001084 if (dictSize<8) dictSize=0, dict=NULL; /* disable dictionary */
Yann Collet1fce6e02016-04-08 20:26:33 +02001085 { size_t const errorCode = ZSTD_decompressBegin_usingDict(dctx, dict, dictSize);
Yann Collet30009522016-05-30 16:17:33 +02001086 CHECK (ZSTD_isError(errorCode), "ZSTD_decompressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode)); }
Yann Collete47c4e52015-12-05 09:23:53 +01001087 totalCSize = 0;
1088 totalGenSize = 0;
Yann Colletd1d210f2016-03-19 12:12:07 +01001089 while (totalCSize < cSize) {
Yann Collet1fce6e02016-04-08 20:26:33 +02001090 size_t const inSize = ZSTD_nextSrcSizeToDecompress(dctx);
1091 size_t const genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize);
Yann Collet45dc3562016-07-12 09:47:31 +02001092 CHECK (ZSTD_isError(genSize), "ZSTD_decompressContinue error : %s", ZSTD_getErrorName(genSize));
Yann Collete47c4e52015-12-05 09:23:53 +01001093 totalGenSize += genSize;
1094 totalCSize += inSize;
1095 }
1096 CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded");
Yann Colletc0a9bf32016-05-30 01:56:08 +02001097 CHECK (totalGenSize != totalTestSize, "streaming decompressed data : wrong size")
Yann Collete47c4e52015-12-05 09:23:53 +01001098 CHECK (totalCSize != cSize, "compressed data should be fully read")
Yann Collet1fce6e02016-04-08 20:26:33 +02001099 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1100 if (crcDest!=crcOrig) {
1101 size_t const errorPos = findDiff(mirrorBuffer, dstBuffer, totalTestSize);
Yann Colletd2858e92016-05-30 15:10:09 +02001102 CHECK (1, "streaming decompressed data corrupted : byte %u / %u (%02X!=%02X)",
Yann Collet1fce6e02016-04-08 20:26:33 +02001103 (U32)errorPos, (U32)totalTestSize, dstBuffer[errorPos], mirrorBuffer[errorPos]);
1104 } }
1105 } /* for ( ; (testNb <= nbTests) */
Yann Collet1c2ddba2015-12-04 17:45:35 +01001106 DISPLAY("\r%u fuzzer tests completed \n", testNb-1);
Yann Collet4856a002015-01-24 01:58:16 +01001107
1108_cleanup:
Yann Colletecd651b2016-01-07 15:35:18 +01001109 ZSTD_freeCCtx(refCtx);
Yann Collet2f648e52015-10-29 18:23:38 +01001110 ZSTD_freeCCtx(ctx);
Yann Collete47c4e52015-12-05 09:23:53 +01001111 ZSTD_freeDCtx(dctx);
Yann Colletd5d9bc32015-08-23 23:13:49 +01001112 free(cNoiseBuffer[0]);
1113 free(cNoiseBuffer[1]);
1114 free(cNoiseBuffer[2]);
1115 free(cNoiseBuffer[3]);
1116 free(cNoiseBuffer[4]);
Yann Collet4856a002015-01-24 01:58:16 +01001117 free(cBuffer);
1118 free(dstBuffer);
Yann Collete47c4e52015-12-05 09:23:53 +01001119 free(mirrorBuffer);
Yann Collet4856a002015-01-24 01:58:16 +01001120 return result;
1121
1122_output_error:
1123 result = 1;
1124 goto _cleanup;
1125}
1126
1127
Yann Colletd1d210f2016-03-19 12:12:07 +01001128/*_*******************************************************
Yann Collet4856a002015-01-24 01:58:16 +01001129* Command line
1130*********************************************************/
Yann Colletc6915422017-04-27 16:24:53 -07001131static int FUZ_usage(const char* programName)
Yann Collet4856a002015-01-24 01:58:16 +01001132{
1133 DISPLAY( "Usage :\n");
1134 DISPLAY( " %s [args]\n", programName);
1135 DISPLAY( "\n");
1136 DISPLAY( "Arguments :\n");
1137 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
1138 DISPLAY( " -s# : Select seed (default:prompt user)\n");
1139 DISPLAY( " -t# : Select starting test number (default:0)\n");
Yann Collet0d9ce042016-03-19 13:21:08 +01001140 DISPLAY( " -P# : Select compressibility in %% (default:%u%%)\n", FUZ_compressibility_default);
Yann Collet4856a002015-01-24 01:58:16 +01001141 DISPLAY( " -v : verbose\n");
Yann Collete9853b22015-08-07 19:07:32 +01001142 DISPLAY( " -p : pause at the end\n");
Yann Collet4856a002015-01-24 01:58:16 +01001143 DISPLAY( " -h : display help and exit\n");
1144 return 0;
1145}
1146
Yann Colletc6915422017-04-27 16:24:53 -07001147/*! readU32FromChar() :
1148 @return : unsigned integer value read from input in `char` format
1149 allows and interprets K, KB, KiB, M, MB and MiB suffix.
1150 Will also modify `*stringPtr`, advancing it to position where it stopped reading.
1151 Note : function result can overflow if digit string > MAX_UINT */
1152static unsigned readU32FromChar(const char** stringPtr)
1153{
1154 unsigned result = 0;
1155 while ((**stringPtr >='0') && (**stringPtr <='9'))
1156 result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
1157 if ((**stringPtr=='K') || (**stringPtr=='M')) {
1158 result <<= 10;
1159 if (**stringPtr=='M') result <<= 10;
1160 (*stringPtr)++ ;
1161 if (**stringPtr=='i') (*stringPtr)++;
1162 if (**stringPtr=='B') (*stringPtr)++;
1163 }
1164 return result;
1165}
Yann Collet4856a002015-01-24 01:58:16 +01001166
Yann Colletd1d210f2016-03-19 12:12:07 +01001167int main(int argc, const char** argv)
Yann Collet4856a002015-01-24 01:58:16 +01001168{
Yann Colletc6915422017-04-27 16:24:53 -07001169 U32 seed = 0;
1170 int seedset = 0;
Yann Collet4856a002015-01-24 01:58:16 +01001171 int argNb;
1172 int nbTests = nbTestsDefault;
1173 int testNb = 0;
Yann Collet0d9ce042016-03-19 13:21:08 +01001174 U32 proba = FUZ_compressibility_default;
Yann Colletc6915422017-04-27 16:24:53 -07001175 int result = 0;
Yann Collet4856a002015-01-24 01:58:16 +01001176 U32 mainPause = 0;
Yann Collet0d9ce042016-03-19 13:21:08 +01001177 U32 maxDuration = 0;
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001178 int bigTests = 1;
Yann Colletc6915422017-04-27 16:24:53 -07001179 const char* const programName = argv[0];
Yann Collet4856a002015-01-24 01:58:16 +01001180
1181 /* Check command line */
Yann Colletd1d210f2016-03-19 12:12:07 +01001182 for (argNb=1; argNb<argc; argNb++) {
1183 const char* argument = argv[argNb];
Yann Collet4856a002015-01-24 01:58:16 +01001184 if(!argument) continue; /* Protection if argument empty */
1185
1186 /* Handle commands. Aggregated commands are allowed */
Yann Colletd1d210f2016-03-19 12:12:07 +01001187 if (argument[0]=='-') {
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001188
1189 if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
1190
Yann Collet4856a002015-01-24 01:58:16 +01001191 argument++;
Yann Colletd1d210f2016-03-19 12:12:07 +01001192 while (*argument!=0) {
Yann Collet4856a002015-01-24 01:58:16 +01001193 switch(*argument)
1194 {
1195 case 'h':
1196 return FUZ_usage(programName);
Yann Colletc6915422017-04-27 16:24:53 -07001197
Yann Collet4856a002015-01-24 01:58:16 +01001198 case 'v':
1199 argument++;
Yann Colletc6915422017-04-27 16:24:53 -07001200 g_displayLevel = 4;
Yann Collet4856a002015-01-24 01:58:16 +01001201 break;
Yann Colletc6915422017-04-27 16:24:53 -07001202
Yann Collet4856a002015-01-24 01:58:16 +01001203 case 'q':
1204 argument++;
1205 g_displayLevel--;
1206 break;
Yann Colletc6915422017-04-27 16:24:53 -07001207
Yann Collet4856a002015-01-24 01:58:16 +01001208 case 'p': /* pause at the end */
1209 argument++;
1210 mainPause = 1;
1211 break;
1212
1213 case 'i':
Yann Colletc6915422017-04-27 16:24:53 -07001214 argument++; maxDuration = 0;
1215 nbTests = readU32FromChar(&argument);
Yann Collet4856a002015-01-24 01:58:16 +01001216 break;
1217
Yann Collet553cf6a2015-12-04 17:25:26 +01001218 case 'T':
1219 argument++;
Yann Colletc6915422017-04-27 16:24:53 -07001220 nbTests = 0;
1221 maxDuration = readU32FromChar(&argument);
1222 if (*argument=='s') argument++; /* seconds */
1223 if (*argument=='m') maxDuration *= 60, argument++; /* minutes */
Yann Collet553cf6a2015-12-04 17:25:26 +01001224 if (*argument=='n') argument++;
Yann Collet553cf6a2015-12-04 17:25:26 +01001225 break;
1226
Yann Collet4856a002015-01-24 01:58:16 +01001227 case 's':
1228 argument++;
Yann Colletc6915422017-04-27 16:24:53 -07001229 seedset = 1;
1230 seed = readU32FromChar(&argument);
Yann Collet4856a002015-01-24 01:58:16 +01001231 break;
1232
1233 case 't':
1234 argument++;
Yann Colletc6915422017-04-27 16:24:53 -07001235 testNb = readU32FromChar(&argument);
Yann Collet4856a002015-01-24 01:58:16 +01001236 break;
1237
1238 case 'P': /* compressibility % */
1239 argument++;
Yann Colletc6915422017-04-27 16:24:53 -07001240 proba = readU32FromChar(&argument);
1241 if (proba>100) proba = 100;
Yann Collet4856a002015-01-24 01:58:16 +01001242 break;
1243
1244 default:
Yann Colletc6915422017-04-27 16:24:53 -07001245 return (FUZ_usage(programName), 1);
Yann Colletd1d210f2016-03-19 12:12:07 +01001246 } } } } /* for (argNb=1; argNb<argc; argNb++) */
Yann Collet4856a002015-01-24 01:58:16 +01001247
1248 /* Get Seed */
Yann Collet45f84ab2016-05-20 12:34:40 +02001249 DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Collet4856a002015-01-24 01:58:16 +01001250
Yann Collet3f01c882016-06-16 13:38:10 +02001251 if (!seedset) {
1252 time_t const t = time(NULL);
1253 U32 const h = XXH32(&t, sizeof(t), 1);
1254 seed = h % 10000;
1255 }
1256
Yann Collet4856a002015-01-24 01:58:16 +01001257 DISPLAY("Seed = %u\n", seed);
Yann Collet0d9ce042016-03-19 13:21:08 +01001258 if (proba!=FUZ_compressibility_default) DISPLAY("Compressibility : %u%%\n", proba);
Yann Collet4856a002015-01-24 01:58:16 +01001259
Yann Collet803c05e2016-06-16 11:32:57 +02001260 if (nbTests < testNb) nbTests = testNb;
1261
Yann Colletd1d210f2016-03-19 12:12:07 +01001262 if (testNb==0)
1263 result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */
Yann Collet4856a002015-01-24 01:58:16 +01001264 if (!result)
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001265 result = fuzzerTests(seed, nbTests, testNb, maxDuration, ((double)proba) / 100, bigTests);
Yann Colletd1d210f2016-03-19 12:12:07 +01001266 if (mainPause) {
Yann Colletb5e06dc2015-07-04 23:20:56 -08001267 int unused;
Yann Collet4856a002015-01-24 01:58:16 +01001268 DISPLAY("Press Enter \n");
Yann Colletb5e06dc2015-07-04 23:20:56 -08001269 unused = getchar();
1270 (void)unused;
Yann Collet4856a002015-01-24 01:58:16 +01001271 }
1272 return result;
1273}