blob: 79516b6cc71f22fa82b3a17c94a0dafe48f848c0 [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 */
15# define _CRT_SECURE_NO_WARNINGS /* fgets */
16# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
cyanb8806312016-05-30 18:20:46 +020017# 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 Colletd4f4e582016-06-27 01:31:35 +020028#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 */
Nick Terrella8b4fe02017-01-02 18:45:19 -080031#define ZDICT_STATIC_LINKING_ONLY
Yann Collet6fa05a22016-07-20 14:58:49 +020032#include "zdict.h" /* ZDICT_trainFromBuffer */
33#include "datagen.h" /* RDG_genBuffer */
Yann Collet353c5d22015-10-21 14:39:26 +010034#include "mem.h"
Yann Collet6c903a82016-05-28 13:34:07 +020035#define XXH_STATIC_LINKING_ONLY
Yann Collet6fa05a22016-07-20 14:58:49 +020036#include "xxhash.h" /* XXH64 */
Yann Collet4856a002015-01-24 01:58:16 +010037
38
Yann Collet0d9ce042016-03-19 13:21:08 +010039/*-************************************
40* Constants
Yann Collet4856a002015-01-24 01:58:16 +010041**************************************/
Yann Collet4856a002015-01-24 01:58:16 +010042#define KB *(1U<<10)
43#define MB *(1U<<20)
44#define GB *(1U<<30)
45
Yann Collet0d9ce042016-03-19 13:21:08 +010046static const U32 FUZ_compressibility_default = 50;
Yann Collet110cc142015-11-19 12:02:28 +010047static const U32 nbTestsDefault = 30000;
Yann Collet4856a002015-01-24 01:58:16 +010048
49
Yann Collet0d9ce042016-03-19 13:21:08 +010050/*-************************************
Yann Collet4856a002015-01-24 01:58:16 +010051* Display Macros
52**************************************/
53#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
54#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
55static U32 g_displayLevel = 2;
56
57#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletea63bb72016-04-08 15:25:32 +020058 if ((FUZ_clockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
59 { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \
Yann Collet4856a002015-01-24 01:58:16 +010060 if (g_displayLevel>=4) fflush(stdout); } }
Yann Collet64deef32016-09-14 00:16:07 +020061static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6;
Yann Colletea63bb72016-04-08 15:25:32 +020062static clock_t g_displayClock = 0;
Yann Collet553cf6a2015-12-04 17:25:26 +010063
Yann Collet4856a002015-01-24 01:58:16 +010064
Yann Collet0d9ce042016-03-19 13:21:08 +010065/*-*******************************************************
Yann Collet4856a002015-01-24 01:58:16 +010066* Fuzzer functions
67*********************************************************/
Yann Collete93add02016-02-15 17:44:14 +010068#define MIN(a,b) ((a)<(b)?(a):(b))
Yann Collet110cc142015-11-19 12:02:28 +010069
Yann Colletea63bb72016-04-08 15:25:32 +020070static clock_t FUZ_clockSpan(clock_t cStart)
Yann Collet4856a002015-01-24 01:58:16 +010071{
Yann Colletea63bb72016-04-08 15:25:32 +020072 return clock() - cStart; /* works even when overflow; max span ~ 30mn */
Yann Collet4856a002015-01-24 01:58:16 +010073}
74
75
Yann Colletc0a9bf32016-05-30 01:56:08 +020076#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
77static unsigned FUZ_rand(unsigned* src)
Yann Collet4856a002015-01-24 01:58:16 +010078{
Yann Collet0d9ce042016-03-19 13:21:08 +010079 static const U32 prime1 = 2654435761U;
80 static const U32 prime2 = 2246822519U;
Yann Collet4856a002015-01-24 01:58:16 +010081 U32 rand32 = *src;
82 rand32 *= prime1;
83 rand32 += prime2;
84 rand32 = FUZ_rotl32(rand32, 13);
85 *src = rand32;
86 return rand32 >> 5;
87}
88
Yann Collet997f9ee2015-08-21 02:44:20 +010089static unsigned FUZ_highbit32(U32 v32)
Yann Collet4856a002015-01-24 01:58:16 +010090{
91 unsigned nbBits = 0;
92 if (v32==0) return 0;
Yann Colletc0a9bf32016-05-30 01:56:08 +020093 while (v32) v32 >>= 1, nbBits++;
Yann Collet4856a002015-01-24 01:58:16 +010094 return nbBits;
95}
Yann Collet4856a002015-01-24 01:58:16 +010096
97
Yann Colletcb327632016-08-23 00:30:31 +020098/*=============================================
99* Basic Unit tests
100=============================================*/
101
Yann Colletd4f4e582016-06-27 01:31:35 +0200102#define CHECK_V(var, fn) size_t const var = fn; if (ZSTD_isError(var)) goto _output_error
103#define CHECK(fn) { CHECK_V(err, fn); }
104#define CHECKPLUS(var, fn, more) { CHECK_V(var, fn); more; }
Yann Collet4856a002015-01-24 01:58:16 +0100105static int basicUnitTests(U32 seed, double compressibility)
106{
Yann Collet30009522016-05-30 16:17:33 +0200107 size_t const CNBuffSize = 5 MB;
Yann Colletd2858e92016-05-30 15:10:09 +0200108 void* const CNBuffer = malloc(CNBuffSize);
109 void* const compressedBuffer = malloc(ZSTD_compressBound(CNBuffSize));
110 void* const decodedBuffer = malloc(CNBuffSize);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200111 int testResult = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100112 U32 testNb=0;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200113 size_t cSize;
Yann Collet4856a002015-01-24 01:58:16 +0100114
Yann Colletc0a9bf32016-05-30 01:56:08 +0200115 /* Create compressible noise */
Yann Colletd1d210f2016-03-19 12:12:07 +0100116 if (!CNBuffer || !compressedBuffer || !decodedBuffer) {
Yann Collet94f998b2015-07-04 23:10:40 -0800117 DISPLAY("Not enough memory, aborting\n");
118 testResult = 1;
119 goto _end;
120 }
Yann Colletd2858e92016-05-30 15:10:09 +0200121 RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed);
Yann Collet4856a002015-01-24 01:58:16 +0100122
Yann Colletd5d9bc32015-08-23 23:13:49 +0100123 /* Basic tests */
Yann Collet9a69ec42016-08-01 16:25:58 +0200124 DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName : ", testNb++);
125 { const char* errorString = ZSTD_getErrorName(0);
126 DISPLAYLEVEL(4, "OK : %s \n", errorString);
127 }
128
129 DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName with wrong value : ", testNb++);
130 { const char* errorString = ZSTD_getErrorName(499);
131 DISPLAYLEVEL(4, "OK : %s \n", errorString);
132 }
133
Yann Colletd2858e92016-05-30 15:10:09 +0200134 DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, (U32)CNBuffSize);
135 CHECKPLUS(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(CNBuffSize),
136 CNBuffer, CNBuffSize, 1),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200137 cSize=r );
Yann Colletd2858e92016-05-30 15:10:09 +0200138 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
Yann Collet4856a002015-01-24 01:58:16 +0100139
Yann Colletf323bf72016-07-07 13:14:21 +0200140 DISPLAYLEVEL(4, "test%3i : decompressed size test : ", testNb++);
Sean Purcell4e709712017-02-07 13:50:09 -0800141 { unsigned long long const rSize = ZSTD_findDecompressedSize(compressedBuffer, cSize);
Yann Colletf323bf72016-07-07 13:14:21 +0200142 if (rSize != CNBuffSize) goto _output_error;
143 }
144 DISPLAYLEVEL(4, "OK \n");
145
Yann Colletd2858e92016-05-30 15:10:09 +0200146 DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)CNBuffSize);
Yann Colletc991cc12016-07-28 00:55:43 +0200147 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
148 if (r != CNBuffSize) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100149 DISPLAYLEVEL(4, "OK \n");
150
Yann Colletc0a9bf32016-05-30 01:56:08 +0200151 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
152 { size_t u;
Yann Colletd2858e92016-05-30 15:10:09 +0200153 for (u=0; u<CNBuffSize; u++) {
Yann Colletc0a9bf32016-05-30 01:56:08 +0200154 if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;;
155 } }
156 DISPLAYLEVEL(4, "OK \n");
Yann Collet4856a002015-01-24 01:58:16 +0100157
158 DISPLAYLEVEL(4, "test%3i : decompress with 1 missing byte : ", testNb++);
Yann Colletd2858e92016-05-30 15:10:09 +0200159 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize-1);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200160 if (!ZSTD_isError(r)) goto _output_error;
161 if (ZSTD_getErrorCode((size_t)r) != ZSTD_error_srcSize_wrong) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100162 DISPLAYLEVEL(4, "OK \n");
163
164 DISPLAYLEVEL(4, "test%3i : decompress with 1 too much byte : ", testNb++);
Yann Colletd2858e92016-05-30 15:10:09 +0200165 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize+1);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200166 if (!ZSTD_isError(r)) goto _output_error;
167 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100168 DISPLAYLEVEL(4, "OK \n");
169
Sean Purcellba2ad9f2017-02-07 16:16:55 -0800170 /* Simple API multiframe test */
171 DISPLAYLEVEL(4, "test%3i : compress multiple frames : ", testNb++);
172 { size_t off = 0;
173 int i;
174 int const segs = 4;
175 /* only use the first half so we don't push against size limit of compressedBuffer */
176 size_t const segSize = (CNBuffSize / 2) / segs;
177 for (i = 0; i < segs; i++) {
178 CHECK_V(r,
179 ZSTD_compress(
180 (BYTE *)compressedBuffer + off, CNBuffSize - off,
181 (BYTE *)CNBuffer + segSize * i,
182 segSize, 5));
183 off += r;
184 if (i == segs/2) {
185 /* insert skippable frame */
186 const U32 skipLen = 128 KB;
187 MEM_writeLE32((BYTE*)compressedBuffer + off, ZSTD_MAGIC_SKIPPABLE_START);
188 MEM_writeLE32((BYTE*)compressedBuffer + off + 4, skipLen);
189 off += skipLen + ZSTD_skippableHeaderSize;
190 }
191 }
192 cSize = off;
193 }
194 DISPLAYLEVEL(4, "OK \n");
195
196 DISPLAYLEVEL(4, "test%3i : get decompressed size of multiple frames : ", testNb++);
197 { unsigned long long const r = ZSTD_findDecompressedSize(compressedBuffer, cSize);
198 if (r != CNBuffSize / 2) goto _output_error; }
199 DISPLAYLEVEL(4, "OK \n");
200
201 DISPLAYLEVEL(4, "test%3i : decompress multiple frames : ", testNb++);
202 { CHECK_V(r, ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize));
203 if (r != CNBuffSize / 2) goto _output_error; }
204 DISPLAYLEVEL(4, "OK \n");
205
206 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
207 if (memcmp(decodedBuffer, CNBuffer, CNBuffSize / 2) != 0) goto _output_error;
208 DISPLAYLEVEL(4, "OK \n");
209
Yann Collet389648c2016-04-12 19:13:08 +0200210 /* Dictionary and CCtx Duplication tests */
Yann Colletc0a9bf32016-05-30 01:56:08 +0200211 { ZSTD_CCtx* const ctxOrig = ZSTD_createCCtx();
212 ZSTD_CCtx* const ctxDuplicated = ZSTD_createCCtx();
213 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
214 static const size_t dictSize = 551;
Yann Collet60096272016-01-08 17:27:50 +0100215
Yann Collet887e7da2016-04-11 20:12:27 +0200216 DISPLAYLEVEL(4, "test%3i : copy context too soon : ", testNb++);
Yann Collet97b378a2016-09-21 17:20:19 +0200217 { size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200218 if (!ZSTD_isError(copyResult)) goto _output_error; } /* error must be detected */
Yann Collet887e7da2016-04-11 20:12:27 +0200219 DISPLAYLEVEL(4, "OK \n");
220
Yann Collet60096272016-01-08 17:27:50 +0100221 DISPLAYLEVEL(4, "test%3i : load dictionary into context : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200222 CHECK( ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2) );
Sean Purcell84b37cc2017-02-09 12:27:32 -0800223 CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0) ); /* Begin_usingDict implies unknown srcSize, so match that */
Yann Collet60096272016-01-08 17:27:50 +0100224 DISPLAYLEVEL(4, "OK \n");
225
Yann Collet30009522016-05-30 16:17:33 +0200226 DISPLAYLEVEL(4, "test%3i : compress with flat dictionary : ", testNb++);
Yann Collet60096272016-01-08 17:27:50 +0100227 cSize = 0;
Yann Collet62470b42016-07-28 15:29:08 +0200228 CHECKPLUS(r, ZSTD_compressEnd(ctxOrig, compressedBuffer, ZSTD_compressBound(CNBuffSize),
Yann Colletd2858e92016-05-30 15:10:09 +0200229 (const char*)CNBuffer + dictSize, CNBuffSize - dictSize),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200230 cSize += r);
Yann Colletd2858e92016-05-30 15:10:09 +0200231 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
Yann Collet60096272016-01-08 17:27:50 +0100232
Yann Collet30009522016-05-30 16:17:33 +0200233 DISPLAYLEVEL(4, "test%3i : frame built with flat dictionary should be decompressible : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200234 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
Yann Colletd2858e92016-05-30 15:10:09 +0200235 decodedBuffer, CNBuffSize,
Yann Colletc0a9bf32016-05-30 01:56:08 +0200236 compressedBuffer, cSize,
237 CNBuffer, dictSize),
Yann Colletd2858e92016-05-30 15:10:09 +0200238 if (r != CNBuffSize - dictSize) goto _output_error);
Yann Collet60096272016-01-08 17:27:50 +0100239 DISPLAYLEVEL(4, "OK \n");
240
241 DISPLAYLEVEL(4, "test%3i : compress with duplicated context : ", testNb++);
Yann Collet389648c2016-04-12 19:13:08 +0200242 { size_t const cSizeOrig = cSize;
243 cSize = 0;
Yann Collet62470b42016-07-28 15:29:08 +0200244 CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(CNBuffSize),
Yann Colletd2858e92016-05-30 15:10:09 +0200245 (const char*)CNBuffer + dictSize, CNBuffSize - dictSize),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200246 cSize += r);
Yann Collet227cc392016-07-15 11:27:09 +0200247 if (cSize != cSizeOrig) goto _output_error; /* should be identical ==> same size */
Yann Collet389648c2016-04-12 19:13:08 +0200248 }
Yann Colletd2858e92016-05-30 15:10:09 +0200249 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
Yann Collet60096272016-01-08 17:27:50 +0100250
251 DISPLAYLEVEL(4, "test%3i : frame built with duplicated context should be decompressible : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200252 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
Yann Colletd2858e92016-05-30 15:10:09 +0200253 decodedBuffer, CNBuffSize,
Yann Collet60096272016-01-08 17:27:50 +0100254 compressedBuffer, cSize,
Yann Colletc0a9bf32016-05-30 01:56:08 +0200255 CNBuffer, dictSize),
Yann Colletd2858e92016-05-30 15:10:09 +0200256 if (r != CNBuffSize - dictSize) goto _output_error);
Yann Collet60096272016-01-08 17:27:50 +0100257 DISPLAYLEVEL(4, "OK \n");
Yann Collet541dc7c2016-04-12 18:00:20 +0200258
Yann Collet8dff9562017-02-25 10:11:15 -0800259 DISPLAYLEVEL(4, "test%3i : decompress with DDict : ", testNb++);
Yann Colletbd7fa212017-02-26 14:43:07 -0800260 { ZSTD_DDict* const ddict = ZSTD_createDDict_byReference(CNBuffer, dictSize);
Yann Collet8dff9562017-02-25 10:11:15 -0800261 size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict);
262 if (r != CNBuffSize - dictSize) goto _output_error;
263 DISPLAYLEVEL(4, "OK (size of DDict : %u) \n", (U32)ZSTD_sizeof_DDict(ddict));
264 ZSTD_freeDDict(ddict);
265 }
266
Yann Collet541dc7c2016-04-12 18:00:20 +0200267 DISPLAYLEVEL(4, "test%3i : check content size on duplicated context : ", testNb++);
Yann Colletd2858e92016-05-30 15:10:09 +0200268 { size_t const testSize = CNBuffSize / 3;
Yann Collet6c6e1752016-06-27 15:28:45 +0200269 { ZSTD_parameters p = ZSTD_getParams(2, testSize, dictSize);
270 p.fParams.contentSizeFlag = 1;
Yann Colletd2858e92016-05-30 15:10:09 +0200271 CHECK( ZSTD_compressBegin_advanced(ctxOrig, CNBuffer, dictSize, p, testSize-1) );
Yann Collet33341de2016-05-29 23:09:51 +0200272 }
Yann Collet97b378a2016-09-21 17:20:19 +0200273 CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, testSize) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200274
Yann Collet97b378a2016-09-21 17:20:19 +0200275 CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(testSize),
276 (const char*)CNBuffer + dictSize, testSize),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200277 cSize = r);
Yann Collet541dc7c2016-04-12 18:00:20 +0200278 { ZSTD_frameParams fp;
Yann Collet33341de2016-05-29 23:09:51 +0200279 if (ZSTD_getFrameParams(&fp, compressedBuffer, cSize)) goto _output_error;
Yann Collet541dc7c2016-04-12 18:00:20 +0200280 if ((fp.frameContentSize != testSize) && (fp.frameContentSize != 0)) goto _output_error;
281 } }
282 DISPLAYLEVEL(4, "OK \n");
283
284 ZSTD_freeCCtx(ctxOrig);
285 ZSTD_freeCCtx(ctxDuplicated);
286 ZSTD_freeDCtx(dctx);
Yann Collet60096272016-01-08 17:27:50 +0100287 }
288
Yann Collet30009522016-05-30 16:17:33 +0200289 /* Dictionary and dictBuilder tests */
290 { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
291 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
292 size_t dictSize = 16 KB;
293 void* dictBuffer = malloc(dictSize);
294 size_t const totalSampleSize = 1 MB;
295 size_t const sampleUnitSize = 8 KB;
Yann Colletb81cbba2016-05-30 22:29:45 +0200296 U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
Yann Collet30009522016-05-30 16:17:33 +0200297 size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
Yann Colletf586bdf2016-12-06 06:11:46 +0100298 U32 dictID;
Yann Collet30009522016-05-30 16:17:33 +0200299
300 if (dictBuffer==NULL || samplesSizes==NULL) {
301 free(dictBuffer);
302 free(samplesSizes);
303 goto _output_error;
304 }
305
306 DISPLAYLEVEL(4, "test%3i : dictBuilder : ", testNb++);
307 { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
308 dictSize = ZDICT_trainFromBuffer(dictBuffer, dictSize,
309 CNBuffer, samplesSizes, nbSamples);
310 if (ZDICT_isError(dictSize)) goto _output_error;
311 DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize);
312
Yann Colletda3fbcb2016-08-19 14:23:58 +0200313 DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++);
Yann Colletf586bdf2016-12-06 06:11:46 +0100314 dictID = ZDICT_getDictID(dictBuffer, dictSize);
315 if (dictID==0) goto _output_error;
316 DISPLAYLEVEL(4, "OK : %u \n", dictID);
Yann Colletda3fbcb2016-08-19 14:23:58 +0200317
Yann Collet30009522016-05-30 16:17:33 +0200318 DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++);
319 cSize = ZSTD_compress_usingDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
320 CNBuffer, CNBuffSize,
321 dictBuffer, dictSize, 4);
322 if (ZSTD_isError(cSize)) goto _output_error;
323 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
324
Yann Collete7a41a52016-12-05 16:21:06 -0800325 DISPLAYLEVEL(4, "test%3i : retrieve dictID from dictionary : ", testNb++);
Yann Colletf586bdf2016-12-06 06:11:46 +0100326 { U32 const did = ZSTD_getDictID_fromDict(dictBuffer, dictSize);
327 if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
Yann Collete7a41a52016-12-05 16:21:06 -0800328 }
329 DISPLAYLEVEL(4, "OK \n");
330
331 DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++);
Yann Colletf586bdf2016-12-06 06:11:46 +0100332 { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
333 if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
Yann Collete7a41a52016-12-05 16:21:06 -0800334 }
335 DISPLAYLEVEL(4, "OK \n");
336
Yann Collet30009522016-05-30 16:17:33 +0200337 DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++);
338 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
339 decodedBuffer, CNBuffSize,
340 compressedBuffer, cSize,
341 dictBuffer, dictSize),
342 if (r != CNBuffSize) goto _output_error);
343 DISPLAYLEVEL(4, "OK \n");
344
345 DISPLAYLEVEL(4, "test%3i : compress without dictID : ", testNb++);
Yann Collet6c6e1752016-06-27 15:28:45 +0200346 { ZSTD_parameters p = ZSTD_getParams(3, CNBuffSize, dictSize);
347 p.fParams.noDictIDFlag = 1;
Yann Collet30009522016-05-30 16:17:33 +0200348 cSize = ZSTD_compress_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize),
349 CNBuffer, CNBuffSize,
350 dictBuffer, dictSize, p);
351 if (ZSTD_isError(cSize)) goto _output_error;
352 }
353 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100);
354
355 DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++);
356 CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
357 decodedBuffer, CNBuffSize,
358 compressedBuffer, cSize,
359 dictBuffer, dictSize),
360 if (r != CNBuffSize) goto _output_error);
361 DISPLAYLEVEL(4, "OK \n");
362
Sean Purcell834ab502017-01-11 17:31:06 -0800363 DISPLAYLEVEL(4, "test%3i : dictionary containing only header should return error : ", testNb++);
364 {
365 const size_t ret = ZSTD_decompress_usingDict(
366 dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize,
367 "\x37\xa4\x30\xec\x11\x22\x33\x44", 8);
368 if (ZSTD_getErrorCode(ret) != ZSTD_error_dictionary_corrupted) goto _output_error;
369 }
Sean Purcellc44c4d52017-01-12 09:38:29 -0800370 DISPLAYLEVEL(4, "OK \n");
Sean Purcell834ab502017-01-11 17:31:06 -0800371
Yann Collet30009522016-05-30 16:17:33 +0200372 ZSTD_freeCCtx(cctx);
373 ZSTD_freeDCtx(dctx);
374 free(dictBuffer);
375 free(samplesSizes);
376 }
377
Nick Terrella8b4fe02017-01-02 18:45:19 -0800378 /* COVER dictionary builder tests */
379 { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
380 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
381 size_t dictSize = 16 KB;
382 size_t optDictSize = dictSize;
383 void* dictBuffer = malloc(dictSize);
384 size_t const totalSampleSize = 1 MB;
385 size_t const sampleUnitSize = 8 KB;
386 U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
387 size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
388 COVER_params_t params;
389 U32 dictID;
390
391 if (dictBuffer==NULL || samplesSizes==NULL) {
392 free(dictBuffer);
393 free(samplesSizes);
394 goto _output_error;
395 }
396
397 DISPLAYLEVEL(4, "test%3i : COVER_trainFromBuffer : ", testNb++);
398 { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
399 memset(&params, 0, sizeof(params));
400 params.d = 1 + (FUZ_rand(&seed) % 16);
401 params.k = params.d + (FUZ_rand(&seed) % 256);
402 dictSize = COVER_trainFromBuffer(dictBuffer, dictSize,
403 CNBuffer, samplesSizes, nbSamples,
404 params);
405 if (ZDICT_isError(dictSize)) goto _output_error;
406 DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize);
407
408 DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++);
409 dictID = ZDICT_getDictID(dictBuffer, dictSize);
410 if (dictID==0) goto _output_error;
411 DISPLAYLEVEL(4, "OK : %u \n", dictID);
412
413 DISPLAYLEVEL(4, "test%3i : COVER_optimizeTrainFromBuffer : ", testNb++);
414 memset(&params, 0, sizeof(params));
415 params.steps = 4;
416 optDictSize = COVER_optimizeTrainFromBuffer(dictBuffer, optDictSize,
417 CNBuffer, samplesSizes, nbSamples,
418 &params);
419 if (ZDICT_isError(optDictSize)) goto _output_error;
420 DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)optDictSize);
421
422 DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++);
423 dictID = ZDICT_getDictID(dictBuffer, optDictSize);
424 if (dictID==0) goto _output_error;
425 DISPLAYLEVEL(4, "OK : %u \n", dictID);
426
427 ZSTD_freeCCtx(cctx);
428 ZSTD_freeDCtx(dctx);
429 free(dictBuffer);
430 free(samplesSizes);
431 }
432
Yann Collet4856a002015-01-24 01:58:16 +0100433 /* Decompression defense tests */
434 DISPLAYLEVEL(4, "test%3i : Check input length for magic number : ", testNb++);
Yann Colletd2858e92016-05-30 15:10:09 +0200435 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 3);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200436 if (!ZSTD_isError(r)) goto _output_error;
437 if (r != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100438 DISPLAYLEVEL(4, "OK \n");
439
440 DISPLAYLEVEL(4, "test%3i : Check magic Number : ", testNb++);
441 ((char*)(CNBuffer))[0] = 1;
Yann Colletd2858e92016-05-30 15:10:09 +0200442 { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 4);
Yann Collet33341de2016-05-29 23:09:51 +0200443 if (!ZSTD_isError(r)) goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100444 DISPLAYLEVEL(4, "OK \n");
445
Yann Colletbf42c8e2016-01-09 01:08:23 +0100446 /* block API tests */
Yann Colletd1d210f2016-03-19 12:12:07 +0100447 { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
Yann Colletbf42c8e2016-01-09 01:08:23 +0100448 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
Yann Colletd3d2db52016-07-15 12:20:26 +0200449 static const size_t dictSize = 65 KB;
450 static const size_t blockSize = 100 KB; /* won't cause pb with small dict size */
Yann Colletd4f4e582016-06-27 01:31:35 +0200451 size_t cSize2;
Yann Colletbf42c8e2016-01-09 01:08:23 +0100452
453 /* basic block compression */
454 DISPLAYLEVEL(4, "test%3i : Block compression test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200455 CHECK( ZSTD_compressBegin(cctx, 5) );
Yann Colletbf42c8e2016-01-09 01:08:23 +0100456 cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize);
457 if (ZSTD_isError(cSize)) goto _output_error;
458 DISPLAYLEVEL(4, "OK \n");
459
460 DISPLAYLEVEL(4, "test%3i : Block decompression test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200461 CHECK( ZSTD_decompressBegin(dctx) );
Yann Colletd4f4e582016-06-27 01:31:35 +0200462 { CHECK_V(r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
Yann Colletd2858e92016-05-30 15:10:09 +0200463 if (r != blockSize) goto _output_error; }
Yann Colletbf42c8e2016-01-09 01:08:23 +0100464 DISPLAYLEVEL(4, "OK \n");
465
Yann Colletb0125102016-01-09 02:00:10 +0100466 /* dictionary block compression */
467 DISPLAYLEVEL(4, "test%3i : Dictionary Block compression test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200468 CHECK( ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5) );
Yann Colletb0125102016-01-09 02:00:10 +0100469 cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize);
470 if (ZSTD_isError(cSize)) goto _output_error;
Yann Colletd4f4e582016-06-27 01:31:35 +0200471 cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize+blockSize, blockSize);
472 if (ZSTD_isError(cSize2)) goto _output_error;
Yann Colletaa2628d2016-07-07 15:28:41 +0200473 memcpy((char*)compressedBuffer+cSize, (char*)CNBuffer+dictSize+blockSize, blockSize); /* fake non-compressed block */
474 cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize+blockSize, ZSTD_compressBound(blockSize),
475 (char*)CNBuffer+dictSize+2*blockSize, blockSize);
476 if (ZSTD_isError(cSize2)) goto _output_error;
Yann Colletb0125102016-01-09 02:00:10 +0100477 DISPLAYLEVEL(4, "OK \n");
478
479 DISPLAYLEVEL(4, "test%3i : Dictionary Block decompression test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200480 CHECK( ZSTD_decompressBegin_usingDict(dctx, CNBuffer, dictSize) );
Yann Colletd4f4e582016-06-27 01:31:35 +0200481 { CHECK_V( r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
482 if (r != blockSize) goto _output_error; }
Yann Colletaa2628d2016-07-07 15:28:41 +0200483 ZSTD_insertBlock(dctx, (char*)decodedBuffer+blockSize, blockSize); /* insert non-compressed block into dctx history */
484 { CHECK_V( r, ZSTD_decompressBlock(dctx, (char*)decodedBuffer+2*blockSize, CNBuffSize, (char*)compressedBuffer+cSize+blockSize, cSize2) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200485 if (r != blockSize) goto _output_error; }
Yann Colletb0125102016-01-09 02:00:10 +0100486 DISPLAYLEVEL(4, "OK \n");
487
Yann Colletbf42c8e2016-01-09 01:08:23 +0100488 ZSTD_freeCCtx(cctx);
489 ZSTD_freeDCtx(dctx);
490 }
491
Yann Collet213089c2015-06-18 07:43:16 -0800492 /* long rle test */
Yann Colletd1d210f2016-03-19 12:12:07 +0100493 { size_t sampleSize = 0;
Yann Collet213089c2015-06-18 07:43:16 -0800494 DISPLAYLEVEL(4, "test%3i : Long RLE test : ", testNb++);
Yann Colletc0a9bf32016-05-30 01:56:08 +0200495 RDG_genBuffer(CNBuffer, sampleSize, compressibility, 0., seed+1);
Yann Collet213089c2015-06-18 07:43:16 -0800496 memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1);
497 sampleSize += 256 KB - 1;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200498 RDG_genBuffer((char*)CNBuffer+sampleSize, 96 KB, compressibility, 0., seed+2);
Yann Collet213089c2015-06-18 07:43:16 -0800499 sampleSize += 96 KB;
Yann Collet5be2dd22015-11-11 13:43:58 +0100500 cSize = ZSTD_compress(compressedBuffer, ZSTD_compressBound(sampleSize), CNBuffer, sampleSize, 1);
Yann Collet213089c2015-06-18 07:43:16 -0800501 if (ZSTD_isError(cSize)) goto _output_error;
Yann Colletd4f4e582016-06-27 01:31:35 +0200502 { CHECK_V(regenSize, ZSTD_decompress(decodedBuffer, sampleSize, compressedBuffer, cSize));
Yann Colletc0a9bf32016-05-30 01:56:08 +0200503 if (regenSize!=sampleSize) goto _output_error; }
Yann Collet213089c2015-06-18 07:43:16 -0800504 DISPLAYLEVEL(4, "OK \n");
505 }
506
Yann Colletc0a9bf32016-05-30 01:56:08 +0200507 /* All zeroes test (test bug #137) */
Yann Collet4ba85342016-03-07 20:01:45 +0100508 #define ZEROESLENGTH 100
509 DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH);
510 memset(CNBuffer, 0, ZEROESLENGTH);
Yann Colletd4f4e582016-06-27 01:31:35 +0200511 { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200512 cSize = r; }
Yann Collet4ba85342016-03-07 20:01:45 +0100513 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100);
514
515 DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH);
Yann Colletd4f4e582016-06-27 01:31:35 +0200516 { CHECK_V(r, ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200517 if (r != ZEROESLENGTH) goto _output_error; }
Yann Collet4ba85342016-03-07 20:01:45 +0100518 DISPLAYLEVEL(4, "OK \n");
519
520 /* nbSeq limit test */
Yann Colletd1d210f2016-03-19 12:12:07 +0100521 #define _3BYTESTESTLENGTH 131000
522 #define NB3BYTESSEQLOG 9
523 #define NB3BYTESSEQ (1 << NB3BYTESSEQLOG)
524 #define NB3BYTESSEQMASK (NB3BYTESSEQ-1)
Yann Collet0d9ce042016-03-19 13:21:08 +0100525 /* creates a buffer full of 3-bytes sequences */
Yann Colletd1d210f2016-03-19 12:12:07 +0100526 { BYTE _3BytesSeqs[NB3BYTESSEQ][3];
Yann Collet0d9ce042016-03-19 13:21:08 +0100527 U32 rSeed = 1;
Yann Collet4ba85342016-03-07 20:01:45 +0100528
Yann Collet0d9ce042016-03-19 13:21:08 +0100529 /* create batch of 3-bytes sequences */
Yann Colletf323bf72016-07-07 13:14:21 +0200530 { int i;
531 for (i=0; i < NB3BYTESSEQ; i++) {
532 _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&rSeed) & 255);
533 _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&rSeed) & 255);
534 _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&rSeed) & 255);
535 } }
Yann Collet4ba85342016-03-07 20:01:45 +0100536
Yann Collet0d9ce042016-03-19 13:21:08 +0100537 /* randomly fills CNBuffer with prepared 3-bytes sequences */
Yann Colletf323bf72016-07-07 13:14:21 +0200538 { int i;
539 for (i=0; i < _3BYTESTESTLENGTH; i += 3) { /* note : CNBuffer size > _3BYTESTESTLENGTH+3 */
540 U32 const id = FUZ_rand(&rSeed) & NB3BYTESSEQMASK;
541 ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0];
542 ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1];
543 ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2];
544 } } }
Yann Collet0d9ce042016-03-19 13:21:08 +0100545 DISPLAYLEVEL(4, "test%3i : compress lots 3-bytes sequences : ", testNb++);
Yann Colletd4f4e582016-06-27 01:31:35 +0200546 { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH),
Yann Colletc0a9bf32016-05-30 01:56:08 +0200547 CNBuffer, _3BYTESTESTLENGTH, 19) );
548 cSize = r; }
Yann Collet0d9ce042016-03-19 13:21:08 +0100549 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/_3BYTESTESTLENGTH*100);
Yann Collet4ba85342016-03-07 20:01:45 +0100550
Yann Collet0d9ce042016-03-19 13:21:08 +0100551 DISPLAYLEVEL(4, "test%3i : decompress lots 3-bytes sequence : ", testNb++);
Yann Colletd4f4e582016-06-27 01:31:35 +0200552 { CHECK_V(r, ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize) );
Yann Colletc0a9bf32016-05-30 01:56:08 +0200553 if (r != _3BYTESTESTLENGTH) goto _output_error; }
Yann Collet0d9ce042016-03-19 13:21:08 +0100554 DISPLAYLEVEL(4, "OK \n");
Yann Collet4ba85342016-03-07 20:01:45 +0100555
Sean Purcell9050e192017-02-22 12:12:32 -0800556 /* findFrameCompressedSize on skippable frames */
557 DISPLAYLEVEL(4, "test%3i : frame compressed size of skippable frame : ", testNb++);
558 { const char* frame = "\x50\x2a\x4d\x18\x05\x0\x0\0abcde";
559 size_t const frameSrcSize = 13;
560 if (ZSTD_findFrameCompressedSize(frame, frameSrcSize) != frameSrcSize) goto _output_error; }
561 DISPLAYLEVEL(4, "OK \n");
562
Sean Purcelle0b32652017-02-08 15:31:47 -0800563 /* error string tests */
564 DISPLAYLEVEL(4, "test%3i : testing ZSTD error code strings : ", testNb++);
565 if (strcmp("No error detected", ZSTD_getErrorName((ZSTD_ErrorCode)(0-ZSTD_error_no_error))) != 0) goto _output_error;
566 if (strcmp("No error detected", ZSTD_getErrorString(ZSTD_error_no_error)) != 0) goto _output_error;
567 if (strcmp("Unspecified error code", ZSTD_getErrorString((ZSTD_ErrorCode)(0-ZSTD_error_GENERIC))) != 0) goto _output_error;
568 if (strcmp("Error (generic)", ZSTD_getErrorName((size_t)0-ZSTD_error_GENERIC)) != 0) goto _output_error;
569 if (strcmp("Error (generic)", ZSTD_getErrorString(ZSTD_error_GENERIC)) != 0) goto _output_error;
570 if (strcmp("No error detected", ZSTD_getErrorName(ZSTD_error_GENERIC)) != 0) goto _output_error;
571 DISPLAYLEVEL(4, "OK \n");
572
Yann Collet4856a002015-01-24 01:58:16 +0100573_end:
574 free(CNBuffer);
575 free(compressedBuffer);
576 free(decodedBuffer);
577 return testResult;
578
579_output_error:
580 testResult = 1;
581 DISPLAY("Error detected in Unit tests ! \n");
582 goto _end;
583}
584
585
586static size_t findDiff(const void* buf1, const void* buf2, size_t max)
587{
Yann Collet213089c2015-06-18 07:43:16 -0800588 const BYTE* b1 = (const BYTE*)buf1;
589 const BYTE* b2 = (const BYTE*)buf2;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200590 size_t u;
591 for (u=0; u<max; u++) {
592 if (b1[u] != b2[u]) break;
Yann Collet4856a002015-01-24 01:58:16 +0100593 }
Yann Colletc0a9bf32016-05-30 01:56:08 +0200594 return u;
Yann Collet4856a002015-01-24 01:58:16 +0100595}
596
Yann Collet1fce6e02016-04-08 20:26:33 +0200597
598static size_t FUZ_rLogLength(U32* seed, U32 logLength)
599{
600 size_t const lengthMask = ((size_t)1 << logLength) - 1;
601 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
602}
603
604static size_t FUZ_randomLength(U32* seed, U32 maxLog)
605{
606 U32 const logLength = FUZ_rand(seed) % maxLog;
607 return FUZ_rLogLength(seed, logLength);
608}
609
Yann Colletc0a9bf32016-05-30 01:56:08 +0200610#undef CHECK
Yann Collet0d9ce042016-03-19 13:21:08 +0100611#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
612 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
Yann Collet4856a002015-01-24 01:58:16 +0100613
Yann Colletea63bb72016-04-08 15:25:32 +0200614static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxDurationS, double compressibility)
Yann Collet4856a002015-01-24 01:58:16 +0100615{
Yann Collet1fce6e02016-04-08 20:26:33 +0200616 static const U32 maxSrcLog = 23;
617 static const U32 maxSampleLog = 22;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200618 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
619 size_t const dstBufferSize = (size_t)1<<maxSampleLog;
620 size_t const cBufferSize = ZSTD_compressBound(dstBufferSize);
Yann Colletd5d9bc32015-08-23 23:13:49 +0100621 BYTE* cNoiseBuffer[5];
Yann Colletc0a9bf32016-05-30 01:56:08 +0200622 BYTE* srcBuffer; /* jumping pointer */
623 BYTE* const cBuffer = (BYTE*) malloc (cBufferSize);
624 BYTE* const dstBuffer = (BYTE*) malloc (dstBufferSize);
625 BYTE* const mirrorBuffer = (BYTE*) malloc (dstBufferSize);
Yann Colletd2858e92016-05-30 15:10:09 +0200626 ZSTD_CCtx* const refCtx = ZSTD_createCCtx();
627 ZSTD_CCtx* const ctx = ZSTD_createCCtx();
628 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
Yann Collet30009522016-05-30 16:17:33 +0200629 U32 result = 0;
630 U32 testNb = 0;
631 U32 coreSeed = seed, lseed = 0;
Yann Colletd2858e92016-05-30 15:10:09 +0200632 clock_t const startClock = clock();
Yann Colletea63bb72016-04-08 15:25:32 +0200633 clock_t const maxClockSpan = maxDurationS * CLOCKS_PER_SEC;
Yann Collet4856a002015-01-24 01:58:16 +0100634
635 /* allocation */
Yann Colletd5d9bc32015-08-23 23:13:49 +0100636 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
637 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
638 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
639 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
640 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Collete47c4e52015-12-05 09:23:53 +0100641 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4]
Yann Colletecd651b2016-01-07 15:35:18 +0100642 || !dstBuffer || !mirrorBuffer || !cBuffer || !refCtx || !ctx || !dctx,
Yann Colletd5d9bc32015-08-23 23:13:49 +0100643 "Not enough memory, fuzzer tests cancelled");
Yann Collet4856a002015-01-24 01:58:16 +0100644
Yann Colletd5d9bc32015-08-23 23:13:49 +0100645 /* Create initial samples */
646 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
647 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
648 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
649 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
650 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
651 srcBuffer = cNoiseBuffer[2];
Yann Collet4856a002015-01-24 01:58:16 +0100652
653 /* catch up testNb */
Yann Collet546c9b12016-03-19 12:47:52 +0100654 for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed);
Yann Collet4856a002015-01-24 01:58:16 +0100655
Yann Collet546c9b12016-03-19 12:47:52 +0100656 /* main test loop */
Yann Colletea63bb72016-04-08 15:25:32 +0200657 for ( ; (testNb <= nbTests) || (FUZ_clockSpan(startClock) < maxClockSpan); testNb++ ) {
Yann Colletc0a9bf32016-05-30 01:56:08 +0200658 size_t sampleSize, maxTestSize, totalTestSize;
659 size_t cSize, totalCSize, totalGenSize;
Yann Collet6c903a82016-05-28 13:34:07 +0200660 XXH64_state_t xxhState;
Yann Collet1fce6e02016-04-08 20:26:33 +0200661 U64 crcOrig;
Yann Collet110cc142015-11-19 12:02:28 +0100662 BYTE* sampleBuffer;
Yann Collet4bfe4152015-12-06 13:18:37 +0100663 const BYTE* dict;
664 size_t dictSize;
Yann Collet4856a002015-01-24 01:58:16 +0100665
Yann Collet546c9b12016-03-19 12:47:52 +0100666 /* notification */
667 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
Yann Collet4c0b44f2016-11-01 11:13:22 -0700668 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Collet1c2ddba2015-12-04 17:45:35 +0100669
Yann Collet4856a002015-01-24 01:58:16 +0100670 FUZ_rand(&coreSeed);
Yann Collet0d9ce042016-03-19 13:21:08 +0100671 { U32 const prime1 = 2654435761U; lseed = coreSeed ^ prime1; }
Yann Collet1fce6e02016-04-08 20:26:33 +0200672
673 /* srcBuffer selection [0-4] */
674 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
675 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
676 else {
677 buffNb >>= 3;
678 if (buffNb & 7) {
679 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
680 buffNb = tnb[buffNb >> 3];
681 } else {
682 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
683 buffNb = tnb[buffNb >> 3];
684 } }
685 srcBuffer = cNoiseBuffer[buffNb];
686 }
687
688 /* select src segment */
Yann Colletd2858e92016-05-30 15:10:09 +0200689 sampleSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet4856a002015-01-24 01:58:16 +0100690
Yann Collet110cc142015-11-19 12:02:28 +0100691 /* create sample buffer (to catch read error with valgrind & sanitizers) */
692 sampleBuffer = (BYTE*)malloc(sampleSize);
Yann Colletd2858e92016-05-30 15:10:09 +0200693 CHECK(sampleBuffer==NULL, "not enough memory for sample buffer");
Yann Colletc0a9bf32016-05-30 01:56:08 +0200694 { size_t const sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
695 memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); }
Yann Collet110cc142015-11-19 12:02:28 +0100696 crcOrig = XXH64(sampleBuffer, sampleSize, 0);
697
Yann Collet1fce6e02016-04-08 20:26:33 +0200698 /* compression tests */
Yann Colletd2858e92016-05-30 15:10:09 +0200699 { unsigned const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (FUZ_highbit32((U32)sampleSize)/3))) + 1;
Yann Collet1fce6e02016-04-08 20:26:33 +0200700 cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel);
Yann Colleta7737f62016-09-06 09:44:59 +0200701 CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed : %s", ZSTD_getErrorName(cSize));
Yann Collet4856a002015-01-24 01:58:16 +0100702
Yann Collet1fce6e02016-04-08 20:26:33 +0200703 /* compression failure test : too small dest buffer */
704 if (cSize > 3) {
705 const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
706 const size_t tooSmallSize = cSize - missing;
707 const U32 endMark = 0x4DC2B1A9;
708 memcpy(dstBuffer+tooSmallSize, &endMark, 4);
709 { size_t const errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel);
710 CHECK(!ZSTD_isError(errorCode), "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (U32)tooSmallSize, (U32)cSize); }
711 { U32 endCheck; memcpy(&endCheck, dstBuffer+tooSmallSize, 4);
712 CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow"); }
Yann Colletd2858e92016-05-30 15:10:09 +0200713 } }
Yann Collet1fce6e02016-04-08 20:26:33 +0200714
Yann Colletf323bf72016-07-07 13:14:21 +0200715 /* Decompressed size test */
Sean Purcell4e709712017-02-07 13:50:09 -0800716 { unsigned long long const rSize = ZSTD_findDecompressedSize(cBuffer, cSize);
Yann Colletf323bf72016-07-07 13:14:21 +0200717 CHECK(rSize != sampleSize, "decompressed size incorrect");
718 }
719
Yann Collet546c9b12016-03-19 12:47:52 +0100720 /* frame header decompression test */
Yann Collet346bffb2016-03-15 15:24:52 +0100721 { ZSTD_frameParams dParams;
722 size_t const check = ZSTD_getFrameParams(&dParams, cBuffer, cSize);
723 CHECK(ZSTD_isError(check), "Frame Parameters extraction failed");
724 CHECK(dParams.frameContentSize != sampleSize, "Frame content size incorrect");
725 }
726
Yann Collet546c9b12016-03-19 12:47:52 +0100727 /* successful decompression test */
Yann Collet1fce6e02016-04-08 20:26:33 +0200728 { size_t const margin = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1;
Yann Colletc0a9bf32016-05-30 01:56:08 +0200729 size_t const dSize = ZSTD_decompress(dstBuffer, sampleSize + margin, cBuffer, cSize);
Yann Collet546c9b12016-03-19 12:47:52 +0100730 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 +0200731 { U64 const crcDest = XXH64(dstBuffer, sampleSize, 0);
732 CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (U32)findDiff(sampleBuffer, dstBuffer, sampleSize), (U32)sampleSize);
733 } }
Yann Collet110cc142015-11-19 12:02:28 +0100734
735 free(sampleBuffer); /* no longer useful after this point */
Yann Colletf3cb79b2015-08-20 00:02:43 +0100736
737 /* truncated src decompression test */
Yann Collet1fce6e02016-04-08 20:26:33 +0200738 { size_t const missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
739 size_t const tooSmallSize = cSize - missing;
Yann Colletd2858e92016-05-30 15:10:09 +0200740 void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch read overflows */
Yann Colletf3cb79b2015-08-20 00:02:43 +0100741 CHECK(cBufferTooSmall == NULL, "not enough memory !");
Yann Colletd5d9bc32015-08-23 23:13:49 +0100742 memcpy(cBufferTooSmall, cBuffer, tooSmallSize);
Yann Collet1fce6e02016-04-08 20:26:33 +0200743 { size_t const errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize);
744 CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); }
Yann Colletf3cb79b2015-08-20 00:02:43 +0100745 free(cBufferTooSmall);
746 }
747
748 /* too small dst decompression test */
Yann Colletd1d210f2016-03-19 12:12:07 +0100749 if (sampleSize > 3) {
Yann Colletea63bb72016-04-08 15:25:32 +0200750 size_t const missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
751 size_t const tooSmallSize = sampleSize - missing;
Yann Colletf3cb79b2015-08-20 00:02:43 +0100752 static const BYTE token = 0xA9;
753 dstBuffer[tooSmallSize] = token;
Yann Collet1fce6e02016-04-08 20:26:33 +0200754 { size_t const errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize);
755 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 +0100756 CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow");
757 }
Yann Collet997f9ee2015-08-21 02:44:20 +0100758
759 /* noisy src decompression test */
Yann Colletd1d210f2016-03-19 12:12:07 +0100760 if (cSize > 6) {
761 /* insert noise into src */
762 { U32 const maxNbBits = FUZ_highbit32((U32)(cSize-4));
763 size_t pos = 4; /* preserve magic number (too easy to detect) */
764 for (;;) {
765 /* keep some original src */
766 { U32 const nbBits = FUZ_rand(&lseed) % maxNbBits;
767 size_t const mask = (1<<nbBits) - 1;
768 size_t const skipLength = FUZ_rand(&lseed) & mask;
769 pos += skipLength;
770 }
771 if (pos <= cSize) break;
772 /* add noise */
Yann Collet33341de2016-05-29 23:09:51 +0200773 { U32 const nbBitsCodes = FUZ_rand(&lseed) % maxNbBits;
774 U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0;
775 size_t const mask = (1<<nbBits) - 1;
776 size_t const rNoiseLength = (FUZ_rand(&lseed) & mask) + 1;
777 size_t const noiseLength = MIN(rNoiseLength, cSize-pos);
778 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength);
Yann Colletd1d210f2016-03-19 12:12:07 +0100779 memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength);
780 pos += noiseLength;
781 } } }
Yann Collet997f9ee2015-08-21 02:44:20 +0100782
783 /* decompress noisy source */
Yann Colletd1d210f2016-03-19 12:12:07 +0100784 { U32 const endMark = 0xA9B1C3D6;
Yann Collet997f9ee2015-08-21 02:44:20 +0100785 memcpy(dstBuffer+sampleSize, &endMark, 4);
Yann Collet1fce6e02016-04-08 20:26:33 +0200786 { size_t const decompressResult = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize);
787 /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */
788 CHECK((!ZSTD_isError(decompressResult)) && (decompressResult>sampleSize),
789 "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (U32)decompressResult, (U32)sampleSize);
790 }
791 { U32 endCheck; memcpy(&endCheck, dstBuffer+sampleSize, 4);
792 CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow");
793 } } } /* noisy src decompression test */
Yann Collet417890c2015-12-04 17:16:37 +0100794
Yann Collet1fce6e02016-04-08 20:26:33 +0200795 /*===== Streaming compression test, scattered segments and dictionary =====*/
796
797 { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
798 int const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
799 maxTestSize = FUZ_rLogLength(&lseed, testLog);
800 if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1;
801
Yann Colletc0a9bf32016-05-30 01:56:08 +0200802 dictSize = FUZ_randomLength(&lseed, maxSampleLog); /* needed also for decompression */
803 dict = srcBuffer + (FUZ_rand(&lseed) % (srcBufferSize - dictSize));
Yann Collet1fce6e02016-04-08 20:26:33 +0200804
Yann Colletf2a3b6e2016-05-31 18:13:56 +0200805 if (FUZ_rand(&lseed) & 0xF) {
Yann Collet33341de2016-05-29 23:09:51 +0200806 size_t const errorCode = ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, cLevel);
807 CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode));
808 } else {
cyanb8806312016-05-30 18:20:46 +0200809 ZSTD_compressionParameters const cPar = ZSTD_getCParams(cLevel, 0, dictSize);
Yann Colletf2a3b6e2016-05-31 18:13:56 +0200810 ZSTD_frameParameters const fpar = { FUZ_rand(&lseed)&1 /* contentSizeFlag */,
811 !(FUZ_rand(&lseed)&3) /* contentChecksumFlag*/,
812 0 /*NodictID*/ }; /* note : since dictionary is fake, dictIDflag has no impact */
cyanb8806312016-05-30 18:20:46 +0200813 ZSTD_parameters p;
814 size_t errorCode;
815 p.cParams = cPar; p.fParams = fpar;
816 errorCode = ZSTD_compressBegin_advanced(refCtx, dict, dictSize, p, 0);
Yann Collet33341de2016-05-29 23:09:51 +0200817 CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_advanced error : %s", ZSTD_getErrorName(errorCode));
818 }
Yann Collet97b378a2016-09-21 17:20:19 +0200819 { size_t const errorCode = ZSTD_copyCCtx(ctx, refCtx, 0);
820 CHECK (ZSTD_isError(errorCode), "ZSTD_copyCCtx error : %s", ZSTD_getErrorName(errorCode));
821 } }
Yann Collet6c903a82016-05-28 13:34:07 +0200822 XXH64_reset(&xxhState, 0);
Yann Collet06e76972017-01-25 16:39:03 -0800823 ZSTD_setCCtxParameter(ctx, ZSTD_p_forceWindow, FUZ_rand(&lseed) & 1);
Yann Colletd2858e92016-05-30 15:10:09 +0200824 { U32 const nbChunks = (FUZ_rand(&lseed) & 127) + 2;
825 U32 n;
826 for (totalTestSize=0, cSize=0, n=0 ; n<nbChunks ; n++) {
827 size_t const segmentSize = FUZ_randomLength(&lseed, maxSampleLog);
828 size_t const segmentStart = FUZ_rand(&lseed) % (srcBufferSize - segmentSize);
Yann Collet417890c2015-12-04 17:16:37 +0100829
Yann Colletd2858e92016-05-30 15:10:09 +0200830 if (cBufferSize-cSize < ZSTD_compressBound(segmentSize)) break; /* avoid invalid dstBufferTooSmall */
831 if (totalTestSize+segmentSize > maxTestSize) break;
Yann Collet417890c2015-12-04 17:16:37 +0100832
Yann Colletd2858e92016-05-30 15:10:09 +0200833 { size_t const compressResult = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+segmentStart, segmentSize);
834 CHECK (ZSTD_isError(compressResult), "multi-segments compression error : %s", ZSTD_getErrorName(compressResult));
835 cSize += compressResult;
836 }
837 XXH64_update(&xxhState, srcBuffer+segmentStart, segmentSize);
838 memcpy(mirrorBuffer + totalTestSize, srcBuffer+segmentStart, segmentSize);
839 totalTestSize += segmentSize;
840 } }
841
Yann Collet62470b42016-07-28 15:29:08 +0200842 { size_t const flushResult = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize, NULL, 0);
Yann Collet1fce6e02016-04-08 20:26:33 +0200843 CHECK (ZSTD_isError(flushResult), "multi-segments epilogue error : %s", ZSTD_getErrorName(flushResult));
844 cSize += flushResult;
845 }
Yann Collet6c903a82016-05-28 13:34:07 +0200846 crcOrig = XXH64_digest(&xxhState);
Yann Collete47c4e52015-12-05 09:23:53 +0100847
848 /* streaming decompression test */
Yann Collet33341de2016-05-29 23:09:51 +0200849 if (dictSize<8) dictSize=0, dict=NULL; /* disable dictionary */
Yann Collet1fce6e02016-04-08 20:26:33 +0200850 { size_t const errorCode = ZSTD_decompressBegin_usingDict(dctx, dict, dictSize);
Yann Collet30009522016-05-30 16:17:33 +0200851 CHECK (ZSTD_isError(errorCode), "ZSTD_decompressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode)); }
Yann Collete47c4e52015-12-05 09:23:53 +0100852 totalCSize = 0;
853 totalGenSize = 0;
Yann Colletd1d210f2016-03-19 12:12:07 +0100854 while (totalCSize < cSize) {
Yann Collet1fce6e02016-04-08 20:26:33 +0200855 size_t const inSize = ZSTD_nextSrcSizeToDecompress(dctx);
856 size_t const genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize);
Yann Collet45dc3562016-07-12 09:47:31 +0200857 CHECK (ZSTD_isError(genSize), "ZSTD_decompressContinue error : %s", ZSTD_getErrorName(genSize));
Yann Collete47c4e52015-12-05 09:23:53 +0100858 totalGenSize += genSize;
859 totalCSize += inSize;
860 }
861 CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded");
Yann Colletc0a9bf32016-05-30 01:56:08 +0200862 CHECK (totalGenSize != totalTestSize, "streaming decompressed data : wrong size")
Yann Collete47c4e52015-12-05 09:23:53 +0100863 CHECK (totalCSize != cSize, "compressed data should be fully read")
Yann Collet1fce6e02016-04-08 20:26:33 +0200864 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
865 if (crcDest!=crcOrig) {
866 size_t const errorPos = findDiff(mirrorBuffer, dstBuffer, totalTestSize);
Yann Colletd2858e92016-05-30 15:10:09 +0200867 CHECK (1, "streaming decompressed data corrupted : byte %u / %u (%02X!=%02X)",
Yann Collet1fce6e02016-04-08 20:26:33 +0200868 (U32)errorPos, (U32)totalTestSize, dstBuffer[errorPos], mirrorBuffer[errorPos]);
869 } }
870 } /* for ( ; (testNb <= nbTests) */
Yann Collet1c2ddba2015-12-04 17:45:35 +0100871 DISPLAY("\r%u fuzzer tests completed \n", testNb-1);
Yann Collet4856a002015-01-24 01:58:16 +0100872
873_cleanup:
Yann Colletecd651b2016-01-07 15:35:18 +0100874 ZSTD_freeCCtx(refCtx);
Yann Collet2f648e52015-10-29 18:23:38 +0100875 ZSTD_freeCCtx(ctx);
Yann Collete47c4e52015-12-05 09:23:53 +0100876 ZSTD_freeDCtx(dctx);
Yann Colletd5d9bc32015-08-23 23:13:49 +0100877 free(cNoiseBuffer[0]);
878 free(cNoiseBuffer[1]);
879 free(cNoiseBuffer[2]);
880 free(cNoiseBuffer[3]);
881 free(cNoiseBuffer[4]);
Yann Collet4856a002015-01-24 01:58:16 +0100882 free(cBuffer);
883 free(dstBuffer);
Yann Collete47c4e52015-12-05 09:23:53 +0100884 free(mirrorBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100885 return result;
886
887_output_error:
888 result = 1;
889 goto _cleanup;
890}
891
892
Yann Colletd1d210f2016-03-19 12:12:07 +0100893/*_*******************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100894* Command line
895*********************************************************/
Yann Colletd1d210f2016-03-19 12:12:07 +0100896int FUZ_usage(const char* programName)
Yann Collet4856a002015-01-24 01:58:16 +0100897{
898 DISPLAY( "Usage :\n");
899 DISPLAY( " %s [args]\n", programName);
900 DISPLAY( "\n");
901 DISPLAY( "Arguments :\n");
902 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
903 DISPLAY( " -s# : Select seed (default:prompt user)\n");
904 DISPLAY( " -t# : Select starting test number (default:0)\n");
Yann Collet0d9ce042016-03-19 13:21:08 +0100905 DISPLAY( " -P# : Select compressibility in %% (default:%u%%)\n", FUZ_compressibility_default);
Yann Collet4856a002015-01-24 01:58:16 +0100906 DISPLAY( " -v : verbose\n");
Yann Collete9853b22015-08-07 19:07:32 +0100907 DISPLAY( " -p : pause at the end\n");
Yann Collet4856a002015-01-24 01:58:16 +0100908 DISPLAY( " -h : display help and exit\n");
909 return 0;
910}
911
912
Yann Colletd1d210f2016-03-19 12:12:07 +0100913int main(int argc, const char** argv)
Yann Collet4856a002015-01-24 01:58:16 +0100914{
915 U32 seed=0;
916 int seedset=0;
917 int argNb;
918 int nbTests = nbTestsDefault;
919 int testNb = 0;
Yann Collet0d9ce042016-03-19 13:21:08 +0100920 U32 proba = FUZ_compressibility_default;
Yann Collet4856a002015-01-24 01:58:16 +0100921 int result=0;
922 U32 mainPause = 0;
Yann Collet0d9ce042016-03-19 13:21:08 +0100923 U32 maxDuration = 0;
Yann Colletea63bb72016-04-08 15:25:32 +0200924 const char* programName = argv[0];
Yann Collet4856a002015-01-24 01:58:16 +0100925
926 /* Check command line */
Yann Colletd1d210f2016-03-19 12:12:07 +0100927 for (argNb=1; argNb<argc; argNb++) {
928 const char* argument = argv[argNb];
Yann Collet4856a002015-01-24 01:58:16 +0100929 if(!argument) continue; /* Protection if argument empty */
930
931 /* Handle commands. Aggregated commands are allowed */
Yann Colletd1d210f2016-03-19 12:12:07 +0100932 if (argument[0]=='-') {
Yann Collet4856a002015-01-24 01:58:16 +0100933 argument++;
Yann Colletd1d210f2016-03-19 12:12:07 +0100934 while (*argument!=0) {
Yann Collet4856a002015-01-24 01:58:16 +0100935 switch(*argument)
936 {
937 case 'h':
938 return FUZ_usage(programName);
939 case 'v':
940 argument++;
941 g_displayLevel=4;
942 break;
943 case 'q':
944 argument++;
945 g_displayLevel--;
946 break;
947 case 'p': /* pause at the end */
948 argument++;
949 mainPause = 1;
950 break;
951
952 case 'i':
Yann Collet0d9ce042016-03-19 13:21:08 +0100953 argument++; maxDuration=0;
Yann Collet4856a002015-01-24 01:58:16 +0100954 nbTests=0;
Yann Colletd1d210f2016-03-19 12:12:07 +0100955 while ((*argument>='0') && (*argument<='9')) {
Yann Collet4856a002015-01-24 01:58:16 +0100956 nbTests *= 10;
957 nbTests += *argument - '0';
958 argument++;
959 }
960 break;
961
Yann Collet553cf6a2015-12-04 17:25:26 +0100962 case 'T':
963 argument++;
Yann Collet0d9ce042016-03-19 13:21:08 +0100964 nbTests=0; maxDuration=0;
Yann Colletd1d210f2016-03-19 12:12:07 +0100965 while ((*argument>='0') && (*argument<='9')) {
Yann Collet0d9ce042016-03-19 13:21:08 +0100966 maxDuration *= 10;
967 maxDuration += *argument - '0';
Yann Collet553cf6a2015-12-04 17:25:26 +0100968 argument++;
969 }
Yann Collet0d9ce042016-03-19 13:21:08 +0100970 if (*argument=='m') maxDuration *=60, argument++;
Yann Collet553cf6a2015-12-04 17:25:26 +0100971 if (*argument=='n') argument++;
Yann Collet553cf6a2015-12-04 17:25:26 +0100972 break;
973
Yann Collet4856a002015-01-24 01:58:16 +0100974 case 's':
975 argument++;
976 seed=0;
977 seedset=1;
Yann Colletd1d210f2016-03-19 12:12:07 +0100978 while ((*argument>='0') && (*argument<='9')) {
Yann Collet4856a002015-01-24 01:58:16 +0100979 seed *= 10;
980 seed += *argument - '0';
981 argument++;
982 }
983 break;
984
985 case 't':
986 argument++;
987 testNb=0;
Yann Colletd1d210f2016-03-19 12:12:07 +0100988 while ((*argument>='0') && (*argument<='9')) {
Yann Collet4856a002015-01-24 01:58:16 +0100989 testNb *= 10;
990 testNb += *argument - '0';
991 argument++;
992 }
993 break;
994
995 case 'P': /* compressibility % */
996 argument++;
997 proba=0;
Yann Colletd1d210f2016-03-19 12:12:07 +0100998 while ((*argument>='0') && (*argument<='9')) {
Yann Collet4856a002015-01-24 01:58:16 +0100999 proba *= 10;
1000 proba += *argument - '0';
1001 argument++;
1002 }
Yann Collet4856a002015-01-24 01:58:16 +01001003 if (proba>100) proba=100;
1004 break;
1005
1006 default:
1007 return FUZ_usage(programName);
Yann Colletd1d210f2016-03-19 12:12:07 +01001008 } } } } /* for (argNb=1; argNb<argc; argNb++) */
Yann Collet4856a002015-01-24 01:58:16 +01001009
1010 /* Get Seed */
Yann Collet45f84ab2016-05-20 12:34:40 +02001011 DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Collet4856a002015-01-24 01:58:16 +01001012
Yann Collet3f01c882016-06-16 13:38:10 +02001013 if (!seedset) {
1014 time_t const t = time(NULL);
1015 U32 const h = XXH32(&t, sizeof(t), 1);
1016 seed = h % 10000;
1017 }
1018
Yann Collet4856a002015-01-24 01:58:16 +01001019 DISPLAY("Seed = %u\n", seed);
Yann Collet0d9ce042016-03-19 13:21:08 +01001020 if (proba!=FUZ_compressibility_default) DISPLAY("Compressibility : %u%%\n", proba);
Yann Collet4856a002015-01-24 01:58:16 +01001021
Yann Collet803c05e2016-06-16 11:32:57 +02001022 if (nbTests < testNb) nbTests = testNb;
1023
Yann Colletd1d210f2016-03-19 12:12:07 +01001024 if (testNb==0)
1025 result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */
Yann Collet4856a002015-01-24 01:58:16 +01001026 if (!result)
Yann Collet0d9ce042016-03-19 13:21:08 +01001027 result = fuzzerTests(seed, nbTests, testNb, maxDuration, ((double)proba) / 100);
Yann Colletd1d210f2016-03-19 12:12:07 +01001028 if (mainPause) {
Yann Colletb5e06dc2015-07-04 23:20:56 -08001029 int unused;
Yann Collet4856a002015-01-24 01:58:16 +01001030 DISPLAY("Press Enter \n");
Yann Colletb5e06dc2015-07-04 23:20:56 -08001031 unused = getchar();
1032 (void)unused;
Yann Collet4856a002015-01-24 01:58:16 +01001033 }
1034 return result;
1035}