blob: a3a56d9d84bbe65be275184efd9d5c5acbe837fb [file] [log] [blame]
Yann Collet4ded9e52016-08-30 10:04:33 -07001/**
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
Yann Collet4856a002015-01-24 01:58:16 +01009
Yann Collet4856a002015-01-24 01:58:16 +010010
Yann Collet0d9ce042016-03-19 13:21:08 +010011/*-************************************
Yann Collet4856a002015-01-24 01:58:16 +010012* Compiler specific
13**************************************/
14#ifdef _MSC_VER /* Visual Studio */
Yann Colleta5ffe3d2017-05-12 16:29:19 -070015# define _CRT_SECURE_NO_WARNINGS /* fgets */
16# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
17# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
Yann Collet4856a002015-01-24 01:58:16 +010018#endif
19
Yann Collet4856a002015-01-24 01:58:16 +010020
Yann Collet0d9ce042016-03-19 13:21:08 +010021/*-************************************
Yann Collet4856a002015-01-24 01:58:16 +010022* Includes
23**************************************/
Yann Collet6fa05a22016-07-20 14:58:49 +020024#include <stdlib.h> /* free */
25#include <stdio.h> /* fgets, sscanf */
Yann Collet6fa05a22016-07-20 14:58:49 +020026#include <string.h> /* strcmp */
27#include <time.h> /* clock_t */
Yann Colleta5ffe3d2017-05-12 16:29:19 -070028#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressContinue, ZSTD_compressBlock */
Yann Collet6fa05a22016-07-20 14:58:49 +020029#include "zstd.h" /* ZSTD_VERSION_STRING */
Yann Collete7a41a52016-12-05 16:21:06 -080030#include "zstd_errors.h" /* ZSTD_getErrorCode */
Yann Collet0ef68032017-03-29 16:58:57 -070031#include "zstdmt_compress.h"
Nick Terrella8b4fe02017-01-02 18:45:19 -080032#define ZDICT_STATIC_LINKING_ONLY
Yann Collet6fa05a22016-07-20 14:58:49 +020033#include "zdict.h" /* ZDICT_trainFromBuffer */
34#include "datagen.h" /* RDG_genBuffer */
Yann Collet353c5d22015-10-21 14:39:26 +010035#include "mem.h"
Yann Collet6c903a82016-05-28 13:34:07 +020036#define XXH_STATIC_LINKING_ONLY
Yann Collet6fa05a22016-07-20 14:58:49 +020037#include "xxhash.h" /* XXH64 */
Yann Collet4856a002015-01-24 01:58:16 +010038
39
Yann Collet0d9ce042016-03-19 13:21:08 +010040/*-************************************
41* Constants
Yann Collet4856a002015-01-24 01:58:16 +010042**************************************/
Yann Collet4856a002015-01-24 01:58:16 +010043#define KB *(1U<<10)
44#define MB *(1U<<20)
45#define GB *(1U<<30)
46
Yann Collet0d9ce042016-03-19 13:21:08 +010047static const U32 FUZ_compressibility_default = 50;
Yann Collet110cc142015-11-19 12:02:28 +010048static const U32 nbTestsDefault = 30000;
Yann Collet4856a002015-01-24 01:58:16 +010049
50
Yann Collet0d9ce042016-03-19 13:21:08 +010051/*-************************************
Yann Collet4856a002015-01-24 01:58:16 +010052* Display Macros
53**************************************/
54#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
55#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
56static U32 g_displayLevel = 2;
57
58#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletea63bb72016-04-08 15:25:32 +020059 if ((FUZ_clockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
60 { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \
Sean Purcell042ba122017-03-23 11:13:52 -070061 if (g_displayLevel>=4) fflush(stderr); } }
Yann Collet64deef32016-09-14 00:16:07 +020062static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6;
Yann Colletea63bb72016-04-08 15:25:32 +020063static clock_t g_displayClock = 0;
Yann Collet553cf6a2015-12-04 17:25:26 +010064
Yann Collet4856a002015-01-24 01:58:16 +010065
Yann Collet0d9ce042016-03-19 13:21:08 +010066/*-*******************************************************
Yann Collet4856a002015-01-24 01:58:16 +010067* Fuzzer functions
68*********************************************************/
Yann Collete93add02016-02-15 17:44:14 +010069#define MIN(a,b) ((a)<(b)?(a):(b))
Sean Purcellf5e50512017-03-15 15:04:54 -070070#define MAX(a,b) ((a)>(b)?(a):(b))
Yann Collet110cc142015-11-19 12:02:28 +010071
Yann Colletea63bb72016-04-08 15:25:32 +020072static clock_t FUZ_clockSpan(clock_t cStart)
Yann Collet4856a002015-01-24 01:58:16 +010073{
Yann Colletea63bb72016-04-08 15:25:32 +020074 return clock() - cStart; /* works even when overflow; max span ~ 30mn */
Yann Collet4856a002015-01-24 01:58:16 +010075}
76
Yann Colletc0a9bf32016-05-30 01:56:08 +020077#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
78static unsigned FUZ_rand(unsigned* src)
Yann Collet4856a002015-01-24 01:58:16 +010079{
Yann Collet0d9ce042016-03-19 13:21:08 +010080 static const U32 prime1 = 2654435761U;
81 static const U32 prime2 = 2246822519U;
Yann Collet4856a002015-01-24 01:58:16 +010082 U32 rand32 = *src;
83 rand32 *= prime1;
84 rand32 += prime2;
85 rand32 = FUZ_rotl32(rand32, 13);
86 *src = rand32;
87 return rand32 >> 5;
88}
89
Yann Collet997f9ee2015-08-21 02:44:20 +010090static unsigned FUZ_highbit32(U32 v32)
Yann Collet4856a002015-01-24 01:58:16 +010091{
92 unsigned nbBits = 0;
93 if (v32==0) return 0;
Yann Colletc0a9bf32016-05-30 01:56:08 +020094 while (v32) v32 >>= 1, nbBits++;
Yann Collet4856a002015-01-24 01:58:16 +010095 return nbBits;
96}
Yann Collet4856a002015-01-24 01:58:16 +010097
98
Yann Colletcb327632016-08-23 00:30:31 +020099/*=============================================
100* Basic Unit tests
101=============================================*/
102
Yann Colletd4f4e582016-06-27 01:31:35 +0200103#define CHECK_V(var, fn) size_t const var = fn; if (ZSTD_isError(var)) goto _output_error
104#define CHECK(fn) { CHECK_V(err, fn); }
105#define CHECKPLUS(var, fn, more) { CHECK_V(var, fn); more; }
Yann Collet0fdc71c2017-05-24 17:41:41 -0700106
Yann Collet4856a002015-01-24 01:58:16 +0100107static int basicUnitTests(U32 seed, double compressibility)
108{
Yann Collet30009522016-05-30 16:17:33 +0200109 size_t const CNBuffSize = 5 MB;
Yann Colletd2858e92016-05-30 15:10:09 +0200110 void* const CNBuffer = malloc(CNBuffSize);
111 void* const compressedBuffer = malloc(ZSTD_compressBound(CNBuffSize));
112 void* const decodedBuffer = malloc(CNBuffSize);
Yann Colletc0b17312017-02-28 01:02:46 -0800113 ZSTD_DCtx* dctx = ZSTD_createDCtx();
Yann Colletc0a9bf32016-05-30 01:56:08 +0200114 int testResult = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100115 U32 testNb=0;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200116 size_t cSize;
Yann Collet4856a002015-01-24 01:58:16 +0100117
Yann Colletc0a9bf32016-05-30 01:56:08 +0200118 /* Create compressible noise */
Yann Colletd1d210f2016-03-19 12:12:07 +0100119 if (!CNBuffer || !compressedBuffer || !decodedBuffer) {
Yann Collet94f998b2015-07-04 23:10:40 -0800120 DISPLAY("Not enough memory, aborting\n");
121 testResult = 1;
122 goto _end;
123 }
Yann Colletd2858e92016-05-30 15:10:09 +0200124 RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed);
Yann Collet4856a002015-01-24 01:58:16 +0100125
Yann Colletd5d9bc32015-08-23 23:13:49 +0100126 /* Basic tests */
Yann Collet9a69ec42016-08-01 16:25:58 +0200127 DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName : ", testNb++);
128 { const char* errorString = ZSTD_getErrorName(0);
129 DISPLAYLEVEL(4, "OK : %s \n", errorString);
130 }
131
132 DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName with wrong value : ", testNb++);
133 { const char* errorString = ZSTD_getErrorName(499);
134 DISPLAYLEVEL(4, "OK : %s \n", errorString);
135 }
136
Yann Collet0ef68032017-03-29 16:58:57 -0700137
Yann Colletd2858e92016-05-30 15:10:09 +0200138 DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, (U32)CNBuffSize);
139 CHECKPLUS(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(CNBuffSize),
140 CNBuffer, CNBuffSize, 1),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200141 cSize=r );
Yann Colletd2858e92016-05-30 15:10:09 +0200142 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
Yann Collet4856a002015-01-24 01:58:16 +0100143
Yann Collet0ef68032017-03-29 16:58:57 -0700144
145 DISPLAYLEVEL(4, "test%3i : ZSTD_getFrameContentSize test : ", testNb++);
146 { unsigned long long const rSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
147 if (rSize != CNBuffSize) goto _output_error;
148 }
149 DISPLAYLEVEL(4, "OK \n");
150
151 DISPLAYLEVEL(4, "test%3i : ZSTD_findDecompressedSize test : ", testNb++);
Sean Purcell4e709712017-02-07 13:50:09 -0800152 { unsigned long long const rSize = ZSTD_findDecompressedSize(compressedBuffer, cSize);
Yann Colletf323bf72016-07-07 13:14:21 +0200153 if (rSize != CNBuffSize) goto _output_error;
154 }
155 DISPLAYLEVEL(4, "OK \n");
156
Yann Colletd2858e92016-05-30 15:10:09 +0200157 DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)CNBuffSize);
Yann Colletc991cc12016-07-28 00:55:43 +0200158 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
159 if (r != CNBuffSize) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100160 DISPLAYLEVEL(4, "OK \n");
161
Yann Colletc0a9bf32016-05-30 01:56:08 +0200162 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
163 { size_t u;
Yann Colletd2858e92016-05-30 15:10:09 +0200164 for (u=0; u<CNBuffSize; u++) {
Yann Colletc0a9bf32016-05-30 01:56:08 +0200165 if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;;
166 } }
167 DISPLAYLEVEL(4, "OK \n");
Yann Collet4856a002015-01-24 01:58:16 +0100168
Yann Collet0ef68032017-03-29 16:58:57 -0700169
Yann Colletc0b17312017-02-28 01:02:46 -0800170 DISPLAYLEVEL(4, "test%3i : decompress with null dict : ", testNb++);
171 { size_t const r = ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, NULL, 0);
172 if (r != CNBuffSize) goto _output_error; }
173 DISPLAYLEVEL(4, "OK \n");
174
175 DISPLAYLEVEL(4, "test%3i : decompress with null DDict : ", testNb++);
176 { size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, NULL);
177 if (r != CNBuffSize) goto _output_error; }
178 DISPLAYLEVEL(4, "OK \n");
179
Yann Collet4856a002015-01-24 01:58:16 +0100180 DISPLAYLEVEL(4, "test%3i : decompress with 1 missing byte : ", testNb++);
Yann Colletd2858e92016-05-30 15:10:09 +0200181 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize-1);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200182 if (!ZSTD_isError(r)) goto _output_error;
183 if (ZSTD_getErrorCode((size_t)r) != ZSTD_error_srcSize_wrong) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100184 DISPLAYLEVEL(4, "OK \n");
185
186 DISPLAYLEVEL(4, "test%3i : decompress with 1 too much byte : ", testNb++);
Yann Colletd2858e92016-05-30 15:10:09 +0200187 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize+1);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200188 if (!ZSTD_isError(r)) goto _output_error;
189 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100190 DISPLAYLEVEL(4, "OK \n");
191
Yann Collet0ef68032017-03-29 16:58:57 -0700192
Yann Colletc7fe2622017-05-23 13:16:00 -0700193 /* Static CCtx tests */
194#define STATIC_CCTX_LEVEL 3
195 DISPLAYLEVEL(4, "test%3i : create static CCtx for level %u :", testNb++, STATIC_CCTX_LEVEL);
196 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(STATIC_CCTX_LEVEL, 0, 0);
197 size_t const staticCCtxSize = ZSTD_estimateCStreamSize(cParams);
Yann Collet0fdc71c2017-05-24 17:41:41 -0700198 void* const staticCCtxBuffer = malloc(staticCCtxSize);
199 size_t const staticDCtxSize = ZSTD_estimateDCtxSize();
200 void* const staticDCtxBuffer = malloc(staticDCtxSize);
201 if (staticCCtxBuffer==NULL || staticDCtxBuffer==NULL) {
202 free(staticCCtxBuffer);
203 free(staticDCtxBuffer);
Yann Colletc7fe2622017-05-23 13:16:00 -0700204 DISPLAY("Not enough memory, aborting\n");
205 testResult = 1;
206 goto _end;
207 }
208 { ZSTD_CCtx* staticCCtx = ZSTD_initStaticCCtx(staticCCtxBuffer, staticCCtxSize);
Yann Collet0fdc71c2017-05-24 17:41:41 -0700209 ZSTD_DCtx* staticDCtx = ZSTD_initStaticDCtx(staticDCtxBuffer, staticDCtxSize);
210 if ((staticCCtx==NULL) || (staticDCtx==NULL)) goto _output_error;
Yann Colletc7fe2622017-05-23 13:16:00 -0700211 DISPLAYLEVEL(4, "OK \n");
212
213 DISPLAYLEVEL(4, "test%3i : init CCtx for level %u : ", testNb++, STATIC_CCTX_LEVEL);
214 { size_t const r = ZSTD_compressBegin(staticCCtx, STATIC_CCTX_LEVEL);
215 if (ZSTD_isError(r)) goto _output_error; }
216 DISPLAYLEVEL(4, "OK \n");
217
218 DISPLAYLEVEL(4, "test%3i : simple compression test with static CCtx : ", testNb++);
219 CHECKPLUS(r, ZSTD_compressCCtx(staticCCtx,
220 compressedBuffer, ZSTD_compressBound(CNBuffSize),
221 CNBuffer, CNBuffSize, STATIC_CCTX_LEVEL),
222 cSize=r );
Yann Collet0fdc71c2017-05-24 17:41:41 -0700223 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n",
224 (U32)cSize, (double)cSize/CNBuffSize*100);
Yann Colletc7fe2622017-05-23 13:16:00 -0700225
Yann Collet0fdc71c2017-05-24 17:41:41 -0700226 DISPLAYLEVEL(4, "test%3i : simple decompression test with static DCtx : ", testNb++);
227 { size_t const r = ZSTD_decompressDCtx(staticDCtx,
228 decodedBuffer, CNBuffSize,
229 compressedBuffer, cSize);
Yann Colletc7fe2622017-05-23 13:16:00 -0700230 if (r != CNBuffSize) goto _output_error; }
231 DISPLAYLEVEL(4, "OK \n");
232
Yann Collet0fdc71c2017-05-24 17:41:41 -0700233 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
234 { size_t u;
235 for (u=0; u<CNBuffSize; u++) {
236 if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u])
237 goto _output_error;;
238 } }
239 DISPLAYLEVEL(4, "OK \n");
240
Yann Colletc7fe2622017-05-23 13:16:00 -0700241 DISPLAYLEVEL(4, "test%3i : init CCtx for too large level (must fail) : ", testNb++);
242 { size_t const r = ZSTD_compressBegin(staticCCtx, ZSTD_maxCLevel());
243 if (!ZSTD_isError(r)) goto _output_error; }
244 DISPLAYLEVEL(4, "OK \n");
245
246 DISPLAYLEVEL(4, "test%3i : init CCtx for small level %u (should work again) : ", testNb++, 1);
247 { size_t const r = ZSTD_compressBegin(staticCCtx, 1);
248 if (ZSTD_isError(r)) goto _output_error; }
249 DISPLAYLEVEL(4, "OK \n");
250
251 DISPLAYLEVEL(4, "test%3i : init CStream for small level %u : ", testNb++, 1);
252 { size_t const r = ZSTD_initCStream(staticCCtx, 1);
253 if (ZSTD_isError(r)) goto _output_error; }
254 DISPLAYLEVEL(4, "OK \n");
255
256 DISPLAYLEVEL(4, "test%3i : init CStream with dictionary (should fail) : ", testNb++);
257 { size_t const r = ZSTD_initCStream_usingDict(staticCCtx, CNBuffer, 64 KB, 1);
258 if (!ZSTD_isError(r)) goto _output_error; }
259 DISPLAYLEVEL(4, "OK \n");
Yann Collet0fdc71c2017-05-24 17:41:41 -0700260
261 DISPLAYLEVEL(4, "test%3i : init DStream (should fail) : ", testNb++);
262 { size_t const r = ZSTD_initDStream(staticDCtx);
263 if (ZSTD_isError(r)) goto _output_error; }
264 { ZSTD_outBuffer output = { decodedBuffer, CNBuffSize, 0 };
265 ZSTD_inBuffer input = { compressedBuffer, ZSTD_FRAMEHEADERSIZE_MAX+1, 0 };
266 size_t const r = ZSTD_decompressStream(staticDCtx, &output, &input);
267 if (!ZSTD_isError(r)) goto _output_error;
268 }
269 DISPLAYLEVEL(4, "OK \n");
Yann Colletc7fe2622017-05-23 13:16:00 -0700270 }
271 free(staticCCtxBuffer);
Yann Collet0fdc71c2017-05-24 17:41:41 -0700272 free(staticDCtxBuffer);
Yann Colletc7fe2622017-05-23 13:16:00 -0700273 }
274
275
276
Yann Collet1e1e26f2017-03-29 17:09:59 -0700277 /* ZSTDMT simple MT compression test */
Yann Collet0ef68032017-03-29 16:58:57 -0700278 DISPLAYLEVEL(4, "test%3i : create ZSTDMT CCtx : ", testNb++);
279 { ZSTDMT_CCtx* mtctx = ZSTDMT_createCCtx(2);
280 if (mtctx==NULL) {
281 DISPLAY("mtctx : mot enough memory, aborting \n");
282 testResult = 1;
283 goto _end;
284 }
285 DISPLAYLEVEL(4, "OK \n");
286
287 DISPLAYLEVEL(4, "test%3i : compress %u bytes with 2 threads : ", testNb++, (U32)CNBuffSize);
288 CHECKPLUS(r, ZSTDMT_compressCCtx(mtctx,
289 compressedBuffer, ZSTD_compressBound(CNBuffSize),
290 CNBuffer, CNBuffSize,
291 1),
292 cSize=r );
293 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
294
295 DISPLAYLEVEL(4, "test%3i : decompressed size test : ", testNb++);
296 { unsigned long long const rSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
297 if (rSize != CNBuffSize) {
298 DISPLAY("ZSTD_getFrameContentSize incorrect : %u != %u \n", (U32)rSize, (U32)CNBuffSize);
299 goto _output_error;
300 } }
301 DISPLAYLEVEL(4, "OK \n");
302
303 DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)CNBuffSize);
304 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
305 if (r != CNBuffSize) goto _output_error; }
306 DISPLAYLEVEL(4, "OK \n");
307
308 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
309 { size_t u;
310 for (u=0; u<CNBuffSize; u++) {
311 if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;;
312 } }
313 DISPLAYLEVEL(4, "OK \n");
314
315 ZSTDMT_freeCCtx(mtctx);
316 }
317
318
Sean Purcellba2ad9f2017-02-07 16:16:55 -0800319 /* Simple API multiframe test */
320 DISPLAYLEVEL(4, "test%3i : compress multiple frames : ", testNb++);
321 { size_t off = 0;
322 int i;
323 int const segs = 4;
324 /* only use the first half so we don't push against size limit of compressedBuffer */
325 size_t const segSize = (CNBuffSize / 2) / segs;
326 for (i = 0; i < segs; i++) {
327 CHECK_V(r,
328 ZSTD_compress(
329 (BYTE *)compressedBuffer + off, CNBuffSize - off,
330 (BYTE *)CNBuffer + segSize * i,
331 segSize, 5));
332 off += r;
333 if (i == segs/2) {
334 /* insert skippable frame */
335 const U32 skipLen = 128 KB;
336 MEM_writeLE32((BYTE*)compressedBuffer + off, ZSTD_MAGIC_SKIPPABLE_START);
337 MEM_writeLE32((BYTE*)compressedBuffer + off + 4, skipLen);
338 off += skipLen + ZSTD_skippableHeaderSize;
339 }
340 }
341 cSize = off;
342 }
343 DISPLAYLEVEL(4, "OK \n");
344
345 DISPLAYLEVEL(4, "test%3i : get decompressed size of multiple frames : ", testNb++);
346 { unsigned long long const r = ZSTD_findDecompressedSize(compressedBuffer, cSize);
347 if (r != CNBuffSize / 2) goto _output_error; }
348 DISPLAYLEVEL(4, "OK \n");
349
350 DISPLAYLEVEL(4, "test%3i : decompress multiple frames : ", testNb++);
351 { CHECK_V(r, ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize));
352 if (r != CNBuffSize / 2) goto _output_error; }
353 DISPLAYLEVEL(4, "OK \n");
354
355 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
356 if (memcmp(decodedBuffer, CNBuffer, CNBuffSize / 2) != 0) goto _output_error;
357 DISPLAYLEVEL(4, "OK \n");
358
Yann Collet389648c2016-04-12 19:13:08 +0200359 /* Dictionary and CCtx Duplication tests */
Yann Colletc0a9bf32016-05-30 01:56:08 +0200360 { ZSTD_CCtx* const ctxOrig = ZSTD_createCCtx();
361 ZSTD_CCtx* const ctxDuplicated = ZSTD_createCCtx();
Yann Colletc0a9bf32016-05-30 01:56:08 +0200362 static const size_t dictSize = 551;
Yann Collet60096272016-01-08 17:27:50 +0100363
Yann Collet887e7da2016-04-11 20:12:27 +0200364 DISPLAYLEVEL(4, "test%3i : copy context too soon : ", testNb++);
Yann Collet97b378a2016-09-21 17:20:19 +0200365 { size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200366 if (!ZSTD_isError(copyResult)) goto _output_error; } /* error must be detected */
Yann Collet887e7da2016-04-11 20:12:27 +0200367 DISPLAYLEVEL(4, "OK \n");
368
Yann Collet60096272016-01-08 17:27:50 +0100369 DISPLAYLEVEL(4, "test%3i : load dictionary into context : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200370 CHECK( ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2) );
Sean Purcell84b37cc2017-02-09 12:27:32 -0800371 CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0) ); /* Begin_usingDict implies unknown srcSize, so match that */
Yann Collet60096272016-01-08 17:27:50 +0100372 DISPLAYLEVEL(4, "OK \n");
373
Yann Collet30009522016-05-30 16:17:33 +0200374 DISPLAYLEVEL(4, "test%3i : compress with flat dictionary : ", testNb++);
Yann Collet60096272016-01-08 17:27:50 +0100375 cSize = 0;
Yann Collet62470b42016-07-28 15:29:08 +0200376 CHECKPLUS(r, ZSTD_compressEnd(ctxOrig, compressedBuffer, ZSTD_compressBound(CNBuffSize),
Yann Colletd2858e92016-05-30 15:10:09 +0200377 (const char*)CNBuffer + dictSize, CNBuffSize - dictSize),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200378 cSize += r);
Yann Colletd2858e92016-05-30 15:10:09 +0200379 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
Yann Collet60096272016-01-08 17:27:50 +0100380
Yann Collet30009522016-05-30 16:17:33 +0200381 DISPLAYLEVEL(4, "test%3i : frame built with flat dictionary should be decompressible : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200382 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
Yann Colletd2858e92016-05-30 15:10:09 +0200383 decodedBuffer, CNBuffSize,
Yann Colletc0a9bf32016-05-30 01:56:08 +0200384 compressedBuffer, cSize,
385 CNBuffer, dictSize),
Yann Colletd2858e92016-05-30 15:10:09 +0200386 if (r != CNBuffSize - dictSize) goto _output_error);
Yann Collet60096272016-01-08 17:27:50 +0100387 DISPLAYLEVEL(4, "OK \n");
388
389 DISPLAYLEVEL(4, "test%3i : compress with duplicated context : ", testNb++);
Yann Collet389648c2016-04-12 19:13:08 +0200390 { size_t const cSizeOrig = cSize;
391 cSize = 0;
Yann Collet62470b42016-07-28 15:29:08 +0200392 CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(CNBuffSize),
Yann Colletd2858e92016-05-30 15:10:09 +0200393 (const char*)CNBuffer + dictSize, CNBuffSize - dictSize),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200394 cSize += r);
Yann Collet227cc392016-07-15 11:27:09 +0200395 if (cSize != cSizeOrig) goto _output_error; /* should be identical ==> same size */
Yann Collet389648c2016-04-12 19:13:08 +0200396 }
Yann Colletd2858e92016-05-30 15:10:09 +0200397 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
Yann Collet60096272016-01-08 17:27:50 +0100398
399 DISPLAYLEVEL(4, "test%3i : frame built with duplicated context should be decompressible : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200400 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
Yann Colletd2858e92016-05-30 15:10:09 +0200401 decodedBuffer, CNBuffSize,
Yann Collet60096272016-01-08 17:27:50 +0100402 compressedBuffer, cSize,
Yann Colletc0a9bf32016-05-30 01:56:08 +0200403 CNBuffer, dictSize),
Yann Colletd2858e92016-05-30 15:10:09 +0200404 if (r != CNBuffSize - dictSize) goto _output_error);
Yann Collet60096272016-01-08 17:27:50 +0100405 DISPLAYLEVEL(4, "OK \n");
Yann Collet541dc7c2016-04-12 18:00:20 +0200406
Yann Collet8dff9562017-02-25 10:11:15 -0800407 DISPLAYLEVEL(4, "test%3i : decompress with DDict : ", testNb++);
Yann Collet57827f92017-05-25 15:44:06 -0700408 { ZSTD_DDict* const ddict = ZSTD_createDDict(CNBuffer, dictSize);
Yann Collet8dff9562017-02-25 10:11:15 -0800409 size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict);
410 if (r != CNBuffSize - dictSize) goto _output_error;
411 DISPLAYLEVEL(4, "OK (size of DDict : %u) \n", (U32)ZSTD_sizeof_DDict(ddict));
412 ZSTD_freeDDict(ddict);
413 }
414
Yann Collet57827f92017-05-25 15:44:06 -0700415 DISPLAYLEVEL(4, "test%3i : decompress with static DDict : ", testNb++);
416 { size_t const ddictBufferSize = ZSTD_estimateDDictSize(dictSize, 0);
417 void* ddictBuffer = malloc(ddictBufferSize);
418 if (ddictBuffer == NULL) goto _output_error;
419 { ZSTD_DDict* const ddict = ZSTD_initStaticDDict(ddictBuffer, ddictBufferSize, CNBuffer, dictSize, 0);
420 size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict);
421 if (r != CNBuffSize - dictSize) goto _output_error;
422 }
Yann Colletff8f83b2017-06-20 12:17:32 -0700423 free(ddictBuffer);
Yann Collet57827f92017-05-25 15:44:06 -0700424 DISPLAYLEVEL(4, "OK (size of static DDict : %u) \n", (U32)ddictBufferSize);
425 }
426
Yann Collet541dc7c2016-04-12 18:00:20 +0200427 DISPLAYLEVEL(4, "test%3i : check content size on duplicated context : ", testNb++);
Yann Colletd2858e92016-05-30 15:10:09 +0200428 { size_t const testSize = CNBuffSize / 3;
Yann Collet6c6e1752016-06-27 15:28:45 +0200429 { ZSTD_parameters p = ZSTD_getParams(2, testSize, dictSize);
430 p.fParams.contentSizeFlag = 1;
Yann Colletd2858e92016-05-30 15:10:09 +0200431 CHECK( ZSTD_compressBegin_advanced(ctxOrig, CNBuffer, dictSize, p, testSize-1) );
Yann Collet33341de2016-05-29 23:09:51 +0200432 }
Yann Collet97b378a2016-09-21 17:20:19 +0200433 CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, testSize) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200434
Yann Collet97b378a2016-09-21 17:20:19 +0200435 CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(testSize),
436 (const char*)CNBuffer + dictSize, testSize),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200437 cSize = r);
Yann Collet542c9df2017-05-09 15:46:07 -0700438 { ZSTD_frameHeader fp;
439 if (ZSTD_getFrameHeader(&fp, compressedBuffer, cSize)) goto _output_error;
Yann Collet541dc7c2016-04-12 18:00:20 +0200440 if ((fp.frameContentSize != testSize) && (fp.frameContentSize != 0)) goto _output_error;
441 } }
442 DISPLAYLEVEL(4, "OK \n");
443
444 ZSTD_freeCCtx(ctxOrig);
445 ZSTD_freeCCtx(ctxDuplicated);
Yann Collet60096272016-01-08 17:27:50 +0100446 }
447
Yann Collet30009522016-05-30 16:17:33 +0200448 /* Dictionary and dictBuilder tests */
449 { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
Yann Collet30009522016-05-30 16:17:33 +0200450 size_t dictSize = 16 KB;
451 void* dictBuffer = malloc(dictSize);
452 size_t const totalSampleSize = 1 MB;
453 size_t const sampleUnitSize = 8 KB;
Yann Colletb81cbba2016-05-30 22:29:45 +0200454 U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
Yann Collet30009522016-05-30 16:17:33 +0200455 size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
Yann Colletf586bdf2016-12-06 06:11:46 +0100456 U32 dictID;
Yann Collet30009522016-05-30 16:17:33 +0200457
458 if (dictBuffer==NULL || samplesSizes==NULL) {
459 free(dictBuffer);
460 free(samplesSizes);
461 goto _output_error;
462 }
463
464 DISPLAYLEVEL(4, "test%3i : dictBuilder : ", testNb++);
465 { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
466 dictSize = ZDICT_trainFromBuffer(dictBuffer, dictSize,
467 CNBuffer, samplesSizes, nbSamples);
468 if (ZDICT_isError(dictSize)) goto _output_error;
469 DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize);
470
Yann Colletda3fbcb2016-08-19 14:23:58 +0200471 DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++);
Yann Colletf586bdf2016-12-06 06:11:46 +0100472 dictID = ZDICT_getDictID(dictBuffer, dictSize);
473 if (dictID==0) goto _output_error;
474 DISPLAYLEVEL(4, "OK : %u \n", dictID);
Yann Colletda3fbcb2016-08-19 14:23:58 +0200475
Yann Collet30009522016-05-30 16:17:33 +0200476 DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++);
477 cSize = ZSTD_compress_usingDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
478 CNBuffer, CNBuffSize,
479 dictBuffer, dictSize, 4);
480 if (ZSTD_isError(cSize)) goto _output_error;
481 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
482
Yann Collete7a41a52016-12-05 16:21:06 -0800483 DISPLAYLEVEL(4, "test%3i : retrieve dictID from dictionary : ", testNb++);
Yann Colletf586bdf2016-12-06 06:11:46 +0100484 { U32 const did = ZSTD_getDictID_fromDict(dictBuffer, dictSize);
485 if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
Yann Collete7a41a52016-12-05 16:21:06 -0800486 }
487 DISPLAYLEVEL(4, "OK \n");
488
489 DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++);
Yann Colletf586bdf2016-12-06 06:11:46 +0100490 { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
491 if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
Yann Collete7a41a52016-12-05 16:21:06 -0800492 }
493 DISPLAYLEVEL(4, "OK \n");
494
Yann Collet30009522016-05-30 16:17:33 +0200495 DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++);
496 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
497 decodedBuffer, CNBuffSize,
498 compressedBuffer, cSize,
499 dictBuffer, dictSize),
500 if (r != CNBuffSize) goto _output_error);
501 DISPLAYLEVEL(4, "OK \n");
502
Yann Colleta1d67042017-05-08 17:51:49 -0700503 DISPLAYLEVEL(4, "test%3i : estimate CDict size : ", testNb++);
504 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
Yann Collet7bd1a292017-06-21 11:50:33 -0700505 size_t const estimatedSize = ZSTD_estimateCDictSize(cParams, dictSize, 1 /*byReference*/);
Yann Colleta1d67042017-05-08 17:51:49 -0700506 DISPLAYLEVEL(4, "OK : %u \n", (U32)estimatedSize);
507 }
508
Yann Collet7bd1a292017-06-21 11:50:33 -0700509 DISPLAYLEVEL(4, "test%3i : compress with CDict ", testNb++);
Yann Collet2f734272017-04-27 14:39:39 -0700510 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
Yann Collet25989e32017-05-25 15:07:37 -0700511 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize,
Yann Collet7bd1a292017-06-21 11:50:33 -0700512 1 /* byReference */, ZSTD_dm_auto,
513 cParams, ZSTD_defaultCMem);
514 DISPLAYLEVEL(4, "(size : %u) : ", (U32)ZSTD_sizeof_CDict(cdict));
Yann Collet2f734272017-04-27 14:39:39 -0700515 cSize = ZSTD_compress_usingCDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
Nick Terrell39a6cc52017-04-03 21:00:44 -0700516 CNBuffer, CNBuffSize, cdict);
Yann Collet2f734272017-04-27 14:39:39 -0700517 ZSTD_freeCDict(cdict);
518 if (ZSTD_isError(cSize)) goto _output_error;
Nick Terrell39a6cc52017-04-03 21:00:44 -0700519 }
520 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
521
522 DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++);
523 { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
524 if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
525 }
526 DISPLAYLEVEL(4, "OK \n");
527
Nick Terrell39a6cc52017-04-03 21:00:44 -0700528 DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++);
529 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
530 decodedBuffer, CNBuffSize,
531 compressedBuffer, cSize,
532 dictBuffer, dictSize),
533 if (r != CNBuffSize) goto _output_error);
534 DISPLAYLEVEL(4, "OK \n");
535
Yann Colletcdf7e822017-05-25 18:05:49 -0700536 DISPLAYLEVEL(4, "test%3i : compress with static CDict : ", testNb++);
537 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
538 size_t const cdictSize = ZSTD_estimateCDictSize(cParams, dictSize, 0);
539 void* const cdictBuffer = malloc(cdictSize);
540 if (cdictBuffer==NULL) goto _output_error;
541 { ZSTD_CDict* const cdict = ZSTD_initStaticCDict(cdictBuffer, cdictSize,
Yann Collet7bd1a292017-06-21 11:50:33 -0700542 dictBuffer, dictSize,
543 0 /* by Reference */, ZSTD_dm_auto,
Yann Colletcdf7e822017-05-25 18:05:49 -0700544 cParams);
545 if (cdict == NULL) {
546 DISPLAY("ZSTD_initStaticCDict failed ");
547 goto _output_error;
548 }
549 cSize = ZSTD_compress_usingCDict(cctx,
550 compressedBuffer, ZSTD_compressBound(CNBuffSize),
551 CNBuffer, CNBuffSize, cdict);
552 if (ZSTD_isError(cSize)) {
553 DISPLAY("ZSTD_compress_usingCDict failed ");
554 goto _output_error;
555 } }
556 free(cdictBuffer);
557 }
558 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
559
Yann Collet2f734272017-04-27 14:39:39 -0700560 DISPLAYLEVEL(4, "test%3i : ZSTD_compress_usingCDict_advanced, no contentSize, no dictID : ", testNb++);
561 { ZSTD_frameParameters const fParams = { 0 /* frameSize */, 1 /* checksum */, 1 /* noDictID*/ };
562 ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
Yann Collet7bd1a292017-06-21 11:50:33 -0700563 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, 1 /*byRef*/, ZSTD_dm_auto, cParams, ZSTD_defaultCMem);
Yann Collet2f734272017-04-27 14:39:39 -0700564 cSize = ZSTD_compress_usingCDict_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
565 CNBuffer, CNBuffSize, cdict, fParams);
566 ZSTD_freeCDict(cdict);
567 if (ZSTD_isError(cSize)) goto _output_error;
568 }
569 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
570
571 DISPLAYLEVEL(4, "test%3i : try retrieving contentSize from frame : ", testNb++);
572 { U64 const contentSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
573 if (contentSize != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
574 }
575 DISPLAYLEVEL(4, "OK (unknown)\n");
576
577 DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++);
578 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
579 decodedBuffer, CNBuffSize,
580 compressedBuffer, cSize,
581 dictBuffer, dictSize),
582 if (r != CNBuffSize) goto _output_error);
583 DISPLAYLEVEL(4, "OK \n");
584
585 DISPLAYLEVEL(4, "test%3i : ZSTD_compress_advanced, no dictID : ", testNb++);
Yann Collet6c6e1752016-06-27 15:28:45 +0200586 { ZSTD_parameters p = ZSTD_getParams(3, CNBuffSize, dictSize);
587 p.fParams.noDictIDFlag = 1;
Yann Collet30009522016-05-30 16:17:33 +0200588 cSize = ZSTD_compress_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
589 CNBuffer, CNBuffSize,
590 dictBuffer, dictSize, p);
591 if (ZSTD_isError(cSize)) goto _output_error;
592 }
593 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
594
595 DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++);
596 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
597 decodedBuffer, CNBuffSize,
598 compressedBuffer, cSize,
599 dictBuffer, dictSize),
600 if (r != CNBuffSize) goto _output_error);
601 DISPLAYLEVEL(4, "OK \n");
602
Sean Purcell834ab502017-01-11 17:31:06 -0800603 DISPLAYLEVEL(4, "test%3i : dictionary containing only header should return error : ", testNb++);
604 {
605 const size_t ret = ZSTD_decompress_usingDict(
606 dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize,
607 "\x37\xa4\x30\xec\x11\x22\x33\x44", 8);
608 if (ZSTD_getErrorCode(ret) != ZSTD_error_dictionary_corrupted) goto _output_error;
609 }
Sean Purcellc44c4d52017-01-12 09:38:29 -0800610 DISPLAYLEVEL(4, "OK \n");
Sean Purcell834ab502017-01-11 17:31:06 -0800611
Yann Collet7bd1a292017-06-21 11:50:33 -0700612 DISPLAYLEVEL(4, "test%3i : Building cdict w/ ZSTD_dm_fullDict on a good dictionary : ", testNb++);
613 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
614 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, 1 /*byRef*/, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem);
615 if (cdict==NULL) goto _output_error;
616 ZSTD_freeCDict(cdict);
617 }
618 DISPLAYLEVEL(4, "OK \n");
619
620 DISPLAYLEVEL(4, "test%3i : Building cdict w/ ZSTD_dm_fullDict on a rawContent (must fail) : ", testNb++);
621 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
622 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced((const char*)dictBuffer+1, dictSize-1, 1 /*byRef*/, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem);
623 if (cdict!=NULL) goto _output_error;
624 ZSTD_freeCDict(cdict);
625 }
626 DISPLAYLEVEL(4, "OK \n");
627
Yann Collet30009522016-05-30 16:17:33 +0200628 ZSTD_freeCCtx(cctx);
Yann Collet30009522016-05-30 16:17:33 +0200629 free(dictBuffer);
630 free(samplesSizes);
631 }
632
Nick Terrella8b4fe02017-01-02 18:45:19 -0800633 /* COVER dictionary builder tests */
634 { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
Nick Terrella8b4fe02017-01-02 18:45:19 -0800635 size_t dictSize = 16 KB;
636 size_t optDictSize = dictSize;
637 void* dictBuffer = malloc(dictSize);
638 size_t const totalSampleSize = 1 MB;
639 size_t const sampleUnitSize = 8 KB;
640 U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
641 size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
642 COVER_params_t params;
643 U32 dictID;
644
645 if (dictBuffer==NULL || samplesSizes==NULL) {
646 free(dictBuffer);
647 free(samplesSizes);
648 goto _output_error;
649 }
650
651 DISPLAYLEVEL(4, "test%3i : COVER_trainFromBuffer : ", testNb++);
652 { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
653 memset(&params, 0, sizeof(params));
654 params.d = 1 + (FUZ_rand(&seed) % 16);
655 params.k = params.d + (FUZ_rand(&seed) % 256);
656 dictSize = COVER_trainFromBuffer(dictBuffer, dictSize,
657 CNBuffer, samplesSizes, nbSamples,
658 params);
659 if (ZDICT_isError(dictSize)) goto _output_error;
660 DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize);
661
662 DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++);
663 dictID = ZDICT_getDictID(dictBuffer, dictSize);
664 if (dictID==0) goto _output_error;
665 DISPLAYLEVEL(4, "OK : %u \n", dictID);
666
667 DISPLAYLEVEL(4, "test%3i : COVER_optimizeTrainFromBuffer : ", testNb++);
668 memset(&params, 0, sizeof(params));
669 params.steps = 4;
670 optDictSize = COVER_optimizeTrainFromBuffer(dictBuffer, optDictSize,
Yann Colletc0b17312017-02-28 01:02:46 -0800671 CNBuffer, samplesSizes, nbSamples / 4,
Nick Terrella8b4fe02017-01-02 18:45:19 -0800672 &params);
673 if (ZDICT_isError(optDictSize)) goto _output_error;
674 DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)optDictSize);
675
676 DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++);
677 dictID = ZDICT_getDictID(dictBuffer, optDictSize);
678 if (dictID==0) goto _output_error;
679 DISPLAYLEVEL(4, "OK : %u \n", dictID);
680
681 ZSTD_freeCCtx(cctx);
Nick Terrella8b4fe02017-01-02 18:45:19 -0800682 free(dictBuffer);
683 free(samplesSizes);
684 }
685
Yann Collet4856a002015-01-24 01:58:16 +0100686 /* Decompression defense tests */
687 DISPLAYLEVEL(4, "test%3i : Check input length for magic number : ", testNb++);
Yann Collet20d5e032017-04-11 18:34:02 -0700688 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 3); /* too small input */
Yann Colletc0a9bf32016-05-30 01:56:08 +0200689 if (!ZSTD_isError(r)) goto _output_error;
Yann Collet20d5e032017-04-11 18:34:02 -0700690 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100691 DISPLAYLEVEL(4, "OK \n");
692
693 DISPLAYLEVEL(4, "test%3i : Check magic Number : ", testNb++);
694 ((char*)(CNBuffer))[0] = 1;
Yann Colletd2858e92016-05-30 15:10:09 +0200695 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 4);
Yann Collet33341de2016-05-29 23:09:51 +0200696 if (!ZSTD_isError(r)) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100697 DISPLAYLEVEL(4, "OK \n");
698
Yann Collet20d5e032017-04-11 18:34:02 -0700699 /* content size verification test */
700 DISPLAYLEVEL(4, "test%3i : Content size verification : ", testNb++);
701 { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
702 size_t const srcSize = 5000;
703 size_t const wrongSrcSize = (srcSize + 1000);
704 ZSTD_parameters params = ZSTD_getParams(1, wrongSrcSize, 0);
705 params.fParams.contentSizeFlag = 1;
Yann Collet7bd1a292017-06-21 11:50:33 -0700706 CHECK( ZSTD_compressBegin_advanced(cctx, NULL, 0, params, wrongSrcSize) );
Yann Collet20d5e032017-04-11 18:34:02 -0700707 { size_t const result = ZSTD_compressEnd(cctx, decodedBuffer, CNBuffSize, CNBuffer, srcSize);
708 if (!ZSTD_isError(result)) goto _output_error;
709 if (ZSTD_getErrorCode(result) != ZSTD_error_srcSize_wrong) goto _output_error;
710 DISPLAYLEVEL(4, "OK : %s \n", ZSTD_getErrorName(result));
Yann Colletf913cbe2017-04-13 22:46:41 -0700711 }
712 ZSTD_freeCCtx(cctx);
713 }
Yann Collet20d5e032017-04-11 18:34:02 -0700714
Yann Colletbf42c8e2016-01-09 01:08:23 +0100715 /* block API tests */
Yann Colletd1d210f2016-03-19 12:12:07 +0100716 { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
Yann Colletd3d2db52016-07-15 12:20:26 +0200717 static const size_t dictSize = 65 KB;
718 static const size_t blockSize = 100 KB; /* won't cause pb with small dict size */
Yann Colletd4f4e582016-06-27 01:31:35 +0200719 size_t cSize2;
Yann Colletbf42c8e2016-01-09 01:08:23 +0100720
721 /* basic block compression */
722 DISPLAYLEVEL(4, "test%3i : Block compression test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200723 CHECK( ZSTD_compressBegin(cctx, 5) );
Yann Colletfa3671e2017-05-19 10:51:30 -0700724 CHECK( ZSTD_getBlockSize(cctx) >= blockSize);
Yann Colletbf42c8e2016-01-09 01:08:23 +0100725 cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize);
726 if (ZSTD_isError(cSize)) goto _output_error;
727 DISPLAYLEVEL(4, "OK \n");
728
729 DISPLAYLEVEL(4, "test%3i : Block decompression test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200730 CHECK( ZSTD_decompressBegin(dctx) );
Yann Colletd4f4e582016-06-27 01:31:35 +0200731 { CHECK_V(r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
Yann Colletd2858e92016-05-30 15:10:09 +0200732 if (r != blockSize) goto _output_error; }
Yann Colletbf42c8e2016-01-09 01:08:23 +0100733 DISPLAYLEVEL(4, "OK \n");
734
Yann Colletb0125102016-01-09 02:00:10 +0100735 /* dictionary block compression */
736 DISPLAYLEVEL(4, "test%3i : Dictionary Block compression test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200737 CHECK( ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5) );
Yann Colletb0125102016-01-09 02:00:10 +0100738 cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize);
739 if (ZSTD_isError(cSize)) goto _output_error;
Yann Colletd4f4e582016-06-27 01:31:35 +0200740 cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize+blockSize, blockSize);
741 if (ZSTD_isError(cSize2)) goto _output_error;
Yann Colletaa2628d2016-07-07 15:28:41 +0200742 memcpy((char*)compressedBuffer+cSize, (char*)CNBuffer+dictSize+blockSize, blockSize); /* fake non-compressed block */
743 cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize+blockSize, ZSTD_compressBound(blockSize),
744 (char*)CNBuffer+dictSize+2*blockSize, blockSize);
745 if (ZSTD_isError(cSize2)) goto _output_error;
Yann Colletb0125102016-01-09 02:00:10 +0100746 DISPLAYLEVEL(4, "OK \n");
747
748 DISPLAYLEVEL(4, "test%3i : Dictionary Block decompression test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200749 CHECK( ZSTD_decompressBegin_usingDict(dctx, CNBuffer, dictSize) );
Yann Colletd4f4e582016-06-27 01:31:35 +0200750 { CHECK_V( r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
751 if (r != blockSize) goto _output_error; }
Yann Colletaa2628d2016-07-07 15:28:41 +0200752 ZSTD_insertBlock(dctx, (char*)decodedBuffer+blockSize, blockSize); /* insert non-compressed block into dctx history */
753 { CHECK_V( r, ZSTD_decompressBlock(dctx, (char*)decodedBuffer+2*blockSize, CNBuffSize, (char*)compressedBuffer+cSize+blockSize, cSize2) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200754 if (r != blockSize) goto _output_error; }
Yann Colletb0125102016-01-09 02:00:10 +0100755 DISPLAYLEVEL(4, "OK \n");
756
Yann Colletbf42c8e2016-01-09 01:08:23 +0100757 ZSTD_freeCCtx(cctx);
758 ZSTD_freeDCtx(dctx);
759 }
760
Yann Collet213089c2015-06-18 07:43:16 -0800761 /* long rle test */
Yann Colletd1d210f2016-03-19 12:12:07 +0100762 { size_t sampleSize = 0;
Yann Collet213089c2015-06-18 07:43:16 -0800763 DISPLAYLEVEL(4, "test%3i : Long RLE test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200764 RDG_genBuffer(CNBuffer, sampleSize, compressibility, 0., seed+1);
Yann Collet213089c2015-06-18 07:43:16 -0800765 memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1);
766 sampleSize += 256 KB - 1;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200767 RDG_genBuffer((char*)CNBuffer+sampleSize, 96 KB, compressibility, 0., seed+2);
Yann Collet213089c2015-06-18 07:43:16 -0800768 sampleSize += 96 KB;
Yann Collet5be2dd22015-11-11 13:43:58 +0100769 cSize = ZSTD_compress(compressedBuffer, ZSTD_compressBound(sampleSize), CNBuffer, sampleSize, 1);
Yann Collet213089c2015-06-18 07:43:16 -0800770 if (ZSTD_isError(cSize)) goto _output_error;
Yann Colletd4f4e582016-06-27 01:31:35 +0200771 { CHECK_V(regenSize, ZSTD_decompress(decodedBuffer, sampleSize, compressedBuffer, cSize));
Yann Colletc0a9bf32016-05-30 01:56:08 +0200772 if (regenSize!=sampleSize) goto _output_error; }
Yann Collet213089c2015-06-18 07:43:16 -0800773 DISPLAYLEVEL(4, "OK \n");
774 }
775
Yann Colletc0a9bf32016-05-30 01:56:08 +0200776 /* All zeroes test (test bug #137) */
Yann Collet4ba85342016-03-07 20:01:45 +0100777 #define ZEROESLENGTH 100
778 DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH);
779 memset(CNBuffer, 0, ZEROESLENGTH);
Yann Colletd4f4e582016-06-27 01:31:35 +0200780 { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200781 cSize = r; }
Yann Collet4ba85342016-03-07 20:01:45 +0100782 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100);
783
784 DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH);
Yann Colletd4f4e582016-06-27 01:31:35 +0200785 { CHECK_V(r, ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200786 if (r != ZEROESLENGTH) goto _output_error; }
Yann Collet4ba85342016-03-07 20:01:45 +0100787 DISPLAYLEVEL(4, "OK \n");
788
789 /* nbSeq limit test */
Yann Colletd1d210f2016-03-19 12:12:07 +0100790 #define _3BYTESTESTLENGTH 131000
791 #define NB3BYTESSEQLOG 9
792 #define NB3BYTESSEQ (1 << NB3BYTESSEQLOG)
793 #define NB3BYTESSEQMASK (NB3BYTESSEQ-1)
Yann Collet0d9ce042016-03-19 13:21:08 +0100794 /* creates a buffer full of 3-bytes sequences */
Yann Colletd1d210f2016-03-19 12:12:07 +0100795 { BYTE _3BytesSeqs[NB3BYTESSEQ][3];
Yann Collet0d9ce042016-03-19 13:21:08 +0100796 U32 rSeed = 1;
Yann Collet4ba85342016-03-07 20:01:45 +0100797
Yann Collet0d9ce042016-03-19 13:21:08 +0100798 /* create batch of 3-bytes sequences */
Yann Colletf323bf72016-07-07 13:14:21 +0200799 { int i;
800 for (i=0; i < NB3BYTESSEQ; i++) {
801 _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&rSeed) & 255);
802 _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&rSeed) & 255);
803 _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&rSeed) & 255);
804 } }
Yann Collet4ba85342016-03-07 20:01:45 +0100805
Yann Collet0d9ce042016-03-19 13:21:08 +0100806 /* randomly fills CNBuffer with prepared 3-bytes sequences */
Yann Colletf323bf72016-07-07 13:14:21 +0200807 { int i;
808 for (i=0; i < _3BYTESTESTLENGTH; i += 3) { /* note : CNBuffer size > _3BYTESTESTLENGTH+3 */
809 U32 const id = FUZ_rand(&rSeed) & NB3BYTESSEQMASK;
810 ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0];
811 ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1];
812 ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2];
813 } } }
Yann Collet0d9ce042016-03-19 13:21:08 +0100814 DISPLAYLEVEL(4, "test%3i : compress lots 3-bytes sequences : ", testNb++);
Yann Colletd4f4e582016-06-27 01:31:35 +0200815 { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200816 CNBuffer, _3BYTESTESTLENGTH, 19) );
817 cSize = r; }
Yann Collet0d9ce042016-03-19 13:21:08 +0100818 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/_3BYTESTESTLENGTH*100);
Yann Collet4ba85342016-03-07 20:01:45 +0100819
Yann Collet0d9ce042016-03-19 13:21:08 +0100820 DISPLAYLEVEL(4, "test%3i : decompress lots 3-bytes sequence : ", testNb++);
Yann Colletd4f4e582016-06-27 01:31:35 +0200821 { CHECK_V(r, ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200822 if (r != _3BYTESTESTLENGTH) goto _output_error; }
Yann Collet0d9ce042016-03-19 13:21:08 +0100823 DISPLAYLEVEL(4, "OK \n");
Yann Collet4ba85342016-03-07 20:01:45 +0100824
Sean Purcell9050e192017-02-22 12:12:32 -0800825 /* findFrameCompressedSize on skippable frames */
826 DISPLAYLEVEL(4, "test%3i : frame compressed size of skippable frame : ", testNb++);
827 { const char* frame = "\x50\x2a\x4d\x18\x05\x0\x0\0abcde";
828 size_t const frameSrcSize = 13;
829 if (ZSTD_findFrameCompressedSize(frame, frameSrcSize) != frameSrcSize) goto _output_error; }
830 DISPLAYLEVEL(4, "OK \n");
831
Sean Purcelle0b32652017-02-08 15:31:47 -0800832 /* error string tests */
833 DISPLAYLEVEL(4, "test%3i : testing ZSTD error code strings : ", testNb++);
834 if (strcmp("No error detected", ZSTD_getErrorName((ZSTD_ErrorCode)(0-ZSTD_error_no_error))) != 0) goto _output_error;
835 if (strcmp("No error detected", ZSTD_getErrorString(ZSTD_error_no_error)) != 0) goto _output_error;
836 if (strcmp("Unspecified error code", ZSTD_getErrorString((ZSTD_ErrorCode)(0-ZSTD_error_GENERIC))) != 0) goto _output_error;
837 if (strcmp("Error (generic)", ZSTD_getErrorName((size_t)0-ZSTD_error_GENERIC)) != 0) goto _output_error;
838 if (strcmp("Error (generic)", ZSTD_getErrorString(ZSTD_error_GENERIC)) != 0) goto _output_error;
839 if (strcmp("No error detected", ZSTD_getErrorName(ZSTD_error_GENERIC)) != 0) goto _output_error;
840 DISPLAYLEVEL(4, "OK \n");
841
Yann Collet4856a002015-01-24 01:58:16 +0100842_end:
843 free(CNBuffer);
844 free(compressedBuffer);
845 free(decodedBuffer);
846 return testResult;
847
848_output_error:
849 testResult = 1;
850 DISPLAY("Error detected in Unit tests ! \n");
851 goto _end;
852}
853
854
855static size_t findDiff(const void* buf1, const void* buf2, size_t max)
856{
Yann Collet213089c2015-06-18 07:43:16 -0800857 const BYTE* b1 = (const BYTE*)buf1;
858 const BYTE* b2 = (const BYTE*)buf2;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200859 size_t u;
860 for (u=0; u<max; u++) {
861 if (b1[u] != b2[u]) break;
Yann Collet4856a002015-01-24 01:58:16 +0100862 }
Yann Colletc0a9bf32016-05-30 01:56:08 +0200863 return u;
Yann Collet4856a002015-01-24 01:58:16 +0100864}
865
Yann Collet1fce6e02016-04-08 20:26:33 +0200866
Yann Collet73213452017-04-27 14:19:34 -0700867static ZSTD_parameters FUZ_makeParams(ZSTD_compressionParameters cParams, ZSTD_frameParameters fParams)
868{
869 ZSTD_parameters params;
870 params.cParams = cParams;
871 params.fParams = fParams;
872 return params;
873}
874
Yann Collet1fce6e02016-04-08 20:26:33 +0200875static size_t FUZ_rLogLength(U32* seed, U32 logLength)
876{
877 size_t const lengthMask = ((size_t)1 << logLength) - 1;
878 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
879}
880
881static size_t FUZ_randomLength(U32* seed, U32 maxLog)
882{
883 U32 const logLength = FUZ_rand(seed) % maxLog;
884 return FUZ_rLogLength(seed, logLength);
885}
886
Yann Colletc0a9bf32016-05-30 01:56:08 +0200887#undef CHECK
Yann Collet7bd1a292017-06-21 11:50:33 -0700888#define CHECK(cond, ...) { \
889 if (cond) { \
890 DISPLAY("Error => "); \
891 DISPLAY(__VA_ARGS__); \
892 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); \
893 goto _output_error; \
894} }
895
896#define CHECK_Z(f) { \
897 size_t const err = f; \
898 if (ZSTD_isError(err)) { \
899 DISPLAY("Error => %s : %s ", \
900 #f, ZSTD_getErrorName(err)); \
901 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); \
902 goto _output_error; \
903} }
904
Yann Collet4856a002015-01-24 01:58:16 +0100905
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700906static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxDurationS, double compressibility, int bigTests)
Yann Collet4856a002015-01-24 01:58:16 +0100907{
Yann Collet1fce6e02016-04-08 20:26:33 +0200908 static const U32 maxSrcLog = 23;
909 static const U32 maxSampleLog = 22;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200910 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
911 size_t const dstBufferSize = (size_t)1<<maxSampleLog;
912 size_t const cBufferSize = ZSTD_compressBound(dstBufferSize);
Yann Colletd5d9bc32015-08-23 23:13:49 +0100913 BYTE* cNoiseBuffer[5];
Yann Colletc0a9bf32016-05-30 01:56:08 +0200914 BYTE* srcBuffer; /* jumping pointer */
915 BYTE* const cBuffer = (BYTE*) malloc (cBufferSize);
916 BYTE* const dstBuffer = (BYTE*) malloc (dstBufferSize);
917 BYTE* const mirrorBuffer = (BYTE*) malloc (dstBufferSize);
Yann Colletd2858e92016-05-30 15:10:09 +0200918 ZSTD_CCtx* const refCtx = ZSTD_createCCtx();
919 ZSTD_CCtx* const ctx = ZSTD_createCCtx();
920 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
Yann Collet30009522016-05-30 16:17:33 +0200921 U32 result = 0;
922 U32 testNb = 0;
923 U32 coreSeed = seed, lseed = 0;
Yann Colletd2858e92016-05-30 15:10:09 +0200924 clock_t const startClock = clock();
Yann Colletea63bb72016-04-08 15:25:32 +0200925 clock_t const maxClockSpan = maxDurationS * CLOCKS_PER_SEC;
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700926 int const cLevelLimiter = bigTests ? 3 : 2;
Yann Collet4856a002015-01-24 01:58:16 +0100927
928 /* allocation */
Yann Colletd5d9bc32015-08-23 23:13:49 +0100929 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
930 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
931 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
932 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
933 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Collete47c4e52015-12-05 09:23:53 +0100934 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4]
Yann Colletecd651b2016-01-07 15:35:18 +0100935 || !dstBuffer || !mirrorBuffer || !cBuffer || !refCtx || !ctx || !dctx,
Yann Colletd5d9bc32015-08-23 23:13:49 +0100936 "Not enough memory, fuzzer tests cancelled");
Yann Collet4856a002015-01-24 01:58:16 +0100937
Yann Colletd5d9bc32015-08-23 23:13:49 +0100938 /* Create initial samples */
939 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
940 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
941 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
942 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
943 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
944 srcBuffer = cNoiseBuffer[2];
Yann Collet4856a002015-01-24 01:58:16 +0100945
946 /* catch up testNb */
Yann Collet546c9b12016-03-19 12:47:52 +0100947 for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed);
Yann Collet4856a002015-01-24 01:58:16 +0100948
Yann Collet546c9b12016-03-19 12:47:52 +0100949 /* main test loop */
Yann Colletea63bb72016-04-08 15:25:32 +0200950 for ( ; (testNb <= nbTests) || (FUZ_clockSpan(startClock) < maxClockSpan); testNb++ ) {
Yann Colletc0a9bf32016-05-30 01:56:08 +0200951 size_t sampleSize, maxTestSize, totalTestSize;
952 size_t cSize, totalCSize, totalGenSize;
Yann Collet1fce6e02016-04-08 20:26:33 +0200953 U64 crcOrig;
Yann Collet110cc142015-11-19 12:02:28 +0100954 BYTE* sampleBuffer;
Yann Collet4bfe4152015-12-06 13:18:37 +0100955 const BYTE* dict;
956 size_t dictSize;
Yann Collet4856a002015-01-24 01:58:16 +0100957
Yann Collet546c9b12016-03-19 12:47:52 +0100958 /* notification */
959 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
Yann Collet4c0b44f2016-11-01 11:13:22 -0700960 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Collet1c2ddba2015-12-04 17:45:35 +0100961
Yann Collet4856a002015-01-24 01:58:16 +0100962 FUZ_rand(&coreSeed);
Yann Collet0d9ce042016-03-19 13:21:08 +0100963 { U32 const prime1 = 2654435761U; lseed = coreSeed ^ prime1; }
Yann Collet1fce6e02016-04-08 20:26:33 +0200964
965 /* srcBuffer selection [0-4] */
966 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
967 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
968 else {
969 buffNb >>= 3;
970 if (buffNb & 7) {
971 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
972 buffNb = tnb[buffNb >> 3];
973 } else {
974 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
975 buffNb = tnb[buffNb >> 3];
976 } }
977 srcBuffer = cNoiseBuffer[buffNb];
978 }
979
980 /* select src segment */
Yann Colletd2858e92016-05-30 15:10:09 +0200981 sampleSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet4856a002015-01-24 01:58:16 +0100982
Yann Collet110cc142015-11-19 12:02:28 +0100983 /* create sample buffer (to catch read error with valgrind & sanitizers) */
984 sampleBuffer = (BYTE*)malloc(sampleSize);
Yann Colletd2858e92016-05-30 15:10:09 +0200985 CHECK(sampleBuffer==NULL, "not enough memory for sample buffer");
Yann Colletc0a9bf32016-05-30 01:56:08 +0200986 { size_t const sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
987 memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); }
Yann Collet110cc142015-11-19 12:02:28 +0100988 crcOrig = XXH64(sampleBuffer, sampleSize, 0);
989
Yann Collet1fce6e02016-04-08 20:26:33 +0200990 /* compression tests */
Yann Collet20d5e032017-04-11 18:34:02 -0700991 { unsigned const cLevel =
992 ( FUZ_rand(&lseed) %
993 (ZSTD_maxCLevel() - (FUZ_highbit32((U32)sampleSize) / cLevelLimiter)) )
994 + 1;
Yann Collet1fce6e02016-04-08 20:26:33 +0200995 cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel);
Yann Colleta7737f62016-09-06 09:44:59 +0200996 CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed : %s", ZSTD_getErrorName(cSize));
Yann Collet4856a002015-01-24 01:58:16 +0100997
Yann Collet1fce6e02016-04-08 20:26:33 +0200998 /* compression failure test : too small dest buffer */
999 if (cSize > 3) {
1000 const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
1001 const size_t tooSmallSize = cSize - missing;
1002 const U32 endMark = 0x4DC2B1A9;
1003 memcpy(dstBuffer+tooSmallSize, &endMark, 4);
1004 { size_t const errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel);
1005 CHECK(!ZSTD_isError(errorCode), "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (U32)tooSmallSize, (U32)cSize); }
1006 { U32 endCheck; memcpy(&endCheck, dstBuffer+tooSmallSize, 4);
1007 CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow"); }
Yann Colletd2858e92016-05-30 15:10:09 +02001008 } }
Yann Collet1fce6e02016-04-08 20:26:33 +02001009
Yann Colletf323bf72016-07-07 13:14:21 +02001010 /* Decompressed size test */
Sean Purcell4e709712017-02-07 13:50:09 -08001011 { unsigned long long const rSize = ZSTD_findDecompressedSize(cBuffer, cSize);
Yann Colletf323bf72016-07-07 13:14:21 +02001012 CHECK(rSize != sampleSize, "decompressed size incorrect");
1013 }
1014
Yann Collet546c9b12016-03-19 12:47:52 +01001015 /* frame header decompression test */
Yann Collet542c9df2017-05-09 15:46:07 -07001016 { ZSTD_frameHeader dParams;
Yann Collet7bd1a292017-06-21 11:50:33 -07001017 CHECK_Z( ZSTD_getFrameHeader(&dParams, cBuffer, cSize) );
Yann Collet346bffb2016-03-15 15:24:52 +01001018 CHECK(dParams.frameContentSize != sampleSize, "Frame content size incorrect");
1019 }
1020
Yann Collet546c9b12016-03-19 12:47:52 +01001021 /* successful decompression test */
Yann Collet1fce6e02016-04-08 20:26:33 +02001022 { size_t const margin = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1;
Yann Colletc0a9bf32016-05-30 01:56:08 +02001023 size_t const dSize = ZSTD_decompress(dstBuffer, sampleSize + margin, cBuffer, cSize);
Yann Collet546c9b12016-03-19 12:47:52 +01001024 CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s) (srcSize : %u ; cSize : %u)", ZSTD_getErrorName(dSize), (U32)sampleSize, (U32)cSize);
Yann Collet1fce6e02016-04-08 20:26:33 +02001025 { U64 const crcDest = XXH64(dstBuffer, sampleSize, 0);
1026 CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (U32)findDiff(sampleBuffer, dstBuffer, sampleSize), (U32)sampleSize);
1027 } }
Yann Collet110cc142015-11-19 12:02:28 +01001028
1029 free(sampleBuffer); /* no longer useful after this point */
Yann Colletf3cb79b2015-08-20 00:02:43 +01001030
1031 /* truncated src decompression test */
Yann Collet1fce6e02016-04-08 20:26:33 +02001032 { size_t const missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
1033 size_t const tooSmallSize = cSize - missing;
Yann Colletd2858e92016-05-30 15:10:09 +02001034 void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch read overflows */
Yann Colletf3cb79b2015-08-20 00:02:43 +01001035 CHECK(cBufferTooSmall == NULL, "not enough memory !");
Yann Colletd5d9bc32015-08-23 23:13:49 +01001036 memcpy(cBufferTooSmall, cBuffer, tooSmallSize);
Yann Collet1fce6e02016-04-08 20:26:33 +02001037 { size_t const errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize);
1038 CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); }
Yann Colletf3cb79b2015-08-20 00:02:43 +01001039 free(cBufferTooSmall);
1040 }
1041
1042 /* too small dst decompression test */
Yann Colletd1d210f2016-03-19 12:12:07 +01001043 if (sampleSize > 3) {
Yann Colletea63bb72016-04-08 15:25:32 +02001044 size_t const missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
1045 size_t const tooSmallSize = sampleSize - missing;
Yann Colletf3cb79b2015-08-20 00:02:43 +01001046 static const BYTE token = 0xA9;
1047 dstBuffer[tooSmallSize] = token;
Yann Collet1fce6e02016-04-08 20:26:33 +02001048 { size_t const errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize);
1049 CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (U32)errorCode, (U32)tooSmallSize); }
Yann Colletf3cb79b2015-08-20 00:02:43 +01001050 CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow");
1051 }
Yann Collet997f9ee2015-08-21 02:44:20 +01001052
1053 /* noisy src decompression test */
Yann Colletd1d210f2016-03-19 12:12:07 +01001054 if (cSize > 6) {
1055 /* insert noise into src */
1056 { U32 const maxNbBits = FUZ_highbit32((U32)(cSize-4));
1057 size_t pos = 4; /* preserve magic number (too easy to detect) */
1058 for (;;) {
1059 /* keep some original src */
1060 { U32 const nbBits = FUZ_rand(&lseed) % maxNbBits;
1061 size_t const mask = (1<<nbBits) - 1;
1062 size_t const skipLength = FUZ_rand(&lseed) & mask;
1063 pos += skipLength;
1064 }
1065 if (pos <= cSize) break;
1066 /* add noise */
Yann Collet33341de2016-05-29 23:09:51 +02001067 { U32 const nbBitsCodes = FUZ_rand(&lseed) % maxNbBits;
1068 U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0;
1069 size_t const mask = (1<<nbBits) - 1;
1070 size_t const rNoiseLength = (FUZ_rand(&lseed) & mask) + 1;
1071 size_t const noiseLength = MIN(rNoiseLength, cSize-pos);
1072 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength);
Yann Colletd1d210f2016-03-19 12:12:07 +01001073 memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength);
1074 pos += noiseLength;
1075 } } }
Yann Collet997f9ee2015-08-21 02:44:20 +01001076
1077 /* decompress noisy source */
Yann Colletd1d210f2016-03-19 12:12:07 +01001078 { U32 const endMark = 0xA9B1C3D6;
Yann Collet997f9ee2015-08-21 02:44:20 +01001079 memcpy(dstBuffer+sampleSize, &endMark, 4);
Yann Collet1fce6e02016-04-08 20:26:33 +02001080 { size_t const decompressResult = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize);
1081 /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */
1082 CHECK((!ZSTD_isError(decompressResult)) && (decompressResult>sampleSize),
1083 "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (U32)decompressResult, (U32)sampleSize);
1084 }
1085 { U32 endCheck; memcpy(&endCheck, dstBuffer+sampleSize, 4);
1086 CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow");
1087 } } } /* noisy src decompression test */
Yann Collet417890c2015-12-04 17:16:37 +01001088
Yann Collet1fce6e02016-04-08 20:26:33 +02001089 /*===== Streaming compression test, scattered segments and dictionary =====*/
1090
1091 { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -07001092 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001093 int const cLevel = (FUZ_rand(&lseed) %
1094 (ZSTD_maxCLevel() -
1095 (MAX(testLog, dictLog) / cLevelLimiter))) +
1096 1;
Yann Collet1fce6e02016-04-08 20:26:33 +02001097 maxTestSize = FUZ_rLogLength(&lseed, testLog);
1098 if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1;
1099
Sean Purcellf5e50512017-03-15 15:04:54 -07001100 dictSize = FUZ_rLogLength(&lseed, dictLog); /* needed also for decompression */
Yann Colletc0a9bf32016-05-30 01:56:08 +02001101 dict = srcBuffer + (FUZ_rand(&lseed) % (srcBufferSize - dictSize));
Yann Collet1fce6e02016-04-08 20:26:33 +02001102
Yann Colletf2a3b6e2016-05-31 18:13:56 +02001103 if (FUZ_rand(&lseed) & 0xF) {
Yann Collet7bd1a292017-06-21 11:50:33 -07001104 CHECK_Z ( ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, cLevel) );
Yann Collet33341de2016-05-29 23:09:51 +02001105 } else {
cyanb8806312016-05-30 18:20:46 +02001106 ZSTD_compressionParameters const cPar = ZSTD_getCParams(cLevel, 0, dictSize);
Yann Colletf4bd8572017-04-27 11:31:55 -07001107 ZSTD_frameParameters const fPar = { FUZ_rand(&lseed)&1 /* contentSizeFlag */,
Yann Colletf2a3b6e2016-05-31 18:13:56 +02001108 !(FUZ_rand(&lseed)&3) /* contentChecksumFlag*/,
1109 0 /*NodictID*/ }; /* note : since dictionary is fake, dictIDflag has no impact */
Yann Collet73213452017-04-27 14:19:34 -07001110 ZSTD_parameters const p = FUZ_makeParams(cPar, fPar);
Yann Collet7bd1a292017-06-21 11:50:33 -07001111 CHECK_Z ( ZSTD_compressBegin_advanced(refCtx, dict, dictSize, p, 0) );
Yann Collet33341de2016-05-29 23:09:51 +02001112 }
Yann Collet7bd1a292017-06-21 11:50:33 -07001113 CHECK_Z( ZSTD_copyCCtx(ctx, refCtx, 0) );
1114 }
Yann Collet06e76972017-01-25 16:39:03 -08001115 ZSTD_setCCtxParameter(ctx, ZSTD_p_forceWindow, FUZ_rand(&lseed) & 1);
Yann Colletf4bd8572017-04-27 11:31:55 -07001116
Yann Colletd2858e92016-05-30 15:10:09 +02001117 { U32 const nbChunks = (FUZ_rand(&lseed) & 127) + 2;
1118 U32 n;
Yann Colletf4bd8572017-04-27 11:31:55 -07001119 XXH64_state_t xxhState;
1120 XXH64_reset(&xxhState, 0);
Yann Colletd2858e92016-05-30 15:10:09 +02001121 for (totalTestSize=0, cSize=0, n=0 ; n<nbChunks ; n++) {
1122 size_t const segmentSize = FUZ_randomLength(&lseed, maxSampleLog);
1123 size_t const segmentStart = FUZ_rand(&lseed) % (srcBufferSize - segmentSize);
Yann Collet417890c2015-12-04 17:16:37 +01001124
Yann Colletd2858e92016-05-30 15:10:09 +02001125 if (cBufferSize-cSize < ZSTD_compressBound(segmentSize)) break; /* avoid invalid dstBufferTooSmall */
1126 if (totalTestSize+segmentSize > maxTestSize) break;
Yann Collet417890c2015-12-04 17:16:37 +01001127
Yann Colletd2858e92016-05-30 15:10:09 +02001128 { size_t const compressResult = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+segmentStart, segmentSize);
1129 CHECK (ZSTD_isError(compressResult), "multi-segments compression error : %s", ZSTD_getErrorName(compressResult));
1130 cSize += compressResult;
1131 }
1132 XXH64_update(&xxhState, srcBuffer+segmentStart, segmentSize);
1133 memcpy(mirrorBuffer + totalTestSize, srcBuffer+segmentStart, segmentSize);
1134 totalTestSize += segmentSize;
Yann Colletf4bd8572017-04-27 11:31:55 -07001135 }
Yann Colletd2858e92016-05-30 15:10:09 +02001136
Yann Colletf4bd8572017-04-27 11:31:55 -07001137 { size_t const flushResult = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize, NULL, 0);
1138 CHECK (ZSTD_isError(flushResult), "multi-segments epilogue error : %s", ZSTD_getErrorName(flushResult));
1139 cSize += flushResult;
1140 }
1141 crcOrig = XXH64_digest(&xxhState);
Yann Collet1fce6e02016-04-08 20:26:33 +02001142 }
Yann Collete47c4e52015-12-05 09:23:53 +01001143
1144 /* streaming decompression test */
Yann Collet33341de2016-05-29 23:09:51 +02001145 if (dictSize<8) dictSize=0, dict=NULL; /* disable dictionary */
Yann Collet7bd1a292017-06-21 11:50:33 -07001146 CHECK_Z( ZSTD_decompressBegin_usingDict(dctx, dict, dictSize) );
Yann Collete47c4e52015-12-05 09:23:53 +01001147 totalCSize = 0;
1148 totalGenSize = 0;
Yann Colletd1d210f2016-03-19 12:12:07 +01001149 while (totalCSize < cSize) {
Yann Collet1fce6e02016-04-08 20:26:33 +02001150 size_t const inSize = ZSTD_nextSrcSizeToDecompress(dctx);
1151 size_t const genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize);
Yann Collet45dc3562016-07-12 09:47:31 +02001152 CHECK (ZSTD_isError(genSize), "ZSTD_decompressContinue error : %s", ZSTD_getErrorName(genSize));
Yann Collete47c4e52015-12-05 09:23:53 +01001153 totalGenSize += genSize;
1154 totalCSize += inSize;
1155 }
1156 CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded");
Yann Colletc0a9bf32016-05-30 01:56:08 +02001157 CHECK (totalGenSize != totalTestSize, "streaming decompressed data : wrong size")
Yann Collete47c4e52015-12-05 09:23:53 +01001158 CHECK (totalCSize != cSize, "compressed data should be fully read")
Yann Collet1fce6e02016-04-08 20:26:33 +02001159 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1160 if (crcDest!=crcOrig) {
1161 size_t const errorPos = findDiff(mirrorBuffer, dstBuffer, totalTestSize);
Yann Colletd2858e92016-05-30 15:10:09 +02001162 CHECK (1, "streaming decompressed data corrupted : byte %u / %u (%02X!=%02X)",
Yann Collet1fce6e02016-04-08 20:26:33 +02001163 (U32)errorPos, (U32)totalTestSize, dstBuffer[errorPos], mirrorBuffer[errorPos]);
1164 } }
1165 } /* for ( ; (testNb <= nbTests) */
Yann Collet1c2ddba2015-12-04 17:45:35 +01001166 DISPLAY("\r%u fuzzer tests completed \n", testNb-1);
Yann Collet4856a002015-01-24 01:58:16 +01001167
1168_cleanup:
Yann Colletecd651b2016-01-07 15:35:18 +01001169 ZSTD_freeCCtx(refCtx);
Yann Collet2f648e52015-10-29 18:23:38 +01001170 ZSTD_freeCCtx(ctx);
Yann Collete47c4e52015-12-05 09:23:53 +01001171 ZSTD_freeDCtx(dctx);
Yann Colletd5d9bc32015-08-23 23:13:49 +01001172 free(cNoiseBuffer[0]);
1173 free(cNoiseBuffer[1]);
1174 free(cNoiseBuffer[2]);
1175 free(cNoiseBuffer[3]);
1176 free(cNoiseBuffer[4]);
Yann Collet4856a002015-01-24 01:58:16 +01001177 free(cBuffer);
1178 free(dstBuffer);
Yann Collete47c4e52015-12-05 09:23:53 +01001179 free(mirrorBuffer);
Yann Collet4856a002015-01-24 01:58:16 +01001180 return result;
1181
1182_output_error:
1183 result = 1;
1184 goto _cleanup;
1185}
1186
1187
Yann Colletd1d210f2016-03-19 12:12:07 +01001188/*_*******************************************************
Yann Collet4856a002015-01-24 01:58:16 +01001189* Command line
1190*********************************************************/
Yann Colletc6915422017-04-27 16:24:53 -07001191static int FUZ_usage(const char* programName)
Yann Collet4856a002015-01-24 01:58:16 +01001192{
1193 DISPLAY( "Usage :\n");
1194 DISPLAY( " %s [args]\n", programName);
1195 DISPLAY( "\n");
1196 DISPLAY( "Arguments :\n");
1197 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
1198 DISPLAY( " -s# : Select seed (default:prompt user)\n");
1199 DISPLAY( " -t# : Select starting test number (default:0)\n");
Yann Collet0d9ce042016-03-19 13:21:08 +01001200 DISPLAY( " -P# : Select compressibility in %% (default:%u%%)\n", FUZ_compressibility_default);
Yann Collet4856a002015-01-24 01:58:16 +01001201 DISPLAY( " -v : verbose\n");
Yann Collete9853b22015-08-07 19:07:32 +01001202 DISPLAY( " -p : pause at the end\n");
Yann Collet4856a002015-01-24 01:58:16 +01001203 DISPLAY( " -h : display help and exit\n");
1204 return 0;
1205}
1206
Yann Colletc6915422017-04-27 16:24:53 -07001207/*! readU32FromChar() :
1208 @return : unsigned integer value read from input in `char` format
1209 allows and interprets K, KB, KiB, M, MB and MiB suffix.
1210 Will also modify `*stringPtr`, advancing it to position where it stopped reading.
1211 Note : function result can overflow if digit string > MAX_UINT */
1212static unsigned readU32FromChar(const char** stringPtr)
1213{
1214 unsigned result = 0;
1215 while ((**stringPtr >='0') && (**stringPtr <='9'))
1216 result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
1217 if ((**stringPtr=='K') || (**stringPtr=='M')) {
1218 result <<= 10;
1219 if (**stringPtr=='M') result <<= 10;
1220 (*stringPtr)++ ;
1221 if (**stringPtr=='i') (*stringPtr)++;
1222 if (**stringPtr=='B') (*stringPtr)++;
1223 }
1224 return result;
1225}
Yann Collet4856a002015-01-24 01:58:16 +01001226
Yann Colletd1d210f2016-03-19 12:12:07 +01001227int main(int argc, const char** argv)
Yann Collet4856a002015-01-24 01:58:16 +01001228{
Yann Colletc6915422017-04-27 16:24:53 -07001229 U32 seed = 0;
1230 int seedset = 0;
Yann Collet4856a002015-01-24 01:58:16 +01001231 int argNb;
1232 int nbTests = nbTestsDefault;
1233 int testNb = 0;
Yann Collet0d9ce042016-03-19 13:21:08 +01001234 U32 proba = FUZ_compressibility_default;
Yann Colletc6915422017-04-27 16:24:53 -07001235 int result = 0;
Yann Collet4856a002015-01-24 01:58:16 +01001236 U32 mainPause = 0;
Yann Collet0d9ce042016-03-19 13:21:08 +01001237 U32 maxDuration = 0;
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001238 int bigTests = 1;
Yann Colletc6915422017-04-27 16:24:53 -07001239 const char* const programName = argv[0];
Yann Collet4856a002015-01-24 01:58:16 +01001240
1241 /* Check command line */
Yann Colletd1d210f2016-03-19 12:12:07 +01001242 for (argNb=1; argNb<argc; argNb++) {
1243 const char* argument = argv[argNb];
Yann Collet4856a002015-01-24 01:58:16 +01001244 if(!argument) continue; /* Protection if argument empty */
1245
1246 /* Handle commands. Aggregated commands are allowed */
Yann Colletd1d210f2016-03-19 12:12:07 +01001247 if (argument[0]=='-') {
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001248
1249 if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
1250
Yann Collet4856a002015-01-24 01:58:16 +01001251 argument++;
Yann Colletd1d210f2016-03-19 12:12:07 +01001252 while (*argument!=0) {
Yann Collet4856a002015-01-24 01:58:16 +01001253 switch(*argument)
1254 {
1255 case 'h':
1256 return FUZ_usage(programName);
Yann Colletc6915422017-04-27 16:24:53 -07001257
Yann Collet4856a002015-01-24 01:58:16 +01001258 case 'v':
1259 argument++;
Yann Colletc6915422017-04-27 16:24:53 -07001260 g_displayLevel = 4;
Yann Collet4856a002015-01-24 01:58:16 +01001261 break;
Yann Colletc6915422017-04-27 16:24:53 -07001262
Yann Collet4856a002015-01-24 01:58:16 +01001263 case 'q':
1264 argument++;
1265 g_displayLevel--;
1266 break;
Yann Colletc6915422017-04-27 16:24:53 -07001267
Yann Collet4856a002015-01-24 01:58:16 +01001268 case 'p': /* pause at the end */
1269 argument++;
1270 mainPause = 1;
1271 break;
1272
1273 case 'i':
Yann Colletc6915422017-04-27 16:24:53 -07001274 argument++; maxDuration = 0;
1275 nbTests = readU32FromChar(&argument);
Yann Collet4856a002015-01-24 01:58:16 +01001276 break;
1277
Yann Collet553cf6a2015-12-04 17:25:26 +01001278 case 'T':
1279 argument++;
Yann Colletc6915422017-04-27 16:24:53 -07001280 nbTests = 0;
1281 maxDuration = readU32FromChar(&argument);
1282 if (*argument=='s') argument++; /* seconds */
1283 if (*argument=='m') maxDuration *= 60, argument++; /* minutes */
Yann Collet553cf6a2015-12-04 17:25:26 +01001284 if (*argument=='n') argument++;
Yann Collet553cf6a2015-12-04 17:25:26 +01001285 break;
1286
Yann Collet4856a002015-01-24 01:58:16 +01001287 case 's':
1288 argument++;
Yann Colletc6915422017-04-27 16:24:53 -07001289 seedset = 1;
1290 seed = readU32FromChar(&argument);
Yann Collet4856a002015-01-24 01:58:16 +01001291 break;
1292
1293 case 't':
1294 argument++;
Yann Colletc6915422017-04-27 16:24:53 -07001295 testNb = readU32FromChar(&argument);
Yann Collet4856a002015-01-24 01:58:16 +01001296 break;
1297
1298 case 'P': /* compressibility % */
1299 argument++;
Yann Colletc6915422017-04-27 16:24:53 -07001300 proba = readU32FromChar(&argument);
1301 if (proba>100) proba = 100;
Yann Collet4856a002015-01-24 01:58:16 +01001302 break;
1303
1304 default:
Yann Colletc6915422017-04-27 16:24:53 -07001305 return (FUZ_usage(programName), 1);
Yann Colletd1d210f2016-03-19 12:12:07 +01001306 } } } } /* for (argNb=1; argNb<argc; argNb++) */
Yann Collet4856a002015-01-24 01:58:16 +01001307
1308 /* Get Seed */
Yann Collet45f84ab2016-05-20 12:34:40 +02001309 DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Collet4856a002015-01-24 01:58:16 +01001310
Yann Collet3f01c882016-06-16 13:38:10 +02001311 if (!seedset) {
1312 time_t const t = time(NULL);
1313 U32 const h = XXH32(&t, sizeof(t), 1);
1314 seed = h % 10000;
1315 }
1316
Yann Collet4856a002015-01-24 01:58:16 +01001317 DISPLAY("Seed = %u\n", seed);
Yann Collet0d9ce042016-03-19 13:21:08 +01001318 if (proba!=FUZ_compressibility_default) DISPLAY("Compressibility : %u%%\n", proba);
Yann Collet4856a002015-01-24 01:58:16 +01001319
Yann Collet803c05e2016-06-16 11:32:57 +02001320 if (nbTests < testNb) nbTests = testNb;
1321
Yann Colletd1d210f2016-03-19 12:12:07 +01001322 if (testNb==0)
1323 result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */
Yann Collet4856a002015-01-24 01:58:16 +01001324 if (!result)
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001325 result = fuzzerTests(seed, nbTests, testNb, maxDuration, ((double)proba) / 100, bigTests);
Yann Colletd1d210f2016-03-19 12:12:07 +01001326 if (mainPause) {
Yann Colletb5e06dc2015-07-04 23:20:56 -08001327 int unused;
Yann Collet4856a002015-01-24 01:58:16 +01001328 DISPLAY("Press Enter \n");
Yann Colletb5e06dc2015-07-04 23:20:56 -08001329 unused = getchar();
1330 (void)unused;
Yann Collet4856a002015-01-24 01:58:16 +01001331 }
1332 return result;
1333}