blob: e08a96d8fc930d106c0bf90eb917021e03664c26 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Yann Collet4ded9e52016-08-30 10:04:33 -07002 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Colletd7883a22016-08-12 16:48:02 +020010
Yann Colletd7883a22016-08-12 16:48:02 +020011
12/*-************************************
13* Compiler specific
14**************************************/
15#ifdef _MSC_VER /* Visual Studio */
Yann Colleta5ffe3d2017-05-12 16:29:19 -070016# define _CRT_SECURE_NO_WARNINGS /* fgets */
17# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
18# pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */
Yann Colletd7883a22016-08-12 16:48:02 +020019#endif
20
21
22/*-************************************
23* Includes
24**************************************/
25#include <stdlib.h> /* free */
26#include <stdio.h> /* fgets, sscanf */
Yann Colletd7883a22016-08-12 16:48:02 +020027#include <string.h> /* strcmp */
Yann Collet01743a32017-06-16 17:56:41 -070028#include <assert.h> /* assert */
Yann Colletd7883a22016-08-12 16:48:02 +020029#include "mem.h"
Yann Colleta5ffe3d2017-05-12 16:29:19 -070030#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem, ZSTD_getDictID_fromFrame */
Yann Colletd7883a22016-08-12 16:48:02 +020031#include "zstd.h" /* ZSTD_compressBound */
Yann Collete795c8a2016-12-13 16:39:36 +010032#include "zstd_errors.h" /* ZSTD_error_srcSize_wrong */
Yann Collet736788f2017-01-19 12:12:50 -080033#include "zstdmt_compress.h"
Yann Collet33fce032017-01-16 19:46:22 -080034#include "zdict.h" /* ZDICT_trainFromBuffer */
Yann Colletd7883a22016-08-12 16:48:02 +020035#include "datagen.h" /* RDG_genBuffer */
Yann Colletef9999f2016-09-01 16:44:48 -070036#define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
Yann Colletd7883a22016-08-12 16:48:02 +020037#include "xxhash.h" /* XXH64_* */
Nick Terrelle600b5d2017-10-16 17:18:43 -070038#include "seqgen.h"
Nick Terrell9a2f6f42017-11-29 19:11:12 -080039#include "util.h"
Yann Colletd7883a22016-08-12 16:48:02 +020040
41
42/*-************************************
43* Constants
44**************************************/
45#define KB *(1U<<10)
46#define MB *(1U<<20)
47#define GB *(1U<<30)
48
49static const U32 nbTestsDefault = 10000;
Yann Colletf99c2c12017-06-21 23:35:58 -070050static const U32 g_cLevelMax_smallTests = 10;
Yann Colletd7883a22016-08-12 16:48:02 +020051#define COMPRESSIBLE_NOISE_LENGTH (10 MB)
52#define FUZ_COMPRESSIBILITY_DEFAULT 50
Yann Collet33fce032017-01-16 19:46:22 -080053static const U32 prime32 = 2654435761U;
Yann Colletd7883a22016-08-12 16:48:02 +020054
55
Yann Colletd7883a22016-08-12 16:48:02 +020056/*-************************************
57* Display Macros
58**************************************/
59#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
Yann Collet0be6fd32017-05-08 16:08:01 -070060#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { \
61 DISPLAY(__VA_ARGS__); \
62 if (g_displayLevel>=4) fflush(stderr); }
Yann Colletd7883a22016-08-12 16:48:02 +020063static U32 g_displayLevel = 2;
64
Nick Terrell9a2f6f42017-11-29 19:11:12 -080065static const U64 g_refreshRate = SEC_TO_MICRO / 6;
66static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
Yann Colletd7883a22016-08-12 16:48:02 +020067
Nick Terrell9a2f6f42017-11-29 19:11:12 -080068#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
69 if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
70 { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
71 if (g_displayLevel>=4) fflush(stderr); } }
72
73static U64 g_clockTime = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020074
75
76/*-*******************************************************
77* Fuzzer functions
78*********************************************************/
Nick Terrell9a2f6f42017-11-29 19:11:12 -080079#undef MIN
80#undef MAX
81#define MIN(a,b) ((a)<(b)?(a):(b))
Yann Colletd7883a22016-08-12 16:48:02 +020082#define MAX(a,b) ((a)>(b)?(a):(b))
Yann Colletd7883a22016-08-12 16:48:02 +020083/*! FUZ_rand() :
84 @return : a 27 bits random value, from a 32-bits `seed`.
85 `seed` is also modified */
Yann Collet95162342016-10-25 16:19:52 -070086#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
Yann Colletd7883a22016-08-12 16:48:02 +020087unsigned int FUZ_rand(unsigned int* seedPtr)
88{
Yann Collet33fce032017-01-16 19:46:22 -080089 static const U32 prime2 = 2246822519U;
Yann Colletd7883a22016-08-12 16:48:02 +020090 U32 rand32 = *seedPtr;
Yann Collet33fce032017-01-16 19:46:22 -080091 rand32 *= prime32;
Yann Colletd7883a22016-08-12 16:48:02 +020092 rand32 += prime2;
93 rand32 = FUZ_rotl32(rand32, 13);
94 *seedPtr = rand32;
95 return rand32 >> 5;
96}
97
Nick Terrelle600b5d2017-10-16 17:18:43 -070098#define CHECK(cond, ...) { \
99 if (cond) { \
100 DISPLAY("Error => "); \
101 DISPLAY(__VA_ARGS__); \
102 DISPLAY(" (seed %u, test nb %u, line %u) \n", \
103 seed, testNb, __LINE__); \
Yann Colletd6770f82017-09-28 02:14:48 -0700104 goto _output_error; \
105} }
106
Nick Terrelle600b5d2017-10-16 17:18:43 -0700107#define CHECK_Z(f) { \
108 size_t const err = f; \
109 CHECK(ZSTD_isError(err), "%s : %s ", \
110 #f, ZSTD_getErrorName(err)); \
111}
112
Yann Colletcb327632016-08-23 00:30:31 +0200113
114/*======================================================
115* Basic Unit tests
116======================================================*/
117
Yann Collet33fce032017-01-16 19:46:22 -0800118typedef struct {
119 void* start;
120 size_t size;
121 size_t filled;
122} buffer_t;
123
124static const buffer_t g_nullBuffer = { NULL, 0 , 0 };
125
126static buffer_t FUZ_createDictionary(const void* src, size_t srcSize, size_t blockSize, size_t requestedDictSize)
127{
128 buffer_t dict = { NULL, 0, 0 };
129 size_t const nbBlocks = (srcSize + (blockSize-1)) / blockSize;
130 size_t* const blockSizes = (size_t*) malloc(nbBlocks * sizeof(size_t));
131 if (!blockSizes) return dict;
132 dict.start = malloc(requestedDictSize);
133 if (!dict.start) { free(blockSizes); return dict; }
134 { size_t nb;
135 for (nb=0; nb<nbBlocks-1; nb++) blockSizes[nb] = blockSize;
136 blockSizes[nbBlocks-1] = srcSize - (blockSize * (nbBlocks-1));
137 }
138 { size_t const dictSize = ZDICT_trainFromBuffer(dict.start, requestedDictSize, src, blockSizes, (unsigned)nbBlocks);
139 free(blockSizes);
Yann Collet2c5514c2017-04-18 22:52:41 -0700140 if (ZDICT_isError(dictSize)) { free(dict.start); return g_nullBuffer; }
Yann Collet33fce032017-01-16 19:46:22 -0800141 dict.size = requestedDictSize;
142 dict.filled = dictSize;
143 return dict; /* how to return dictSize ? */
144 }
145}
146
147static void FUZ_freeDictionary(buffer_t dict)
148{
149 free(dict.start);
150}
151
Nick Terrelle600b5d2017-10-16 17:18:43 -0700152/* Round trips data and updates xxh with the decompressed data produced */
153static size_t SEQ_roundTrip(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
154 XXH64_state_t* xxh, void* data, size_t size,
155 ZSTD_EndDirective endOp)
156{
157 static BYTE compressed[1024];
158 static BYTE uncompressed[1024];
159
160 ZSTD_inBuffer cin = {data, size, 0};
161 size_t cret;
162
163 do {
164 ZSTD_outBuffer cout = {compressed, sizeof(compressed), 0};
165 ZSTD_inBuffer din = {compressed, 0, 0};
166 ZSTD_outBuffer dout = {uncompressed, 0, 0};
167
168 cret = ZSTD_compress_generic(cctx, &cout, &cin, endOp);
169 if (ZSTD_isError(cret))
170 return cret;
171
172 din.size = cout.pos;
173 while (din.pos < din.size || (endOp == ZSTD_e_end && cret == 0)) {
174 size_t dret;
175
176 dout.pos = 0;
177 dout.size = sizeof(uncompressed);
178 dret = ZSTD_decompressStream(dctx, &dout, &din);
179 if (ZSTD_isError(dret))
180 return dret;
181 XXH64_update(xxh, dout.dst, dout.pos);
182 if (dret == 0)
183 break;
184 }
185 } while (cin.pos < cin.size || (endOp != ZSTD_e_continue && cret != 0));
186 return 0;
187}
188
189/* Generates some data and round trips it */
190static size_t SEQ_generateRoundTrip(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
191 XXH64_state_t* xxh, SEQ_stream* seq,
192 SEQ_gen_type type, unsigned value)
193{
194 static BYTE data[1024];
195 size_t gen;
196
197 do {
198 SEQ_outBuffer sout = {data, sizeof(data), 0};
199 size_t ret;
200 gen = SEQ_gen(seq, type, value, &sout);
201
202 ret = SEQ_roundTrip(cctx, dctx, xxh, sout.dst, sout.pos, ZSTD_e_continue);
203 if (ZSTD_isError(ret))
204 return ret;
205 } while (gen != 0);
206
207 return 0;
208}
Yann Collet33fce032017-01-16 19:46:22 -0800209
Yann Colletbd88f632017-11-27 12:15:23 -0800210static int basicUnitTests(U32 seed, double compressibility)
Yann Colletd7883a22016-08-12 16:48:02 +0200211{
Yann Colletb3060f72016-09-09 16:44:16 +0200212 size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
Yann Colletd7883a22016-08-12 16:48:02 +0200213 void* CNBuffer = malloc(CNBufferSize);
Nick Terrelle19b0822017-11-01 13:10:03 -0700214 size_t const skippableFrameSize = 200 KB;
Yann Colletd7883a22016-08-12 16:48:02 +0200215 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
216 void* compressedBuffer = malloc(compressedBufferSize);
217 size_t const decodedBufferSize = CNBufferSize;
218 void* decodedBuffer = malloc(decodedBufferSize);
219 size_t cSize;
Yann Colletb3060f72016-09-09 16:44:16 +0200220 int testResult = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700221 U32 testNb = 1;
Yann Colletbd88f632017-11-27 12:15:23 -0800222 ZSTD_CStream* zc = ZSTD_createCStream();
223 ZSTD_DStream* zd = ZSTD_createDStream();
Stella Lau90a31bf2017-08-30 14:36:54 -0700224 ZSTDMT_CCtx* mtctx = ZSTDMT_createCCtx(2);
225
Yann Collet9ffbeea2016-12-02 18:37:38 -0800226 ZSTD_inBuffer inBuff, inBuff2;
Yann Collet53e17fb2016-08-17 01:39:22 +0200227 ZSTD_outBuffer outBuff;
Yann Collet33fce032017-01-16 19:46:22 -0800228 buffer_t dictionary = g_nullBuffer;
Yann Collet30ab64e2017-05-10 11:30:19 -0700229 size_t const dictSize = 128 KB;
Yann Collet33fce032017-01-16 19:46:22 -0800230 unsigned dictID = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200231
232 /* Create compressible test buffer */
233 if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
Yann Collet33fce032017-01-16 19:46:22 -0800234 DISPLAY("Not enough memory, aborting \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200235 goto _output_error;
236 }
237 RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
238
Yann Collet33fce032017-01-16 19:46:22 -0800239 /* Create dictionary */
Yann Collete0065cf2017-09-28 18:34:38 -0700240 DISPLAYLEVEL(3, "creating dictionary for unit tests \n");
241 dictionary = FUZ_createDictionary(CNBuffer, CNBufferSize / 2, 8 KB, 40 KB);
Yann Collet33fce032017-01-16 19:46:22 -0800242 if (!dictionary.start) {
243 DISPLAY("Error creating dictionary, aborting \n");
244 goto _output_error;
245 }
246 dictID = ZDICT_getDictID(dictionary.start, dictionary.filled);
247
Yann Collet21f76722017-12-07 03:06:01 -0500248 /* Basic compression test */
249 DISPLAYLEVEL(3, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
250 CHECK_Z( ZSTD_initCStream(zc, 1 /* cLevel */) );
251 outBuff.dst = (char*)(compressedBuffer);
252 outBuff.size = compressedBufferSize;
253 outBuff.pos = 0;
254 inBuff.src = CNBuffer;
255 inBuff.size = CNBufferSize;
256 inBuff.pos = 0;
257 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
258 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
259 { size_t const r = ZSTD_endStream(zc, &outBuff);
260 if (r != 0) goto _output_error; } /* error, or some data not flushed */
261 DISPLAYLEVEL(3, "OK (%u bytes)\n", (U32)outBuff.pos);
262
Yann Colletd7883a22016-08-12 16:48:02 +0200263 /* generate skippable frame */
264 MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
265 MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
266 cSize = skippableFrameSize + 8;
267
Yann Collet21f76722017-12-07 03:06:01 -0500268 /* Basic compression test using dict */
269 DISPLAYLEVEL(3, "test%3i : skipframe + compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collet3c1e3f82017-10-13 18:32:06 -0700270 CHECK_Z( ZSTD_initCStream_usingDict(zc, CNBuffer, dictSize, 1 /* cLevel */) );
Yann Collet53e17fb2016-08-17 01:39:22 +0200271 outBuff.dst = (char*)(compressedBuffer)+cSize;
Yann Collet3c1e3f82017-10-13 18:32:06 -0700272 assert(compressedBufferSize > cSize);
273 outBuff.size = compressedBufferSize - cSize;
Yann Collet53e17fb2016-08-17 01:39:22 +0200274 outBuff.pos = 0;
275 inBuff.src = CNBuffer;
276 inBuff.size = CNBufferSize;
277 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700278 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collet53e17fb2016-08-17 01:39:22 +0200279 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
280 { size_t const r = ZSTD_endStream(zc, &outBuff);
Yann Collet9a021c12016-08-26 09:05:06 +0200281 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Yann Collet53e17fb2016-08-17 01:39:22 +0200282 cSize += outBuff.pos;
Yann Collet736788f2017-01-19 12:12:50 -0800283 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
Yann Colletd7883a22016-08-12 16:48:02 +0200284
Yann Collet30ab64e2017-05-10 11:30:19 -0700285 /* context size functions */
286 DISPLAYLEVEL(3, "test%3i : estimate CStream size : ", testNb++);
287 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictSize);
Yann Collet96f0cde2017-09-24 16:47:02 -0700288 size_t const cstreamSize = ZSTD_estimateCStreamSize_usingCParams(cParams);
289 size_t const cdictSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy); /* uses ZSTD_initCStream_usingDict() */
290 if (ZSTD_isError(cstreamSize)) goto _output_error;
291 if (ZSTD_isError(cdictSize)) goto _output_error;
292 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)(cstreamSize + cdictSize));
Yann Collet30ab64e2017-05-10 11:30:19 -0700293 }
294
295 DISPLAYLEVEL(3, "test%3i : check actual CStream size : ", testNb++);
Yann Colletdce78922017-06-21 15:53:42 -0700296 { size_t const s = ZSTD_sizeof_CStream(zc);
297 if (ZSTD_isError(s)) goto _output_error;
298 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Colletcb327632016-08-23 00:30:31 +0200299 }
300
Yann Collet4b987ad2017-04-10 17:50:44 -0700301 /* Attempt bad compression parameters */
302 DISPLAYLEVEL(3, "test%3i : use bad compression parameters : ", testNb++);
303 { size_t r;
304 ZSTD_parameters params = ZSTD_getParams(1, 0, 0);
305 params.cParams.searchLength = 2;
306 r = ZSTD_initCStream_advanced(zc, NULL, 0, params, 0);
307 if (!ZSTD_isError(r)) goto _output_error;
308 DISPLAYLEVEL(3, "init error : %s \n", ZSTD_getErrorName(r));
309 }
310
Yann Colletd7883a22016-08-12 16:48:02 +0200311 /* skippable frame test */
Yann Collet736788f2017-01-19 12:12:50 -0800312 DISPLAYLEVEL(3, "test%3i : decompress skippable frame : ", testNb++);
Yann Colletd6770f82017-09-28 02:14:48 -0700313 CHECK_Z( ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize) );
Yann Collet53e17fb2016-08-17 01:39:22 +0200314 inBuff.src = compressedBuffer;
315 inBuff.size = cSize;
316 inBuff.pos = 0;
317 outBuff.dst = decodedBuffer;
318 outBuff.size = CNBufferSize;
319 outBuff.pos = 0;
Yann Colletdce78922017-06-21 15:53:42 -0700320 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
321 DISPLAYLEVEL(5, " ( ZSTD_decompressStream => %u ) ", (U32)r);
322 if (r != 0) goto _output_error;
323 }
Yann Colleta33ae642017-02-28 01:15:28 -0800324 if (outBuff.pos != 0) goto _output_error; /* skippable frame output len is 0 */
Yann Collet736788f2017-01-19 12:12:50 -0800325 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200326
327 /* Basic decompression test */
Yann Collet9ffbeea2016-12-02 18:37:38 -0800328 inBuff2 = inBuff;
Yann Collet736788f2017-01-19 12:12:50 -0800329 DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collet30ab64e2017-05-10 11:30:19 -0700330 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletd6770f82017-09-28 02:14:48 -0700331 CHECK_Z( ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000000000) ); /* large limit */
Yann Collet9ffbeea2016-12-02 18:37:38 -0800332 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff);
333 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
334 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
335 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800336 DISPLAYLEVEL(3, "OK \n");
Yann Collet9ffbeea2016-12-02 18:37:38 -0800337
338 /* Re-use without init */
Yann Collet736788f2017-01-19 12:12:50 -0800339 DISPLAYLEVEL(3, "test%3i : decompress again without init (re-use previous settings): ", testNb++);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800340 outBuff.pos = 0;
341 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2);
342 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 +0200343 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
344 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800345 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200346
347 /* check regenerated data is byte exact */
Yann Collet736788f2017-01-19 12:12:50 -0800348 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200349 { size_t i;
350 for (i=0; i<CNBufferSize; i++) {
351 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
352 } }
Yann Collet736788f2017-01-19 12:12:50 -0800353 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200354
Yann Colletf16f4492017-05-09 16:18:17 -0700355 /* context size functions */
356 DISPLAYLEVEL(3, "test%3i : estimate DStream size : ", testNb++);
357 { ZSTD_frameHeader fhi;
358 const void* cStart = (char*)compressedBuffer + (skippableFrameSize + 8);
359 size_t const gfhError = ZSTD_getFrameHeader(&fhi, cStart, cSize);
360 if (gfhError!=0) goto _output_error;
Yann Colletdde10b22017-06-26 17:44:26 -0700361 DISPLAYLEVEL(5, " (windowSize : %u) ", (U32)fhi.windowSize);
362 { size_t const s = ZSTD_estimateDStreamSize(fhi.windowSize)
Yann Collet25989e32017-05-25 15:07:37 -0700363 /* uses ZSTD_initDStream_usingDict() */
Stella Lauc88fb922017-08-29 11:55:02 -0700364 + ZSTD_estimateDDictSize(dictSize, ZSTD_dlm_byCopy);
Yann Colletf16f4492017-05-09 16:18:17 -0700365 if (ZSTD_isError(s)) goto _output_error;
366 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
367 } }
368
369 DISPLAYLEVEL(3, "test%3i : check actual DStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200370 { size_t const s = ZSTD_sizeof_DStream(zd);
Yann Colletcb327632016-08-23 00:30:31 +0200371 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800372 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Colletcb327632016-08-23 00:30:31 +0200373 }
374
Yann Colletd7883a22016-08-12 16:48:02 +0200375 /* Byte-by-byte decompression test */
Yann Collet736788f2017-01-19 12:12:50 -0800376 DISPLAYLEVEL(3, "test%3i : decompress byte-by-byte : ", testNb++);
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200377 { /* skippable frame */
378 size_t r = 1;
Yann Collet30ab64e2017-05-10 11:30:19 -0700379 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200380 inBuff.src = compressedBuffer;
381 outBuff.dst = decodedBuffer;
382 inBuff.pos = 0;
383 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200384 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200385 inBuff.size = inBuff.pos + 1;
386 outBuff.size = outBuff.pos + 1;
387 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200388 if (ZSTD_isError(r)) goto _output_error;
389 }
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200390 /* normal frame */
Yann Collet30ab64e2017-05-10 11:30:19 -0700391 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200392 r=1;
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200393 while (r) {
Yann Collet53e17fb2016-08-17 01:39:22 +0200394 inBuff.size = inBuff.pos + 1;
395 outBuff.size = outBuff.pos + 1;
396 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200397 if (ZSTD_isError(r)) goto _output_error;
398 }
399 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200400 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
401 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800402 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200403
404 /* check regenerated data is byte exact */
Yann Collet736788f2017-01-19 12:12:50 -0800405 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200406 { size_t i;
407 for (i=0; i<CNBufferSize; i++) {
408 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
409 } }
Yann Collet736788f2017-01-19 12:12:50 -0800410 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200411
Yann Collete795c8a2016-12-13 16:39:36 +0100412 /* _srcSize compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800413 DISPLAYLEVEL(3, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collete795c8a2016-12-13 16:39:36 +0100414 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize);
Yann Colletd564faa2016-12-18 21:39:15 +0100415 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100416 outBuff.size = compressedBufferSize;
417 outBuff.pos = 0;
418 inBuff.src = CNBuffer;
419 inBuff.size = CNBufferSize;
420 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700421 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collete795c8a2016-12-13 16:39:36 +0100422 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
423 { size_t const r = ZSTD_endStream(zc, &outBuff);
424 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Sean Purcell4e709712017-02-07 13:50:09 -0800425 { unsigned long long origSize = ZSTD_findDecompressedSize(outBuff.dst, outBuff.pos);
Yann Colletd564faa2016-12-18 21:39:15 +0100426 if ((size_t)origSize != CNBufferSize) goto _output_error; } /* exact original size must be present */
Yann Collet736788f2017-01-19 12:12:50 -0800427 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
Yann Collete795c8a2016-12-13 16:39:36 +0100428
429 /* wrong _srcSize compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800430 DISPLAYLEVEL(3, "test%3i : wrong srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1);
Yann Collete795c8a2016-12-13 16:39:36 +0100431 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1);
Yann Colletd564faa2016-12-18 21:39:15 +0100432 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100433 outBuff.size = compressedBufferSize;
434 outBuff.pos = 0;
435 inBuff.src = CNBuffer;
436 inBuff.size = CNBufferSize;
437 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700438 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collete795c8a2016-12-13 16:39:36 +0100439 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
440 { size_t const r = ZSTD_endStream(zc, &outBuff);
441 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */
Yann Collet736788f2017-01-19 12:12:50 -0800442 DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); }
Yann Collete795c8a2016-12-13 16:39:36 +0100443
Yann Collet12083a42016-09-06 15:01:51 +0200444 /* Complex context re-use scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800445 DISPLAYLEVEL(3, "test%3i : context re-use : ", testNb++);
Yann Collet12083a42016-09-06 15:01:51 +0200446 ZSTD_freeCStream(zc);
Yann Colletbd88f632017-11-27 12:15:23 -0800447 zc = ZSTD_createCStream();
Yann Collet12083a42016-09-06 15:01:51 +0200448 if (zc==NULL) goto _output_error; /* memory allocation issue */
449 /* use 1 */
450 { size_t const inSize = 513;
Yann Collet0be6fd32017-05-08 16:08:01 -0700451 DISPLAYLEVEL(5, "use1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200452 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
453 inBuff.src = CNBuffer;
454 inBuff.size = inSize;
455 inBuff.pos = 0;
456 outBuff.dst = (char*)(compressedBuffer)+cSize;
457 outBuff.size = ZSTD_compressBound(inSize);
458 outBuff.pos = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700459 DISPLAYLEVEL(5, "compress1 ");
Yann Colletd6770f82017-09-28 02:14:48 -0700460 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collet12083a42016-09-06 15:01:51 +0200461 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Yann Collet0be6fd32017-05-08 16:08:01 -0700462 DISPLAYLEVEL(5, "end1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200463 { size_t const r = ZSTD_endStream(zc, &outBuff);
464 if (r != 0) goto _output_error; } /* error, or some data not flushed */
465 }
466 /* use 2 */
467 { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */
Yann Collet0be6fd32017-05-08 16:08:01 -0700468 DISPLAYLEVEL(5, "use2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200469 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
470 inBuff.src = CNBuffer;
471 inBuff.size = inSize;
472 inBuff.pos = 0;
473 outBuff.dst = (char*)(compressedBuffer)+cSize;
474 outBuff.size = ZSTD_compressBound(inSize);
475 outBuff.pos = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700476 DISPLAYLEVEL(5, "compress2 ");
Yann Colletd6770f82017-09-28 02:14:48 -0700477 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collet12083a42016-09-06 15:01:51 +0200478 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Yann Collet0be6fd32017-05-08 16:08:01 -0700479 DISPLAYLEVEL(5, "end2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200480 { size_t const r = ZSTD_endStream(zc, &outBuff);
481 if (r != 0) goto _output_error; } /* error, or some data not flushed */
482 }
Yann Collet736788f2017-01-19 12:12:50 -0800483 DISPLAYLEVEL(3, "OK \n");
Yann Collet12083a42016-09-06 15:01:51 +0200484
Yann Collet95162342016-10-25 16:19:52 -0700485 /* CDict scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800486 DISPLAYLEVEL(3, "test%3i : digested dictionary : ", testNb++);
Yann Collet2e427422017-06-27 17:09:12 -0700487 { ZSTD_CDict* const cdict = ZSTD_createCDict(dictionary.start, dictionary.filled, 1 /*byRef*/ );
Yann Collet95162342016-10-25 16:19:52 -0700488 size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
Yann Collet15768ca2017-11-16 15:02:28 -0800489 DISPLAYLEVEL(5, "ZSTD_initCStream_usingCDict result : %u ", (U32)initError);
Yann Collet95162342016-10-25 16:19:52 -0700490 if (ZSTD_isError(initError)) goto _output_error;
491 cSize = 0;
492 outBuff.dst = compressedBuffer;
493 outBuff.size = compressedBufferSize;
494 outBuff.pos = 0;
495 inBuff.src = CNBuffer;
496 inBuff.size = CNBufferSize;
497 inBuff.pos = 0;
Yann Collet15768ca2017-11-16 15:02:28 -0800498 DISPLAYLEVEL(5, "- starting ZSTD_compressStream ");
Yann Colletd6770f82017-09-28 02:14:48 -0700499 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collet95162342016-10-25 16:19:52 -0700500 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Yann Collet15768ca2017-11-16 15:02:28 -0800501 { size_t const r = ZSTD_endStream(zc, &outBuff);
502 DISPLAYLEVEL(5, "- ZSTD_endStream result : %u ", (U32)r);
503 if (r != 0) goto _output_error; /* error, or some data not flushed */
504 }
Yann Collet95162342016-10-25 16:19:52 -0700505 cSize = outBuff.pos;
506 ZSTD_freeCDict(cdict);
Yann Collet736788f2017-01-19 12:12:50 -0800507 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
Yann Collet95162342016-10-25 16:19:52 -0700508 }
509
Yann Collet736788f2017-01-19 12:12:50 -0800510 DISPLAYLEVEL(3, "test%3i : check CStream size : ", testNb++);
Yann Collet12083a42016-09-06 15:01:51 +0200511 { size_t const s = ZSTD_sizeof_CStream(zc);
512 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800513 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Collet12083a42016-09-06 15:01:51 +0200514 }
515
Yann Collet33fce032017-01-16 19:46:22 -0800516 DISPLAYLEVEL(4, "test%3i : check Dictionary ID : ", testNb++);
517 { unsigned const dID = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
518 if (dID != dictID) goto _output_error;
519 DISPLAYLEVEL(4, "OK (%u) \n", dID);
520 }
521
Yann Collet335ad5d2016-10-25 17:47:02 -0700522 /* DDict scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800523 DISPLAYLEVEL(3, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize);
Yann Collet33fce032017-01-16 19:46:22 -0800524 { ZSTD_DDict* const ddict = ZSTD_createDDict(dictionary.start, dictionary.filled);
Yann Collet335ad5d2016-10-25 17:47:02 -0700525 size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict);
526 if (ZSTD_isError(initError)) goto _output_error;
Yann Collet5098d1f2017-12-12 12:48:31 -0800527 inBuff.src = compressedBuffer;
528 inBuff.size = cSize;
529 inBuff.pos = 0;
Yann Collet8a104fd2017-12-12 12:51:49 -0800530 outBuff.dst = decodedBuffer;
531 outBuff.size = CNBufferSize;
532 outBuff.pos = 0;
Yann Collet335ad5d2016-10-25 17:47:02 -0700533 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
534 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
535 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
536 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
537 ZSTD_freeDDict(ddict);
Yann Collet736788f2017-01-19 12:12:50 -0800538 DISPLAYLEVEL(3, "OK \n");
Yann Collet335ad5d2016-10-25 17:47:02 -0700539 }
540
Yann Collet12083a42016-09-06 15:01:51 +0200541 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet736788f2017-01-19 12:12:50 -0800542 DISPLAYLEVEL(3, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
Yann Collet17e482e2016-08-23 16:58:10 +0200543 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
544 if (!ZSTD_isError(r)) goto _output_error; }
Yann Collet736788f2017-01-19 12:12:50 -0800545 DISPLAYLEVEL(3, "OK \n");
Yann Collet17e482e2016-08-23 16:58:10 +0200546
547 /* Memory restriction */
Yann Collet736788f2017-01-19 12:12:50 -0800548 DISPLAYLEVEL(3, "test%3i : maxWindowSize < frame requirement : ", testNb++);
Yann Collet30ab64e2017-05-10 11:30:19 -0700549 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletd6770f82017-09-28 02:14:48 -0700550 CHECK_Z( ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000) ); /* too small limit */
Yann Collet5098d1f2017-12-12 12:48:31 -0800551 inBuff.src = compressedBuffer;
552 inBuff.size = cSize;
553 inBuff.pos = 0;
Yann Collet8a104fd2017-12-12 12:51:49 -0800554 outBuff.dst = decodedBuffer;
555 outBuff.size = CNBufferSize;
556 outBuff.pos = 0;
Yann Collet17e482e2016-08-23 16:58:10 +0200557 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
558 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
Yann Collet736788f2017-01-19 12:12:50 -0800559 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); }
Yann Collet17e482e2016-08-23 16:58:10 +0200560
Yann Collet7d283cd2017-04-27 14:48:34 -0700561 DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_usingCDict_advanced with masked dictID : ", testNb++);
562 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictionary.filled);
563 ZSTD_frameParameters const fParams = { 1 /* contentSize */, 1 /* checksum */, 1 /* noDictID */};
Yann Colletbd88f632017-11-27 12:15:23 -0800564 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dm_auto, cParams, ZSTD_defaultCMem);
Yann Collet8c910d22017-06-03 01:15:02 -0700565 size_t const initError = ZSTD_initCStream_usingCDict_advanced(zc, cdict, fParams, CNBufferSize);
Yann Collet7d283cd2017-04-27 14:48:34 -0700566 if (ZSTD_isError(initError)) goto _output_error;
Nick Terrell62ecad32017-04-03 20:56:39 -0700567 cSize = 0;
568 outBuff.dst = compressedBuffer;
569 outBuff.size = compressedBufferSize;
570 outBuff.pos = 0;
571 inBuff.src = CNBuffer;
572 inBuff.size = CNBufferSize;
573 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700574 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collet8c910d22017-06-03 01:15:02 -0700575 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Nick Terrell62ecad32017-04-03 20:56:39 -0700576 { size_t const r = ZSTD_endStream(zc, &outBuff);
577 if (r != 0) goto _output_error; } /* error, or some data not flushed */
578 cSize = outBuff.pos;
579 ZSTD_freeCDict(cdict);
580 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
Yann Collet4ee6b152017-04-11 11:59:44 -0700581 }
Nick Terrell62ecad32017-04-03 20:56:39 -0700582
Yann Colleta92cbb72017-04-27 15:08:56 -0700583 DISPLAYLEVEL(3, "test%3i : try retrieving dictID from frame : ", testNb++);
584 { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
585 if (did != 0) goto _output_error;
586 }
587 DISPLAYLEVEL(3, "OK (not detected) \n");
588
Yann Collet4ee6b152017-04-11 11:59:44 -0700589 DISPLAYLEVEL(3, "test%3i : decompress without dictionary : ", testNb++);
590 { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);
591 if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
592 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r));
Nick Terrell62ecad32017-04-03 20:56:39 -0700593 }
594
Yann Collet62f7efc2017-06-28 16:25:13 -0700595 DISPLAYLEVEL(3, "test%3i : compress with ZSTD_CCtx_refPrefix : ", testNb++);
Yann Colletd6770f82017-09-28 02:14:48 -0700596 CHECK_Z( ZSTD_CCtx_refPrefix(zc, dictionary.start, dictionary.filled) );
Yann Collet62f7efc2017-06-28 16:25:13 -0700597 outBuff.dst = compressedBuffer;
598 outBuff.size = compressedBufferSize;
599 outBuff.pos = 0;
600 inBuff.src = CNBuffer;
601 inBuff.size = CNBufferSize;
602 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700603 CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end) );
Yann Collet62f7efc2017-06-28 16:25:13 -0700604 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
605 cSize = outBuff.pos;
606 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
607
608 DISPLAYLEVEL(3, "test%3i : decompress with dictionary : ", testNb++);
609 { size_t const r = ZSTD_decompress_usingDict(zd,
610 decodedBuffer, CNBufferSize,
611 compressedBuffer, cSize,
612 dictionary.start, dictionary.filled);
613 if (ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
614 DISPLAYLEVEL(3, "OK \n");
615 }
616
617 DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should fail): ", testNb++);
618 { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);
619 if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
620 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r));
621 }
622
623 DISPLAYLEVEL(3, "test%3i : compress again with ZSTD_compress_generic : ", testNb++);
624 outBuff.dst = compressedBuffer;
625 outBuff.size = compressedBufferSize;
626 outBuff.pos = 0;
627 inBuff.src = CNBuffer;
628 inBuff.size = CNBufferSize;
629 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700630 CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end) );
Yann Collet62f7efc2017-06-28 16:25:13 -0700631 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
632 cSize = outBuff.pos;
633 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
634
635 DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should work): ", testNb++);
Yann Colletd6770f82017-09-28 02:14:48 -0700636 CHECK_Z( ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize) );
637 DISPLAYLEVEL(3, "OK \n");
Yann Collet62f7efc2017-06-28 16:25:13 -0700638
Yann Collet0bb381d2017-04-18 15:08:52 -0700639 /* Empty srcSize */
640 DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_advanced with pledgedSrcSize=0 and dict : ", testNb++);
641 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
642 params.fParams.contentSizeFlag = 1;
Yann Collet213ef3b2017-10-13 19:01:58 -0700643 CHECK_Z( ZSTD_initCStream_advanced(zc, dictionary.start, dictionary.filled, params, 0 /* pledgedSrcSize==0 means "empty" when params.fParams.contentSizeFlag is set */) );
Yann Collet0bb381d2017-04-18 15:08:52 -0700644 } /* cstream advanced shall write content size = 0 */
Yann Collet5098d1f2017-12-12 12:48:31 -0800645 inBuff.src = CNBuffer;
646 inBuff.size = 0;
647 inBuff.pos = 0;
Yann Collet8a104fd2017-12-12 12:51:49 -0800648 outBuff.dst = compressedBuffer;
649 outBuff.size = compressedBufferSize;
650 outBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700651 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collet0bb381d2017-04-18 15:08:52 -0700652 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
653 cSize = outBuff.pos;
654 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
655 DISPLAYLEVEL(3, "OK \n");
656
Sean Purcell2db72492017-02-09 10:50:43 -0800657 DISPLAYLEVEL(3, "test%3i : pledgedSrcSize == 0 behaves properly : ", testNb++);
658 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
659 params.fParams.contentSizeFlag = 1;
Yann Colletd6770f82017-09-28 02:14:48 -0700660 CHECK_Z( ZSTD_initCStream_advanced(zc, NULL, 0, params, 0) );
Yann Collet4ee6b152017-04-11 11:59:44 -0700661 } /* cstream advanced shall write content size = 0 */
Sean Purcell2db72492017-02-09 10:50:43 -0800662 inBuff.src = CNBuffer;
663 inBuff.size = 0;
664 inBuff.pos = 0;
665 outBuff.dst = compressedBuffer;
666 outBuff.size = compressedBufferSize;
667 outBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700668 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Sean Purcell2db72492017-02-09 10:50:43 -0800669 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
670 cSize = outBuff.pos;
671 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
672
673 ZSTD_resetCStream(zc, 0); /* resetCStream should treat 0 as unknown */
Yann Collet5098d1f2017-12-12 12:48:31 -0800674 inBuff.src = CNBuffer;
675 inBuff.size = 0;
676 inBuff.pos = 0;
Yann Collet8a104fd2017-12-12 12:51:49 -0800677 outBuff.dst = compressedBuffer;
678 outBuff.size = compressedBufferSize;
679 outBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700680 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Sean Purcell2db72492017-02-09 10:50:43 -0800681 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
682 cSize = outBuff.pos;
683 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
684 DISPLAYLEVEL(3, "OK \n");
Yann Collet17e482e2016-08-23 16:58:10 +0200685
Stella Lau90a31bf2017-08-30 14:36:54 -0700686 /* Basic multithreading compression test */
687 DISPLAYLEVEL(3, "test%3i : compress %u bytes with multiple threads : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Colletd6770f82017-09-28 02:14:48 -0700688 { ZSTD_parameters const params = ZSTD_getParams(1, 0, 0);
689 CHECK_Z( ZSTDMT_initCStream_advanced(mtctx, CNBuffer, dictSize, params, CNBufferSize) );
690 }
Yann Collet8a104fd2017-12-12 12:51:49 -0800691 outBuff.dst = (char*)(compressedBuffer);
Stella Lau90a31bf2017-08-30 14:36:54 -0700692 outBuff.size = compressedBufferSize;
693 outBuff.pos = 0;
694 inBuff.src = CNBuffer;
695 inBuff.size = CNBufferSize;
696 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700697 CHECK_Z( ZSTDMT_compressStream_generic(mtctx, &outBuff, &inBuff, ZSTD_e_end) );
Stella Lau90a31bf2017-08-30 14:36:54 -0700698 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
699 { size_t const r = ZSTDMT_endStream(mtctx, &outBuff);
700 if (r != 0) goto _output_error; } /* error, or some data not flushed */
701 DISPLAYLEVEL(3, "OK \n");
702
Nick Terrelle600b5d2017-10-16 17:18:43 -0700703 DISPLAYLEVEL(3, "test%3i : check dictionary FSE tables can represent every code : ", testNb++);
704 { unsigned const kMaxWindowLog = 24;
705 unsigned value;
706 ZSTD_compressionParameters cParams = ZSTD_getCParams(3, 1U << kMaxWindowLog, 1024);
707 ZSTD_CDict* cdict;
708 ZSTD_DDict* ddict;
709 SEQ_stream seq = SEQ_initStream(0x87654321);
710 SEQ_gen_type type;
711 XXH64_state_t xxh;
712
713 XXH64_reset(&xxh, 0);
714 cParams.windowLog = kMaxWindowLog;
715 cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem);
716 ddict = ZSTD_createDDict(dictionary.start, dictionary.filled);
717
718 if (!cdict || !ddict) goto _output_error;
719
720 ZSTD_CCtx_reset(zc);
721 ZSTD_resetDStream(zd);
722 CHECK_Z(ZSTD_CCtx_refCDict(zc, cdict));
723 CHECK_Z(ZSTD_initDStream_usingDDict(zd, ddict));
724 CHECK_Z(ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1U << kMaxWindowLog));
725 /* Test all values < 300 */
726 for (value = 0; value < 300; ++value) {
727 for (type = (SEQ_gen_type)0; type < SEQ_gen_max; ++type) {
728 CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value));
729 }
730 }
731 /* Test values 2^8 to 2^17 */
732 for (value = (1 << 8); value < (1 << 17); value <<= 1) {
733 for (type = (SEQ_gen_type)0; type < SEQ_gen_max; ++type) {
734 CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value));
735 CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value + (value >> 2)));
736 }
737 }
738 /* Test offset values up to the max window log */
739 for (value = 8; value <= kMaxWindowLog; ++value) {
740 CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, SEQ_gen_of, (1U << value) - 1));
741 }
742
743 CHECK_Z(SEQ_roundTrip(zc, zd, &xxh, NULL, 0, ZSTD_e_end));
744 CHECK(SEQ_digest(&seq) != XXH64_digest(&xxh), "SEQ XXH64 does not match");
745
746 ZSTD_freeCDict(cdict);
747 ZSTD_freeDDict(ddict);
748 }
749 DISPLAYLEVEL(3, "OK \n");
Stella Lau90a31bf2017-08-30 14:36:54 -0700750
Sean Purcell887eaa92017-02-15 16:43:45 -0800751 /* Overlen overwriting window data bug */
752 DISPLAYLEVEL(3, "test%3i : wildcopy doesn't overwrite potential match data : ", testNb++);
Sean Purcell0ed39012017-02-16 13:29:47 -0800753 { /* This test has a window size of 1024 bytes and consists of 3 blocks:
754 1. 'a' repeated 517 times
755 2. 'b' repeated 516 times
756 3. a compressed block with no literals and 3 sequence commands:
757 litlength = 0, offset = 24, match length = 24
758 litlength = 0, offset = 24, match length = 3 (this one creates an overlength write of length 2*WILDCOPY_OVERLENGTH - 3)
759 litlength = 0, offset = 1021, match length = 3 (this one will try to read from overwritten data if the buffer is too small) */
760
761 const char* testCase =
762 "\x28\xB5\x2F\xFD\x04\x00\x4C\x00\x00\x10\x61\x61\x01\x00\x00\x2A"
763 "\x80\x05\x44\x00\x00\x08\x62\x01\x00\x00\x2A\x20\x04\x5D\x00\x00"
764 "\x00\x03\x40\x00\x00\x64\x60\x27\xB0\xE0\x0C\x67\x62\xCE\xE0";
Yann Colletd6770f82017-09-28 02:14:48 -0700765 ZSTD_DStream* const zds = ZSTD_createDStream();
766 if (zds==NULL) goto _output_error;
Sean Purcell887eaa92017-02-15 16:43:45 -0800767
Yann Colletd6770f82017-09-28 02:14:48 -0700768 CHECK_Z( ZSTD_initDStream(zds) );
Sean Purcell887eaa92017-02-15 16:43:45 -0800769 inBuff.src = testCase;
Sean Purcell0ed39012017-02-16 13:29:47 -0800770 inBuff.size = 47;
Sean Purcell887eaa92017-02-15 16:43:45 -0800771 inBuff.pos = 0;
772 outBuff.dst = decodedBuffer;
773 outBuff.size = CNBufferSize;
774 outBuff.pos = 0;
775
776 while (inBuff.pos < inBuff.size) {
Yann Colletd6770f82017-09-28 02:14:48 -0700777 CHECK_Z( ZSTD_decompressStream(zds, &outBuff, &inBuff) );
Sean Purcell887eaa92017-02-15 16:43:45 -0800778 }
Przemyslaw Skibinski684858e2017-02-21 18:17:24 +0100779
780 ZSTD_freeDStream(zds);
Sean Purcell887eaa92017-02-15 16:43:45 -0800781 }
782 DISPLAYLEVEL(3, "OK \n");
783
Yann Colletd7883a22016-08-12 16:48:02 +0200784_end:
Yann Collet33fce032017-01-16 19:46:22 -0800785 FUZ_freeDictionary(dictionary);
Yann Colletd7883a22016-08-12 16:48:02 +0200786 ZSTD_freeCStream(zc);
787 ZSTD_freeDStream(zd);
Stella Lau90a31bf2017-08-30 14:36:54 -0700788 ZSTDMT_freeCCtx(mtctx);
Yann Colletd7883a22016-08-12 16:48:02 +0200789 free(CNBuffer);
790 free(compressedBuffer);
791 free(decodedBuffer);
792 return testResult;
793
794_output_error:
795 testResult = 1;
796 DISPLAY("Error detected in Unit tests ! \n");
797 goto _end;
798}
799
800
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200801/* ====== Fuzzer tests ====== */
802
Yann Colletd7883a22016-08-12 16:48:02 +0200803static size_t findDiff(const void* buf1, const void* buf2, size_t max)
804{
805 const BYTE* b1 = (const BYTE*)buf1;
806 const BYTE* b2 = (const BYTE*)buf2;
807 size_t u;
808 for (u=0; u<max; u++) {
809 if (b1[u] != b2[u]) break;
810 }
Yann Collet736788f2017-01-19 12:12:50 -0800811 DISPLAY("Error at position %u / %u \n", (U32)u, (U32)max);
Yann Colletbb002742017-01-25 16:25:38 -0800812 DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n",
813 b1[u-3], b1[u-2], b1[u-1], b1[u-0], b1[u+1], b1[u+2], b1[u+3], b1[u+4], b1[u+5]);
814 DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n",
815 b2[u-3], b2[u-2], b2[u-1], b2[u-0], b2[u+1], b2[u+2], b2[u+3], b2[u+4], b2[u+5]);
Yann Colletd7883a22016-08-12 16:48:02 +0200816 return u;
817}
818
819static size_t FUZ_rLogLength(U32* seed, U32 logLength)
820{
821 size_t const lengthMask = ((size_t)1 << logLength) - 1;
822 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
823}
824
825static size_t FUZ_randomLength(U32* seed, U32 maxLog)
826{
827 U32 const logLength = FUZ_rand(seed) % maxLog;
828 return FUZ_rLogLength(seed, logLength);
829}
830
Stella Lau9e406022017-09-06 08:39:46 -0700831/* Return value in range minVal <= v <= maxVal */
832static U32 FUZ_randomClampedLength(U32* seed, U32 minVal, U32 maxVal)
833{
834 U32 const mod = maxVal < minVal ? 1 : (maxVal + 1) - minVal;
835 return (U32)((FUZ_rand(seed) % mod) + minVal);
836}
837
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700838static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
Yann Colletd7883a22016-08-12 16:48:02 +0200839{
Yann Colletf99c2c12017-06-21 23:35:58 -0700840 U32 const maxSrcLog = bigTests ? 24 : 22;
Yann Colletd7883a22016-08-12 16:48:02 +0200841 static const U32 maxSampleLog = 19;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200842 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200843 BYTE* cNoiseBuffer[5];
Yann Colletbc32b402017-09-27 17:27:38 -0700844 size_t const copyBufferSize = srcBufferSize + (1<<maxSampleLog);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200845 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
Yann Colletbc32b402017-09-27 17:27:38 -0700846 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200847 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
848 size_t const dstBufferSize = srcBufferSize;
849 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200850 U32 result = 0;
851 U32 testNb = 0;
852 U32 coreSeed = seed;
Yann Colletbc32b402017-09-27 17:27:38 -0700853 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be re-created sometimes */
854 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be re-created sometimes */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200855 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
Nick Terrell9a2f6f42017-11-29 19:11:12 -0800856 UTIL_time_t const startClock = UTIL_getTime();
Yann Colletbc32b402017-09-27 17:27:38 -0700857 const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */
Yann Colletcf409a72016-09-26 16:41:05 +0200858 size_t dictSize = 0;
859 U32 oldTestLog = 0;
Yann Collet49f84592017-06-21 18:43:39 -0700860 U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel() : g_cLevelMax_smallTests;
Yann Colletd7883a22016-08-12 16:48:02 +0200861
862 /* allocations */
Yann Colletd7883a22016-08-12 16:48:02 +0200863 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
864 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
865 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
866 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
867 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200868 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
Yann Collet58d5dfe2016-09-25 01:34:03 +0200869 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
Yann Colletd7883a22016-08-12 16:48:02 +0200870 "Not enough memory, fuzzer tests cancelled");
871
872 /* Create initial samples */
873 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
874 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
875 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
876 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
877 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
878 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200879 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Colletd7883a22016-08-12 16:48:02 +0200880
881 /* catch up testNb */
882 for (testNb=1; testNb < startTest; testNb++)
883 FUZ_rand(&coreSeed);
884
885 /* test loop */
Nick Terrell9a2f6f42017-11-29 19:11:12 -0800886 for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200887 U32 lseed;
888 const BYTE* srcBuffer;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200889 size_t totalTestSize, totalGenSize, cSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200890 XXH64_state_t xxhState;
891 U64 crcOrig;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200892 U32 resetAllowed = 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200893 size_t maxTestSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200894
895 /* init */
Yann Collet4c0b44f2016-11-01 11:13:22 -0700896 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
897 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Colletd7883a22016-08-12 16:48:02 +0200898 FUZ_rand(&coreSeed);
Yann Collet33fce032017-01-16 19:46:22 -0800899 lseed = coreSeed ^ prime32;
Yann Colletd7883a22016-08-12 16:48:02 +0200900
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200901 /* states full reset (deliberately not synchronized) */
902 /* some issues can only happen when reusing states */
Yann Colleted1d0392017-06-19 11:07:33 -0700903 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
904 ZSTD_freeCStream(zc);
905 zc = ZSTD_createCStream();
Yann Colletdce78922017-06-21 15:53:42 -0700906 CHECK(zc==NULL, "ZSTD_createCStream : allocation error");
Yann Colleted1d0392017-06-19 11:07:33 -0700907 resetAllowed=0;
908 }
909 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
910 ZSTD_freeDStream(zd);
911 zd = ZSTD_createDStream();
Yann Colletdce78922017-06-21 15:53:42 -0700912 CHECK(zd==NULL, "ZSTD_createDStream : allocation error");
913 CHECK_Z( ZSTD_initDStream_usingDict(zd, NULL, 0) ); /* ensure at least one init */
Yann Colleted1d0392017-06-19 11:07:33 -0700914 }
Yann Colletd7883a22016-08-12 16:48:02 +0200915
916 /* srcBuffer selection [0-4] */
917 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
918 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
919 else {
920 buffNb >>= 3;
921 if (buffNb & 7) {
922 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
923 buffNb = tnb[buffNb >> 3];
924 } else {
925 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
926 buffNb = tnb[buffNb >> 3];
927 } }
928 srcBuffer = cNoiseBuffer[buffNb];
929 }
930
931 /* compression init */
Yann Colletcf409a72016-09-26 16:41:05 +0200932 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
933 && oldTestLog /* at least one test happened */ && resetAllowed) {
934 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
Yann Colletbc32b402017-09-27 17:27:38 -0700935 maxTestSize = MIN(maxTestSize, srcBufferSize-16);
Yann Colletcf409a72016-09-26 16:41:05 +0200936 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
Yann Collet01743a32017-06-16 17:56:41 -0700937 CHECK_Z( ZSTD_resetCStream(zc, pledgedSrcSize) );
Yann Colletcf409a72016-09-26 16:41:05 +0200938 }
Yann Collet58d5dfe2016-09-25 01:34:03 +0200939 } else {
940 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -0700941 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletbfc2f002017-06-21 17:57:14 -0700942 U32 const cLevelCandidate = ( FUZ_rand(&lseed) %
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700943 (ZSTD_maxCLevel() -
Yann Colletbfc2f002017-06-21 17:57:14 -0700944 (MAX(testLog, dictLog) / 3)))
Yann Colletce800982017-04-05 16:34:09 -0700945 + 1;
Yann Colletbfc2f002017-06-21 17:57:14 -0700946 U32 const cLevel = MIN(cLevelCandidate, cLevelMax);
Yann Colletd7883a22016-08-12 16:48:02 +0200947 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Colletcf409a72016-09-26 16:41:05 +0200948 oldTestLog = testLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200949 /* random dictionary selection */
Yann Colletf99c2c12017-06-21 23:35:58 -0700950 dictSize = ((FUZ_rand(&lseed)&7)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200951 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
952 dict = srcBuffer + dictStart;
953 }
Yann Collet213ef3b2017-10-13 19:01:58 -0700954 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize;
Yann Colletcf409a72016-09-26 16:41:05 +0200955 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200956 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
957 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Collet213ef3b2017-10-13 19:01:58 -0700958 params.fParams.contentSizeFlag = FUZ_rand(&lseed) & 1;
Yann Collet01743a32017-06-16 17:56:41 -0700959 CHECK_Z ( ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) );
960 } }
Yann Colletd7883a22016-08-12 16:48:02 +0200961
962 /* multi-segments compression test */
963 XXH64_reset(&xxhState, 0);
Yann Collet2f263942016-09-26 14:06:08 +0200964 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200965 U32 n;
Yann Collet2f263942016-09-26 14:06:08 +0200966 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
Yann Collete795c8a2016-12-13 16:39:36 +0100967 /* compress random chunks into randomly sized dst buffers */
Yann Collet2f263942016-09-26 14:06:08 +0200968 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet150354c2017-11-01 16:57:48 -0700969 size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize);
Yann Collet2f263942016-09-26 14:06:08 +0200970 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200971 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
972 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet2f263942016-09-26 14:06:08 +0200973 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
Yann Collet53e17fb2016-08-17 01:39:22 +0200974 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200975
Yann Colleted1d0392017-06-19 11:07:33 -0700976 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Colletd7883a22016-08-12 16:48:02 +0200977
Yann Collet53e17fb2016-08-17 01:39:22 +0200978 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
979 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
980 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200981 }
982
983 /* random flush operation, to mess around */
984 if ((FUZ_rand(&lseed) & 15) == 0) {
985 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200986 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
987 outBuff.size = outBuff.pos + adjustedDstSize;
Yann Colleted1d0392017-06-19 11:07:33 -0700988 CHECK_Z( ZSTD_flushStream(zc, &outBuff) );
989 } }
Yann Colletd7883a22016-08-12 16:48:02 +0200990
991 /* final frame epilogue */
992 { size_t remainingToFlush = (size_t)(-1);
993 while (remainingToFlush) {
994 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
995 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200996 outBuff.size = outBuff.pos + adjustedDstSize;
997 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Collet009d6042017-05-19 10:17:59 -0700998 CHECK (ZSTD_isError(remainingToFlush), "end error : %s", ZSTD_getErrorName(remainingToFlush));
Yann Colletd7883a22016-08-12 16:48:02 +0200999 } }
1000 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +02001001 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +02001002 }
1003
1004 /* multi - fragments decompression test */
Yann Collet58d5dfe2016-09-25 01:34:03 +02001005 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Colletdce78922017-06-21 15:53:42 -07001006 CHECK_Z ( ZSTD_resetDStream(zd) );
Yann Collet9ffbeea2016-12-02 18:37:38 -08001007 } else {
Yann Colletdce78922017-06-21 15:53:42 -07001008 CHECK_Z ( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
Yann Collet9ffbeea2016-12-02 18:37:38 -08001009 }
Yann Colletd7883a22016-08-12 16:48:02 +02001010 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +02001011 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1012 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1013 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +02001014 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1015 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1016 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +02001017 inBuff.size = inBuff.pos + readCSrcSize;
Yann Colletca306c12017-09-27 00:39:41 -07001018 outBuff.size = outBuff.pos + dstBuffSize;
Yann Collet53e17fb2016-08-17 01:39:22 +02001019 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletb3f33cc2017-09-09 14:37:28 -07001020 if (ZSTD_getErrorCode(decompressionResult) == ZSTD_error_checksum_wrong) {
1021 DISPLAY("checksum error : \n");
1022 findDiff(copyBuffer, dstBuffer, totalTestSize);
1023 }
1024 CHECK( ZSTD_isError(decompressionResult), "decompression error : %s",
1025 ZSTD_getErrorName(decompressionResult) );
Yann Colletd7883a22016-08-12 16:48:02 +02001026 }
1027 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Colletb3f33cc2017-09-09 14:37:28 -07001028 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)",
1029 (U32)outBuff.pos, (U32)totalTestSize);
Yann Collet53e17fb2016-08-17 01:39:22 +02001030 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +02001031 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1032 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1033 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1034 } }
1035
1036 /*===== noisy/erroneous src decompression test =====*/
1037
1038 /* add some noise */
1039 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1040 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1041 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1042 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1043 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1044 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1045 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1046 } }
1047
1048 /* try decompression on noisy data */
Yann Colletdce78922017-06-21 15:53:42 -07001049 CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */
Yann Collet53e17fb2016-08-17 01:39:22 +02001050 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1051 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1052 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +02001053 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1054 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +02001055 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
Yann Collet64bf8ff2017-01-27 17:25:07 -08001056 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
Yann Collet53e17fb2016-08-17 01:39:22 +02001057 outBuff.size = outBuff.pos + adjustedDstSize;
Yann Collet64bf8ff2017-01-27 17:25:07 -08001058 inBuff.size = inBuff.pos + adjustedCSrcSize;
Yann Collet53e17fb2016-08-17 01:39:22 +02001059 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +02001060 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Yann Collet64bf8ff2017-01-27 17:25:07 -08001061 /* No forward progress possible */
1062 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
Yann Colletd7883a22016-08-12 16:48:02 +02001063 } } } }
1064 DISPLAY("\r%u fuzzer tests completed \n", testNb);
1065
1066_cleanup:
1067 ZSTD_freeCStream(zc);
1068 ZSTD_freeDStream(zd);
Yann Collet58d5dfe2016-09-25 01:34:03 +02001069 ZSTD_freeDStream(zd_noise);
Yann Colletd7883a22016-08-12 16:48:02 +02001070 free(cNoiseBuffer[0]);
1071 free(cNoiseBuffer[1]);
1072 free(cNoiseBuffer[2]);
1073 free(cNoiseBuffer[3]);
1074 free(cNoiseBuffer[4]);
1075 free(copyBuffer);
1076 free(cBuffer);
1077 free(dstBuffer);
1078 return result;
1079
1080_output_error:
1081 result = 1;
1082 goto _cleanup;
1083}
1084
1085
Yann Collet736788f2017-01-19 12:12:50 -08001086/* Multi-threading version of fuzzer Tests */
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001087static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
Yann Collet736788f2017-01-19 12:12:50 -08001088{
Yann Colletf99c2c12017-06-21 23:35:58 -07001089 const U32 maxSrcLog = bigTests ? 24 : 22;
Yann Collet736788f2017-01-19 12:12:50 -08001090 static const U32 maxSampleLog = 19;
1091 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
1092 BYTE* cNoiseBuffer[5];
1093 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
1094 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
1095 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
1096 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
1097 size_t const dstBufferSize = srcBufferSize;
1098 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
1099 U32 result = 0;
1100 U32 testNb = 0;
1101 U32 coreSeed = seed;
Yann Colletbc32b402017-09-27 17:27:38 -07001102 U32 nbThreads = 2;
1103 ZSTDMT_CCtx* zc = ZSTDMT_createCCtx(nbThreads); /* will be reset sometimes */
Yann Collet736788f2017-01-19 12:12:50 -08001104 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
1105 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
Nick Terrell9a2f6f42017-11-29 19:11:12 -08001106 UTIL_time_t const startClock = UTIL_getTime();
Yann Collet736788f2017-01-19 12:12:50 -08001107 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
1108 size_t dictSize = 0;
1109 U32 oldTestLog = 0;
Yann Colletbc32b402017-09-27 17:27:38 -07001110 int const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests;
1111 U32 const nbThreadsMax = bigTests ? 4 : 2;
Yann Collet736788f2017-01-19 12:12:50 -08001112
1113 /* allocations */
1114 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
1115 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
1116 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
1117 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
1118 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
1119 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
1120 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
1121 "Not enough memory, fuzzer tests cancelled");
1122
1123 /* Create initial samples */
1124 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
1125 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
1126 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
1127 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
1128 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
1129 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
1130 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1131
1132 /* catch up testNb */
1133 for (testNb=1; testNb < startTest; testNb++)
1134 FUZ_rand(&coreSeed);
1135
1136 /* test loop */
Nick Terrell9a2f6f42017-11-29 19:11:12 -08001137 for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) {
Yann Collet736788f2017-01-19 12:12:50 -08001138 U32 lseed;
1139 const BYTE* srcBuffer;
1140 size_t totalTestSize, totalGenSize, cSize;
1141 XXH64_state_t xxhState;
1142 U64 crcOrig;
1143 U32 resetAllowed = 1;
1144 size_t maxTestSize;
1145
1146 /* init */
Yann Colletbc32b402017-09-27 17:27:38 -07001147 if (testNb < nbTests) {
1148 DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests);
1149 } else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Collet736788f2017-01-19 12:12:50 -08001150 FUZ_rand(&coreSeed);
Yann Colletd7e3cb52017-01-20 16:44:50 -08001151 lseed = coreSeed ^ prime32;
Yann Collet736788f2017-01-19 12:12:50 -08001152
1153 /* states full reset (deliberately not synchronized) */
1154 /* some issues can only happen when reusing states */
1155 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
Yann Collet9fe50ed2017-09-28 01:42:06 -07001156 nbThreads = (FUZ_rand(&lseed) % nbThreadsMax) + 1;
Yann Collet30c76982017-03-31 18:27:03 -07001157 DISPLAYLEVEL(5, "Creating new context with %u threads \n", nbThreads);
Yann Collet736788f2017-01-19 12:12:50 -08001158 ZSTDMT_freeCCtx(zc);
1159 zc = ZSTDMT_createCCtx(nbThreads);
Yann Colletdce78922017-06-21 15:53:42 -07001160 CHECK(zc==NULL, "ZSTDMT_createCCtx allocation error")
Yann Collet736788f2017-01-19 12:12:50 -08001161 resetAllowed=0;
1162 }
1163 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
1164 ZSTD_freeDStream(zd);
1165 zd = ZSTD_createDStream();
Yann Colletdce78922017-06-21 15:53:42 -07001166 CHECK(zd==NULL, "ZSTDMT_createCCtx allocation error")
Yann Collet736788f2017-01-19 12:12:50 -08001167 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1168 }
1169
1170 /* srcBuffer selection [0-4] */
1171 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
1172 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
1173 else {
1174 buffNb >>= 3;
1175 if (buffNb & 7) {
1176 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
1177 buffNb = tnb[buffNb >> 3];
1178 } else {
1179 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
1180 buffNb = tnb[buffNb >> 3];
1181 } }
1182 srcBuffer = cNoiseBuffer[buffNb];
1183 }
1184
1185 /* compression init */
1186 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
1187 && oldTestLog /* at least one test happened */ && resetAllowed) {
1188 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
1189 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
1190 { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
Yann Colletdce78922017-06-21 15:53:42 -07001191 CHECK_Z( ZSTDMT_initCStream(zc, compressionLevel) );
Yann Collet736788f2017-01-19 12:12:50 -08001192 }
1193 } else {
1194 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -07001195 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletbc32b402017-09-27 17:27:38 -07001196 int const cLevelCandidate = ( FUZ_rand(&lseed)
1197 % (ZSTD_maxCLevel() - (MAX(testLog, dictLog) / 2)) )
1198 + 1;
1199 int const cLevelThreadAdjusted = cLevelCandidate - (nbThreads * 2) + 2; /* reduce cLevel when multiple threads to reduce memory consumption */
1200 int const cLevelMin = MAX(cLevelThreadAdjusted, 1); /* no negative cLevel yet */
1201 int const cLevel = MIN(cLevelMin, cLevelMax);
Yann Collet736788f2017-01-19 12:12:50 -08001202 maxTestSize = FUZ_rLogLength(&lseed, testLog);
1203 oldTestLog = testLog;
1204 /* random dictionary selection */
Sean Purcellf5e50512017-03-15 15:04:54 -07001205 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
Yann Collet19d670b2017-01-19 15:32:07 -08001206 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
1207 dict = srcBuffer + dictStart;
1208 }
Yann Colletbeb9b4b2017-10-13 19:09:30 -07001209 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize;
Yann Collet19d670b2017-01-19 15:32:07 -08001210 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletce800982017-04-05 16:34:09 -07001211 DISPLAYLEVEL(5, "Init with windowLog = %u and pledgedSrcSize = %u \n",
1212 params.cParams.windowLog, (U32)pledgedSrcSize);
Yann Collet19d670b2017-01-19 15:32:07 -08001213 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
1214 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletbeb9b4b2017-10-13 19:09:30 -07001215 params.fParams.contentSizeFlag = FUZ_rand(&lseed) & 1;
Yann Collet2c5514c2017-04-18 22:52:41 -07001216 DISPLAYLEVEL(5, "checksumFlag : %u \n", params.fParams.checksumFlag);
Yann Colletdce78922017-06-21 15:53:42 -07001217 CHECK_Z( ZSTDMT_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) );
1218 CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_overlapSectionLog, FUZ_rand(&lseed) % 12) );
1219 CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_sectionSize, FUZ_rand(&lseed) % (2*maxTestSize+1)) );
Yann Colletcd23dd22017-01-30 12:46:35 -08001220 } }
Yann Collet736788f2017-01-19 12:12:50 -08001221
1222 /* multi-segments compression test */
1223 XXH64_reset(&xxhState, 0);
1224 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
1225 U32 n;
1226 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
1227 /* compress random chunks into randomly sized dst buffers */
1228 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1229 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
1230 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
1231 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1232 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
1233 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
1234 outBuff.size = outBuff.pos + dstBuffSize;
1235
1236 DISPLAYLEVEL(5, "Sending %u bytes to compress \n", (U32)srcSize);
Yann Colletdce78922017-06-21 15:53:42 -07001237 CHECK_Z( ZSTDMT_compressStream(zc, &outBuff, &inBuff) );
Yann Collet19d670b2017-01-19 15:32:07 -08001238 DISPLAYLEVEL(5, "%u bytes read by ZSTDMT_compressStream \n", (U32)inBuff.pos);
Yann Collet736788f2017-01-19 12:12:50 -08001239
1240 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
1241 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
1242 totalTestSize += inBuff.pos;
1243 }
1244
1245 /* random flush operation, to mess around */
1246 if ((FUZ_rand(&lseed) & 15) == 0) {
1247 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1248 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1249 outBuff.size = outBuff.pos + adjustedDstSize;
1250 DISPLAYLEVEL(5, "Flushing into dst buffer of size %u \n", (U32)adjustedDstSize);
Yann Colletdce78922017-06-21 15:53:42 -07001251 CHECK_Z( ZSTDMT_flushStream(zc, &outBuff) );
1252 } }
Yann Collet736788f2017-01-19 12:12:50 -08001253
1254 /* final frame epilogue */
1255 { size_t remainingToFlush = (size_t)(-1);
1256 while (remainingToFlush) {
1257 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1258 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1259 outBuff.size = outBuff.pos + adjustedDstSize;
1260 DISPLAYLEVEL(5, "Ending into dst buffer of size %u \n", (U32)adjustedDstSize);
1261 remainingToFlush = ZSTDMT_endStream(zc, &outBuff);
Yann Collet30c76982017-03-31 18:27:03 -07001262 CHECK (ZSTD_isError(remainingToFlush), "ZSTDMT_endStream error : %s", ZSTD_getErrorName(remainingToFlush));
Yann Collet736788f2017-01-19 12:12:50 -08001263 DISPLAYLEVEL(5, "endStream : remainingToFlush : %u \n", (U32)remainingToFlush);
1264 } }
Yann Collet736788f2017-01-19 12:12:50 -08001265 crcOrig = XXH64_digest(&xxhState);
1266 cSize = outBuff.pos;
Yann Collet2c5514c2017-04-18 22:52:41 -07001267 DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize);
Yann Collet736788f2017-01-19 12:12:50 -08001268 }
1269
1270 /* multi - fragments decompression test */
1271 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Colletdce78922017-06-21 15:53:42 -07001272 CHECK_Z( ZSTD_resetDStream(zd) );
Yann Collet736788f2017-01-19 12:12:50 -08001273 } else {
Yann Colletdce78922017-06-21 15:53:42 -07001274 CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
Yann Collet736788f2017-01-19 12:12:50 -08001275 }
1276 { size_t decompressionResult = 1;
1277 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1278 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1279 for (totalGenSize = 0 ; decompressionResult ; ) {
1280 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1281 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1282 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
1283 inBuff.size = inBuff.pos + readCSrcSize;
Yann Colletbfabd1d2017-09-27 01:01:11 -07001284 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletce800982017-04-05 16:34:09 -07001285 DISPLAYLEVEL(5, "ZSTD_decompressStream input %u bytes \n", (U32)readCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001286 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1287 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Collet2c5514c2017-04-18 22:52:41 -07001288 DISPLAYLEVEL(5, "inBuff.pos = %u \n", (U32)readCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001289 }
Yann Colletbb002742017-01-25 16:25:38 -08001290 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize);
1291 CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize);
Yann Collet736788f2017-01-19 12:12:50 -08001292 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1293 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1294 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1295 } }
1296
1297 /*===== noisy/erroneous src decompression test =====*/
1298
1299 /* add some noise */
1300 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1301 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1302 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1303 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1304 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1305 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1306 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1307 } }
1308
1309 /* try decompression on noisy data */
Yann Colletdce78922017-06-21 15:53:42 -07001310 CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */
Yann Collet736788f2017-01-19 12:12:50 -08001311 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1312 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1313 while (outBuff.pos < dstBufferSize) {
1314 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1315 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1316 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
Nick Terrelld98bf492017-01-27 15:42:36 -08001317 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001318 outBuff.size = outBuff.pos + adjustedDstSize;
Nick Terrelld98bf492017-01-27 15:42:36 -08001319 inBuff.size = inBuff.pos + adjustedCSrcSize;
Yann Collet736788f2017-01-19 12:12:50 -08001320 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1321 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Nick Terrelld98bf492017-01-27 15:42:36 -08001322 /* No forward progress possible */
1323 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
Yann Collet736788f2017-01-19 12:12:50 -08001324 } } } }
1325 DISPLAY("\r%u fuzzer tests completed \n", testNb);
1326
1327_cleanup:
1328 ZSTDMT_freeCCtx(zc);
1329 ZSTD_freeDStream(zd);
1330 ZSTD_freeDStream(zd_noise);
1331 free(cNoiseBuffer[0]);
1332 free(cNoiseBuffer[1]);
1333 free(cNoiseBuffer[2]);
1334 free(cNoiseBuffer[3]);
1335 free(cNoiseBuffer[4]);
1336 free(copyBuffer);
1337 free(cBuffer);
1338 free(dstBuffer);
1339 return result;
1340
1341_output_error:
1342 result = 1;
1343 goto _cleanup;
1344}
1345
Stella Lau73c73bf2017-08-21 12:41:19 -07001346/** If useOpaqueAPI, sets param in cctxParams.
1347 * Otherwise, sets the param in zc. */
1348static size_t setCCtxParameter(ZSTD_CCtx* zc, ZSTD_CCtx_params* cctxParams,
1349 ZSTD_cParameter param, unsigned value,
1350 U32 useOpaqueAPI)
1351{
1352 if (useOpaqueAPI) {
1353 return ZSTD_CCtxParam_setParameter(cctxParams, param, value);
1354 } else {
1355 return ZSTD_CCtx_setParameter(zc, param, value);
1356 }
1357}
Yann Collet736788f2017-01-19 12:12:50 -08001358
Yann Colletd7a3bff2017-06-19 11:53:01 -07001359/* Tests for ZSTD_compress_generic() API */
Stella Lau73c73bf2017-08-21 12:41:19 -07001360static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests, U32 const useOpaqueAPI)
Yann Collet01743a32017-06-16 17:56:41 -07001361{
Yann Colletf99c2c12017-06-21 23:35:58 -07001362 U32 const maxSrcLog = bigTests ? 24 : 22;
Yann Collet01743a32017-06-16 17:56:41 -07001363 static const U32 maxSampleLog = 19;
1364 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
1365 BYTE* cNoiseBuffer[5];
1366 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
1367 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
1368 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
1369 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
1370 size_t const dstBufferSize = srcBufferSize;
1371 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
1372 U32 result = 0;
1373 U32 testNb = 0;
1374 U32 coreSeed = seed;
1375 ZSTD_CCtx* zc = ZSTD_createCCtx(); /* will be reset sometimes */
1376 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
1377 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
Nick Terrell9a2f6f42017-11-29 19:11:12 -08001378 UTIL_time_t const startClock = UTIL_getTime();
Yann Colletd7a3bff2017-06-19 11:53:01 -07001379 const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */
Yann Collet01743a32017-06-16 17:56:41 -07001380 size_t dictSize = 0;
1381 U32 oldTestLog = 0;
Yann Colletaa800c42017-09-27 18:00:15 -07001382 U32 windowLogMalus = 0; /* can survive between 2 loops */
1383 U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests;
1384 U32 const nbThreadsMax = bigTests ? 4 : 2;
Stella Lau023b24e2017-08-20 22:55:07 -07001385 ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams();
Yann Collet01743a32017-06-16 17:56:41 -07001386
1387 /* allocations */
1388 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
1389 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
1390 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
1391 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
1392 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
1393 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
1394 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
1395 "Not enough memory, fuzzer tests cancelled");
1396
1397 /* Create initial samples */
1398 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
1399 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
1400 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
1401 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
1402 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
1403 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Colletdce78922017-06-21 15:53:42 -07001404 CHECK_Z( ZSTD_initDStream_usingDict(zd, NULL, 0) ); /* ensure at least one init */
Yann Collet01743a32017-06-16 17:56:41 -07001405
1406 /* catch up testNb */
1407 for (testNb=1; testNb < startTest; testNb++)
1408 FUZ_rand(&coreSeed);
1409
1410 /* test loop */
Nick Terrell9a2f6f42017-11-29 19:11:12 -08001411 for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) {
Yann Collet01743a32017-06-16 17:56:41 -07001412 U32 lseed;
1413 const BYTE* srcBuffer;
1414 size_t totalTestSize, totalGenSize, cSize;
1415 XXH64_state_t xxhState;
1416 U64 crcOrig;
1417 U32 resetAllowed = 1;
1418 size_t maxTestSize;
1419
1420 /* init */
1421 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
1422 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
1423 FUZ_rand(&coreSeed);
1424 lseed = coreSeed ^ prime32;
Yann Collet9b5b47a2017-09-28 01:25:40 -07001425 DISPLAYLEVEL(5, " *** Test %u *** \n", testNb);
Yann Collet01743a32017-06-16 17:56:41 -07001426
1427 /* states full reset (deliberately not synchronized) */
1428 /* some issues can only happen when reusing states */
1429 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
1430 DISPLAYLEVEL(5, "Creating new context \n");
1431 ZSTD_freeCCtx(zc);
1432 zc = ZSTD_createCCtx();
Yann Colletdce78922017-06-21 15:53:42 -07001433 CHECK(zc==NULL, "ZSTD_createCCtx allocation error");
Yann Collet01743a32017-06-16 17:56:41 -07001434 resetAllowed=0;
1435 }
1436 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
1437 ZSTD_freeDStream(zd);
1438 zd = ZSTD_createDStream();
Yann Colletdce78922017-06-21 15:53:42 -07001439 CHECK(zd==NULL, "ZSTD_createDStream allocation error");
Yann Collet01743a32017-06-16 17:56:41 -07001440 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1441 }
1442
1443 /* srcBuffer selection [0-4] */
1444 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
1445 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
1446 else {
1447 buffNb >>= 3;
1448 if (buffNb & 7) {
1449 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
1450 buffNb = tnb[buffNb >> 3];
1451 } else {
1452 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
1453 buffNb = tnb[buffNb >> 3];
1454 } }
1455 srcBuffer = cNoiseBuffer[buffNb];
1456 }
1457
1458 /* compression init */
Yann Colletb7372932017-06-27 15:49:12 -07001459 CHECK_Z( ZSTD_CCtx_loadDictionary(zc, NULL, 0) ); /* cancel previous dict /*/
Yann Collet01743a32017-06-16 17:56:41 -07001460 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
1461 && oldTestLog /* at least one test happened */ && resetAllowed) {
1462 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
1463 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
Yann Collet01743a32017-06-16 17:56:41 -07001464 { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
Stella Lau73c73bf2017-08-21 12:41:19 -07001465 CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_compressionLevel, compressionLevel, useOpaqueAPI) );
Yann Collet01743a32017-06-16 17:56:41 -07001466 }
1467 } else {
1468 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
1469 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletbfc2f002017-06-21 17:57:14 -07001470 U32 const cLevelCandidate = (FUZ_rand(&lseed) %
Yann Collet01743a32017-06-16 17:56:41 -07001471 (ZSTD_maxCLevel() -
Yann Collet9fe50ed2017-09-28 01:42:06 -07001472 (MAX(testLog, dictLog) / 2))) +
Yann Collet01743a32017-06-16 17:56:41 -07001473 1;
Yann Colletbfc2f002017-06-21 17:57:14 -07001474 U32 const cLevel = MIN(cLevelCandidate, cLevelMax);
Yann Collet9b5b47a2017-09-28 01:25:40 -07001475 DISPLAYLEVEL(5, "t%u: cLevel : %u \n", testNb, cLevel);
Yann Collet01743a32017-06-16 17:56:41 -07001476 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Collet9b5b47a2017-09-28 01:25:40 -07001477 DISPLAYLEVEL(5, "t%u: maxTestSize : %u \n", testNb, (U32)maxTestSize);
Yann Collet01743a32017-06-16 17:56:41 -07001478 oldTestLog = testLog;
1479 /* random dictionary selection */
1480 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
1481 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
1482 dict = srcBuffer + dictStart;
Yann Colletd7a3bff2017-06-19 11:53:01 -07001483 if (!dictSize) dict=NULL;
Yann Collet01743a32017-06-16 17:56:41 -07001484 }
1485 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize;
1486 ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletbbef0582017-09-28 11:48:45 -07001487 static const U32 windowLogMax = 24;
Yann Collet01743a32017-06-16 17:56:41 -07001488
1489 /* mess with compression parameters */
1490 cParams.windowLog += (FUZ_rand(&lseed) & 3) - 1;
Nick Terrellc233bdb2017-09-22 14:04:39 -07001491 cParams.windowLog = MIN(windowLogMax, cParams.windowLog);
Yann Collet01743a32017-06-16 17:56:41 -07001492 cParams.hashLog += (FUZ_rand(&lseed) & 3) - 1;
1493 cParams.chainLog += (FUZ_rand(&lseed) & 3) - 1;
1494 cParams.searchLog += (FUZ_rand(&lseed) & 3) - 1;
1495 cParams.searchLength += (FUZ_rand(&lseed) & 3) - 1;
Yann Colletaa800c42017-09-27 18:00:15 -07001496 cParams.targetLength = (U32)((cParams.targetLength + 1 ) * (0.5 + ((double)(FUZ_rand(&lseed) & 127) / 128)));
Yann Collet01743a32017-06-16 17:56:41 -07001497 cParams = ZSTD_adjustCParams(cParams, 0, 0);
1498
Yann Colletaa800c42017-09-27 18:00:15 -07001499 if (FUZ_rand(&lseed) & 1) {
Yann Colletd3c59ed2017-11-29 16:42:20 -08001500 DISPLAYLEVEL(5, "t%u: windowLog : %u \n", testNb, cParams.windowLog);
Yann Colletaa800c42017-09-27 18:00:15 -07001501 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_windowLog, cParams.windowLog, useOpaqueAPI) );
1502 assert(cParams.windowLog >= ZSTD_WINDOWLOG_MIN); /* guaranteed by ZSTD_adjustCParams() */
1503 windowLogMalus = (cParams.windowLog - ZSTD_WINDOWLOG_MIN) / 5;
1504 }
Yann Collet9b5b47a2017-09-28 01:25:40 -07001505 if (FUZ_rand(&lseed) & 1) {
Yann Collet9b5b47a2017-09-28 01:25:40 -07001506 DISPLAYLEVEL(5, "t%u: hashLog : %u \n", testNb, cParams.hashLog);
Yann Colletd3c59ed2017-11-29 16:42:20 -08001507 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_hashLog, cParams.hashLog, useOpaqueAPI) );
Yann Collet9b5b47a2017-09-28 01:25:40 -07001508 }
1509 if (FUZ_rand(&lseed) & 1) {
Yann Collet9b5b47a2017-09-28 01:25:40 -07001510 DISPLAYLEVEL(5, "t%u: chainLog : %u \n", testNb, cParams.chainLog);
Yann Colletd3c59ed2017-11-29 16:42:20 -08001511 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_chainLog, cParams.chainLog, useOpaqueAPI) );
Yann Collet9b5b47a2017-09-28 01:25:40 -07001512 }
Stella Lau73c73bf2017-08-21 12:41:19 -07001513 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_searchLog, cParams.searchLog, useOpaqueAPI) );
1514 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_minMatch, cParams.searchLength, useOpaqueAPI) );
1515 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_targetLength, cParams.targetLength, useOpaqueAPI) );
Yann Collet01743a32017-06-16 17:56:41 -07001516
Stella Lau9e406022017-09-06 08:39:46 -07001517 /* mess with long distance matching parameters */
Yann Colletd3c59ed2017-11-29 16:42:20 -08001518 if (bigTests) {
1519 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_enableLongDistanceMatching, FUZ_rand(&lseed) & 63, useOpaqueAPI) );
1520 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashLog, FUZ_randomClampedLength(&lseed, ZSTD_HASHLOG_MIN, 23), useOpaqueAPI) );
1521 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmMinMatch, FUZ_randomClampedLength(&lseed, ZSTD_LDM_MINMATCH_MIN, ZSTD_LDM_MINMATCH_MAX), useOpaqueAPI) );
1522 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmBucketSizeLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_LDM_BUCKETSIZELOG_MAX), useOpaqueAPI) );
1523 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashEveryLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN), useOpaqueAPI) );
1524 }
Stella Lau6a546ef2017-07-28 15:51:33 -07001525
Yann Collet01743a32017-06-16 17:56:41 -07001526 /* mess with frame parameters */
Stella Lau73c73bf2017-08-21 12:41:19 -07001527 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_checksumFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
1528 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
1529 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
Yann Colletd3c59ed2017-11-29 16:42:20 -08001530 if (FUZ_rand(&lseed) & 1) {
1531 DISPLAYLEVEL(5, "t%u: pledgedSrcSize : %u \n", testNb, (U32)pledgedSrcSize);
1532 CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) );
1533 }
Yann Collet01743a32017-06-16 17:56:41 -07001534
1535 /* multi-threading parameters */
Yann Colletbfc2f002017-06-21 17:57:14 -07001536 { U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1;
Yann Colletaa800c42017-09-27 18:00:15 -07001537 U32 const nbThreadsAdjusted = (windowLogMalus < nbThreadsCandidate) ? nbThreadsCandidate - windowLogMalus : 1;
1538 U32 const nbThreads = MIN(nbThreadsAdjusted, nbThreadsMax);
Yann Collet9b5b47a2017-09-28 01:25:40 -07001539 DISPLAYLEVEL(5, "t%u: nbThreads : %u \n", testNb, nbThreads);
Yann Colletd3c59ed2017-11-29 16:42:20 -08001540 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_nbThreads, nbThreads, useOpaqueAPI) );
Yann Collet01743a32017-06-16 17:56:41 -07001541 if (nbThreads > 1) {
Yann Colleted1d0392017-06-19 11:07:33 -07001542 U32 const jobLog = FUZ_rand(&lseed) % (testLog+1);
Stella Lau73c73bf2017-08-21 12:41:19 -07001543 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10, useOpaqueAPI) );
1544 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog), useOpaqueAPI) );
Stella Lau023b24e2017-08-20 22:55:07 -07001545 }
1546 }
1547
Yann Colletd3c59ed2017-11-29 16:42:20 -08001548 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
Stella Lau023b24e2017-08-20 22:55:07 -07001549
1550 /* Apply parameters */
Stella Lau73c73bf2017-08-21 12:41:19 -07001551 if (useOpaqueAPI) {
Yann Colletd3c59ed2017-11-29 16:42:20 -08001552 DISPLAYLEVEL(6," t%u: applying CCtxParams \n", testNb);
Stella Lau82d636b2017-08-29 18:03:06 -07001553 CHECK_Z (ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams) );
Stella Lau73c73bf2017-08-21 12:41:19 -07001554 }
Stella Lau023b24e2017-08-20 22:55:07 -07001555
1556 if (FUZ_rand(&lseed) & 1) {
Stella Laueb7bbab2017-08-25 10:48:07 -07001557 if (FUZ_rand(&lseed) & 1) {
1558 CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) );
1559 } else {
1560 CHECK_Z( ZSTD_CCtx_loadDictionary_byReference(zc, dict, dictSize) );
1561 }
Stella Lau023b24e2017-08-20 22:55:07 -07001562 if (dict && dictSize) {
1563 /* test that compression parameters are rejected (correctly) after loading a non-NULL dictionary */
Stella Lau73c73bf2017-08-21 12:41:19 -07001564 if (useOpaqueAPI) {
Stella Lau82d636b2017-08-29 18:03:06 -07001565 size_t const setError = ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams);
1566 CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParametersUsingCCtxParams should have failed");
Stella Lau73c73bf2017-08-21 12:41:19 -07001567 } else {
1568 size_t const setError = ZSTD_CCtx_setParameter(zc, ZSTD_p_windowLog, cParams.windowLog-1);
1569 CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParameter should have failed");
1570 }
1571 }
1572 } else {
Stella Lau023b24e2017-08-20 22:55:07 -07001573 CHECK_Z( ZSTD_CCtx_refPrefix(zc, dict, dictSize) );
1574 }
1575 } }
Yann Collet01743a32017-06-16 17:56:41 -07001576
1577 /* multi-segments compression test */
1578 XXH64_reset(&xxhState, 0);
1579 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet052a95f2017-07-11 17:18:26 -07001580 for (cSize=0, totalTestSize=0 ; (totalTestSize < maxTestSize) ; ) {
Yann Collet01743a32017-06-16 17:56:41 -07001581 /* compress random chunks into randomly sized dst buffers */
Yann Colleted1d0392017-06-19 11:07:33 -07001582 size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1583 size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize);
1584 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Collet052a95f2017-07-11 17:18:26 -07001585 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1);
Yann Colleted1d0392017-06-19 11:07:33 -07001586 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
1587 ZSTD_EndDirective const flush = (FUZ_rand(&lseed) & 15) ? ZSTD_e_continue : ZSTD_e_flush;
1588 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
1589 outBuff.size = outBuff.pos + dstBuffSize;
Yann Collet01743a32017-06-16 17:56:41 -07001590
Yann Colleted1d0392017-06-19 11:07:33 -07001591 CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, flush) );
Yann Colletd3c59ed2017-11-29 16:42:20 -08001592 DISPLAYLEVEL(6, "t%u: compress consumed %u bytes (total : %u) \n",
1593 testNb, (U32)inBuff.pos, (U32)(totalTestSize + inBuff.pos));
Yann Collet01743a32017-06-16 17:56:41 -07001594
Yann Colleted1d0392017-06-19 11:07:33 -07001595 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
1596 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
1597 totalTestSize += inBuff.pos;
1598 }
Yann Collet01743a32017-06-16 17:56:41 -07001599
1600 /* final frame epilogue */
1601 { size_t remainingToFlush = (size_t)(-1);
1602 while (remainingToFlush) {
1603 ZSTD_inBuffer inBuff = { NULL, 0, 0 };
Yann Collet052a95f2017-07-11 17:18:26 -07001604 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1);
Yann Collet01743a32017-06-16 17:56:41 -07001605 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1606 outBuff.size = outBuff.pos + adjustedDstSize;
Yann Colletd6770f82017-09-28 02:14:48 -07001607 DISPLAYLEVEL(6, "End-flush into dst buffer of size %u \n", (U32)adjustedDstSize);
Yann Collet01743a32017-06-16 17:56:41 -07001608 remainingToFlush = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end);
Yann Colletd6770f82017-09-28 02:14:48 -07001609 CHECK( ZSTD_isError(remainingToFlush),
1610 "ZSTD_compress_generic w/ ZSTD_e_end error : %s",
1611 ZSTD_getErrorName(remainingToFlush) );
Yann Collet01743a32017-06-16 17:56:41 -07001612 } }
1613 crcOrig = XXH64_digest(&xxhState);
1614 cSize = outBuff.pos;
1615 DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize);
1616 }
1617
1618 /* multi - fragments decompression test */
1619 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Collet33a66392017-06-28 11:09:43 -07001620 DISPLAYLEVEL(5, "resetting DCtx (dict:%08X) \n", (U32)(size_t)dict);
Yann Colletdce78922017-06-21 15:53:42 -07001621 CHECK_Z( ZSTD_resetDStream(zd) );
Yann Collet01743a32017-06-16 17:56:41 -07001622 } else {
Yann Collet33a66392017-06-28 11:09:43 -07001623 DISPLAYLEVEL(5, "using dict of size %u \n", (U32)dictSize);
Yann Colletdce78922017-06-21 15:53:42 -07001624 CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
Yann Collet01743a32017-06-16 17:56:41 -07001625 }
1626 { size_t decompressionResult = 1;
1627 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1628 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1629 for (totalGenSize = 0 ; decompressionResult ; ) {
1630 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1631 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1632 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
1633 inBuff.size = inBuff.pos + readCSrcSize;
Yann Colletbfabd1d2017-09-27 01:01:11 -07001634 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd6770f82017-09-28 02:14:48 -07001635 DISPLAYLEVEL(6, "ZSTD_decompressStream input %u bytes (pos:%u/%u)\n",
Yann Collet33a66392017-06-28 11:09:43 -07001636 (U32)readCSrcSize, (U32)inBuff.pos, (U32)cSize);
Yann Collet01743a32017-06-16 17:56:41 -07001637 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1638 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd6770f82017-09-28 02:14:48 -07001639 DISPLAYLEVEL(6, "inBuff.pos = %u \n", (U32)readCSrcSize);
Yann Collet01743a32017-06-16 17:56:41 -07001640 }
1641 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize);
1642 CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize);
1643 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1644 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1645 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1646 } }
1647
1648 /*===== noisy/erroneous src decompression test =====*/
1649
1650 /* add some noise */
1651 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1652 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1653 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1654 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1655 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1656 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1657 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1658 } }
1659
1660 /* try decompression on noisy data */
Yann Colletdce78922017-06-21 15:53:42 -07001661 CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */
Yann Collet01743a32017-06-16 17:56:41 -07001662 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1663 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1664 while (outBuff.pos < dstBufferSize) {
1665 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1666 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1667 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
1668 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
1669 outBuff.size = outBuff.pos + adjustedDstSize;
1670 inBuff.size = inBuff.pos + adjustedCSrcSize;
1671 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1672 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Yann Colleted1d0392017-06-19 11:07:33 -07001673 /* Good so far, but no more progress possible */
Yann Collet01743a32017-06-16 17:56:41 -07001674 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
1675 } } } }
Yann Collet8dee0ec2017-06-18 23:25:15 -07001676 DISPLAY("\r%u fuzzer tests completed \n", testNb-1);
Yann Collet01743a32017-06-16 17:56:41 -07001677
1678_cleanup:
1679 ZSTD_freeCCtx(zc);
1680 ZSTD_freeDStream(zd);
1681 ZSTD_freeDStream(zd_noise);
Stella Lau023b24e2017-08-20 22:55:07 -07001682 ZSTD_freeCCtxParams(cctxParams);
Yann Collet01743a32017-06-16 17:56:41 -07001683 free(cNoiseBuffer[0]);
1684 free(cNoiseBuffer[1]);
1685 free(cNoiseBuffer[2]);
1686 free(cNoiseBuffer[3]);
1687 free(cNoiseBuffer[4]);
1688 free(copyBuffer);
1689 free(cBuffer);
1690 free(dstBuffer);
1691 return result;
1692
1693_output_error:
1694 result = 1;
1695 goto _cleanup;
1696}
1697
Yann Colletd7883a22016-08-12 16:48:02 +02001698/*-*******************************************************
1699* Command line
1700*********************************************************/
1701int FUZ_usage(const char* programName)
1702{
1703 DISPLAY( "Usage :\n");
1704 DISPLAY( " %s [args]\n", programName);
1705 DISPLAY( "\n");
1706 DISPLAY( "Arguments :\n");
1707 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
1708 DISPLAY( " -s# : Select seed (default:prompt user)\n");
1709 DISPLAY( " -t# : Select starting test number (default:0)\n");
1710 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
1711 DISPLAY( " -v : verbose\n");
1712 DISPLAY( " -p : pause at the end\n");
1713 DISPLAY( " -h : display help and exit\n");
1714 return 0;
1715}
1716
Yann Collet01743a32017-06-16 17:56:41 -07001717typedef enum { simple_api, mt_api, advanced_api } e_api;
Yann Colletd7883a22016-08-12 16:48:02 +02001718
1719int main(int argc, const char** argv)
1720{
Yann Colletbd88f632017-11-27 12:15:23 -08001721 U32 seed = 0;
1722 int seedset = 0;
Yann Colletd7883a22016-08-12 16:48:02 +02001723 int nbTests = nbTestsDefault;
1724 int testNb = 0;
1725 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
Yann Colletbd88f632017-11-27 12:15:23 -08001726 int result = 0;
Yann Collet19d670b2017-01-19 15:32:07 -08001727 int mainPause = 0;
Yann Colletafb0aca2017-06-29 18:19:09 -07001728 int bigTests = (sizeof(size_t) == 8);
Yann Collet01743a32017-06-16 17:56:41 -07001729 e_api selected_api = simple_api;
Yann Collet19d670b2017-01-19 15:32:07 -08001730 const char* const programName = argv[0];
Stella Lau73c73bf2017-08-21 12:41:19 -07001731 U32 useOpaqueAPI = 0;
Yann Colletbd88f632017-11-27 12:15:23 -08001732 int argNb;
Yann Colletd7883a22016-08-12 16:48:02 +02001733
1734 /* Check command line */
1735 for(argNb=1; argNb<argc; argNb++) {
1736 const char* argument = argv[argNb];
Yann Colletbd88f632017-11-27 12:15:23 -08001737 assert(argument != NULL);
Yann Colletd7883a22016-08-12 16:48:02 +02001738
1739 /* Parsing commands. Aggregated commands are allowed */
1740 if (argument[0]=='-') {
Yann Colletd7883a22016-08-12 16:48:02 +02001741
Yann Collet47c6a952017-09-28 18:27:22 -07001742 if (!strcmp(argument, "--mt")) { selected_api=mt_api; testNb += !testNb; continue; }
1743 if (!strcmp(argument, "--newapi")) { selected_api=advanced_api; testNb += !testNb; continue; }
1744 if (!strcmp(argument, "--opaqueapi")) { selected_api=advanced_api; testNb += !testNb; useOpaqueAPI = 1; continue; }
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001745 if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
Yann Collet19d670b2017-01-19 15:32:07 -08001746
1747 argument++;
Yann Colletd7883a22016-08-12 16:48:02 +02001748 while (*argument!=0) {
1749 switch(*argument)
1750 {
1751 case 'h':
1752 return FUZ_usage(programName);
Yann Collet736788f2017-01-19 12:12:50 -08001753
Yann Colletd7883a22016-08-12 16:48:02 +02001754 case 'v':
1755 argument++;
Yann Collet736788f2017-01-19 12:12:50 -08001756 g_displayLevel++;
Yann Colletd7883a22016-08-12 16:48:02 +02001757 break;
Yann Collet736788f2017-01-19 12:12:50 -08001758
Yann Colletd7883a22016-08-12 16:48:02 +02001759 case 'q':
1760 argument++;
1761 g_displayLevel--;
1762 break;
Yann Collet736788f2017-01-19 12:12:50 -08001763
Yann Colletd7883a22016-08-12 16:48:02 +02001764 case 'p': /* pause at the end */
1765 argument++;
1766 mainPause = 1;
1767 break;
1768
Yann Collet736788f2017-01-19 12:12:50 -08001769 case 'i': /* limit tests by nb of iterations (default) */
Yann Colletd7883a22016-08-12 16:48:02 +02001770 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001771 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001772 while ((*argument>='0') && (*argument<='9')) {
1773 nbTests *= 10;
1774 nbTests += *argument - '0';
1775 argument++;
1776 }
1777 break;
1778
Yann Collet736788f2017-01-19 12:12:50 -08001779 case 'T': /* limit tests by time */
Yann Colletd7883a22016-08-12 16:48:02 +02001780 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001781 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001782 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -07001783 g_clockTime *= 10;
1784 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +02001785 argument++;
1786 }
Yann Colletbd88f632017-11-27 12:15:23 -08001787 if (*argument=='m') { /* -T1m == -T60 */
1788 g_clockTime *=60, argument++;
1789 if (*argument=='n') argument++; /* -T1mn == -T60 */
1790 } else if (*argument=='s') argument++; /* -T10s == -T10 */
Nick Terrell9a2f6f42017-11-29 19:11:12 -08001791 g_clockTime *= SEC_TO_MICRO;
Yann Colletd7883a22016-08-12 16:48:02 +02001792 break;
1793
Yann Collet736788f2017-01-19 12:12:50 -08001794 case 's': /* manually select seed */
Yann Colletd7883a22016-08-12 16:48:02 +02001795 argument++;
Yann Colletd7883a22016-08-12 16:48:02 +02001796 seedset=1;
Yann Colletbd88f632017-11-27 12:15:23 -08001797 seed=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001798 while ((*argument>='0') && (*argument<='9')) {
1799 seed *= 10;
1800 seed += *argument - '0';
1801 argument++;
1802 }
1803 break;
1804
Yann Collet736788f2017-01-19 12:12:50 -08001805 case 't': /* select starting test number */
Yann Colletd7883a22016-08-12 16:48:02 +02001806 argument++;
1807 testNb=0;
1808 while ((*argument>='0') && (*argument<='9')) {
1809 testNb *= 10;
1810 testNb += *argument - '0';
1811 argument++;
1812 }
1813 break;
1814
1815 case 'P': /* compressibility % */
1816 argument++;
1817 proba=0;
1818 while ((*argument>='0') && (*argument<='9')) {
1819 proba *= 10;
1820 proba += *argument - '0';
1821 argument++;
1822 }
1823 if (proba<0) proba=0;
1824 if (proba>100) proba=100;
1825 break;
1826
1827 default:
1828 return FUZ_usage(programName);
1829 }
1830 } } } /* for(argNb=1; argNb<argc; argNb++) */
1831
1832 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +02001833 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +02001834
Yann Colletef9999f2016-09-01 16:44:48 -07001835 if (!seedset) {
1836 time_t const t = time(NULL);
1837 U32 const h = XXH32(&t, sizeof(t), 1);
1838 seed = h % 10000;
1839 }
1840
Yann Colletd7883a22016-08-12 16:48:02 +02001841 DISPLAY("Seed = %u\n", seed);
1842 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
1843
1844 if (nbTests<=0) nbTests=1;
1845
1846 if (testNb==0) {
Yann Colletbd88f632017-11-27 12:15:23 -08001847 result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */
Yann Collet4616fad2017-07-10 17:16:41 -07001848 }
Yann Colletd7883a22016-08-12 16:48:02 +02001849
Yann Collet01743a32017-06-16 17:56:41 -07001850 if (!result) {
1851 switch(selected_api)
1852 {
1853 case simple_api :
1854 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
1855 break;
1856 case mt_api :
1857 result = fuzzerTests_MT(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
1858 break;
1859 case advanced_api :
Stella Lau73c73bf2017-08-21 12:41:19 -07001860 result = fuzzerTests_newAPI(seed, nbTests, testNb, ((double)proba) / 100, bigTests, useOpaqueAPI);
Yann Collet01743a32017-06-16 17:56:41 -07001861 break;
1862 default :
1863 assert(0); /* impossible */
1864 }
1865 }
Yann Colletd7883a22016-08-12 16:48:02 +02001866
1867 if (mainPause) {
1868 int unused;
1869 DISPLAY("Press Enter \n");
1870 unused = getchar();
1871 (void)unused;
1872 }
1873 return result;
1874}