1*01826a49SYabin Cui /*
2*01826a49SYabin Cui * Copyright (c) Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui * All rights reserved.
4*01826a49SYabin Cui *
5*01826a49SYabin Cui * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui */
10*01826a49SYabin Cui
11*01826a49SYabin Cui
12*01826a49SYabin Cui /*-************************************
13*01826a49SYabin Cui * Compiler specific
14*01826a49SYabin Cui **************************************/
15*01826a49SYabin Cui #ifdef _MSC_VER /* Visual Studio */
16*01826a49SYabin Cui # define _CRT_SECURE_NO_WARNINGS /* fgets */
17*01826a49SYabin Cui # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
18*01826a49SYabin Cui # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
19*01826a49SYabin Cui #endif
20*01826a49SYabin Cui
21*01826a49SYabin Cui
22*01826a49SYabin Cui /*-************************************
23*01826a49SYabin Cui * Includes
24*01826a49SYabin Cui **************************************/
25*01826a49SYabin Cui #include <stdlib.h> /* free */
26*01826a49SYabin Cui #include <stdio.h> /* fgets, sscanf */
27*01826a49SYabin Cui #include <string.h> /* strcmp */
28*01826a49SYabin Cui #include <time.h> /* time(), time_t */
29*01826a49SYabin Cui #undef NDEBUG /* always enable assert() */
30*01826a49SYabin Cui #include <assert.h>
31*01826a49SYabin Cui #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressContinue, ZSTD_compressBlock */
32*01826a49SYabin Cui #include "debug.h" /* DEBUG_STATIC_ASSERT */
33*01826a49SYabin Cui #include "fse.h"
34*01826a49SYabin Cui #define ZSTD_DISABLE_DEPRECATE_WARNINGS /* No deprecation warnings, we still test some deprecated functions */
35*01826a49SYabin Cui #include "zstd.h" /* ZSTD_VERSION_STRING */
36*01826a49SYabin Cui #include "zstd_errors.h" /* ZSTD_getErrorCode */
37*01826a49SYabin Cui #define ZDICT_STATIC_LINKING_ONLY
38*01826a49SYabin Cui #include "zdict.h" /* ZDICT_trainFromBuffer */
39*01826a49SYabin Cui #include "mem.h"
40*01826a49SYabin Cui #include "datagen.h" /* RDG_genBuffer */
41*01826a49SYabin Cui #define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
42*01826a49SYabin Cui #include "xxhash.h" /* XXH64 */
43*01826a49SYabin Cui #include "util.h"
44*01826a49SYabin Cui #include "timefn.h" /* SEC_TO_MICRO, UTIL_time_t, UTIL_TIME_INITIALIZER, UTIL_clockSpanMicro, UTIL_getTime */
45*01826a49SYabin Cui /* must be included after util.h, due to ERROR macro redefinition issue on Visual Studio */
46*01826a49SYabin Cui #include "zstd_internal.h" /* ZSTD_WORKSPACETOOLARGE_MAXDURATION, ZSTD_WORKSPACETOOLARGE_FACTOR, KB, MB */
47*01826a49SYabin Cui #include "threading.h" /* ZSTD_pthread_create, ZSTD_pthread_join */
48*01826a49SYabin Cui
49*01826a49SYabin Cui
50*01826a49SYabin Cui /*-************************************
51*01826a49SYabin Cui * Constants
52*01826a49SYabin Cui **************************************/
53*01826a49SYabin Cui #define GB *(1U<<30)
54*01826a49SYabin Cui
55*01826a49SYabin Cui static const int FUZ_compressibility_default = 50;
56*01826a49SYabin Cui static const int nbTestsDefault = 30000;
57*01826a49SYabin Cui
58*01826a49SYabin Cui
59*01826a49SYabin Cui /*-************************************
60*01826a49SYabin Cui * Display Macros
61*01826a49SYabin Cui **************************************/
62*01826a49SYabin Cui #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
63*01826a49SYabin Cui #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
64*01826a49SYabin Cui static U32 g_displayLevel = 2;
65*01826a49SYabin Cui
66*01826a49SYabin Cui static const U64 g_refreshRate = SEC_TO_MICRO / 6;
67*01826a49SYabin Cui static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
68*01826a49SYabin Cui
69*01826a49SYabin Cui #define DISPLAYUPDATE(l, ...) \
70*01826a49SYabin Cui if (g_displayLevel>=l) { \
71*01826a49SYabin Cui if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
72*01826a49SYabin Cui { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
73*01826a49SYabin Cui if (g_displayLevel>=4) fflush(stderr); } \
74*01826a49SYabin Cui }
75*01826a49SYabin Cui
76*01826a49SYabin Cui
77*01826a49SYabin Cui /*-*******************************************************
78*01826a49SYabin Cui * Compile time test
79*01826a49SYabin Cui *********************************************************/
80*01826a49SYabin Cui #undef MIN
81*01826a49SYabin Cui #undef MAX
82*01826a49SYabin Cui /* Declaring the function, to avoid -Wmissing-prototype */
83*01826a49SYabin Cui void FUZ_bug976(void);
FUZ_bug976(void)84*01826a49SYabin Cui void FUZ_bug976(void)
85*01826a49SYabin Cui { /* these constants shall not depend on MIN() macro */
86*01826a49SYabin Cui DEBUG_STATIC_ASSERT(ZSTD_HASHLOG_MAX < 31);
87*01826a49SYabin Cui DEBUG_STATIC_ASSERT(ZSTD_CHAINLOG_MAX < 31);
88*01826a49SYabin Cui }
89*01826a49SYabin Cui
90*01826a49SYabin Cui
91*01826a49SYabin Cui /*-*******************************************************
92*01826a49SYabin Cui * Internal functions
93*01826a49SYabin Cui *********************************************************/
94*01826a49SYabin Cui #define MIN(a,b) ((a)<(b)?(a):(b))
95*01826a49SYabin Cui #define MAX(a,b) ((a)>(b)?(a):(b))
96*01826a49SYabin Cui
97*01826a49SYabin Cui #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
FUZ_rand(U32 * src)98*01826a49SYabin Cui static U32 FUZ_rand(U32* src)
99*01826a49SYabin Cui {
100*01826a49SYabin Cui static const U32 prime1 = 2654435761U;
101*01826a49SYabin Cui static const U32 prime2 = 2246822519U;
102*01826a49SYabin Cui U32 rand32 = *src;
103*01826a49SYabin Cui rand32 *= prime1;
104*01826a49SYabin Cui rand32 += prime2;
105*01826a49SYabin Cui rand32 = FUZ_rotl32(rand32, 13);
106*01826a49SYabin Cui *src = rand32;
107*01826a49SYabin Cui return rand32 >> 5;
108*01826a49SYabin Cui }
109*01826a49SYabin Cui
FUZ_highbit32(U32 v32)110*01826a49SYabin Cui static U32 FUZ_highbit32(U32 v32)
111*01826a49SYabin Cui {
112*01826a49SYabin Cui unsigned nbBits = 0;
113*01826a49SYabin Cui if (v32==0) return 0;
114*01826a49SYabin Cui while (v32) v32 >>= 1, nbBits++;
115*01826a49SYabin Cui return nbBits;
116*01826a49SYabin Cui }
117*01826a49SYabin Cui
118*01826a49SYabin Cui
119*01826a49SYabin Cui /*=============================================
120*01826a49SYabin Cui * Test macros
121*01826a49SYabin Cui =============================================*/
122*01826a49SYabin Cui #define CHECK(fn) { if(!(fn)) { DISPLAYLEVEL(1, "Error : test (%s) failed \n", #fn); exit(1); } }
123*01826a49SYabin Cui
124*01826a49SYabin Cui #define CHECK_Z(f) { \
125*01826a49SYabin Cui size_t const err = f; \
126*01826a49SYabin Cui if (ZSTD_isError(err)) { \
127*01826a49SYabin Cui DISPLAY("Error => %s : %s ", \
128*01826a49SYabin Cui #f, ZSTD_getErrorName(err)); \
129*01826a49SYabin Cui exit(1); \
130*01826a49SYabin Cui } }
131*01826a49SYabin Cui
132*01826a49SYabin Cui #define CHECK_VAR(var, fn) var = fn; if (ZSTD_isError(var)) { DISPLAYLEVEL(1, "%s : fails : %s \n", #fn, ZSTD_getErrorName(var)); exit(1); }
133*01826a49SYabin Cui #define CHECK_NEWV(var, fn) size_t const CHECK_VAR(var, fn)
134*01826a49SYabin Cui #define CHECKPLUS(var, fn, more) { CHECK_NEWV(var, fn); more; }
135*01826a49SYabin Cui
136*01826a49SYabin Cui #define CHECK_OP(op, lhs, rhs) { \
137*01826a49SYabin Cui if (!((lhs) op (rhs))) { \
138*01826a49SYabin Cui DISPLAY("Error L%u => FAILED %s %s %s ", __LINE__, #lhs, #op, #rhs); \
139*01826a49SYabin Cui exit(1); \
140*01826a49SYabin Cui } \
141*01826a49SYabin Cui }
142*01826a49SYabin Cui #define CHECK_EQ(lhs, rhs) CHECK_OP(==, lhs, rhs)
143*01826a49SYabin Cui #define CHECK_LT(lhs, rhs) CHECK_OP(<, lhs, rhs)
144*01826a49SYabin Cui
145*01826a49SYabin Cui
146*01826a49SYabin Cui /*=============================================
147*01826a49SYabin Cui * Memory Tests
148*01826a49SYabin Cui =============================================*/
149*01826a49SYabin Cui #if defined(__APPLE__) && defined(__MACH__)
150*01826a49SYabin Cui
151*01826a49SYabin Cui #include <malloc/malloc.h> /* malloc_size */
152*01826a49SYabin Cui
153*01826a49SYabin Cui typedef struct {
154*01826a49SYabin Cui unsigned long long totalMalloc;
155*01826a49SYabin Cui size_t currentMalloc;
156*01826a49SYabin Cui size_t peakMalloc;
157*01826a49SYabin Cui unsigned nbMalloc;
158*01826a49SYabin Cui unsigned nbFree;
159*01826a49SYabin Cui } mallocCounter_t;
160*01826a49SYabin Cui
161*01826a49SYabin Cui static const mallocCounter_t INIT_MALLOC_COUNTER = { 0, 0, 0, 0, 0 };
162*01826a49SYabin Cui
FUZ_mallocDebug(void * counter,size_t size)163*01826a49SYabin Cui static void* FUZ_mallocDebug(void* counter, size_t size)
164*01826a49SYabin Cui {
165*01826a49SYabin Cui mallocCounter_t* const mcPtr = (mallocCounter_t*)counter;
166*01826a49SYabin Cui void* const ptr = malloc(size);
167*01826a49SYabin Cui if (ptr==NULL) return NULL;
168*01826a49SYabin Cui DISPLAYLEVEL(4, "allocating %u KB => effectively %u KB \n",
169*01826a49SYabin Cui (unsigned)(size >> 10), (unsigned)(malloc_size(ptr) >> 10)); /* OS-X specific */
170*01826a49SYabin Cui mcPtr->totalMalloc += size;
171*01826a49SYabin Cui mcPtr->currentMalloc += size;
172*01826a49SYabin Cui if (mcPtr->currentMalloc > mcPtr->peakMalloc)
173*01826a49SYabin Cui mcPtr->peakMalloc = mcPtr->currentMalloc;
174*01826a49SYabin Cui mcPtr->nbMalloc += 1;
175*01826a49SYabin Cui return ptr;
176*01826a49SYabin Cui }
177*01826a49SYabin Cui
FUZ_freeDebug(void * counter,void * address)178*01826a49SYabin Cui static void FUZ_freeDebug(void* counter, void* address)
179*01826a49SYabin Cui {
180*01826a49SYabin Cui mallocCounter_t* const mcPtr = (mallocCounter_t*)counter;
181*01826a49SYabin Cui DISPLAYLEVEL(4, "freeing %u KB \n", (unsigned)(malloc_size(address) >> 10));
182*01826a49SYabin Cui mcPtr->nbFree += 1;
183*01826a49SYabin Cui mcPtr->currentMalloc -= malloc_size(address); /* OS-X specific */
184*01826a49SYabin Cui free(address);
185*01826a49SYabin Cui }
186*01826a49SYabin Cui
FUZ_displayMallocStats(mallocCounter_t count)187*01826a49SYabin Cui static void FUZ_displayMallocStats(mallocCounter_t count)
188*01826a49SYabin Cui {
189*01826a49SYabin Cui DISPLAYLEVEL(3, "peak:%6u KB, nbMallocs:%2u, total:%6u KB \n",
190*01826a49SYabin Cui (unsigned)(count.peakMalloc >> 10),
191*01826a49SYabin Cui count.nbMalloc,
192*01826a49SYabin Cui (unsigned)(count.totalMalloc >> 10));
193*01826a49SYabin Cui }
194*01826a49SYabin Cui
FUZ_mallocTests_internal(unsigned seed,double compressibility,unsigned part,void * inBuffer,size_t inSize,void * outBuffer,size_t outSize)195*01826a49SYabin Cui static int FUZ_mallocTests_internal(unsigned seed, double compressibility, unsigned part,
196*01826a49SYabin Cui void* inBuffer, size_t inSize, void* outBuffer, size_t outSize)
197*01826a49SYabin Cui {
198*01826a49SYabin Cui /* test only played in verbose mode, as they are long */
199*01826a49SYabin Cui if (g_displayLevel<3) return 0;
200*01826a49SYabin Cui
201*01826a49SYabin Cui /* Create compressible noise */
202*01826a49SYabin Cui if (!inBuffer || !outBuffer) {
203*01826a49SYabin Cui DISPLAY("Not enough memory, aborting\n");
204*01826a49SYabin Cui exit(1);
205*01826a49SYabin Cui }
206*01826a49SYabin Cui RDG_genBuffer(inBuffer, inSize, compressibility, 0. /*auto*/, seed);
207*01826a49SYabin Cui
208*01826a49SYabin Cui /* simple compression tests */
209*01826a49SYabin Cui if (part <= 1)
210*01826a49SYabin Cui { int compressionLevel;
211*01826a49SYabin Cui for (compressionLevel=1; compressionLevel<=6; compressionLevel++) {
212*01826a49SYabin Cui mallocCounter_t malcount = INIT_MALLOC_COUNTER;
213*01826a49SYabin Cui ZSTD_customMem const cMem = { FUZ_mallocDebug, FUZ_freeDebug, &malcount };
214*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(cMem);
215*01826a49SYabin Cui CHECK_Z( ZSTD_compressCCtx(cctx, outBuffer, outSize, inBuffer, inSize, compressionLevel) );
216*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
217*01826a49SYabin Cui DISPLAYLEVEL(3, "compressCCtx level %i : ", compressionLevel);
218*01826a49SYabin Cui FUZ_displayMallocStats(malcount);
219*01826a49SYabin Cui } }
220*01826a49SYabin Cui
221*01826a49SYabin Cui /* streaming compression tests */
222*01826a49SYabin Cui if (part <= 2)
223*01826a49SYabin Cui { int compressionLevel;
224*01826a49SYabin Cui for (compressionLevel=1; compressionLevel<=6; compressionLevel++) {
225*01826a49SYabin Cui mallocCounter_t malcount = INIT_MALLOC_COUNTER;
226*01826a49SYabin Cui ZSTD_customMem const cMem = { FUZ_mallocDebug, FUZ_freeDebug, &malcount };
227*01826a49SYabin Cui ZSTD_CCtx* const cstream = ZSTD_createCStream_advanced(cMem);
228*01826a49SYabin Cui ZSTD_outBuffer out = { outBuffer, outSize, 0 };
229*01826a49SYabin Cui ZSTD_inBuffer in = { inBuffer, inSize, 0 };
230*01826a49SYabin Cui CHECK_Z( ZSTD_initCStream(cstream, compressionLevel) );
231*01826a49SYabin Cui CHECK_Z( ZSTD_compressStream(cstream, &out, &in) );
232*01826a49SYabin Cui CHECK_Z( ZSTD_endStream(cstream, &out) );
233*01826a49SYabin Cui ZSTD_freeCStream(cstream);
234*01826a49SYabin Cui DISPLAYLEVEL(3, "compressStream level %i : ", compressionLevel);
235*01826a49SYabin Cui FUZ_displayMallocStats(malcount);
236*01826a49SYabin Cui } }
237*01826a49SYabin Cui
238*01826a49SYabin Cui /* advanced MT API test */
239*01826a49SYabin Cui if (part <= 3)
240*01826a49SYabin Cui { int nbThreads;
241*01826a49SYabin Cui for (nbThreads=1; nbThreads<=4; nbThreads++) {
242*01826a49SYabin Cui int compressionLevel;
243*01826a49SYabin Cui for (compressionLevel=1; compressionLevel<=6; compressionLevel++) {
244*01826a49SYabin Cui mallocCounter_t malcount = INIT_MALLOC_COUNTER;
245*01826a49SYabin Cui ZSTD_customMem const cMem = { FUZ_mallocDebug, FUZ_freeDebug, &malcount };
246*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(cMem);
247*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionLevel) );
248*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbThreads) );
249*01826a49SYabin Cui CHECK_Z( ZSTD_compress2(cctx, outBuffer, outSize, inBuffer, inSize) );
250*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
251*01826a49SYabin Cui DISPLAYLEVEL(3, "compress_generic,-T%i,end level %i : ",
252*01826a49SYabin Cui nbThreads, compressionLevel);
253*01826a49SYabin Cui FUZ_displayMallocStats(malcount);
254*01826a49SYabin Cui } } }
255*01826a49SYabin Cui
256*01826a49SYabin Cui /* advanced MT streaming API test */
257*01826a49SYabin Cui if (part <= 4)
258*01826a49SYabin Cui { int nbThreads;
259*01826a49SYabin Cui for (nbThreads=1; nbThreads<=4; nbThreads++) {
260*01826a49SYabin Cui int compressionLevel;
261*01826a49SYabin Cui for (compressionLevel=1; compressionLevel<=6; compressionLevel++) {
262*01826a49SYabin Cui mallocCounter_t malcount = INIT_MALLOC_COUNTER;
263*01826a49SYabin Cui ZSTD_customMem const cMem = { FUZ_mallocDebug, FUZ_freeDebug, &malcount };
264*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(cMem);
265*01826a49SYabin Cui ZSTD_outBuffer out = { outBuffer, outSize, 0 };
266*01826a49SYabin Cui ZSTD_inBuffer in = { inBuffer, inSize, 0 };
267*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionLevel) );
268*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbThreads) );
269*01826a49SYabin Cui CHECK_Z( ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue) );
270*01826a49SYabin Cui while ( ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end) ) {}
271*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
272*01826a49SYabin Cui DISPLAYLEVEL(3, "compress_generic,-T%i,continue level %i : ",
273*01826a49SYabin Cui nbThreads, compressionLevel);
274*01826a49SYabin Cui FUZ_displayMallocStats(malcount);
275*01826a49SYabin Cui } } }
276*01826a49SYabin Cui
277*01826a49SYabin Cui return 0;
278*01826a49SYabin Cui }
279*01826a49SYabin Cui
FUZ_mallocTests(unsigned seed,double compressibility,unsigned part)280*01826a49SYabin Cui static int FUZ_mallocTests(unsigned seed, double compressibility, unsigned part)
281*01826a49SYabin Cui {
282*01826a49SYabin Cui size_t const inSize = 64 MB + 16 MB + 4 MB + 1 MB + 256 KB + 64 KB; /* 85.3 MB */
283*01826a49SYabin Cui size_t const outSize = ZSTD_compressBound(inSize);
284*01826a49SYabin Cui void* const inBuffer = malloc(inSize);
285*01826a49SYabin Cui void* const outBuffer = malloc(outSize);
286*01826a49SYabin Cui int result;
287*01826a49SYabin Cui
288*01826a49SYabin Cui /* Create compressible noise */
289*01826a49SYabin Cui if (!inBuffer || !outBuffer) {
290*01826a49SYabin Cui DISPLAY("Not enough memory, aborting \n");
291*01826a49SYabin Cui exit(1);
292*01826a49SYabin Cui }
293*01826a49SYabin Cui
294*01826a49SYabin Cui result = FUZ_mallocTests_internal(seed, compressibility, part,
295*01826a49SYabin Cui inBuffer, inSize, outBuffer, outSize);
296*01826a49SYabin Cui
297*01826a49SYabin Cui free(inBuffer);
298*01826a49SYabin Cui free(outBuffer);
299*01826a49SYabin Cui return result;
300*01826a49SYabin Cui }
301*01826a49SYabin Cui
302*01826a49SYabin Cui #else
303*01826a49SYabin Cui
FUZ_mallocTests(unsigned seed,double compressibility,unsigned part)304*01826a49SYabin Cui static int FUZ_mallocTests(unsigned seed, double compressibility, unsigned part)
305*01826a49SYabin Cui {
306*01826a49SYabin Cui (void)seed; (void)compressibility; (void)part;
307*01826a49SYabin Cui return 0;
308*01826a49SYabin Cui }
309*01826a49SYabin Cui
310*01826a49SYabin Cui #endif
311*01826a49SYabin Cui
FUZ_decodeSequences(BYTE * dst,ZSTD_Sequence * seqs,size_t seqsSize,BYTE * src,size_t size,ZSTD_sequenceFormat_e format)312*01826a49SYabin Cui static void FUZ_decodeSequences(BYTE* dst, ZSTD_Sequence* seqs, size_t seqsSize,
313*01826a49SYabin Cui BYTE* src, size_t size, ZSTD_sequenceFormat_e format)
314*01826a49SYabin Cui {
315*01826a49SYabin Cui size_t i;
316*01826a49SYabin Cui size_t j;
317*01826a49SYabin Cui for(i = 0; i < seqsSize; ++i) {
318*01826a49SYabin Cui assert(dst + seqs[i].litLength + seqs[i].matchLength <= dst + size);
319*01826a49SYabin Cui assert(src + seqs[i].litLength + seqs[i].matchLength <= src + size);
320*01826a49SYabin Cui if (format == ZSTD_sf_noBlockDelimiters) {
321*01826a49SYabin Cui assert(seqs[i].matchLength != 0 || seqs[i].offset != 0);
322*01826a49SYabin Cui }
323*01826a49SYabin Cui
324*01826a49SYabin Cui memcpy(dst, src, seqs[i].litLength);
325*01826a49SYabin Cui dst += seqs[i].litLength;
326*01826a49SYabin Cui src += seqs[i].litLength;
327*01826a49SYabin Cui size -= seqs[i].litLength;
328*01826a49SYabin Cui
329*01826a49SYabin Cui if (seqs[i].offset != 0) {
330*01826a49SYabin Cui for (j = 0; j < seqs[i].matchLength; ++j)
331*01826a49SYabin Cui dst[j] = dst[(ptrdiff_t)(j - seqs[i].offset)];
332*01826a49SYabin Cui dst += seqs[i].matchLength;
333*01826a49SYabin Cui src += seqs[i].matchLength;
334*01826a49SYabin Cui size -= seqs[i].matchLength;
335*01826a49SYabin Cui }
336*01826a49SYabin Cui }
337*01826a49SYabin Cui if (format == ZSTD_sf_noBlockDelimiters) {
338*01826a49SYabin Cui memcpy(dst, src, size);
339*01826a49SYabin Cui }
340*01826a49SYabin Cui }
341*01826a49SYabin Cui
342*01826a49SYabin Cui #ifdef ZSTD_MULTITHREAD
343*01826a49SYabin Cui
344*01826a49SYabin Cui typedef struct {
345*01826a49SYabin Cui ZSTD_CCtx* cctx;
346*01826a49SYabin Cui ZSTD_threadPool* pool;
347*01826a49SYabin Cui void* CNBuffer;
348*01826a49SYabin Cui size_t CNBuffSize;
349*01826a49SYabin Cui void* compressedBuffer;
350*01826a49SYabin Cui size_t compressedBufferSize;
351*01826a49SYabin Cui void* decodedBuffer;
352*01826a49SYabin Cui int err;
353*01826a49SYabin Cui } threadPoolTests_compressionJob_payload;
354*01826a49SYabin Cui
threadPoolTests_compressionJob(void * payload)355*01826a49SYabin Cui static void* threadPoolTests_compressionJob(void* payload) {
356*01826a49SYabin Cui threadPoolTests_compressionJob_payload* args = (threadPoolTests_compressionJob_payload*)payload;
357*01826a49SYabin Cui size_t cSize;
358*01826a49SYabin Cui if (ZSTD_isError(ZSTD_CCtx_refThreadPool(args->cctx, args->pool))) args->err = 1;
359*01826a49SYabin Cui cSize = ZSTD_compress2(args->cctx, args->compressedBuffer, args->compressedBufferSize, args->CNBuffer, args->CNBuffSize);
360*01826a49SYabin Cui if (ZSTD_isError(cSize)) args->err = 1;
361*01826a49SYabin Cui if (ZSTD_isError(ZSTD_decompress(args->decodedBuffer, args->CNBuffSize, args->compressedBuffer, cSize))) args->err = 1;
362*01826a49SYabin Cui return payload;
363*01826a49SYabin Cui }
364*01826a49SYabin Cui
threadPoolTests(void)365*01826a49SYabin Cui static int threadPoolTests(void) {
366*01826a49SYabin Cui int testResult = 0;
367*01826a49SYabin Cui size_t err;
368*01826a49SYabin Cui
369*01826a49SYabin Cui size_t const CNBuffSize = 5 MB;
370*01826a49SYabin Cui void* const CNBuffer = malloc(CNBuffSize);
371*01826a49SYabin Cui size_t const compressedBufferSize = ZSTD_compressBound(CNBuffSize);
372*01826a49SYabin Cui void* const compressedBuffer = malloc(compressedBufferSize);
373*01826a49SYabin Cui void* const decodedBuffer = malloc(CNBuffSize);
374*01826a49SYabin Cui
375*01826a49SYabin Cui size_t const kPoolNumThreads = 8;
376*01826a49SYabin Cui
377*01826a49SYabin Cui RDG_genBuffer(CNBuffer, CNBuffSize, 0.5, 0.5, 0);
378*01826a49SYabin Cui
379*01826a49SYabin Cui DISPLAYLEVEL(3, "thread pool test : threadPool reuse roundtrips: ");
380*01826a49SYabin Cui {
381*01826a49SYabin Cui ZSTD_CCtx* cctx = ZSTD_createCCtx();
382*01826a49SYabin Cui ZSTD_threadPool* pool = ZSTD_createThreadPool(kPoolNumThreads);
383*01826a49SYabin Cui
384*01826a49SYabin Cui size_t nbThreads = 1;
385*01826a49SYabin Cui for (; nbThreads <= kPoolNumThreads; ++nbThreads) {
386*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
387*01826a49SYabin Cui ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, (int)nbThreads);
388*01826a49SYabin Cui err = ZSTD_CCtx_refThreadPool(cctx, pool);
389*01826a49SYabin Cui if (ZSTD_isError(err)) {
390*01826a49SYabin Cui DISPLAYLEVEL(3, "refThreadPool error!\n");
391*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
392*01826a49SYabin Cui goto _output_error;
393*01826a49SYabin Cui }
394*01826a49SYabin Cui err = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
395*01826a49SYabin Cui if (ZSTD_isError(err)) {
396*01826a49SYabin Cui DISPLAYLEVEL(3, "Compression error!\n");
397*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
398*01826a49SYabin Cui goto _output_error;
399*01826a49SYabin Cui }
400*01826a49SYabin Cui err = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, err);
401*01826a49SYabin Cui if (ZSTD_isError(err)) {
402*01826a49SYabin Cui DISPLAYLEVEL(3, "Decompression error!\n");
403*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
404*01826a49SYabin Cui goto _output_error;
405*01826a49SYabin Cui }
406*01826a49SYabin Cui }
407*01826a49SYabin Cui
408*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
409*01826a49SYabin Cui ZSTD_freeThreadPool(pool);
410*01826a49SYabin Cui }
411*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
412*01826a49SYabin Cui
413*01826a49SYabin Cui DISPLAYLEVEL(3, "thread pool test : threadPool simultaneous usage: ");
414*01826a49SYabin Cui {
415*01826a49SYabin Cui void* const decodedBuffer2 = malloc(CNBuffSize);
416*01826a49SYabin Cui void* const compressedBuffer2 = malloc(compressedBufferSize);
417*01826a49SYabin Cui ZSTD_threadPool* pool = ZSTD_createThreadPool(kPoolNumThreads);
418*01826a49SYabin Cui ZSTD_CCtx* cctx1 = ZSTD_createCCtx();
419*01826a49SYabin Cui ZSTD_CCtx* cctx2 = ZSTD_createCCtx();
420*01826a49SYabin Cui
421*01826a49SYabin Cui ZSTD_pthread_t t1;
422*01826a49SYabin Cui ZSTD_pthread_t t2;
423*01826a49SYabin Cui threadPoolTests_compressionJob_payload p1 = {cctx1, pool, CNBuffer, CNBuffSize,
424*01826a49SYabin Cui compressedBuffer, compressedBufferSize, decodedBuffer, 0 /* err */};
425*01826a49SYabin Cui threadPoolTests_compressionJob_payload p2 = {cctx2, pool, CNBuffer, CNBuffSize,
426*01826a49SYabin Cui compressedBuffer2, compressedBufferSize, decodedBuffer2, 0 /* err */};
427*01826a49SYabin Cui
428*01826a49SYabin Cui ZSTD_CCtx_setParameter(cctx1, ZSTD_c_nbWorkers, 2);
429*01826a49SYabin Cui ZSTD_CCtx_setParameter(cctx2, ZSTD_c_nbWorkers, 2);
430*01826a49SYabin Cui ZSTD_CCtx_refThreadPool(cctx1, pool);
431*01826a49SYabin Cui ZSTD_CCtx_refThreadPool(cctx2, pool);
432*01826a49SYabin Cui
433*01826a49SYabin Cui ZSTD_pthread_create(&t1, NULL, threadPoolTests_compressionJob, &p1);
434*01826a49SYabin Cui ZSTD_pthread_create(&t2, NULL, threadPoolTests_compressionJob, &p2);
435*01826a49SYabin Cui ZSTD_pthread_join(t1);
436*01826a49SYabin Cui ZSTD_pthread_join(t2);
437*01826a49SYabin Cui
438*01826a49SYabin Cui assert(!memcmp(decodedBuffer, decodedBuffer2, CNBuffSize));
439*01826a49SYabin Cui free(decodedBuffer2);
440*01826a49SYabin Cui free(compressedBuffer2);
441*01826a49SYabin Cui
442*01826a49SYabin Cui ZSTD_freeThreadPool(pool);
443*01826a49SYabin Cui ZSTD_freeCCtx(cctx1);
444*01826a49SYabin Cui ZSTD_freeCCtx(cctx2);
445*01826a49SYabin Cui
446*01826a49SYabin Cui if (p1.err || p2.err) goto _output_error;
447*01826a49SYabin Cui }
448*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
449*01826a49SYabin Cui
450*01826a49SYabin Cui _end:
451*01826a49SYabin Cui free(CNBuffer);
452*01826a49SYabin Cui free(compressedBuffer);
453*01826a49SYabin Cui free(decodedBuffer);
454*01826a49SYabin Cui return testResult;
455*01826a49SYabin Cui
456*01826a49SYabin Cui _output_error:
457*01826a49SYabin Cui testResult = 1;
458*01826a49SYabin Cui DISPLAY("Error detected in Unit tests ! \n");
459*01826a49SYabin Cui goto _end;
460*01826a49SYabin Cui }
461*01826a49SYabin Cui #endif /* ZSTD_MULTITHREAD */
462*01826a49SYabin Cui
463*01826a49SYabin Cui /*=============================================
464*01826a49SYabin Cui * Unit tests
465*01826a49SYabin Cui =============================================*/
466*01826a49SYabin Cui
test_compressBound(unsigned tnb)467*01826a49SYabin Cui static void test_compressBound(unsigned tnb)
468*01826a49SYabin Cui {
469*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : compressBound : ", tnb);
470*01826a49SYabin Cui
471*01826a49SYabin Cui /* check ZSTD_compressBound == ZSTD_COMPRESSBOUND
472*01826a49SYabin Cui * for a large range of known valid values */
473*01826a49SYabin Cui DEBUG_STATIC_ASSERT(sizeof(size_t) >= 4);
474*01826a49SYabin Cui { int s;
475*01826a49SYabin Cui for (s=0; s<30; s++) {
476*01826a49SYabin Cui size_t const w = (size_t)1 << s;
477*01826a49SYabin Cui CHECK_EQ(ZSTD_compressBound(w), ZSTD_COMPRESSBOUND(w));
478*01826a49SYabin Cui } }
479*01826a49SYabin Cui
480*01826a49SYabin Cui /* Ensure error if srcSize too big */
481*01826a49SYabin Cui { size_t const w = ZSTD_MAX_INPUT_SIZE + 1;
482*01826a49SYabin Cui CHECK(ZSTD_isError(ZSTD_compressBound(w))); /* must fail */
483*01826a49SYabin Cui CHECK_EQ(ZSTD_COMPRESSBOUND(w), 0);
484*01826a49SYabin Cui }
485*01826a49SYabin Cui
486*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
487*01826a49SYabin Cui }
488*01826a49SYabin Cui
test_decompressBound(unsigned tnb)489*01826a49SYabin Cui static void test_decompressBound(unsigned tnb)
490*01826a49SYabin Cui {
491*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : decompressBound : ", tnb);
492*01826a49SYabin Cui
493*01826a49SYabin Cui /* Simple compression, with size : should provide size; */
494*01826a49SYabin Cui { const char example[] = "abcd";
495*01826a49SYabin Cui char cBuffer[ZSTD_COMPRESSBOUND(sizeof(example))];
496*01826a49SYabin Cui size_t const cSize = ZSTD_compress(cBuffer, sizeof(cBuffer), example, sizeof(example), 0);
497*01826a49SYabin Cui CHECK_Z(cSize);
498*01826a49SYabin Cui CHECK_EQ(ZSTD_decompressBound(cBuffer, cSize), (unsigned long long)sizeof(example));
499*01826a49SYabin Cui }
500*01826a49SYabin Cui
501*01826a49SYabin Cui /* Simple small compression without size : should provide 1 block size */
502*01826a49SYabin Cui { char cBuffer[ZSTD_COMPRESSBOUND(0)];
503*01826a49SYabin Cui ZSTD_outBuffer out = { cBuffer, sizeof(cBuffer), 0 };
504*01826a49SYabin Cui ZSTD_inBuffer in = { NULL, 0, 0 };
505*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
506*01826a49SYabin Cui assert(cctx);
507*01826a49SYabin Cui CHECK_Z( ZSTD_initCStream(cctx, 0) );
508*01826a49SYabin Cui CHECK_Z( ZSTD_compressStream(cctx, &out, &in) );
509*01826a49SYabin Cui CHECK_EQ( ZSTD_endStream(cctx, &out), 0 );
510*01826a49SYabin Cui CHECK_EQ( ZSTD_decompressBound(cBuffer, out.pos), ZSTD_BLOCKSIZE_MAX );
511*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
512*01826a49SYabin Cui }
513*01826a49SYabin Cui
514*01826a49SYabin Cui /* Attempt to overflow 32-bit intermediate multiplication result
515*01826a49SYabin Cui * This requires dBound >= 4 GB, aka 2^32.
516*01826a49SYabin Cui * This requires 2^32 / 2^17 = 2^15 blocks
517*01826a49SYabin Cui * => create 2^15 blocks (can be empty, or just 1 byte). */
518*01826a49SYabin Cui { const char input[] = "a";
519*01826a49SYabin Cui size_t const nbBlocks = (1 << 15) + 1;
520*01826a49SYabin Cui size_t blockNb;
521*01826a49SYabin Cui size_t const outCapacity = 1 << 18; /* large margin */
522*01826a49SYabin Cui char* const outBuffer = malloc (outCapacity);
523*01826a49SYabin Cui ZSTD_outBuffer out = { outBuffer, outCapacity, 0 };
524*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
525*01826a49SYabin Cui assert(cctx);
526*01826a49SYabin Cui assert(outBuffer);
527*01826a49SYabin Cui CHECK_Z( ZSTD_initCStream(cctx, 0) );
528*01826a49SYabin Cui for (blockNb=0; blockNb<nbBlocks; blockNb++) {
529*01826a49SYabin Cui ZSTD_inBuffer in = { input, sizeof(input), 0 };
530*01826a49SYabin Cui CHECK_Z( ZSTD_compressStream(cctx, &out, &in) );
531*01826a49SYabin Cui CHECK_EQ( ZSTD_flushStream(cctx, &out), 0 );
532*01826a49SYabin Cui }
533*01826a49SYabin Cui CHECK_EQ( ZSTD_endStream(cctx, &out), 0 );
534*01826a49SYabin Cui CHECK( ZSTD_decompressBound(outBuffer, out.pos) > 0x100000000ULL /* 4 GB */ );
535*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
536*01826a49SYabin Cui free(outBuffer);
537*01826a49SYabin Cui }
538*01826a49SYabin Cui
539*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
540*01826a49SYabin Cui }
541*01826a49SYabin Cui
test_setCParams(unsigned tnb)542*01826a49SYabin Cui static void test_setCParams(unsigned tnb)
543*01826a49SYabin Cui {
544*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
545*01826a49SYabin Cui ZSTD_compressionParameters cparams;
546*01826a49SYabin Cui assert(cctx);
547*01826a49SYabin Cui
548*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : ZSTD_CCtx_setCParams : ", tnb);
549*01826a49SYabin Cui
550*01826a49SYabin Cui /* valid cparams */
551*01826a49SYabin Cui cparams = ZSTD_getCParams(1, 0, 0);
552*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setCParams(cctx, cparams));
553*01826a49SYabin Cui
554*01826a49SYabin Cui /* invalid cparams (must fail) */
555*01826a49SYabin Cui cparams.windowLog = 99;
556*01826a49SYabin Cui CHECK(ZSTD_isError(ZSTD_CCtx_setCParams(cctx, cparams)));
557*01826a49SYabin Cui
558*01826a49SYabin Cui free(cctx);
559*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
560*01826a49SYabin Cui }
561*01826a49SYabin Cui
basicUnitTests(U32 const seed,double compressibility)562*01826a49SYabin Cui static int basicUnitTests(U32 const seed, double compressibility)
563*01826a49SYabin Cui {
564*01826a49SYabin Cui size_t const CNBuffSize = 5 MB;
565*01826a49SYabin Cui void* const CNBuffer = malloc(CNBuffSize);
566*01826a49SYabin Cui size_t const compressedBufferSize = ZSTD_compressBound(CNBuffSize);
567*01826a49SYabin Cui void* const compressedBuffer = malloc(compressedBufferSize);
568*01826a49SYabin Cui void* const decodedBuffer = malloc(CNBuffSize);
569*01826a49SYabin Cui int testResult = 0;
570*01826a49SYabin Cui unsigned testNb=0;
571*01826a49SYabin Cui size_t cSize;
572*01826a49SYabin Cui
573*01826a49SYabin Cui /* Create compressible noise */
574*01826a49SYabin Cui if (!CNBuffer || !compressedBuffer || !decodedBuffer) {
575*01826a49SYabin Cui DISPLAY("Not enough memory, aborting\n");
576*01826a49SYabin Cui testResult = 1;
577*01826a49SYabin Cui goto _end;
578*01826a49SYabin Cui }
579*01826a49SYabin Cui RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed);
580*01826a49SYabin Cui
581*01826a49SYabin Cui /* Basic tests */
582*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : ZSTD_getErrorName : ", testNb++);
583*01826a49SYabin Cui { const char* errorString = ZSTD_getErrorName(0);
584*01826a49SYabin Cui DISPLAYLEVEL(3, "OK : %s \n", errorString);
585*01826a49SYabin Cui }
586*01826a49SYabin Cui
587*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : ZSTD_getErrorName with wrong value : ", testNb++);
588*01826a49SYabin Cui { const char* errorString = ZSTD_getErrorName(499);
589*01826a49SYabin Cui DISPLAYLEVEL(3, "OK : %s \n", errorString);
590*01826a49SYabin Cui }
591*01826a49SYabin Cui
592*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : min compression level : ", testNb++);
593*01826a49SYabin Cui { int const mcl = ZSTD_minCLevel();
594*01826a49SYabin Cui DISPLAYLEVEL(3, "%i (OK) \n", mcl);
595*01826a49SYabin Cui }
596*01826a49SYabin Cui
597*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : default compression level : ", testNb++);
598*01826a49SYabin Cui { int const defaultCLevel = ZSTD_defaultCLevel();
599*01826a49SYabin Cui if (defaultCLevel != ZSTD_CLEVEL_DEFAULT) goto _output_error;
600*01826a49SYabin Cui DISPLAYLEVEL(3, "%i (OK) \n", defaultCLevel);
601*01826a49SYabin Cui }
602*01826a49SYabin Cui
603*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : ZSTD_versionNumber : ", testNb++);
604*01826a49SYabin Cui { unsigned const vn = ZSTD_versionNumber();
605*01826a49SYabin Cui DISPLAYLEVEL(3, "%u (OK) \n", vn);
606*01826a49SYabin Cui }
607*01826a49SYabin Cui
608*01826a49SYabin Cui test_compressBound(testNb++);
609*01826a49SYabin Cui
610*01826a49SYabin Cui test_decompressBound(testNb++);
611*01826a49SYabin Cui
612*01826a49SYabin Cui test_setCParams(testNb++);
613*01826a49SYabin Cui
614*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : ZSTD_adjustCParams : ", testNb++);
615*01826a49SYabin Cui {
616*01826a49SYabin Cui ZSTD_compressionParameters params;
617*01826a49SYabin Cui memset(¶ms, 0, sizeof(params));
618*01826a49SYabin Cui params.windowLog = 10;
619*01826a49SYabin Cui params.hashLog = 19;
620*01826a49SYabin Cui params.chainLog = 19;
621*01826a49SYabin Cui params = ZSTD_adjustCParams(params, 1000, 100000);
622*01826a49SYabin Cui if (params.hashLog != 18) goto _output_error;
623*01826a49SYabin Cui if (params.chainLog != 17) goto _output_error;
624*01826a49SYabin Cui }
625*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
626*01826a49SYabin Cui
627*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : compress %u bytes : ", testNb++, (unsigned)CNBuffSize);
628*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
629*01826a49SYabin Cui if (cctx==NULL) goto _output_error;
630*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compressCCtx(cctx,
631*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
632*01826a49SYabin Cui CNBuffer, CNBuffSize, 1) );
633*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
634*01826a49SYabin Cui
635*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : size of cctx for level 1 : ", testNb++);
636*01826a49SYabin Cui { size_t const cctxSize = ZSTD_sizeof_CCtx(cctx);
637*01826a49SYabin Cui DISPLAYLEVEL(3, "%u bytes \n", (unsigned)cctxSize);
638*01826a49SYabin Cui }
639*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
640*01826a49SYabin Cui }
641*01826a49SYabin Cui
642*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress skippable frame -8 size : ", testNb++);
643*01826a49SYabin Cui {
644*01826a49SYabin Cui char const skippable8[] = "\x50\x2a\x4d\x18\xf8\xff\xff\xff";
645*01826a49SYabin Cui size_t const size = ZSTD_decompress(NULL, 0, skippable8, 8);
646*01826a49SYabin Cui if (!ZSTD_isError(size)) goto _output_error;
647*01826a49SYabin Cui }
648*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
649*01826a49SYabin Cui
650*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_getFrameContentSize test : ", testNb++);
651*01826a49SYabin Cui { unsigned long long const rSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
652*01826a49SYabin Cui if (rSize != CNBuffSize) goto _output_error;
653*01826a49SYabin Cui }
654*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
655*01826a49SYabin Cui
656*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_getDecompressedSize test : ", testNb++);
657*01826a49SYabin Cui { unsigned long long const rSize = ZSTD_getDecompressedSize(compressedBuffer, cSize);
658*01826a49SYabin Cui if (rSize != CNBuffSize) goto _output_error;
659*01826a49SYabin Cui }
660*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
661*01826a49SYabin Cui
662*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_findDecompressedSize test : ", testNb++);
663*01826a49SYabin Cui { unsigned long long const rSize = ZSTD_findDecompressedSize(compressedBuffer, cSize);
664*01826a49SYabin Cui if (rSize != CNBuffSize) goto _output_error;
665*01826a49SYabin Cui }
666*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
667*01826a49SYabin Cui
668*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : tight ZSTD_decompressBound test : ", testNb++);
669*01826a49SYabin Cui {
670*01826a49SYabin Cui unsigned long long bound = ZSTD_decompressBound(compressedBuffer, cSize);
671*01826a49SYabin Cui if (bound != CNBuffSize) goto _output_error;
672*01826a49SYabin Cui }
673*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
674*01826a49SYabin Cui
675*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_decompressBound test with invalid srcSize : ", testNb++);
676*01826a49SYabin Cui {
677*01826a49SYabin Cui unsigned long long bound = ZSTD_decompressBound(compressedBuffer, cSize - 1);
678*01826a49SYabin Cui if (bound != ZSTD_CONTENTSIZE_ERROR) goto _output_error;
679*01826a49SYabin Cui }
680*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
681*01826a49SYabin Cui
682*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, (unsigned)CNBuffSize);
683*01826a49SYabin Cui { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
684*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error; }
685*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
686*01826a49SYabin Cui
687*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress %u bytes with Huffman assembly disabled : ", testNb++, (unsigned)CNBuffSize);
688*01826a49SYabin Cui {
689*01826a49SYabin Cui ZSTD_DCtx* dctx = ZSTD_createDCtx();
690*01826a49SYabin Cui size_t r;
691*01826a49SYabin Cui CHECK_Z(ZSTD_DCtx_setParameter(dctx, ZSTD_d_disableHuffmanAssembly, 1));
692*01826a49SYabin Cui r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
693*01826a49SYabin Cui if (r != CNBuffSize || memcmp(decodedBuffer, CNBuffer, CNBuffSize)) goto _output_error;
694*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
695*01826a49SYabin Cui }
696*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
697*01826a49SYabin Cui
698*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
699*01826a49SYabin Cui { size_t u;
700*01826a49SYabin Cui for (u=0; u<CNBuffSize; u++) {
701*01826a49SYabin Cui if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;
702*01826a49SYabin Cui } }
703*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
704*01826a49SYabin Cui
705*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : invalid endDirective : ", testNb++);
706*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
707*01826a49SYabin Cui ZSTD_inBuffer inb = { CNBuffer, CNBuffSize, 0 };
708*01826a49SYabin Cui ZSTD_outBuffer outb = { compressedBuffer, compressedBufferSize, 0 };
709*01826a49SYabin Cui if (cctx==NULL) goto _output_error;
710*01826a49SYabin Cui CHECK( ZSTD_isError( ZSTD_compressStream2(cctx, &outb, &inb, (ZSTD_EndDirective) 3) ) ); /* must fail */
711*01826a49SYabin Cui CHECK( ZSTD_isError( ZSTD_compressStream2(cctx, &outb, &inb, (ZSTD_EndDirective)-1) ) ); /* must fail */
712*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
713*01826a49SYabin Cui }
714*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
715*01826a49SYabin Cui
716*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_checkCParams : ", testNb++);
717*01826a49SYabin Cui {
718*01826a49SYabin Cui ZSTD_parameters params = ZSTD_getParams(3, 0, 0);
719*01826a49SYabin Cui assert(!ZSTD_checkCParams(params.cParams));
720*01826a49SYabin Cui }
721*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
722*01826a49SYabin Cui
723*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_createDCtx_advanced and ZSTD_sizeof_DCtx: ", testNb++);
724*01826a49SYabin Cui {
725*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx_advanced(ZSTD_defaultCMem);
726*01826a49SYabin Cui assert(dctx != NULL);
727*01826a49SYabin Cui assert(ZSTD_sizeof_DCtx(dctx) != 0);
728*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
729*01826a49SYabin Cui }
730*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
731*01826a49SYabin Cui
732*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : misc unaccounted for zstd symbols : ", testNb++);
733*01826a49SYabin Cui {
734*01826a49SYabin Cui /* %p takes a void*. In ISO C, it's illegal to cast a function pointer
735*01826a49SYabin Cui * to a data pointer. (Although in POSIX you're required to be allowed
736*01826a49SYabin Cui * to do it...) So we have to fall back to our trusty friend memcpy. */
737*01826a49SYabin Cui unsigned (* const funcptr_getDictID)(const ZSTD_DDict* ddict) =
738*01826a49SYabin Cui ZSTD_getDictID_fromDDict;
739*01826a49SYabin Cui ZSTD_DStream* (* const funcptr_createDStream)(
740*01826a49SYabin Cui ZSTD_customMem customMem) = ZSTD_createDStream_advanced;
741*01826a49SYabin Cui void (* const funcptr_copyDCtx)(
742*01826a49SYabin Cui ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx) = ZSTD_copyDCtx;
743*01826a49SYabin Cui ZSTD_nextInputType_e (* const funcptr_nextInputType)(ZSTD_DCtx* dctx) =
744*01826a49SYabin Cui ZSTD_nextInputType;
745*01826a49SYabin Cui const void *voidptr_getDictID;
746*01826a49SYabin Cui const void *voidptr_createDStream;
747*01826a49SYabin Cui const void *voidptr_copyDCtx;
748*01826a49SYabin Cui const void *voidptr_nextInputType;
749*01826a49SYabin Cui DEBUG_STATIC_ASSERT(sizeof(funcptr_getDictID) == sizeof(voidptr_getDictID));
750*01826a49SYabin Cui memcpy(
751*01826a49SYabin Cui (void*)&voidptr_getDictID,
752*01826a49SYabin Cui (const void*)&funcptr_getDictID,
753*01826a49SYabin Cui sizeof(void*));
754*01826a49SYabin Cui memcpy(
755*01826a49SYabin Cui (void*)&voidptr_createDStream,
756*01826a49SYabin Cui (const void*)&funcptr_createDStream,
757*01826a49SYabin Cui sizeof(void*));
758*01826a49SYabin Cui memcpy(
759*01826a49SYabin Cui (void*)&voidptr_copyDCtx,
760*01826a49SYabin Cui (const void*)&funcptr_copyDCtx,
761*01826a49SYabin Cui sizeof(void*));
762*01826a49SYabin Cui memcpy(
763*01826a49SYabin Cui (void*)&voidptr_nextInputType,
764*01826a49SYabin Cui (const void*)&funcptr_nextInputType,
765*01826a49SYabin Cui sizeof(void*));
766*01826a49SYabin Cui DISPLAYLEVEL(3, "%p ", voidptr_getDictID);
767*01826a49SYabin Cui DISPLAYLEVEL(3, "%p ", voidptr_createDStream);
768*01826a49SYabin Cui DISPLAYLEVEL(3, "%p ", voidptr_copyDCtx);
769*01826a49SYabin Cui DISPLAYLEVEL(3, "%p ", voidptr_nextInputType);
770*01826a49SYabin Cui }
771*01826a49SYabin Cui DISPLAYLEVEL(3, ": OK \n");
772*01826a49SYabin Cui
773*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress with null dict : ", testNb++);
774*01826a49SYabin Cui { ZSTD_DCtx* const dctx = ZSTD_createDCtx(); assert(dctx != NULL);
775*01826a49SYabin Cui { size_t const r = ZSTD_decompress_usingDict(dctx,
776*01826a49SYabin Cui decodedBuffer, CNBuffSize,
777*01826a49SYabin Cui compressedBuffer, cSize,
778*01826a49SYabin Cui NULL, 0);
779*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error;
780*01826a49SYabin Cui }
781*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
782*01826a49SYabin Cui }
783*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
784*01826a49SYabin Cui
785*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress with null DDict : ", testNb++);
786*01826a49SYabin Cui { ZSTD_DCtx* const dctx = ZSTD_createDCtx(); assert(dctx != NULL);
787*01826a49SYabin Cui { size_t const r = ZSTD_decompress_usingDDict(dctx,
788*01826a49SYabin Cui decodedBuffer, CNBuffSize,
789*01826a49SYabin Cui compressedBuffer, cSize,
790*01826a49SYabin Cui NULL);
791*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error;
792*01826a49SYabin Cui }
793*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
794*01826a49SYabin Cui }
795*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
796*01826a49SYabin Cui
797*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress with 1 missing byte : ", testNb++);
798*01826a49SYabin Cui { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize-1);
799*01826a49SYabin Cui if (!ZSTD_isError(r)) goto _output_error;
800*01826a49SYabin Cui if (ZSTD_getErrorCode((size_t)r) != ZSTD_error_srcSize_wrong) goto _output_error; }
801*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
802*01826a49SYabin Cui
803*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress with 1 too much byte : ", testNb++);
804*01826a49SYabin Cui { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize+1);
805*01826a49SYabin Cui if (!ZSTD_isError(r)) goto _output_error;
806*01826a49SYabin Cui if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
807*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
808*01826a49SYabin Cui
809*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress too large input : ", testNb++);
810*01826a49SYabin Cui { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, compressedBufferSize);
811*01826a49SYabin Cui if (!ZSTD_isError(r)) goto _output_error;
812*01826a49SYabin Cui if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
813*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
814*01826a49SYabin Cui
815*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress into NULL buffer : ", testNb++);
816*01826a49SYabin Cui { size_t const r = ZSTD_decompress(NULL, 0, compressedBuffer, compressedBufferSize);
817*01826a49SYabin Cui if (!ZSTD_isError(r)) goto _output_error;
818*01826a49SYabin Cui if (ZSTD_getErrorCode(r) != ZSTD_error_dstSize_tooSmall) goto _output_error; }
819*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
820*01826a49SYabin Cui
821*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress with corrupted checksum : ", testNb++);
822*01826a49SYabin Cui { /* create compressed buffer with checksumming enabled */
823*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
824*01826a49SYabin Cui if (!cctx) {
825*01826a49SYabin Cui DISPLAY("Not enough memory, aborting\n");
826*01826a49SYabin Cui testResult = 1;
827*01826a49SYabin Cui goto _end;
828*01826a49SYabin Cui }
829*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1) );
830*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compress2(cctx,
831*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
832*01826a49SYabin Cui CNBuffer, CNBuffSize) );
833*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
834*01826a49SYabin Cui }
835*01826a49SYabin Cui { /* copy the compressed buffer and corrupt the checksum */
836*01826a49SYabin Cui size_t r;
837*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
838*01826a49SYabin Cui if (!dctx) {
839*01826a49SYabin Cui DISPLAY("Not enough memory, aborting\n");
840*01826a49SYabin Cui testResult = 1;
841*01826a49SYabin Cui goto _end;
842*01826a49SYabin Cui }
843*01826a49SYabin Cui
844*01826a49SYabin Cui ((char*)compressedBuffer)[cSize-1] += 1;
845*01826a49SYabin Cui r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
846*01826a49SYabin Cui if (!ZSTD_isError(r)) goto _output_error;
847*01826a49SYabin Cui if (ZSTD_getErrorCode(r) != ZSTD_error_checksum_wrong) goto _output_error;
848*01826a49SYabin Cui
849*01826a49SYabin Cui CHECK_Z(ZSTD_DCtx_setParameter(dctx, ZSTD_d_forceIgnoreChecksum, ZSTD_d_ignoreChecksum));
850*01826a49SYabin Cui r = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize-1);
851*01826a49SYabin Cui if (!ZSTD_isError(r)) goto _output_error; /* wrong checksum size should still throw error */
852*01826a49SYabin Cui r = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
853*01826a49SYabin Cui if (ZSTD_isError(r)) goto _output_error;
854*01826a49SYabin Cui
855*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
856*01826a49SYabin Cui }
857*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
858*01826a49SYabin Cui
859*01826a49SYabin Cui
860*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_decompressBound test with content size missing : ", testNb++);
861*01826a49SYabin Cui { /* create compressed buffer with content size missing */
862*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
863*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_contentSizeFlag, 0) );
864*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compress2(cctx,
865*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
866*01826a49SYabin Cui CNBuffer, CNBuffSize) );
867*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
868*01826a49SYabin Cui }
869*01826a49SYabin Cui { /* ensure frame content size is missing */
870*01826a49SYabin Cui ZSTD_frameHeader zfh;
871*01826a49SYabin Cui size_t const ret = ZSTD_getFrameHeader(&zfh, compressedBuffer, compressedBufferSize);
872*01826a49SYabin Cui if (ret != 0 || zfh.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
873*01826a49SYabin Cui }
874*01826a49SYabin Cui { /* ensure CNBuffSize <= decompressBound */
875*01826a49SYabin Cui unsigned long long const bound = ZSTD_decompressBound(compressedBuffer, compressedBufferSize);
876*01826a49SYabin Cui if (CNBuffSize > bound) goto _output_error;
877*01826a49SYabin Cui }
878*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
879*01826a49SYabin Cui
880*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d: check DCtx size is reduced after many oversized calls : ", testNb++);
881*01826a49SYabin Cui {
882*01826a49SYabin Cui size_t const largeFrameSrcSize = 200;
883*01826a49SYabin Cui size_t const smallFrameSrcSize = 10;
884*01826a49SYabin Cui size_t const nbFrames = 256;
885*01826a49SYabin Cui
886*01826a49SYabin Cui size_t i = 0, consumed = 0, produced = 0, prevDCtxSize = 0;
887*01826a49SYabin Cui int sizeReduced = 0;
888*01826a49SYabin Cui
889*01826a49SYabin Cui BYTE* const dst = (BYTE*)compressedBuffer;
890*01826a49SYabin Cui ZSTD_DCtx* dctx = ZSTD_createDCtx();
891*01826a49SYabin Cui
892*01826a49SYabin Cui /* create a large frame and then a bunch of small frames */
893*01826a49SYabin Cui size_t srcSize = ZSTD_compress((void*)dst,
894*01826a49SYabin Cui compressedBufferSize, CNBuffer, largeFrameSrcSize, 3);
895*01826a49SYabin Cui for (i = 0; i < nbFrames; i++)
896*01826a49SYabin Cui srcSize += ZSTD_compress((void*)(dst + srcSize),
897*01826a49SYabin Cui compressedBufferSize - srcSize, CNBuffer,
898*01826a49SYabin Cui smallFrameSrcSize, 3);
899*01826a49SYabin Cui
900*01826a49SYabin Cui /* decompressStream and make sure that dctx size was reduced at least once */
901*01826a49SYabin Cui while (consumed < srcSize) {
902*01826a49SYabin Cui ZSTD_inBuffer in = {(void*)(dst + consumed), MIN(1, srcSize - consumed), 0};
903*01826a49SYabin Cui ZSTD_outBuffer out = {(BYTE*)CNBuffer + produced, CNBuffSize - produced, 0};
904*01826a49SYabin Cui ZSTD_decompressStream(dctx, &out, &in);
905*01826a49SYabin Cui consumed += in.pos;
906*01826a49SYabin Cui produced += out.pos;
907*01826a49SYabin Cui
908*01826a49SYabin Cui /* success! size was reduced from the previous frame */
909*01826a49SYabin Cui if (prevDCtxSize > ZSTD_sizeof_DCtx(dctx))
910*01826a49SYabin Cui sizeReduced = 1;
911*01826a49SYabin Cui
912*01826a49SYabin Cui prevDCtxSize = ZSTD_sizeof_DCtx(dctx);
913*01826a49SYabin Cui }
914*01826a49SYabin Cui
915*01826a49SYabin Cui assert(sizeReduced);
916*01826a49SYabin Cui
917*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
918*01826a49SYabin Cui }
919*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
920*01826a49SYabin Cui
921*01826a49SYabin Cui {
922*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
923*01826a49SYabin Cui ZSTD_CDict* const cdict = ZSTD_createCDict(CNBuffer, 100, 1);
924*01826a49SYabin Cui ZSTD_parameters const params = ZSTD_getParams(1, 0, 0);
925*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_format, ZSTD_f_zstd1_magicless) );
926*01826a49SYabin Cui
927*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_compressCCtx() doesn't use advanced parameters", testNb++);
928*01826a49SYabin Cui CHECK_Z(ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, NULL, 0, 1));
929*01826a49SYabin Cui if (MEM_readLE32(compressedBuffer) != ZSTD_MAGICNUMBER) goto _output_error;
930*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
931*01826a49SYabin Cui
932*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_compress_usingDict() doesn't use advanced parameters: ", testNb++);
933*01826a49SYabin Cui CHECK_Z(ZSTD_compress_usingDict(cctx, compressedBuffer, compressedBufferSize, NULL, 0, NULL, 0, 1));
934*01826a49SYabin Cui if (MEM_readLE32(compressedBuffer) != ZSTD_MAGICNUMBER) goto _output_error;
935*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
936*01826a49SYabin Cui
937*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_compress_usingCDict() doesn't use advanced parameters: ", testNb++);
938*01826a49SYabin Cui CHECK_Z(ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize, NULL, 0, cdict));
939*01826a49SYabin Cui if (MEM_readLE32(compressedBuffer) != ZSTD_MAGICNUMBER) goto _output_error;
940*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
941*01826a49SYabin Cui
942*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_compress_advanced() doesn't use advanced parameters: ", testNb++);
943*01826a49SYabin Cui CHECK_Z(ZSTD_compress_advanced(cctx, compressedBuffer, compressedBufferSize, NULL, 0, NULL, 0, params));
944*01826a49SYabin Cui if (MEM_readLE32(compressedBuffer) != ZSTD_MAGICNUMBER) goto _output_error;
945*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
946*01826a49SYabin Cui
947*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_compress_usingCDict_advanced() doesn't use advanced parameters: ", testNb++);
948*01826a49SYabin Cui CHECK_Z(ZSTD_compress_usingCDict_advanced(cctx, compressedBuffer, compressedBufferSize, NULL, 0, cdict, params.fParams));
949*01826a49SYabin Cui if (MEM_readLE32(compressedBuffer) != ZSTD_MAGICNUMBER) goto _output_error;
950*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
951*01826a49SYabin Cui
952*01826a49SYabin Cui ZSTD_freeCDict(cdict);
953*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
954*01826a49SYabin Cui }
955*01826a49SYabin Cui
956*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : maxBlockSize = 2K", testNb++);
957*01826a49SYabin Cui {
958*01826a49SYabin Cui ZSTD_CCtx* cctx = ZSTD_createCCtx();
959*01826a49SYabin Cui ZSTD_DCtx* dctx = ZSTD_createDCtx();
960*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
961*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_maxBlockSize, 2048));
962*01826a49SYabin Cui CHECK_Z(ZSTD_DCtx_setParameter(dctx, ZSTD_d_maxBlockSize, 2048));
963*01826a49SYabin Cui
964*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
965*01826a49SYabin Cui CHECK_Z(cSize);
966*01826a49SYabin Cui CHECK_Z(ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize));
967*01826a49SYabin Cui
968*01826a49SYabin Cui CHECK_Z(ZSTD_DCtx_setParameter(dctx, ZSTD_d_maxBlockSize, 1024));
969*01826a49SYabin Cui CHECK(ZSTD_isError(ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize)));
970*01826a49SYabin Cui
971*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
972*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
973*01826a49SYabin Cui }
974*01826a49SYabin Cui
975*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ldm fill dict out-of-bounds check", testNb++);
976*01826a49SYabin Cui {
977*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
978*01826a49SYabin Cui
979*01826a49SYabin Cui size_t const size = (1U << 10);
980*01826a49SYabin Cui size_t const dstCapacity = ZSTD_compressBound(size);
981*01826a49SYabin Cui void* dict = (void*)malloc(size);
982*01826a49SYabin Cui void* src = (void*)malloc(size);
983*01826a49SYabin Cui void* dst = (void*)malloc(dstCapacity);
984*01826a49SYabin Cui
985*01826a49SYabin Cui RDG_genBuffer(dict, size, 0.5, 0.5, seed);
986*01826a49SYabin Cui RDG_genBuffer(src, size, 0.5, 0.5, seed);
987*01826a49SYabin Cui
988*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_enable));
989*01826a49SYabin Cui assert(!ZSTD_isError(ZSTD_compress_usingDict(cctx, dst, dstCapacity, src, size, dict, size, 3)));
990*01826a49SYabin Cui
991*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
992*01826a49SYabin Cui free(dict);
993*01826a49SYabin Cui free(src);
994*01826a49SYabin Cui free(dst);
995*01826a49SYabin Cui }
996*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
997*01826a49SYabin Cui
998*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : testing dict compression with enableLdm and forceMaxWindow : ", testNb++);
999*01826a49SYabin Cui {
1000*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1001*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
1002*01826a49SYabin Cui void* dict = (void*)malloc(CNBuffSize);
1003*01826a49SYabin Cui int nbWorkers;
1004*01826a49SYabin Cui
1005*01826a49SYabin Cui for (nbWorkers = 0; nbWorkers < 3; ++nbWorkers) {
1006*01826a49SYabin Cui RDG_genBuffer(dict, CNBuffSize, 0.5, 0.5, seed);
1007*01826a49SYabin Cui RDG_genBuffer(CNBuffer, CNBuffSize, 0.6, 0.6, seed);
1008*01826a49SYabin Cui
1009*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbWorkers));
1010*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
1011*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_forceMaxWindow, 1));
1012*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_enable));
1013*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_refPrefix(cctx, dict, CNBuffSize));
1014*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
1015*01826a49SYabin Cui CHECK_Z(cSize);
1016*01826a49SYabin Cui CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, dict, CNBuffSize));
1017*01826a49SYabin Cui }
1018*01826a49SYabin Cui
1019*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1020*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
1021*01826a49SYabin Cui free(dict);
1022*01826a49SYabin Cui }
1023*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1024*01826a49SYabin Cui
1025*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : testing dict compression for determinism : ", testNb++);
1026*01826a49SYabin Cui {
1027*01826a49SYabin Cui size_t const testSize = 1024;
1028*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1029*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
1030*01826a49SYabin Cui char* dict = (char*)malloc(2 * testSize);
1031*01826a49SYabin Cui int ldmEnabled, level;
1032*01826a49SYabin Cui
1033*01826a49SYabin Cui RDG_genBuffer(dict, testSize, 0.5, 0.5, seed);
1034*01826a49SYabin Cui RDG_genBuffer(CNBuffer, testSize, 0.6, 0.6, seed);
1035*01826a49SYabin Cui memcpy(dict + testSize, CNBuffer, testSize);
1036*01826a49SYabin Cui for (level = 1; level <= 5; ++level) {
1037*01826a49SYabin Cui for (ldmEnabled = ZSTD_ps_enable; ldmEnabled <= ZSTD_ps_disable; ++ldmEnabled) {
1038*01826a49SYabin Cui size_t cSize0;
1039*01826a49SYabin Cui XXH64_hash_t compressedChecksum0;
1040*01826a49SYabin Cui
1041*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
1042*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, level));
1043*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ldmEnabled));
1044*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_deterministicRefPrefix, 1));
1045*01826a49SYabin Cui
1046*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_refPrefix(cctx, dict, testSize));
1047*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, testSize);
1048*01826a49SYabin Cui CHECK_Z(cSize);
1049*01826a49SYabin Cui CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, testSize, compressedBuffer, cSize, dict, testSize));
1050*01826a49SYabin Cui
1051*01826a49SYabin Cui cSize0 = cSize;
1052*01826a49SYabin Cui compressedChecksum0 = XXH64(compressedBuffer, cSize, 0);
1053*01826a49SYabin Cui
1054*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_refPrefix(cctx, dict, testSize));
1055*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, dict + testSize, testSize);
1056*01826a49SYabin Cui CHECK_Z(cSize);
1057*01826a49SYabin Cui
1058*01826a49SYabin Cui if (cSize != cSize0) goto _output_error;
1059*01826a49SYabin Cui if (XXH64(compressedBuffer, cSize, 0) != compressedChecksum0) goto _output_error;
1060*01826a49SYabin Cui }
1061*01826a49SYabin Cui }
1062*01826a49SYabin Cui
1063*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1064*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
1065*01826a49SYabin Cui free(dict);
1066*01826a49SYabin Cui }
1067*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1068*01826a49SYabin Cui
1069*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : LDM + opt parser with small uncompressible block ", testNb++);
1070*01826a49SYabin Cui { ZSTD_CCtx* cctx = ZSTD_createCCtx();
1071*01826a49SYabin Cui ZSTD_DCtx* dctx = ZSTD_createDCtx();
1072*01826a49SYabin Cui size_t const srcSize = 300 KB;
1073*01826a49SYabin Cui size_t const flushSize = 128 KB + 5;
1074*01826a49SYabin Cui size_t const dstSize = ZSTD_compressBound(srcSize);
1075*01826a49SYabin Cui char* src = (char*)CNBuffer;
1076*01826a49SYabin Cui char* dst = (char*)compressedBuffer;
1077*01826a49SYabin Cui
1078*01826a49SYabin Cui ZSTD_outBuffer out = { dst, dstSize, 0 };
1079*01826a49SYabin Cui ZSTD_inBuffer in = { src, flushSize, 0 };
1080*01826a49SYabin Cui
1081*01826a49SYabin Cui if (!cctx || !dctx) {
1082*01826a49SYabin Cui DISPLAY("Not enough memory, aborting\n");
1083*01826a49SYabin Cui testResult = 1;
1084*01826a49SYabin Cui goto _end;
1085*01826a49SYabin Cui }
1086*01826a49SYabin Cui
1087*01826a49SYabin Cui RDG_genBuffer(src, srcSize, 0.5, 0.5, seed);
1088*01826a49SYabin Cui /* Force an LDM to exist that crosses block boundary into uncompressible block */
1089*01826a49SYabin Cui memcpy(src + 125 KB, src, 3 KB + 5);
1090*01826a49SYabin Cui
1091*01826a49SYabin Cui /* Enable MT, LDM, and opt parser */
1092*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 1));
1093*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_enable));
1094*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
1095*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 19));
1096*01826a49SYabin Cui
1097*01826a49SYabin Cui /* Flushes a block of 128 KB and block of 5 bytes */
1098*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
1099*01826a49SYabin Cui
1100*01826a49SYabin Cui /* Compress the rest */
1101*01826a49SYabin Cui in.size = 300 KB;
1102*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
1103*01826a49SYabin Cui
1104*01826a49SYabin Cui CHECK_Z(ZSTD_decompress(decodedBuffer, CNBuffSize, dst, out.pos));
1105*01826a49SYabin Cui
1106*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1107*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
1108*01826a49SYabin Cui }
1109*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1110*01826a49SYabin Cui
1111*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : testing ldm dictionary gets invalidated : ", testNb++);
1112*01826a49SYabin Cui {
1113*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1114*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
1115*01826a49SYabin Cui void* dict = (void*)malloc(CNBuffSize);
1116*01826a49SYabin Cui size_t const kWindowLog = 10;
1117*01826a49SYabin Cui size_t const kWindowSize = (size_t)1 << kWindowLog;
1118*01826a49SYabin Cui size_t const dictSize = kWindowSize * 10;
1119*01826a49SYabin Cui size_t const srcSize1 = kWindowSize / 2;
1120*01826a49SYabin Cui size_t const srcSize2 = kWindowSize * 10;
1121*01826a49SYabin Cui
1122*01826a49SYabin Cui CHECK(cctx!=NULL);
1123*01826a49SYabin Cui CHECK(dctx!=NULL);
1124*01826a49SYabin Cui CHECK(dict!=NULL);
1125*01826a49SYabin Cui if (CNBuffSize < dictSize) goto _output_error;
1126*01826a49SYabin Cui
1127*01826a49SYabin Cui RDG_genBuffer(dict, dictSize, 0.5, 0.5, seed);
1128*01826a49SYabin Cui RDG_genBuffer(CNBuffer, srcSize1 + srcSize2, 0.5, 0.5, seed);
1129*01826a49SYabin Cui
1130*01826a49SYabin Cui /* Enable checksum to verify round trip. */
1131*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
1132*01826a49SYabin Cui /* Disable content size to skip single-pass decompression. */
1133*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_contentSizeFlag, 0));
1134*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, (int)kWindowLog));
1135*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_enable));
1136*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_ldmMinMatch, 32));
1137*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_ldmHashRateLog, 1));
1138*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_ldmHashLog, 16));
1139*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_ldmBucketSizeLog, 3));
1140*01826a49SYabin Cui
1141*01826a49SYabin Cui /* Round trip once with a dictionary. */
1142*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_refPrefix(cctx, dict, dictSize));
1143*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, srcSize1);
1144*01826a49SYabin Cui CHECK_Z(cSize);
1145*01826a49SYabin Cui CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, dict, dictSize));
1146*01826a49SYabin Cui
1147*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, srcSize2);
1148*01826a49SYabin Cui /* Streaming decompression to catch out of bounds offsets. */
1149*01826a49SYabin Cui {
1150*01826a49SYabin Cui ZSTD_inBuffer in = {compressedBuffer, cSize, 0};
1151*01826a49SYabin Cui ZSTD_outBuffer out = {decodedBuffer, CNBuffSize, 0};
1152*01826a49SYabin Cui size_t const dSize = ZSTD_decompressStream(dctx, &out, &in);
1153*01826a49SYabin Cui CHECK_Z(dSize);
1154*01826a49SYabin Cui if (dSize != 0) goto _output_error;
1155*01826a49SYabin Cui }
1156*01826a49SYabin Cui
1157*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 2));
1158*01826a49SYabin Cui /* Round trip once with a dictionary. */
1159*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_refPrefix(cctx, dict, dictSize));
1160*01826a49SYabin Cui { ZSTD_inBuffer in = {CNBuffer, srcSize1, 0};
1161*01826a49SYabin Cui ZSTD_outBuffer out = {compressedBuffer, compressedBufferSize, 0};
1162*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
1163*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
1164*01826a49SYabin Cui cSize = out.pos;
1165*01826a49SYabin Cui }
1166*01826a49SYabin Cui CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, dict, dictSize));
1167*01826a49SYabin Cui
1168*01826a49SYabin Cui { ZSTD_inBuffer in = {CNBuffer, srcSize2, 0};
1169*01826a49SYabin Cui ZSTD_outBuffer out = {compressedBuffer, compressedBufferSize, 0};
1170*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
1171*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
1172*01826a49SYabin Cui cSize = out.pos;
1173*01826a49SYabin Cui }
1174*01826a49SYabin Cui /* Streaming decompression to catch out of bounds offsets. */
1175*01826a49SYabin Cui { ZSTD_inBuffer in = {compressedBuffer, cSize, 0};
1176*01826a49SYabin Cui ZSTD_outBuffer out = {decodedBuffer, CNBuffSize, 0};
1177*01826a49SYabin Cui size_t const dSize = ZSTD_decompressStream(dctx, &out, &in);
1178*01826a49SYabin Cui CHECK_Z(dSize);
1179*01826a49SYabin Cui if (dSize != 0) goto _output_error;
1180*01826a49SYabin Cui }
1181*01826a49SYabin Cui
1182*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1183*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
1184*01826a49SYabin Cui free(dict);
1185*01826a49SYabin Cui }
1186*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1187*01826a49SYabin Cui
1188*01826a49SYabin Cui /* Note: this test takes 0.5 seconds to run */
1189*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : testing refPrefx vs refPrefx + ldm (size comparison) : ", testNb++);
1190*01826a49SYabin Cui {
1191*01826a49SYabin Cui /* test a big buffer so that ldm can take effect */
1192*01826a49SYabin Cui size_t const size = 100 MB;
1193*01826a49SYabin Cui int const windowLog = 27;
1194*01826a49SYabin Cui size_t const dstSize = ZSTD_compressBound(size);
1195*01826a49SYabin Cui
1196*01826a49SYabin Cui void* dict = (void*)malloc(size);
1197*01826a49SYabin Cui void* src = (void*)malloc(size);
1198*01826a49SYabin Cui void* dst = (void*)malloc(dstSize);
1199*01826a49SYabin Cui void* recon = (void*)malloc(size);
1200*01826a49SYabin Cui
1201*01826a49SYabin Cui size_t refPrefixCompressedSize = 0;
1202*01826a49SYabin Cui size_t refPrefixLdmCompressedSize = 0;
1203*01826a49SYabin Cui size_t reconSize = 0;
1204*01826a49SYabin Cui
1205*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1206*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
1207*01826a49SYabin Cui
1208*01826a49SYabin Cui /* make dict and src the same uncompressible data */
1209*01826a49SYabin Cui RDG_genBuffer(src, size, 0, 0, seed);
1210*01826a49SYabin Cui memcpy(dict, src, size);
1211*01826a49SYabin Cui assert(!memcmp(dict, src, size));
1212*01826a49SYabin Cui
1213*01826a49SYabin Cui /* set level 1 and windowLog to cover src */
1214*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 1));
1215*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, windowLog));
1216*01826a49SYabin Cui
1217*01826a49SYabin Cui /* compress on level 1 using just refPrefix and no ldm */
1218*01826a49SYabin Cui ZSTD_CCtx_refPrefix(cctx, dict, size);
1219*01826a49SYabin Cui refPrefixCompressedSize = ZSTD_compress2(cctx, dst, dstSize, src, size);
1220*01826a49SYabin Cui assert(!ZSTD_isError(refPrefixCompressedSize));
1221*01826a49SYabin Cui
1222*01826a49SYabin Cui /* test round trip just refPrefix */
1223*01826a49SYabin Cui ZSTD_DCtx_refPrefix(dctx, dict, size);
1224*01826a49SYabin Cui reconSize = ZSTD_decompressDCtx(dctx, recon, size, dst, refPrefixCompressedSize);
1225*01826a49SYabin Cui assert(!ZSTD_isError(reconSize));
1226*01826a49SYabin Cui assert(reconSize == size);
1227*01826a49SYabin Cui assert(!memcmp(recon, src, size));
1228*01826a49SYabin Cui
1229*01826a49SYabin Cui /* compress on level 1 using refPrefix and ldm */
1230*01826a49SYabin Cui ZSTD_CCtx_refPrefix(cctx, dict, size);;
1231*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_enable))
1232*01826a49SYabin Cui refPrefixLdmCompressedSize = ZSTD_compress2(cctx, dst, dstSize, src, size);
1233*01826a49SYabin Cui assert(!ZSTD_isError(refPrefixLdmCompressedSize));
1234*01826a49SYabin Cui
1235*01826a49SYabin Cui /* test round trip refPrefix + ldm*/
1236*01826a49SYabin Cui ZSTD_DCtx_refPrefix(dctx, dict, size);
1237*01826a49SYabin Cui reconSize = ZSTD_decompressDCtx(dctx, recon, size, dst, refPrefixLdmCompressedSize);
1238*01826a49SYabin Cui assert(!ZSTD_isError(reconSize));
1239*01826a49SYabin Cui assert(reconSize == size);
1240*01826a49SYabin Cui assert(!memcmp(recon, src, size));
1241*01826a49SYabin Cui
1242*01826a49SYabin Cui /* make sure that refPrefixCompressedSize is significantly greater */
1243*01826a49SYabin Cui assert(refPrefixCompressedSize > 10 * refPrefixLdmCompressedSize);
1244*01826a49SYabin Cui /* make sure the ldm compressed size is less than 1% of original */
1245*01826a49SYabin Cui assert((double)refPrefixLdmCompressedSize / (double)size < 0.01);
1246*01826a49SYabin Cui
1247*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
1248*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1249*01826a49SYabin Cui free(recon);
1250*01826a49SYabin Cui free(dict);
1251*01826a49SYabin Cui free(src);
1252*01826a49SYabin Cui free(dst);
1253*01826a49SYabin Cui }
1254*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1255*01826a49SYabin Cui
1256*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : in-place decompression : ", testNb++);
1257*01826a49SYabin Cui cSize = ZSTD_compress(compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize, -ZSTD_BLOCKSIZE_MAX);
1258*01826a49SYabin Cui CHECK_Z(cSize);
1259*01826a49SYabin Cui CHECK_LT(CNBuffSize, cSize);
1260*01826a49SYabin Cui {
1261*01826a49SYabin Cui size_t const margin = ZSTD_decompressionMargin(compressedBuffer, cSize);
1262*01826a49SYabin Cui size_t const outputSize = (CNBuffSize + margin);
1263*01826a49SYabin Cui char* output = malloc(outputSize);
1264*01826a49SYabin Cui char* input = output + outputSize - cSize;
1265*01826a49SYabin Cui CHECK_LT(cSize, CNBuffSize + margin);
1266*01826a49SYabin Cui CHECK(output != NULL);
1267*01826a49SYabin Cui CHECK_Z(margin);
1268*01826a49SYabin Cui CHECK(margin <= ZSTD_DECOMPRESSION_MARGIN(CNBuffSize, ZSTD_BLOCKSIZE_MAX));
1269*01826a49SYabin Cui memcpy(input, compressedBuffer, cSize);
1270*01826a49SYabin Cui
1271*01826a49SYabin Cui {
1272*01826a49SYabin Cui size_t const dSize = ZSTD_decompress(output, outputSize, input, cSize);
1273*01826a49SYabin Cui CHECK_Z(dSize);
1274*01826a49SYabin Cui CHECK_EQ(dSize, CNBuffSize);
1275*01826a49SYabin Cui }
1276*01826a49SYabin Cui CHECK(!memcmp(output, CNBuffer, CNBuffSize));
1277*01826a49SYabin Cui free(output);
1278*01826a49SYabin Cui }
1279*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1280*01826a49SYabin Cui
1281*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : in-place decompression with 2 frames : ", testNb++);
1282*01826a49SYabin Cui cSize = ZSTD_compress(compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize / 3, -ZSTD_BLOCKSIZE_MAX);
1283*01826a49SYabin Cui CHECK_Z(cSize);
1284*01826a49SYabin Cui {
1285*01826a49SYabin Cui size_t const cSize2 = ZSTD_compress((char*)compressedBuffer + cSize, compressedBufferSize - cSize, (char const*)CNBuffer + (CNBuffSize / 3), CNBuffSize / 3, -ZSTD_BLOCKSIZE_MAX);
1286*01826a49SYabin Cui CHECK_Z(cSize2);
1287*01826a49SYabin Cui cSize += cSize2;
1288*01826a49SYabin Cui }
1289*01826a49SYabin Cui {
1290*01826a49SYabin Cui size_t const srcSize = (CNBuffSize / 3) * 2;
1291*01826a49SYabin Cui size_t const margin = ZSTD_decompressionMargin(compressedBuffer, cSize);
1292*01826a49SYabin Cui size_t const outputSize = (CNBuffSize + margin);
1293*01826a49SYabin Cui char* output = malloc(outputSize);
1294*01826a49SYabin Cui char* input = output + outputSize - cSize;
1295*01826a49SYabin Cui CHECK_LT(cSize, CNBuffSize + margin);
1296*01826a49SYabin Cui CHECK(output != NULL);
1297*01826a49SYabin Cui CHECK_Z(margin);
1298*01826a49SYabin Cui memcpy(input, compressedBuffer, cSize);
1299*01826a49SYabin Cui
1300*01826a49SYabin Cui {
1301*01826a49SYabin Cui size_t const dSize = ZSTD_decompress(output, outputSize, input, cSize);
1302*01826a49SYabin Cui CHECK_Z(dSize);
1303*01826a49SYabin Cui CHECK_EQ(dSize, srcSize);
1304*01826a49SYabin Cui }
1305*01826a49SYabin Cui CHECK(!memcmp(output, CNBuffer, srcSize));
1306*01826a49SYabin Cui free(output);
1307*01826a49SYabin Cui }
1308*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1309*01826a49SYabin Cui
1310*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Check block splitter with 64K literal length : ", testNb++);
1311*01826a49SYabin Cui { ZSTD_CCtx* cctx = ZSTD_createCCtx();
1312*01826a49SYabin Cui size_t const srcSize = 256 * 1024;
1313*01826a49SYabin Cui U32 const compressibleLenU32 = 32 * 1024 / 4;
1314*01826a49SYabin Cui U32 const blockSizeU32 = 128 * 1024 / 4;
1315*01826a49SYabin Cui U32 const litLenU32 = 64 * 1024 / 4;
1316*01826a49SYabin Cui U32* data = (U32*)malloc(srcSize);
1317*01826a49SYabin Cui size_t dSize;
1318*01826a49SYabin Cui
1319*01826a49SYabin Cui if (data == NULL || cctx == NULL) goto _output_error;
1320*01826a49SYabin Cui
1321*01826a49SYabin Cui /* Generate data without any matches */
1322*01826a49SYabin Cui RDG_genBuffer(data, srcSize, 0.0, 0.01, 2654435761U);
1323*01826a49SYabin Cui /* Generate 32K of compressible data */
1324*01826a49SYabin Cui RDG_genBuffer(data, compressibleLenU32 * 4, 0.5, 0.5, 0xcafebabe);
1325*01826a49SYabin Cui
1326*01826a49SYabin Cui /* Add a match of offset=12, length=8 at idx=16, 32, 48, 64 */
1327*01826a49SYabin Cui data[compressibleLenU32 + 0] = 0xFFFFFFFF;
1328*01826a49SYabin Cui data[compressibleLenU32 + 1] = 0xEEEEEEEE;
1329*01826a49SYabin Cui data[compressibleLenU32 + 4] = 0xFFFFFFFF;
1330*01826a49SYabin Cui data[compressibleLenU32 + 5] = 0xEEEEEEEE;
1331*01826a49SYabin Cui
1332*01826a49SYabin Cui /* Add a match of offset=16, length=8 at idx=64K + 64.
1333*01826a49SYabin Cui * This generates a sequence with llen=64K, and repeat code 1.
1334*01826a49SYabin Cui * The block splitter thought this was ll0, and corrupted the
1335*01826a49SYabin Cui * repeat offset history.
1336*01826a49SYabin Cui */
1337*01826a49SYabin Cui data[compressibleLenU32 + litLenU32 + 2 + 0] = 0xDDDDDDDD;
1338*01826a49SYabin Cui data[compressibleLenU32 + litLenU32 + 2 + 1] = 0xCCCCCCCC;
1339*01826a49SYabin Cui data[compressibleLenU32 + litLenU32 + 2 + 4] = 0xDDDDDDDD;
1340*01826a49SYabin Cui data[compressibleLenU32 + litLenU32 + 2 + 5] = 0xCCCCCCCC;
1341*01826a49SYabin Cui
1342*01826a49SYabin Cui /* Add a match of offset=16, length=8 at idx=128K + 16.
1343*01826a49SYabin Cui * This should generate a sequence with repeat code = 1.
1344*01826a49SYabin Cui * But the block splitters mistake caused zstd to generate
1345*01826a49SYabin Cui * repeat code = 2, corrupting the data.
1346*01826a49SYabin Cui */
1347*01826a49SYabin Cui data[blockSizeU32] = 0xBBBBBBBB;
1348*01826a49SYabin Cui data[blockSizeU32 + 1] = 0xAAAAAAAA;
1349*01826a49SYabin Cui data[blockSizeU32 + 4] = 0xBBBBBBBB;
1350*01826a49SYabin Cui data[blockSizeU32 + 5] = 0xAAAAAAAA;
1351*01826a49SYabin Cui
1352*01826a49SYabin Cui /* Generate a golden file from this data in case datagen changes and
1353*01826a49SYabin Cui * doesn't generate the exact same data. We will also test this golden file.
1354*01826a49SYabin Cui */
1355*01826a49SYabin Cui if (0) {
1356*01826a49SYabin Cui FILE* f = fopen("golden-compression/PR-3517-block-splitter-corruption-test", "wb");
1357*01826a49SYabin Cui fwrite(data, 1, srcSize, f);
1358*01826a49SYabin Cui fclose(f);
1359*01826a49SYabin Cui }
1360*01826a49SYabin Cui
1361*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 19));
1362*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_minMatch, 7));
1363*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_useBlockSplitter, ZSTD_ps_enable));
1364*01826a49SYabin Cui
1365*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, data, srcSize);
1366*01826a49SYabin Cui CHECK_Z(cSize);
1367*01826a49SYabin Cui dSize = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
1368*01826a49SYabin Cui CHECK_Z(dSize);
1369*01826a49SYabin Cui CHECK_EQ(dSize, srcSize);
1370*01826a49SYabin Cui CHECK(!memcmp(decodedBuffer, data, srcSize));
1371*01826a49SYabin Cui
1372*01826a49SYabin Cui free(data);
1373*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1374*01826a49SYabin Cui }
1375*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1376*01826a49SYabin Cui
1377*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : superblock uncompressible data: too many nocompress superblocks : ", testNb++);
1378*01826a49SYabin Cui {
1379*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1380*01826a49SYabin Cui const BYTE* src = (BYTE*)CNBuffer; BYTE* dst = (BYTE*)compressedBuffer;
1381*01826a49SYabin Cui size_t srcSize = 321656; size_t dstCapacity = ZSTD_compressBound(srcSize);
1382*01826a49SYabin Cui
1383*01826a49SYabin Cui /* This is the number of bytes to stream before ending. This value
1384*01826a49SYabin Cui * was obtained by trial and error :/. */
1385*01826a49SYabin Cui
1386*01826a49SYabin Cui const size_t streamCompressThreshold = 161792;
1387*01826a49SYabin Cui const size_t streamCompressDelta = 1024;
1388*01826a49SYabin Cui
1389*01826a49SYabin Cui /* The first 1/5 of the buffer is compressible and the last 4/5 is
1390*01826a49SYabin Cui * uncompressible. This is an approximation of the type of data
1391*01826a49SYabin Cui * the fuzzer generated to catch this bug. Streams like this were making
1392*01826a49SYabin Cui * zstd generate noCompress superblocks (which are larger than the src
1393*01826a49SYabin Cui * they come from). Do this enough times, and we'll run out of room
1394*01826a49SYabin Cui * and throw a dstSize_tooSmall error. */
1395*01826a49SYabin Cui
1396*01826a49SYabin Cui const size_t compressiblePartSize = srcSize/5;
1397*01826a49SYabin Cui const size_t uncompressiblePartSize = srcSize-compressiblePartSize;
1398*01826a49SYabin Cui RDG_genBuffer(CNBuffer, compressiblePartSize, 0.5, 0.5, seed);
1399*01826a49SYabin Cui RDG_genBuffer((BYTE*)CNBuffer+compressiblePartSize, uncompressiblePartSize, 0, 0, seed);
1400*01826a49SYabin Cui
1401*01826a49SYabin Cui /* Setting target block size so that superblock is used */
1402*01826a49SYabin Cui
1403*01826a49SYabin Cui assert(cctx != NULL);
1404*01826a49SYabin Cui ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetCBlockSize, 81);
1405*01826a49SYabin Cui
1406*01826a49SYabin Cui { size_t read;
1407*01826a49SYabin Cui for (read = 0; read < streamCompressThreshold; read += streamCompressDelta) {
1408*01826a49SYabin Cui ZSTD_inBuffer in = {src, streamCompressDelta, 0};
1409*01826a49SYabin Cui ZSTD_outBuffer out = {dst, dstCapacity, 0};
1410*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue));
1411*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
1412*01826a49SYabin Cui src += streamCompressDelta; srcSize -= streamCompressDelta;
1413*01826a49SYabin Cui dst += out.pos; dstCapacity -= out.pos;
1414*01826a49SYabin Cui } }
1415*01826a49SYabin Cui
1416*01826a49SYabin Cui /* This is trying to catch a dstSize_tooSmall error */
1417*01826a49SYabin Cui
1418*01826a49SYabin Cui { ZSTD_inBuffer in = {src, srcSize, 0};
1419*01826a49SYabin Cui ZSTD_outBuffer out = {dst, dstCapacity, 0};
1420*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
1421*01826a49SYabin Cui }
1422*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1423*01826a49SYabin Cui }
1424*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1425*01826a49SYabin Cui
1426*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d: superblock with no literals : ", testNb++);
1427*01826a49SYabin Cui /* Generate the same data 20 times over */
1428*01826a49SYabin Cui { size_t const avgChunkSize = CNBuffSize / 20;
1429*01826a49SYabin Cui size_t b;
1430*01826a49SYabin Cui for (b = 0; b < CNBuffSize; b += avgChunkSize) {
1431*01826a49SYabin Cui size_t const chunkSize = MIN(CNBuffSize - b, avgChunkSize);
1432*01826a49SYabin Cui RDG_genBuffer((char*)CNBuffer + b, chunkSize, compressibility, 0. /* auto */, seed);
1433*01826a49SYabin Cui } }
1434*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1435*01826a49SYabin Cui size_t const normalCSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
1436*01826a49SYabin Cui size_t const allowedExpansion = (CNBuffSize * 3 / 1000);
1437*01826a49SYabin Cui size_t superCSize;
1438*01826a49SYabin Cui CHECK_Z(normalCSize);
1439*01826a49SYabin Cui ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 19);
1440*01826a49SYabin Cui ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetCBlockSize, 1000);
1441*01826a49SYabin Cui superCSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
1442*01826a49SYabin Cui CHECK_Z(superCSize);
1443*01826a49SYabin Cui if (superCSize > normalCSize + allowedExpansion) {
1444*01826a49SYabin Cui DISPLAYLEVEL(1, "Superblock too big: %u > %u + %u \n", (U32)superCSize, (U32)normalCSize, (U32)allowedExpansion);
1445*01826a49SYabin Cui goto _output_error;
1446*01826a49SYabin Cui }
1447*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1448*01826a49SYabin Cui }
1449*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1450*01826a49SYabin Cui
1451*01826a49SYabin Cui RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0. /*auto*/, seed);
1452*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d: superblock enough room for checksum : ", testNb++)
1453*01826a49SYabin Cui /* This tests whether or not we leave enough room for the checksum at the end
1454*01826a49SYabin Cui * of the dst buffer. The bug that motivated this test was found by the
1455*01826a49SYabin Cui * stream_round_trip fuzzer but this crashes for the same reason and is
1456*01826a49SYabin Cui * far more compact than re-creating the stream_round_trip fuzzer's code path */
1457*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1458*01826a49SYabin Cui ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetCBlockSize, 64);
1459*01826a49SYabin Cui assert(!ZSTD_isError(ZSTD_compress2(cctx, compressedBuffer, 1339, CNBuffer, 1278)));
1460*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1461*01826a49SYabin Cui }
1462*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1463*01826a49SYabin Cui
1464*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress a NULL input with each level : ", testNb++);
1465*01826a49SYabin Cui { int level = -1;
1466*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1467*01826a49SYabin Cui if (!cctx) goto _output_error;
1468*01826a49SYabin Cui for (level = -1; level <= ZSTD_maxCLevel(); ++level) {
1469*01826a49SYabin Cui CHECK_Z( ZSTD_compress(compressedBuffer, compressedBufferSize, NULL, 0, level) );
1470*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, level) );
1471*01826a49SYabin Cui CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, NULL, 0) );
1472*01826a49SYabin Cui }
1473*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1474*01826a49SYabin Cui }
1475*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1476*01826a49SYabin Cui
1477*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : check CCtx size after compressing empty input : ", testNb++);
1478*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1479*01826a49SYabin Cui size_t const r = ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, NULL, 0, 19);
1480*01826a49SYabin Cui if (ZSTD_isError(r)) goto _output_error;
1481*01826a49SYabin Cui if (ZSTD_sizeof_CCtx(cctx) > (1U << 20)) goto _output_error;
1482*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1483*01826a49SYabin Cui cSize = r;
1484*01826a49SYabin Cui }
1485*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1486*01826a49SYabin Cui
1487*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : decompress empty frame into NULL : ", testNb++);
1488*01826a49SYabin Cui { size_t const r = ZSTD_decompress(NULL, 0, compressedBuffer, cSize);
1489*01826a49SYabin Cui if (ZSTD_isError(r)) goto _output_error;
1490*01826a49SYabin Cui if (r != 0) goto _output_error;
1491*01826a49SYabin Cui }
1492*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1493*01826a49SYabin Cui ZSTD_outBuffer output;
1494*01826a49SYabin Cui if (cctx==NULL) goto _output_error;
1495*01826a49SYabin Cui output.dst = compressedBuffer;
1496*01826a49SYabin Cui output.size = compressedBufferSize;
1497*01826a49SYabin Cui output.pos = 0;
1498*01826a49SYabin Cui CHECK_Z( ZSTD_initCStream(cctx, 1) ); /* content size unknown */
1499*01826a49SYabin Cui CHECK_Z( ZSTD_flushStream(cctx, &output) ); /* ensure no possibility to "concatenate" and determine the content size */
1500*01826a49SYabin Cui CHECK_Z( ZSTD_endStream(cctx, &output) );
1501*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1502*01826a49SYabin Cui /* single scan decompression */
1503*01826a49SYabin Cui { size_t const r = ZSTD_decompress(NULL, 0, compressedBuffer, output.pos);
1504*01826a49SYabin Cui if (ZSTD_isError(r)) goto _output_error;
1505*01826a49SYabin Cui if (r != 0) goto _output_error;
1506*01826a49SYabin Cui }
1507*01826a49SYabin Cui /* streaming decompression */
1508*01826a49SYabin Cui { ZSTD_DCtx* const dstream = ZSTD_createDStream();
1509*01826a49SYabin Cui ZSTD_inBuffer dinput;
1510*01826a49SYabin Cui ZSTD_outBuffer doutput;
1511*01826a49SYabin Cui size_t ipos;
1512*01826a49SYabin Cui if (dstream==NULL) goto _output_error;
1513*01826a49SYabin Cui dinput.src = compressedBuffer;
1514*01826a49SYabin Cui dinput.size = 0;
1515*01826a49SYabin Cui dinput.pos = 0;
1516*01826a49SYabin Cui doutput.dst = NULL;
1517*01826a49SYabin Cui doutput.size = 0;
1518*01826a49SYabin Cui doutput.pos = 0;
1519*01826a49SYabin Cui CHECK_Z ( ZSTD_initDStream(dstream) );
1520*01826a49SYabin Cui for (ipos=1; ipos<=output.pos; ipos++) {
1521*01826a49SYabin Cui dinput.size = ipos;
1522*01826a49SYabin Cui CHECK_Z ( ZSTD_decompressStream(dstream, &doutput, &dinput) );
1523*01826a49SYabin Cui }
1524*01826a49SYabin Cui if (doutput.pos != 0) goto _output_error;
1525*01826a49SYabin Cui ZSTD_freeDStream(dstream);
1526*01826a49SYabin Cui }
1527*01826a49SYabin Cui }
1528*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1529*01826a49SYabin Cui
1530*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : reuse CCtx with expanding block size : ", testNb++);
1531*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1532*01826a49SYabin Cui ZSTD_parameters const params = ZSTD_getParams(1, ZSTD_CONTENTSIZE_UNKNOWN, 0);
1533*01826a49SYabin Cui assert(params.fParams.contentSizeFlag == 1); /* block size will be adapted if pledgedSrcSize is enabled */
1534*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin_advanced(cctx, NULL, 0, params, 1 /*pledgedSrcSize*/) );
1535*01826a49SYabin Cui CHECK_Z( ZSTD_compressEnd(cctx, compressedBuffer, compressedBufferSize, CNBuffer, 1) ); /* creates a block size of 1 */
1536*01826a49SYabin Cui
1537*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin_advanced(cctx, NULL, 0, params, ZSTD_CONTENTSIZE_UNKNOWN) ); /* reuse same parameters */
1538*01826a49SYabin Cui { size_t const inSize = 2* 128 KB;
1539*01826a49SYabin Cui size_t const outSize = ZSTD_compressBound(inSize);
1540*01826a49SYabin Cui CHECK_Z( ZSTD_compressEnd(cctx, compressedBuffer, outSize, CNBuffer, inSize) );
1541*01826a49SYabin Cui /* will fail if blockSize is not resized */
1542*01826a49SYabin Cui }
1543*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1544*01826a49SYabin Cui }
1545*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1546*01826a49SYabin Cui
1547*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : re-using a CCtx should compress the same : ", testNb++);
1548*01826a49SYabin Cui { size_t const sampleSize = 30;
1549*01826a49SYabin Cui int i;
1550*01826a49SYabin Cui for (i=0; i<20; i++)
1551*01826a49SYabin Cui ((char*)CNBuffer)[i] = (char)i; /* ensure no match during initial section */
1552*01826a49SYabin Cui memcpy((char*)CNBuffer + 20, CNBuffer, 10); /* create one match, starting from beginning of sample, which is the difficult case (see #1241) */
1553*01826a49SYabin Cui for (i=1; i<=19; i++) {
1554*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1555*01826a49SYabin Cui size_t size1, size2;
1556*01826a49SYabin Cui DISPLAYLEVEL(5, "l%i ", i);
1557*01826a49SYabin Cui size1 = ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, CNBuffer, sampleSize, i);
1558*01826a49SYabin Cui CHECK_Z(size1);
1559*01826a49SYabin Cui
1560*01826a49SYabin Cui size2 = ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, CNBuffer, sampleSize, i);
1561*01826a49SYabin Cui CHECK_Z(size2);
1562*01826a49SYabin Cui CHECK_EQ(size1, size2);
1563*01826a49SYabin Cui
1564*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, i) );
1565*01826a49SYabin Cui size2 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, sampleSize);
1566*01826a49SYabin Cui CHECK_Z(size2);
1567*01826a49SYabin Cui CHECK_EQ(size1, size2);
1568*01826a49SYabin Cui
1569*01826a49SYabin Cui size2 = ZSTD_compress2(cctx, compressedBuffer, ZSTD_compressBound(sampleSize) - 1, CNBuffer, sampleSize); /* force streaming, as output buffer is not large enough to guarantee success */
1570*01826a49SYabin Cui CHECK_Z(size2);
1571*01826a49SYabin Cui CHECK_EQ(size1, size2);
1572*01826a49SYabin Cui
1573*01826a49SYabin Cui { ZSTD_inBuffer inb;
1574*01826a49SYabin Cui ZSTD_outBuffer outb;
1575*01826a49SYabin Cui inb.src = CNBuffer;
1576*01826a49SYabin Cui inb.pos = 0;
1577*01826a49SYabin Cui inb.size = sampleSize;
1578*01826a49SYabin Cui outb.dst = compressedBuffer;
1579*01826a49SYabin Cui outb.pos = 0;
1580*01826a49SYabin Cui outb.size = ZSTD_compressBound(sampleSize) - 1; /* force streaming, as output buffer is not large enough to guarantee success */
1581*01826a49SYabin Cui CHECK_Z( ZSTD_compressStream2(cctx, &outb, &inb, ZSTD_e_end) );
1582*01826a49SYabin Cui assert(inb.pos == inb.size);
1583*01826a49SYabin Cui CHECK_EQ(size1, outb.pos);
1584*01826a49SYabin Cui }
1585*01826a49SYabin Cui
1586*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1587*01826a49SYabin Cui }
1588*01826a49SYabin Cui }
1589*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1590*01826a49SYabin Cui
1591*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : btultra2 & 1st block : ", testNb++);
1592*01826a49SYabin Cui { size_t const sampleSize = 1024;
1593*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1594*01826a49SYabin Cui ZSTD_inBuffer inb;
1595*01826a49SYabin Cui ZSTD_outBuffer outb;
1596*01826a49SYabin Cui inb.src = CNBuffer;
1597*01826a49SYabin Cui inb.pos = 0;
1598*01826a49SYabin Cui inb.size = 0;
1599*01826a49SYabin Cui outb.dst = compressedBuffer;
1600*01826a49SYabin Cui outb.pos = 0;
1601*01826a49SYabin Cui outb.size = compressedBufferSize;
1602*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, ZSTD_maxCLevel()) );
1603*01826a49SYabin Cui
1604*01826a49SYabin Cui inb.size = sampleSize; /* start with something, so that context is already used */
1605*01826a49SYabin Cui CHECK_Z( ZSTD_compressStream2(cctx, &outb, &inb, ZSTD_e_end) ); /* will break internal assert if stats_init is not disabled */
1606*01826a49SYabin Cui assert(inb.pos == inb.size);
1607*01826a49SYabin Cui outb.pos = 0; /* cancel output */
1608*01826a49SYabin Cui
1609*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(cctx, sampleSize) );
1610*01826a49SYabin Cui inb.size = 4; /* too small size : compression will be skipped */
1611*01826a49SYabin Cui inb.pos = 0;
1612*01826a49SYabin Cui CHECK_Z( ZSTD_compressStream2(cctx, &outb, &inb, ZSTD_e_flush) );
1613*01826a49SYabin Cui assert(inb.pos == inb.size);
1614*01826a49SYabin Cui
1615*01826a49SYabin Cui inb.size += 5; /* too small size : compression will be skipped */
1616*01826a49SYabin Cui CHECK_Z( ZSTD_compressStream2(cctx, &outb, &inb, ZSTD_e_flush) );
1617*01826a49SYabin Cui assert(inb.pos == inb.size);
1618*01826a49SYabin Cui
1619*01826a49SYabin Cui inb.size += 11; /* small enough to attempt compression */
1620*01826a49SYabin Cui CHECK_Z( ZSTD_compressStream2(cctx, &outb, &inb, ZSTD_e_flush) );
1621*01826a49SYabin Cui assert(inb.pos == inb.size);
1622*01826a49SYabin Cui
1623*01826a49SYabin Cui assert(inb.pos < sampleSize);
1624*01826a49SYabin Cui inb.size = sampleSize; /* large enough to trigger stats_init, but no longer at beginning */
1625*01826a49SYabin Cui CHECK_Z( ZSTD_compressStream2(cctx, &outb, &inb, ZSTD_e_end) ); /* will break internal assert if stats_init is not disabled */
1626*01826a49SYabin Cui assert(inb.pos == inb.size);
1627*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1628*01826a49SYabin Cui }
1629*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1630*01826a49SYabin Cui
1631*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : ZSTD_CCtx_getParameter() : ", testNb++);
1632*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1633*01826a49SYabin Cui ZSTD_outBuffer out = {NULL, 0, 0};
1634*01826a49SYabin Cui ZSTD_inBuffer in = {NULL, 0, 0};
1635*01826a49SYabin Cui int value;
1636*01826a49SYabin Cui
1637*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_compressionLevel, &value));
1638*01826a49SYabin Cui CHECK_EQ(value, 3);
1639*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
1640*01826a49SYabin Cui CHECK_EQ(value, 0);
1641*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_hashLog, ZSTD_HASHLOG_MIN));
1642*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_compressionLevel, &value));
1643*01826a49SYabin Cui CHECK_EQ(value, 3);
1644*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
1645*01826a49SYabin Cui CHECK_EQ(value, ZSTD_HASHLOG_MIN);
1646*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 7));
1647*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_compressionLevel, &value));
1648*01826a49SYabin Cui CHECK_EQ(value, 7);
1649*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
1650*01826a49SYabin Cui CHECK_EQ(value, ZSTD_HASHLOG_MIN);
1651*01826a49SYabin Cui /* Start a compression job */
1652*01826a49SYabin Cui ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue);
1653*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_compressionLevel, &value));
1654*01826a49SYabin Cui CHECK_EQ(value, 7);
1655*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
1656*01826a49SYabin Cui CHECK_EQ(value, ZSTD_HASHLOG_MIN);
1657*01826a49SYabin Cui /* Reset the CCtx */
1658*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
1659*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_compressionLevel, &value));
1660*01826a49SYabin Cui CHECK_EQ(value, 7);
1661*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
1662*01826a49SYabin Cui CHECK_EQ(value, ZSTD_HASHLOG_MIN);
1663*01826a49SYabin Cui /* Reset the parameters */
1664*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_parameters);
1665*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_compressionLevel, &value));
1666*01826a49SYabin Cui CHECK_EQ(value, 3);
1667*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
1668*01826a49SYabin Cui CHECK_EQ(value, 0);
1669*01826a49SYabin Cui
1670*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1671*01826a49SYabin Cui }
1672*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1673*01826a49SYabin Cui
1674*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : ZSTD_CCtx_setCParams() : ", testNb++);
1675*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1676*01826a49SYabin Cui int value;
1677*01826a49SYabin Cui ZSTD_compressionParameters cparams = ZSTD_getCParams(1, 0, 0);
1678*01826a49SYabin Cui cparams.strategy = -1;
1679*01826a49SYabin Cui /* Set invalid cParams == no change. */
1680*01826a49SYabin Cui CHECK(ZSTD_isError(ZSTD_CCtx_setCParams(cctx, cparams)));
1681*01826a49SYabin Cui
1682*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_windowLog, &value));
1683*01826a49SYabin Cui CHECK_EQ(value, 0);
1684*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_chainLog, &value));
1685*01826a49SYabin Cui CHECK_EQ(value, 0);
1686*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
1687*01826a49SYabin Cui CHECK_EQ(value, 0);
1688*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_searchLog, &value));
1689*01826a49SYabin Cui CHECK_EQ(value, 0);
1690*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_minMatch, &value));
1691*01826a49SYabin Cui CHECK_EQ(value, 0);
1692*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_targetLength, &value));
1693*01826a49SYabin Cui CHECK_EQ(value, 0);
1694*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_strategy, &value));
1695*01826a49SYabin Cui CHECK_EQ(value, 0);
1696*01826a49SYabin Cui
1697*01826a49SYabin Cui cparams = ZSTD_getCParams(12, 0, 0);
1698*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setCParams(cctx, cparams));
1699*01826a49SYabin Cui
1700*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_windowLog, &value));
1701*01826a49SYabin Cui CHECK_EQ(value, (int)cparams.windowLog);
1702*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_chainLog, &value));
1703*01826a49SYabin Cui CHECK_EQ(value, (int)cparams.chainLog);
1704*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
1705*01826a49SYabin Cui CHECK_EQ(value, (int)cparams.hashLog);
1706*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_searchLog, &value));
1707*01826a49SYabin Cui CHECK_EQ(value, (int)cparams.searchLog);
1708*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_minMatch, &value));
1709*01826a49SYabin Cui CHECK_EQ(value, (int)cparams.minMatch);
1710*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_targetLength, &value));
1711*01826a49SYabin Cui CHECK_EQ(value, (int)cparams.targetLength);
1712*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_strategy, &value));
1713*01826a49SYabin Cui CHECK_EQ(value, (int)cparams.strategy);
1714*01826a49SYabin Cui
1715*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1716*01826a49SYabin Cui }
1717*01826a49SYabin Cui
1718*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : ZSTD_CCtx_setFParams() : ", testNb++);
1719*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1720*01826a49SYabin Cui int value;
1721*01826a49SYabin Cui ZSTD_frameParameters fparams = {0, 1, 1};
1722*01826a49SYabin Cui
1723*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_contentSizeFlag, &value));
1724*01826a49SYabin Cui CHECK_EQ(value, 1);
1725*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_checksumFlag, &value));
1726*01826a49SYabin Cui CHECK_EQ(value, 0);
1727*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_dictIDFlag, &value));
1728*01826a49SYabin Cui CHECK_EQ(value, 1);
1729*01826a49SYabin Cui
1730*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setFParams(cctx, fparams));
1731*01826a49SYabin Cui
1732*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_contentSizeFlag, &value));
1733*01826a49SYabin Cui CHECK_EQ(value, fparams.contentSizeFlag);
1734*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_checksumFlag, &value));
1735*01826a49SYabin Cui CHECK_EQ(value, fparams.checksumFlag);
1736*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_dictIDFlag, &value));
1737*01826a49SYabin Cui CHECK_EQ(value, !fparams.noDictIDFlag);
1738*01826a49SYabin Cui
1739*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1740*01826a49SYabin Cui }
1741*01826a49SYabin Cui
1742*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : ZSTD_CCtx_setCarams() : ", testNb++);
1743*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1744*01826a49SYabin Cui int value;
1745*01826a49SYabin Cui ZSTD_parameters params = ZSTD_getParams(1, 0, 0);
1746*01826a49SYabin Cui params.cParams.strategy = -1;
1747*01826a49SYabin Cui /* Set invalid params == no change. */
1748*01826a49SYabin Cui CHECK(ZSTD_isError(ZSTD_CCtx_setParams(cctx, params)));
1749*01826a49SYabin Cui
1750*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_windowLog, &value));
1751*01826a49SYabin Cui CHECK_EQ(value, 0);
1752*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_chainLog, &value));
1753*01826a49SYabin Cui CHECK_EQ(value, 0);
1754*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
1755*01826a49SYabin Cui CHECK_EQ(value, 0);
1756*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_searchLog, &value));
1757*01826a49SYabin Cui CHECK_EQ(value, 0);
1758*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_minMatch, &value));
1759*01826a49SYabin Cui CHECK_EQ(value, 0);
1760*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_targetLength, &value));
1761*01826a49SYabin Cui CHECK_EQ(value, 0);
1762*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_strategy, &value));
1763*01826a49SYabin Cui CHECK_EQ(value, 0);
1764*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_contentSizeFlag, &value));
1765*01826a49SYabin Cui CHECK_EQ(value, 1);
1766*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_checksumFlag, &value));
1767*01826a49SYabin Cui CHECK_EQ(value, 0);
1768*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_dictIDFlag, &value));
1769*01826a49SYabin Cui CHECK_EQ(value, 1);
1770*01826a49SYabin Cui
1771*01826a49SYabin Cui params = ZSTD_getParams(12, 0, 0);
1772*01826a49SYabin Cui params.fParams.contentSizeFlag = 0;
1773*01826a49SYabin Cui params.fParams.checksumFlag = 1;
1774*01826a49SYabin Cui params.fParams.noDictIDFlag = 1;
1775*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParams(cctx, params));
1776*01826a49SYabin Cui
1777*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_windowLog, &value));
1778*01826a49SYabin Cui CHECK_EQ(value, (int)params.cParams.windowLog);
1779*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_chainLog, &value));
1780*01826a49SYabin Cui CHECK_EQ(value, (int)params.cParams.chainLog);
1781*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_hashLog, &value));
1782*01826a49SYabin Cui CHECK_EQ(value, (int)params.cParams.hashLog);
1783*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_searchLog, &value));
1784*01826a49SYabin Cui CHECK_EQ(value, (int)params.cParams.searchLog);
1785*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_minMatch, &value));
1786*01826a49SYabin Cui CHECK_EQ(value, (int)params.cParams.minMatch);
1787*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_targetLength, &value));
1788*01826a49SYabin Cui CHECK_EQ(value, (int)params.cParams.targetLength);
1789*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_strategy, &value));
1790*01826a49SYabin Cui CHECK_EQ(value, (int)params.cParams.strategy);
1791*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_contentSizeFlag, &value));
1792*01826a49SYabin Cui CHECK_EQ(value, params.fParams.contentSizeFlag);
1793*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_checksumFlag, &value));
1794*01826a49SYabin Cui CHECK_EQ(value, params.fParams.checksumFlag);
1795*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_dictIDFlag, &value));
1796*01826a49SYabin Cui CHECK_EQ(value, !params.fParams.noDictIDFlag);
1797*01826a49SYabin Cui
1798*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1799*01826a49SYabin Cui }
1800*01826a49SYabin Cui
1801*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : ldm conditionally enabled by default doesn't change cctx params: ", testNb++);
1802*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1803*01826a49SYabin Cui ZSTD_outBuffer out = {NULL, 0, 0};
1804*01826a49SYabin Cui ZSTD_inBuffer in = {NULL, 0, 0};
1805*01826a49SYabin Cui int value;
1806*01826a49SYabin Cui
1807*01826a49SYabin Cui /* Even if LDM will be enabled by default in the applied params (since wlog >= 27 and strategy >= btopt),
1808*01826a49SYabin Cui * we should not modify the actual parameter specified by the user within the CCtx
1809*01826a49SYabin Cui */
1810*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 27));
1811*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_strategy, ZSTD_btopt));
1812*01826a49SYabin Cui
1813*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue));
1814*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_c_enableLongDistanceMatching, &value));
1815*01826a49SYabin Cui CHECK_EQ(value, 0);
1816*01826a49SYabin Cui
1817*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1818*01826a49SYabin Cui }
1819*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1820*01826a49SYabin Cui
1821*01826a49SYabin Cui /* this test is really too long, and should be made faster */
1822*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : overflow protection with large windowLog : ", testNb++);
1823*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1824*01826a49SYabin Cui ZSTD_parameters params = ZSTD_getParams(-999, ZSTD_CONTENTSIZE_UNKNOWN, 0);
1825*01826a49SYabin Cui size_t const nbCompressions = ((1U << 31) / CNBuffSize) + 2; /* ensure U32 overflow protection is triggered */
1826*01826a49SYabin Cui size_t cnb;
1827*01826a49SYabin Cui assert(cctx != NULL);
1828*01826a49SYabin Cui params.fParams.contentSizeFlag = 0;
1829*01826a49SYabin Cui params.cParams.windowLog = ZSTD_WINDOWLOG_MAX;
1830*01826a49SYabin Cui for (cnb = 0; cnb < nbCompressions; ++cnb) {
1831*01826a49SYabin Cui DISPLAYLEVEL(6, "run %zu / %zu \n", cnb, nbCompressions);
1832*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin_advanced(cctx, NULL, 0, params, ZSTD_CONTENTSIZE_UNKNOWN) ); /* reuse same parameters */
1833*01826a49SYabin Cui CHECK_Z( ZSTD_compressEnd(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize) );
1834*01826a49SYabin Cui }
1835*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
1836*01826a49SYabin Cui }
1837*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1838*01826a49SYabin Cui
1839*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : size down context : ", testNb++);
1840*01826a49SYabin Cui { ZSTD_CCtx* const largeCCtx = ZSTD_createCCtx();
1841*01826a49SYabin Cui assert(largeCCtx != NULL);
1842*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin(largeCCtx, 19) ); /* streaming implies ZSTD_CONTENTSIZE_UNKNOWN, which maximizes memory usage */
1843*01826a49SYabin Cui CHECK_Z( ZSTD_compressEnd(largeCCtx, compressedBuffer, compressedBufferSize, CNBuffer, 1) );
1844*01826a49SYabin Cui { size_t const largeCCtxSize = ZSTD_sizeof_CCtx(largeCCtx); /* size of context must be measured after compression */
1845*01826a49SYabin Cui { ZSTD_CCtx* const smallCCtx = ZSTD_createCCtx();
1846*01826a49SYabin Cui assert(smallCCtx != NULL);
1847*01826a49SYabin Cui CHECK_Z(ZSTD_compressCCtx(smallCCtx, compressedBuffer, compressedBufferSize, CNBuffer, 1, 1));
1848*01826a49SYabin Cui { size_t const smallCCtxSize = ZSTD_sizeof_CCtx(smallCCtx);
1849*01826a49SYabin Cui DISPLAYLEVEL(5, "(large) %zuKB > 32*%zuKB (small) : ",
1850*01826a49SYabin Cui largeCCtxSize>>10, smallCCtxSize>>10);
1851*01826a49SYabin Cui assert(largeCCtxSize > 32* smallCCtxSize); /* note : "too large" definition is handled within zstd_compress.c .
1852*01826a49SYabin Cui * make this test case extreme, so that it doesn't depend on a possibly fluctuating definition */
1853*01826a49SYabin Cui }
1854*01826a49SYabin Cui ZSTD_freeCCtx(smallCCtx);
1855*01826a49SYabin Cui }
1856*01826a49SYabin Cui { U32 const maxNbAttempts = 1100; /* nb of usages before triggering size down is handled within zstd_compress.c.
1857*01826a49SYabin Cui * currently defined as 128x, but could be adjusted in the future.
1858*01826a49SYabin Cui * make this test long enough so that it's not too much tied to the current definition within zstd_compress.c */
1859*01826a49SYabin Cui unsigned u;
1860*01826a49SYabin Cui for (u=0; u<maxNbAttempts; u++) {
1861*01826a49SYabin Cui CHECK_Z(ZSTD_compressCCtx(largeCCtx, compressedBuffer, compressedBufferSize, CNBuffer, 1, 1));
1862*01826a49SYabin Cui if (ZSTD_sizeof_CCtx(largeCCtx) < largeCCtxSize) break; /* sized down */
1863*01826a49SYabin Cui }
1864*01826a49SYabin Cui DISPLAYLEVEL(5, "size down after %u attempts : ", u);
1865*01826a49SYabin Cui if (u==maxNbAttempts) goto _output_error; /* no sizedown happened */
1866*01826a49SYabin Cui }
1867*01826a49SYabin Cui }
1868*01826a49SYabin Cui ZSTD_freeCCtx(largeCCtx);
1869*01826a49SYabin Cui }
1870*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1871*01826a49SYabin Cui
1872*01826a49SYabin Cui /* Static CCtx tests */
1873*01826a49SYabin Cui #define STATIC_CCTX_LEVEL 4
1874*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : create static CCtx for level %u : ", testNb++, STATIC_CCTX_LEVEL);
1875*01826a49SYabin Cui { size_t const staticCStreamSize = ZSTD_estimateCStreamSize(STATIC_CCTX_LEVEL);
1876*01826a49SYabin Cui void* const staticCCtxBuffer = malloc(staticCStreamSize);
1877*01826a49SYabin Cui size_t const staticDCtxSize = ZSTD_estimateDCtxSize();
1878*01826a49SYabin Cui void* const staticDCtxBuffer = malloc(staticDCtxSize);
1879*01826a49SYabin Cui DISPLAYLEVEL(4, "CStream size = %u, ", (U32)staticCStreamSize);
1880*01826a49SYabin Cui if (staticCCtxBuffer==NULL || staticDCtxBuffer==NULL) {
1881*01826a49SYabin Cui free(staticCCtxBuffer);
1882*01826a49SYabin Cui free(staticDCtxBuffer);
1883*01826a49SYabin Cui DISPLAY("Not enough memory, aborting\n");
1884*01826a49SYabin Cui testResult = 1;
1885*01826a49SYabin Cui goto _end;
1886*01826a49SYabin Cui }
1887*01826a49SYabin Cui { size_t const smallInSize = 32 KB;
1888*01826a49SYabin Cui ZSTD_compressionParameters const cparams_small = ZSTD_getCParams(STATIC_CCTX_LEVEL, smallInSize, 0);
1889*01826a49SYabin Cui size_t const smallCCtxSize = ZSTD_estimateCCtxSize_usingCParams(cparams_small);
1890*01826a49SYabin Cui size_t const staticCCtxSize = ZSTD_estimateCCtxSize(STATIC_CCTX_LEVEL);
1891*01826a49SYabin Cui ZSTD_CCtx* staticCCtx = ZSTD_initStaticCCtx(staticCCtxBuffer, smallCCtxSize);
1892*01826a49SYabin Cui ZSTD_DCtx* const staticDCtx = ZSTD_initStaticDCtx(staticDCtxBuffer, staticDCtxSize);
1893*01826a49SYabin Cui DISPLAYLEVEL(4, "Full CCtx size = %u, ", (U32)staticCCtxSize);
1894*01826a49SYabin Cui DISPLAYLEVEL(4, "CCtx for 32 KB = %u, ", (U32)smallCCtxSize);
1895*01826a49SYabin Cui if ((staticCCtx==NULL) || (staticDCtx==NULL)) goto _output_error;
1896*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1897*01826a49SYabin Cui
1898*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress small input with small static CCtx : ", testNb++);
1899*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compressCCtx(staticCCtx,
1900*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
1901*01826a49SYabin Cui CNBuffer, smallInSize, STATIC_CCTX_LEVEL) );
1902*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n",
1903*01826a49SYabin Cui (unsigned)cSize, (double)cSize/smallInSize*100);
1904*01826a49SYabin Cui
1905*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress large input with small static CCtx (must fail) : ", testNb++);
1906*01826a49SYabin Cui { size_t const r = ZSTD_compressCCtx(staticCCtx,
1907*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
1908*01826a49SYabin Cui CNBuffer, CNBuffSize, STATIC_CCTX_LEVEL);
1909*01826a49SYabin Cui if (ZSTD_getErrorCode((size_t)r) != ZSTD_error_memory_allocation) goto _output_error;
1910*01826a49SYabin Cui }
1911*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1912*01826a49SYabin Cui
1913*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : resize context to full CCtx size : ", testNb++);
1914*01826a49SYabin Cui staticCCtx = ZSTD_initStaticCStream(staticCCtxBuffer, staticCCtxSize);
1915*01826a49SYabin Cui DISPLAYLEVEL(4, "staticCCtxBuffer = %p, staticCCtx = %p , ", staticCCtxBuffer, (void*)staticCCtx);
1916*01826a49SYabin Cui if (staticCCtx == NULL) goto _output_error;
1917*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1918*01826a49SYabin Cui
1919*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress large input with static CCtx : ", testNb++);
1920*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compressCCtx(staticCCtx,
1921*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
1922*01826a49SYabin Cui CNBuffer, CNBuffSize, STATIC_CCTX_LEVEL) );
1923*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n",
1924*01826a49SYabin Cui (unsigned)cSize, (double)cSize/CNBuffSize*100);
1925*01826a49SYabin Cui
1926*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress small input often enough to trigger context reduce : ", testNb++);
1927*01826a49SYabin Cui { int nbc;
1928*01826a49SYabin Cui assert(staticCCtxSize > smallCCtxSize * ZSTD_WORKSPACETOOLARGE_FACTOR); /* ensure size down scenario */
1929*01826a49SYabin Cui assert(CNBuffSize > smallInSize + ZSTD_WORKSPACETOOLARGE_MAXDURATION + 3);
1930*01826a49SYabin Cui for (nbc=0; nbc<ZSTD_WORKSPACETOOLARGE_MAXDURATION+2; nbc++) {
1931*01826a49SYabin Cui CHECK_Z(ZSTD_compressCCtx(staticCCtx,
1932*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
1933*01826a49SYabin Cui (char*)CNBuffer + nbc, smallInSize,
1934*01826a49SYabin Cui STATIC_CCTX_LEVEL) );
1935*01826a49SYabin Cui } }
1936*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n")
1937*01826a49SYabin Cui
1938*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : init CCtx for level %u : ", testNb++, STATIC_CCTX_LEVEL);
1939*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin(staticCCtx, STATIC_CCTX_LEVEL) );
1940*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1941*01826a49SYabin Cui
1942*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compression again with static CCtx : ", testNb++);
1943*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compressCCtx(staticCCtx,
1944*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
1945*01826a49SYabin Cui CNBuffer, CNBuffSize, STATIC_CCTX_LEVEL) );
1946*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n",
1947*01826a49SYabin Cui (unsigned)cSize, (double)cSize/CNBuffSize*100);
1948*01826a49SYabin Cui
1949*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : simple decompression test with static DCtx : ", testNb++);
1950*01826a49SYabin Cui { size_t const r = ZSTD_decompressDCtx(staticDCtx,
1951*01826a49SYabin Cui decodedBuffer, CNBuffSize,
1952*01826a49SYabin Cui compressedBuffer, cSize);
1953*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error; }
1954*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1955*01826a49SYabin Cui
1956*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
1957*01826a49SYabin Cui if (memcmp(decodedBuffer, CNBuffer, CNBuffSize)) goto _output_error;
1958*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1959*01826a49SYabin Cui
1960*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : init CCtx for too large level (must fail) : ", testNb++);
1961*01826a49SYabin Cui { size_t const r = ZSTD_compressBegin(staticCCtx, ZSTD_maxCLevel());
1962*01826a49SYabin Cui if (!ZSTD_isError(r)) goto _output_error; }
1963*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1964*01826a49SYabin Cui
1965*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : init CCtx for small level %u (should work again) : ", testNb++, 1);
1966*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin(staticCCtx, 1) );
1967*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1968*01826a49SYabin Cui
1969*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : use CStream on CCtx-sized static context (should fail) : ", testNb++);
1970*01826a49SYabin Cui CHECK_Z( ZSTD_initCStream(staticCCtx, STATIC_CCTX_LEVEL) ); /* note : doesn't allocate */
1971*01826a49SYabin Cui { ZSTD_outBuffer output = { compressedBuffer, compressedBufferSize, 0 };
1972*01826a49SYabin Cui ZSTD_inBuffer input = { CNBuffer, CNBuffSize, 0 };
1973*01826a49SYabin Cui size_t const r = ZSTD_compressStream(staticCCtx, &output, &input); /* now allocates, should fail */
1974*01826a49SYabin Cui if (!ZSTD_isError(r)) goto _output_error;
1975*01826a49SYabin Cui }
1976*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1977*01826a49SYabin Cui
1978*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : resize context to CStream size, then stream compress : ", testNb++);
1979*01826a49SYabin Cui staticCCtx = ZSTD_initStaticCStream(staticCCtxBuffer, staticCStreamSize);
1980*01826a49SYabin Cui assert(staticCCtx != NULL);
1981*01826a49SYabin Cui CHECK_Z( ZSTD_initCStream(staticCCtx, STATIC_CCTX_LEVEL) ); /* note : doesn't allocate */
1982*01826a49SYabin Cui { ZSTD_outBuffer output = { compressedBuffer, compressedBufferSize, 0 };
1983*01826a49SYabin Cui ZSTD_inBuffer input = { CNBuffer, CNBuffSize, 0 };
1984*01826a49SYabin Cui CHECK_Z( ZSTD_compressStream(staticCCtx, &output, &input) );
1985*01826a49SYabin Cui }
1986*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1987*01826a49SYabin Cui
1988*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : CStream for small level %u : ", testNb++, 1);
1989*01826a49SYabin Cui CHECK_Z( ZSTD_initCStream(staticCCtx, 1) ); /* note : doesn't allocate */
1990*01826a49SYabin Cui { ZSTD_outBuffer output = { compressedBuffer, compressedBufferSize, 0 };
1991*01826a49SYabin Cui ZSTD_inBuffer input = { CNBuffer, CNBuffSize, 0 };
1992*01826a49SYabin Cui CHECK_Z( ZSTD_compressStream(staticCCtx, &output, &input) );
1993*01826a49SYabin Cui }
1994*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
1995*01826a49SYabin Cui
1996*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : init static CStream with dictionary (should fail) : ", testNb++);
1997*01826a49SYabin Cui { size_t const r = ZSTD_initCStream_usingDict(staticCCtx, CNBuffer, 64 KB, 1);
1998*01826a49SYabin Cui if (!ZSTD_isError(r)) goto _output_error; }
1999*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2000*01826a49SYabin Cui
2001*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : use DStream on DCtx-sized static context (should fail) : ", testNb++);
2002*01826a49SYabin Cui CHECK_Z( ZSTD_initDStream(staticDCtx) );
2003*01826a49SYabin Cui { ZSTD_outBuffer output = { decodedBuffer, CNBuffSize, 0 };
2004*01826a49SYabin Cui ZSTD_inBuffer input = { compressedBuffer, ZSTD_FRAMEHEADERSIZE_MAX+1, 0 };
2005*01826a49SYabin Cui size_t const r = ZSTD_decompressStream(staticDCtx, &output, &input);
2006*01826a49SYabin Cui if (!ZSTD_isError(r)) goto _output_error;
2007*01826a49SYabin Cui }
2008*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2009*01826a49SYabin Cui
2010*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : test estimation functions with default cctx params : ", testNb++);
2011*01826a49SYabin Cui {
2012*01826a49SYabin Cui // Test ZSTD_estimateCCtxSize_usingCCtxParams
2013*01826a49SYabin Cui {
2014*01826a49SYabin Cui ZSTD_CCtx_params* params = ZSTD_createCCtxParams();
2015*01826a49SYabin Cui size_t const cctxSizeDefault = ZSTD_estimateCCtxSize_usingCCtxParams(params);
2016*01826a49SYabin Cui staticCCtx = ZSTD_initStaticCCtx(staticCCtxBuffer, cctxSizeDefault);
2017*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compressCCtx(staticCCtx,
2018*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2019*01826a49SYabin Cui CNBuffer, CNBuffSize, 3));
2020*01826a49SYabin Cui
2021*01826a49SYabin Cui {
2022*01826a49SYabin Cui size_t const r = ZSTD_decompressDCtx(staticDCtx,
2023*01826a49SYabin Cui decodedBuffer, CNBuffSize,
2024*01826a49SYabin Cui compressedBuffer, cSize);
2025*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error;
2026*01826a49SYabin Cui if (memcmp(decodedBuffer, CNBuffer, CNBuffSize)) goto _output_error;
2027*01826a49SYabin Cui }
2028*01826a49SYabin Cui ZSTD_freeCCtxParams(params);
2029*01826a49SYabin Cui }
2030*01826a49SYabin Cui
2031*01826a49SYabin Cui // Test ZSTD_estimateCStreamSize_usingCCtxParams
2032*01826a49SYabin Cui {
2033*01826a49SYabin Cui ZSTD_CCtx_params* params = ZSTD_createCCtxParams();
2034*01826a49SYabin Cui size_t const cctxSizeDefault = ZSTD_estimateCStreamSize_usingCCtxParams(params);
2035*01826a49SYabin Cui staticCCtx = ZSTD_initStaticCCtx(staticCCtxBuffer, cctxSizeDefault);
2036*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compressCCtx(staticCCtx,
2037*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2038*01826a49SYabin Cui CNBuffer, CNBuffSize, 3) );
2039*01826a49SYabin Cui
2040*01826a49SYabin Cui {
2041*01826a49SYabin Cui size_t const r = ZSTD_decompressDCtx(staticDCtx,
2042*01826a49SYabin Cui decodedBuffer, CNBuffSize,
2043*01826a49SYabin Cui compressedBuffer, cSize);
2044*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error;
2045*01826a49SYabin Cui if (memcmp(decodedBuffer, CNBuffer, CNBuffSize)) goto _output_error;
2046*01826a49SYabin Cui }
2047*01826a49SYabin Cui ZSTD_freeCCtxParams(params);
2048*01826a49SYabin Cui }
2049*01826a49SYabin Cui }
2050*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2051*01826a49SYabin Cui
2052*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : test estimation functions with maxBlockSize = 0 : ", testNb++);
2053*01826a49SYabin Cui {
2054*01826a49SYabin Cui // Test ZSTD_estimateCCtxSize_usingCCtxParams
2055*01826a49SYabin Cui {
2056*01826a49SYabin Cui ZSTD_CCtx_params* params = ZSTD_createCCtxParams();
2057*01826a49SYabin Cui size_t cctxSizeDefault;
2058*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(params, ZSTD_c_maxBlockSize, 0));
2059*01826a49SYabin Cui cctxSizeDefault = ZSTD_estimateCCtxSize_usingCCtxParams(params);
2060*01826a49SYabin Cui staticCCtx = ZSTD_initStaticCCtx(staticCCtxBuffer, cctxSizeDefault);
2061*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compressCCtx(staticCCtx,
2062*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2063*01826a49SYabin Cui CNBuffer, CNBuffSize, 3) );
2064*01826a49SYabin Cui
2065*01826a49SYabin Cui {
2066*01826a49SYabin Cui size_t const r = ZSTD_decompressDCtx(staticDCtx,
2067*01826a49SYabin Cui decodedBuffer, CNBuffSize,
2068*01826a49SYabin Cui compressedBuffer, cSize);
2069*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error;
2070*01826a49SYabin Cui if (memcmp(decodedBuffer, CNBuffer, CNBuffSize)) goto _output_error;
2071*01826a49SYabin Cui }
2072*01826a49SYabin Cui ZSTD_freeCCtxParams(params);
2073*01826a49SYabin Cui }
2074*01826a49SYabin Cui
2075*01826a49SYabin Cui // Test ZSTD_estimateCStreamSize_usingCCtxParams
2076*01826a49SYabin Cui {
2077*01826a49SYabin Cui ZSTD_CCtx_params* params = ZSTD_createCCtxParams();
2078*01826a49SYabin Cui size_t cctxSizeDefault;
2079*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(params, ZSTD_c_maxBlockSize, 0));
2080*01826a49SYabin Cui cctxSizeDefault = ZSTD_estimateCStreamSize_usingCCtxParams(params);
2081*01826a49SYabin Cui staticCCtx = ZSTD_initStaticCCtx(staticCCtxBuffer, cctxSizeDefault);
2082*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compressCCtx(staticCCtx,
2083*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2084*01826a49SYabin Cui CNBuffer, CNBuffSize, 3) );
2085*01826a49SYabin Cui
2086*01826a49SYabin Cui {
2087*01826a49SYabin Cui size_t const r = ZSTD_decompressDCtx(staticDCtx,
2088*01826a49SYabin Cui decodedBuffer, CNBuffSize,
2089*01826a49SYabin Cui compressedBuffer, cSize);
2090*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error;
2091*01826a49SYabin Cui if (memcmp(decodedBuffer, CNBuffer, CNBuffSize)) goto _output_error;
2092*01826a49SYabin Cui }
2093*01826a49SYabin Cui ZSTD_freeCCtxParams(params);
2094*01826a49SYabin Cui }
2095*01826a49SYabin Cui }
2096*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2097*01826a49SYabin Cui }
2098*01826a49SYabin Cui free(staticCCtxBuffer);
2099*01826a49SYabin Cui free(staticDCtxBuffer);
2100*01826a49SYabin Cui }
2101*01826a49SYabin Cui
2102*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Static context sizes for negative levels : ", testNb++);
2103*01826a49SYabin Cui { size_t const cctxSizeN1 = ZSTD_estimateCCtxSize(-1);
2104*01826a49SYabin Cui size_t const cctxSizeP1 = ZSTD_estimateCCtxSize(1);
2105*01826a49SYabin Cui size_t const cstreamSizeN1 = ZSTD_estimateCStreamSize(-1);
2106*01826a49SYabin Cui size_t const cstreamSizeP1 = ZSTD_estimateCStreamSize(1);
2107*01826a49SYabin Cui
2108*01826a49SYabin Cui if (!(0 < cctxSizeN1 && cctxSizeN1 <= cctxSizeP1)) goto _output_error;
2109*01826a49SYabin Cui if (!(0 < cstreamSizeN1 && cstreamSizeN1 <= cstreamSizeP1)) goto _output_error;
2110*01826a49SYabin Cui }
2111*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2112*01826a49SYabin Cui
2113*01826a49SYabin Cui
2114*01826a49SYabin Cui /* ZSTDMT simple MT compression test */
2115*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : create ZSTDMT CCtx : ", testNb++);
2116*01826a49SYabin Cui { ZSTD_CCtx* const mtctx = ZSTD_createCCtx();
2117*01826a49SYabin Cui if (mtctx==NULL) {
2118*01826a49SYabin Cui DISPLAY("mtctx : not enough memory, aborting \n");
2119*01826a49SYabin Cui testResult = 1;
2120*01826a49SYabin Cui goto _end;
2121*01826a49SYabin Cui }
2122*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(mtctx, ZSTD_c_nbWorkers, 2) );
2123*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(mtctx, ZSTD_c_compressionLevel, 1) );
2124*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2125*01826a49SYabin Cui
2126*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : compress %u bytes with 2 threads : ", testNb++, (unsigned)CNBuffSize);
2127*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compress2(mtctx,
2128*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2129*01826a49SYabin Cui CNBuffer, CNBuffSize) );
2130*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
2131*01826a49SYabin Cui
2132*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompressed size test : ", testNb++);
2133*01826a49SYabin Cui { unsigned long long const rSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
2134*01826a49SYabin Cui if (rSize != CNBuffSize) {
2135*01826a49SYabin Cui DISPLAY("ZSTD_getFrameContentSize incorrect : %u != %u \n", (unsigned)rSize, (unsigned)CNBuffSize);
2136*01826a49SYabin Cui goto _output_error;
2137*01826a49SYabin Cui } }
2138*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2139*01826a49SYabin Cui
2140*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, (unsigned)CNBuffSize);
2141*01826a49SYabin Cui { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
2142*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error; }
2143*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2144*01826a49SYabin Cui
2145*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
2146*01826a49SYabin Cui { size_t u;
2147*01826a49SYabin Cui for (u=0; u<CNBuffSize; u++) {
2148*01826a49SYabin Cui if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;
2149*01826a49SYabin Cui } }
2150*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2151*01826a49SYabin Cui
2152*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress -T2 with checksum : ", testNb++);
2153*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(mtctx, ZSTD_c_checksumFlag, 1) );
2154*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(mtctx, ZSTD_c_contentSizeFlag, 1) );
2155*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(mtctx, ZSTD_c_overlapLog, 3) );
2156*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compress2(mtctx,
2157*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2158*01826a49SYabin Cui CNBuffer, CNBuffSize) );
2159*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
2160*01826a49SYabin Cui
2161*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, (unsigned)CNBuffSize);
2162*01826a49SYabin Cui { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize);
2163*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error; }
2164*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2165*01826a49SYabin Cui
2166*01826a49SYabin Cui ZSTD_freeCCtx(mtctx);
2167*01826a49SYabin Cui }
2168*01826a49SYabin Cui
2169*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3u : compress empty string and decompress with small window log : ", testNb++);
2170*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
2171*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
2172*01826a49SYabin Cui char out[32];
2173*01826a49SYabin Cui if (cctx == NULL || dctx == NULL) goto _output_error;
2174*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_contentSizeFlag, 0) );
2175*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compress2(cctx, out, sizeof(out), NULL, 0) );
2176*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes)\n", (unsigned)cSize);
2177*01826a49SYabin Cui
2178*01826a49SYabin Cui CHECK_Z( ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, 10) );
2179*01826a49SYabin Cui { char const* outPtr = out;
2180*01826a49SYabin Cui ZSTD_inBuffer inBuffer = { outPtr, cSize, 0 };
2181*01826a49SYabin Cui ZSTD_outBuffer outBuffer = { NULL, 0, 0 };
2182*01826a49SYabin Cui size_t dSize;
2183*01826a49SYabin Cui CHECK_VAR(dSize, ZSTD_decompressStream(dctx, &outBuffer, &inBuffer) );
2184*01826a49SYabin Cui if (dSize != 0) goto _output_error;
2185*01826a49SYabin Cui }
2186*01826a49SYabin Cui
2187*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
2188*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
2189*01826a49SYabin Cui }
2190*01826a49SYabin Cui
2191*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress with block splitting : ", testNb++)
2192*01826a49SYabin Cui { ZSTD_CCtx* cctx = ZSTD_createCCtx();
2193*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_useBlockSplitter, ZSTD_ps_enable) );
2194*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
2195*01826a49SYabin Cui CHECK_Z(cSize);
2196*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
2197*01826a49SYabin Cui }
2198*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2199*01826a49SYabin Cui
2200*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress -T2 with/without literals compression : ", testNb++)
2201*01826a49SYabin Cui { ZSTD_CCtx* cctx = ZSTD_createCCtx();
2202*01826a49SYabin Cui size_t cSize1, cSize2;
2203*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 1) );
2204*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 2) );
2205*01826a49SYabin Cui cSize1 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
2206*01826a49SYabin Cui CHECK_Z(cSize1);
2207*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_literalCompressionMode, ZSTD_ps_disable) );
2208*01826a49SYabin Cui cSize2 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
2209*01826a49SYabin Cui CHECK_Z(cSize2);
2210*01826a49SYabin Cui CHECK_LT(cSize1, cSize2);
2211*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
2212*01826a49SYabin Cui }
2213*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2214*01826a49SYabin Cui
2215*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Multithreaded ZSTD_compress2() with rsyncable : ", testNb++)
2216*01826a49SYabin Cui { ZSTD_CCtx* cctx = ZSTD_createCCtx();
2217*01826a49SYabin Cui /* Set rsyncable and don't give the ZSTD_compressBound(CNBuffSize) so
2218*01826a49SYabin Cui * ZSTDMT is forced to not take the shortcut.
2219*01826a49SYabin Cui */
2220*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 1) );
2221*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 1) );
2222*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_rsyncable, 1) );
2223*01826a49SYabin Cui CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize - 1, CNBuffer, CNBuffSize) );
2224*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
2225*01826a49SYabin Cui }
2226*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2227*01826a49SYabin Cui
2228*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : setting multithreaded parameters : ", testNb++)
2229*01826a49SYabin Cui { ZSTD_CCtx_params* params = ZSTD_createCCtxParams();
2230*01826a49SYabin Cui int const jobSize = 512 KB;
2231*01826a49SYabin Cui int value;
2232*01826a49SYabin Cui /* Check that the overlap log and job size are unset. */
2233*01826a49SYabin Cui CHECK_Z( ZSTD_CCtxParams_getParameter(params, ZSTD_c_overlapLog, &value) );
2234*01826a49SYabin Cui CHECK_EQ(value, 0);
2235*01826a49SYabin Cui CHECK_Z( ZSTD_CCtxParams_getParameter(params, ZSTD_c_jobSize, &value) );
2236*01826a49SYabin Cui CHECK_EQ(value, 0);
2237*01826a49SYabin Cui /* Set and check the overlap log and job size. */
2238*01826a49SYabin Cui CHECK_Z( ZSTD_CCtxParams_setParameter(params, ZSTD_c_overlapLog, 5) );
2239*01826a49SYabin Cui CHECK_Z( ZSTD_CCtxParams_setParameter(params, ZSTD_c_jobSize, jobSize) );
2240*01826a49SYabin Cui CHECK_Z( ZSTD_CCtxParams_getParameter(params, ZSTD_c_overlapLog, &value) );
2241*01826a49SYabin Cui CHECK_EQ(value, 5);
2242*01826a49SYabin Cui CHECK_Z( ZSTD_CCtxParams_getParameter(params, ZSTD_c_jobSize, &value) );
2243*01826a49SYabin Cui CHECK_EQ(value, jobSize);
2244*01826a49SYabin Cui /* Set the number of workers and check the overlap log and job size. */
2245*01826a49SYabin Cui CHECK_Z( ZSTD_CCtxParams_setParameter(params, ZSTD_c_nbWorkers, 2) );
2246*01826a49SYabin Cui CHECK_Z( ZSTD_CCtxParams_getParameter(params, ZSTD_c_overlapLog, &value) );
2247*01826a49SYabin Cui CHECK_EQ(value, 5);
2248*01826a49SYabin Cui CHECK_Z( ZSTD_CCtxParams_getParameter(params, ZSTD_c_jobSize, &value) );
2249*01826a49SYabin Cui CHECK_EQ(value, jobSize);
2250*01826a49SYabin Cui ZSTD_freeCCtxParams(params);
2251*01826a49SYabin Cui }
2252*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2253*01826a49SYabin Cui
2254*01826a49SYabin Cui /* Simple API multiframe test */
2255*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress multiple frames : ", testNb++);
2256*01826a49SYabin Cui { size_t off = 0;
2257*01826a49SYabin Cui int i;
2258*01826a49SYabin Cui int const segs = 4;
2259*01826a49SYabin Cui /* only use the first half so we don't push against size limit of compressedBuffer */
2260*01826a49SYabin Cui size_t const segSize = (CNBuffSize / 2) / segs;
2261*01826a49SYabin Cui
2262*01826a49SYabin Cui const U32 skipLen = 129 KB;
2263*01826a49SYabin Cui char* const skipBuff = (char*)malloc(skipLen);
2264*01826a49SYabin Cui assert(skipBuff != NULL);
2265*01826a49SYabin Cui memset(skipBuff, 0, skipLen);
2266*01826a49SYabin Cui for (i = 0; i < segs; i++) {
2267*01826a49SYabin Cui CHECK_NEWV(r, ZSTD_compress(
2268*01826a49SYabin Cui (BYTE*)compressedBuffer + off, CNBuffSize - off,
2269*01826a49SYabin Cui (BYTE*)CNBuffer + segSize * (size_t)i, segSize,
2270*01826a49SYabin Cui 5) );
2271*01826a49SYabin Cui off += r;
2272*01826a49SYabin Cui if (i == segs/2) {
2273*01826a49SYabin Cui /* insert skippable frame */
2274*01826a49SYabin Cui size_t const skippableSize =
2275*01826a49SYabin Cui ZSTD_writeSkippableFrame((BYTE*)compressedBuffer + off, compressedBufferSize,
2276*01826a49SYabin Cui skipBuff, skipLen, seed % 15);
2277*01826a49SYabin Cui CHECK_Z(skippableSize);
2278*01826a49SYabin Cui off += skippableSize;
2279*01826a49SYabin Cui }
2280*01826a49SYabin Cui }
2281*01826a49SYabin Cui cSize = off;
2282*01826a49SYabin Cui free(skipBuff);
2283*01826a49SYabin Cui }
2284*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2285*01826a49SYabin Cui
2286*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : get decompressed size of multiple frames : ", testNb++);
2287*01826a49SYabin Cui { unsigned long long const r = ZSTD_findDecompressedSize(compressedBuffer, cSize);
2288*01826a49SYabin Cui if (r != CNBuffSize / 2) goto _output_error; }
2289*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2290*01826a49SYabin Cui
2291*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : get tight decompressed bound of multiple frames : ", testNb++);
2292*01826a49SYabin Cui { unsigned long long const bound = ZSTD_decompressBound(compressedBuffer, cSize);
2293*01826a49SYabin Cui if (bound != CNBuffSize / 2) goto _output_error; }
2294*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2295*01826a49SYabin Cui
2296*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress multiple frames : ", testNb++);
2297*01826a49SYabin Cui { CHECK_NEWV(r, ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize));
2298*01826a49SYabin Cui if (r != CNBuffSize / 2) goto _output_error; }
2299*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2300*01826a49SYabin Cui
2301*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
2302*01826a49SYabin Cui if (memcmp(decodedBuffer, CNBuffer, CNBuffSize / 2) != 0) goto _output_error;
2303*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2304*01826a49SYabin Cui
2305*01826a49SYabin Cui /* Simple API skippable frame test */
2306*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : read/write a skippable frame : ", testNb++);
2307*01826a49SYabin Cui { U32 i;
2308*01826a49SYabin Cui unsigned readMagic;
2309*01826a49SYabin Cui unsigned long long receivedSize;
2310*01826a49SYabin Cui size_t skippableSize;
2311*01826a49SYabin Cui const U32 skipLen = 129 KB;
2312*01826a49SYabin Cui char* const skipBuff = (char*)malloc(skipLen);
2313*01826a49SYabin Cui assert(skipBuff != NULL);
2314*01826a49SYabin Cui for (i = 0; i < skipLen; i++)
2315*01826a49SYabin Cui skipBuff[i] = (char) ((seed + i) % 256);
2316*01826a49SYabin Cui skippableSize = ZSTD_writeSkippableFrame(
2317*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2318*01826a49SYabin Cui skipBuff, skipLen, seed % 15);
2319*01826a49SYabin Cui CHECK_Z(skippableSize);
2320*01826a49SYabin Cui CHECK_EQ(1, ZSTD_isSkippableFrame(compressedBuffer, skippableSize));
2321*01826a49SYabin Cui receivedSize = ZSTD_readSkippableFrame(decodedBuffer, CNBuffSize, &readMagic, compressedBuffer, skippableSize);
2322*01826a49SYabin Cui CHECK_EQ(skippableSize, receivedSize + ZSTD_SKIPPABLEHEADERSIZE);
2323*01826a49SYabin Cui CHECK_EQ(seed % 15, readMagic);
2324*01826a49SYabin Cui if (memcmp(decodedBuffer, skipBuff, skipLen) != 0) goto _output_error;
2325*01826a49SYabin Cui
2326*01826a49SYabin Cui free(skipBuff);
2327*01826a49SYabin Cui }
2328*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2329*01826a49SYabin Cui
2330*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : read/write an empty skippable frame : ", testNb++);
2331*01826a49SYabin Cui {
2332*01826a49SYabin Cui unsigned readMagic;
2333*01826a49SYabin Cui unsigned long long receivedSize;
2334*01826a49SYabin Cui size_t skippableSize;
2335*01826a49SYabin Cui skippableSize = ZSTD_writeSkippableFrame(
2336*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2337*01826a49SYabin Cui CNBuffer, 0, seed % 15);
2338*01826a49SYabin Cui CHECK_EQ(ZSTD_SKIPPABLEHEADERSIZE, skippableSize);
2339*01826a49SYabin Cui CHECK_EQ(1, ZSTD_isSkippableFrame(compressedBuffer, skippableSize));
2340*01826a49SYabin Cui receivedSize = ZSTD_readSkippableFrame(NULL, 0, &readMagic, compressedBuffer, skippableSize);
2341*01826a49SYabin Cui CHECK_EQ(skippableSize, receivedSize + ZSTD_SKIPPABLEHEADERSIZE);
2342*01826a49SYabin Cui CHECK_EQ(seed % 15, readMagic);
2343*01826a49SYabin Cui }
2344*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2345*01826a49SYabin Cui
2346*01826a49SYabin Cui /* Dictionary and CCtx Duplication tests */
2347*01826a49SYabin Cui { ZSTD_CCtx* const ctxOrig = ZSTD_createCCtx();
2348*01826a49SYabin Cui ZSTD_CCtx* const ctxDuplicated = ZSTD_createCCtx();
2349*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
2350*01826a49SYabin Cui static const size_t dictSize = 551;
2351*01826a49SYabin Cui assert(dctx != NULL); assert(ctxOrig != NULL); assert(ctxDuplicated != NULL);
2352*01826a49SYabin Cui
2353*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : copy context too soon : ", testNb++);
2354*01826a49SYabin Cui { size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0);
2355*01826a49SYabin Cui if (!ZSTD_isError(copyResult)) goto _output_error; } /* error must be detected */
2356*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2357*01826a49SYabin Cui
2358*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : load dictionary into context : ", testNb++);
2359*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2) );
2360*01826a49SYabin Cui CHECK_Z( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0) ); /* Begin_usingDict implies unknown srcSize, so match that */
2361*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2362*01826a49SYabin Cui
2363*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress with flat dictionary : ", testNb++);
2364*01826a49SYabin Cui cSize = 0;
2365*01826a49SYabin Cui CHECKPLUS(r, ZSTD_compressEnd(ctxOrig,
2366*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2367*01826a49SYabin Cui (const char*)CNBuffer + dictSize, CNBuffSize - dictSize),
2368*01826a49SYabin Cui cSize += r);
2369*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
2370*01826a49SYabin Cui
2371*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : frame built with flat dictionary should be decompressible : ", testNb++);
2372*01826a49SYabin Cui CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
2373*01826a49SYabin Cui decodedBuffer, CNBuffSize,
2374*01826a49SYabin Cui compressedBuffer, cSize,
2375*01826a49SYabin Cui CNBuffer, dictSize),
2376*01826a49SYabin Cui if (r != CNBuffSize - dictSize) goto _output_error);
2377*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2378*01826a49SYabin Cui
2379*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress with duplicated context : ", testNb++);
2380*01826a49SYabin Cui { size_t const cSizeOrig = cSize;
2381*01826a49SYabin Cui cSize = 0;
2382*01826a49SYabin Cui CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated,
2383*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2384*01826a49SYabin Cui (const char*)CNBuffer + dictSize, CNBuffSize - dictSize),
2385*01826a49SYabin Cui cSize += r);
2386*01826a49SYabin Cui if (cSize != cSizeOrig) goto _output_error; /* should be identical ==> same size */
2387*01826a49SYabin Cui }
2388*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
2389*01826a49SYabin Cui
2390*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : frame built with duplicated context should be decompressible : ", testNb++);
2391*01826a49SYabin Cui CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
2392*01826a49SYabin Cui decodedBuffer, CNBuffSize,
2393*01826a49SYabin Cui compressedBuffer, cSize,
2394*01826a49SYabin Cui CNBuffer, dictSize),
2395*01826a49SYabin Cui if (r != CNBuffSize - dictSize) goto _output_error);
2396*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2397*01826a49SYabin Cui
2398*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress with DDict : ", testNb++);
2399*01826a49SYabin Cui { ZSTD_DDict* const ddict = ZSTD_createDDict(CNBuffer, dictSize);
2400*01826a49SYabin Cui size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict);
2401*01826a49SYabin Cui if (r != CNBuffSize - dictSize) goto _output_error;
2402*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (size of DDict : %u) \n", (unsigned)ZSTD_sizeof_DDict(ddict));
2403*01826a49SYabin Cui ZSTD_freeDDict(ddict);
2404*01826a49SYabin Cui }
2405*01826a49SYabin Cui
2406*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress with static DDict : ", testNb++);
2407*01826a49SYabin Cui { size_t const ddictBufferSize = ZSTD_estimateDDictSize(dictSize, ZSTD_dlm_byCopy);
2408*01826a49SYabin Cui void* const ddictBuffer = malloc(ddictBufferSize);
2409*01826a49SYabin Cui if (ddictBuffer == NULL) goto _output_error;
2410*01826a49SYabin Cui { const ZSTD_DDict* const ddict = ZSTD_initStaticDDict(ddictBuffer, ddictBufferSize, CNBuffer, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto);
2411*01826a49SYabin Cui size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict);
2412*01826a49SYabin Cui if (r != CNBuffSize - dictSize) goto _output_error;
2413*01826a49SYabin Cui }
2414*01826a49SYabin Cui free(ddictBuffer);
2415*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (size of static DDict : %u) \n", (unsigned)ddictBufferSize);
2416*01826a49SYabin Cui }
2417*01826a49SYabin Cui
2418*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : check content size on duplicated context : ", testNb++);
2419*01826a49SYabin Cui { size_t const testSize = CNBuffSize / 3;
2420*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin(ctxOrig, ZSTD_defaultCLevel()) );
2421*01826a49SYabin Cui CHECK_Z( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, testSize) );
2422*01826a49SYabin Cui
2423*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(testSize),
2424*01826a49SYabin Cui (const char*)CNBuffer + dictSize, testSize) );
2425*01826a49SYabin Cui { ZSTD_frameHeader zfh;
2426*01826a49SYabin Cui if (ZSTD_getFrameHeader(&zfh, compressedBuffer, cSize)) goto _output_error;
2427*01826a49SYabin Cui if ((zfh.frameContentSize != testSize) && (zfh.frameContentSize != 0)) goto _output_error;
2428*01826a49SYabin Cui } }
2429*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2430*01826a49SYabin Cui
2431*01826a49SYabin Cui #if !defined(ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR) \
2432*01826a49SYabin Cui && !defined(ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR) \
2433*01826a49SYabin Cui && !defined(ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR) \
2434*01826a49SYabin Cui && !defined(ZSTD_EXCLUDE_LAZY_BLOCK_COMPRESSOR) \
2435*01826a49SYabin Cui && !defined(ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR) \
2436*01826a49SYabin Cui && !defined(ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR) \
2437*01826a49SYabin Cui && !defined(ZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR) \
2438*01826a49SYabin Cui && !defined(ZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR)
2439*01826a49SYabin Cui /* Note : these tests should be replaced by proper regression tests,
2440*01826a49SYabin Cui * but existing ones do not focus on small data + dictionary + all levels.
2441*01826a49SYabin Cui */
2442*01826a49SYabin Cui if ((int)(compressibility * 100 + 0.1) == FUZ_compressibility_default) { /* test only valid with known input */
2443*01826a49SYabin Cui size_t const flatdictSize = 22 KB;
2444*01826a49SYabin Cui size_t const contentSize = 9 KB;
2445*01826a49SYabin Cui const void* const dict = (const char*)CNBuffer;
2446*01826a49SYabin Cui const void* const contentStart = (const char*)dict + flatdictSize;
2447*01826a49SYabin Cui /* These upper bounds are generally within a few bytes of the compressed size */
2448*01826a49SYabin Cui size_t target_nodict_cSize[22+1] = { 3840, 3770, 3870, 3830, 3770,
2449*01826a49SYabin Cui 3770, 3770, 3770, 3750, 3750,
2450*01826a49SYabin Cui 3742, 3675, 3674, 3665, 3664,
2451*01826a49SYabin Cui 3663, 3662, 3661, 3660, 3660,
2452*01826a49SYabin Cui 3660, 3660, 3660 };
2453*01826a49SYabin Cui size_t const target_wdict_cSize[22+1] = { 2830, 2896, 2893, 2820, 2940,
2454*01826a49SYabin Cui 2950, 2950, 2925, 2900, 2892,
2455*01826a49SYabin Cui 2910, 2910, 2910, 2780, 2775,
2456*01826a49SYabin Cui 2765, 2760, 2755, 2754, 2753,
2457*01826a49SYabin Cui 2753, 2753, 2753 };
2458*01826a49SYabin Cui int l = 1;
2459*01826a49SYabin Cui int const maxLevel = ZSTD_maxCLevel();
2460*01826a49SYabin Cui /* clevels with strategies that support rowhash on small inputs */
2461*01826a49SYabin Cui int rowLevel = 4;
2462*01826a49SYabin Cui int const rowLevelEnd = 8;
2463*01826a49SYabin Cui
2464*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : flat-dictionary efficiency test : \n", testNb++);
2465*01826a49SYabin Cui assert(maxLevel == 22);
2466*01826a49SYabin Cui RDG_genBuffer(CNBuffer, flatdictSize + contentSize, compressibility, 0., seed);
2467*01826a49SYabin Cui DISPLAYLEVEL(4, "content hash : %016llx; dict hash : %016llx \n",
2468*01826a49SYabin Cui (unsigned long long)XXH64(contentStart, contentSize, 0),
2469*01826a49SYabin Cui (unsigned long long)XXH64(dict, flatdictSize, 0));
2470*01826a49SYabin Cui
2471*01826a49SYabin Cui for ( ; l <= maxLevel; l++) {
2472*01826a49SYabin Cui size_t const nodict_cSize = ZSTD_compress(compressedBuffer, compressedBufferSize,
2473*01826a49SYabin Cui contentStart, contentSize, l);
2474*01826a49SYabin Cui if (nodict_cSize > target_nodict_cSize[l]) {
2475*01826a49SYabin Cui DISPLAYLEVEL(1, "error : compression at level %i worse than expected (%u > %u) \n",
2476*01826a49SYabin Cui l, (unsigned)nodict_cSize, (unsigned)target_nodict_cSize[l]);
2477*01826a49SYabin Cui goto _output_error;
2478*01826a49SYabin Cui }
2479*01826a49SYabin Cui DISPLAYLEVEL(4, "level %i : max expected %u >= reached %u \n",
2480*01826a49SYabin Cui l, (unsigned)target_nodict_cSize[l], (unsigned)nodict_cSize);
2481*01826a49SYabin Cui }
2482*01826a49SYabin Cui for ( l=1 ; l <= maxLevel; l++) {
2483*01826a49SYabin Cui size_t const wdict_cSize = ZSTD_compress_usingDict(ctxOrig,
2484*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2485*01826a49SYabin Cui contentStart, contentSize,
2486*01826a49SYabin Cui dict, flatdictSize,
2487*01826a49SYabin Cui l);
2488*01826a49SYabin Cui if (wdict_cSize > target_wdict_cSize[l]) {
2489*01826a49SYabin Cui DISPLAYLEVEL(1, "error : compression with dictionary at level %i worse than expected (%u > %u) \n",
2490*01826a49SYabin Cui l, (unsigned)wdict_cSize, (unsigned)target_wdict_cSize[l]);
2491*01826a49SYabin Cui goto _output_error;
2492*01826a49SYabin Cui }
2493*01826a49SYabin Cui DISPLAYLEVEL(4, "level %i with dictionary : max expected %u >= reached %u \n",
2494*01826a49SYabin Cui l, (unsigned)target_wdict_cSize[l], (unsigned)wdict_cSize);
2495*01826a49SYabin Cui }
2496*01826a49SYabin Cui /* Compression with ZSTD_compress2 and row match finder force enabled.
2497*01826a49SYabin Cui * Give some slack for force-enabled row matchfinder since we're on a small input (9KB)
2498*01826a49SYabin Cui */
2499*01826a49SYabin Cui for ( ; rowLevel <= rowLevelEnd; ++rowLevel) target_nodict_cSize[rowLevel] += 5;
2500*01826a49SYabin Cui for (l=1 ; l <= maxLevel; l++) {
2501*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
2502*01826a49SYabin Cui size_t nodict_cSize;
2503*01826a49SYabin Cui ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, l);
2504*01826a49SYabin Cui ZSTD_CCtx_setParameter(cctx, ZSTD_c_useRowMatchFinder, ZSTD_ps_enable);
2505*01826a49SYabin Cui nodict_cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize,
2506*01826a49SYabin Cui contentStart, contentSize);
2507*01826a49SYabin Cui if (nodict_cSize > target_nodict_cSize[l]) {
2508*01826a49SYabin Cui DISPLAYLEVEL(1, "error : compression with compress2 at level %i worse than expected (%u > %u) \n",
2509*01826a49SYabin Cui l, (unsigned)nodict_cSize, (unsigned)target_nodict_cSize[l]);
2510*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
2511*01826a49SYabin Cui goto _output_error;
2512*01826a49SYabin Cui }
2513*01826a49SYabin Cui DISPLAYLEVEL(4, "level %i with compress2 : max expected %u >= reached %u \n",
2514*01826a49SYabin Cui l, (unsigned)target_nodict_cSize[l], (unsigned)nodict_cSize);
2515*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
2516*01826a49SYabin Cui }
2517*01826a49SYabin Cui /* Dict compression with DMS */
2518*01826a49SYabin Cui for ( l=1 ; l <= maxLevel; l++) {
2519*01826a49SYabin Cui size_t wdict_cSize;
2520*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_loadDictionary(ctxOrig, dict, flatdictSize) );
2521*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(ctxOrig, ZSTD_c_compressionLevel, l) );
2522*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(ctxOrig, ZSTD_c_enableDedicatedDictSearch, 0) );
2523*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(ctxOrig, ZSTD_c_forceAttachDict, ZSTD_dictForceAttach) );
2524*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(ctxOrig, ZSTD_c_prefetchCDictTables, seed % 3) );
2525*01826a49SYabin Cui wdict_cSize = ZSTD_compress2(ctxOrig, compressedBuffer, compressedBufferSize, contentStart, contentSize);
2526*01826a49SYabin Cui if (wdict_cSize > target_wdict_cSize[l]) {
2527*01826a49SYabin Cui DISPLAYLEVEL(1, "error : compression with dictionary and compress2 at level %i worse than expected (%u > %u) \n",
2528*01826a49SYabin Cui l, (unsigned)wdict_cSize, (unsigned)target_wdict_cSize[l]);
2529*01826a49SYabin Cui goto _output_error;
2530*01826a49SYabin Cui }
2531*01826a49SYabin Cui DISPLAYLEVEL(4, "level %i with dictionary and compress2 : max expected %u >= reached %u \n",
2532*01826a49SYabin Cui l, (unsigned)target_wdict_cSize[l], (unsigned)wdict_cSize);
2533*01826a49SYabin Cui }
2534*01826a49SYabin Cui
2535*01826a49SYabin Cui DISPLAYLEVEL(4, "compression efficiency tests OK \n");
2536*01826a49SYabin Cui }
2537*01826a49SYabin Cui #endif
2538*01826a49SYabin Cui
2539*01826a49SYabin Cui ZSTD_freeCCtx(ctxOrig);
2540*01826a49SYabin Cui ZSTD_freeCCtx(ctxDuplicated);
2541*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
2542*01826a49SYabin Cui }
2543*01826a49SYabin Cui
2544*01826a49SYabin Cui /* Dictionary and dictBuilder tests */
2545*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
2546*01826a49SYabin Cui size_t const dictBufferCapacity = 16 KB;
2547*01826a49SYabin Cui void* const dictBuffer = malloc(dictBufferCapacity);
2548*01826a49SYabin Cui size_t const totalSampleSize = 1 MB;
2549*01826a49SYabin Cui size_t const sampleUnitSize = 8 KB;
2550*01826a49SYabin Cui U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
2551*01826a49SYabin Cui size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
2552*01826a49SYabin Cui size_t dictSize;
2553*01826a49SYabin Cui U32 dictID;
2554*01826a49SYabin Cui size_t dictHeaderSize;
2555*01826a49SYabin Cui size_t dictBufferFixedSize = 144;
2556*01826a49SYabin Cui unsigned char const dictBufferFixed[144] = {0x37, 0xa4, 0x30, 0xec, 0x63, 0x00, 0x00, 0x00, 0x08, 0x10, 0x00, 0x1f,
2557*01826a49SYabin Cui 0x0f, 0x00, 0x28, 0xe5, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2558*01826a49SYabin Cui 0x00, 0x80, 0x0f, 0x9e, 0x0f, 0x00, 0x00, 0x24, 0x40, 0x80, 0x00, 0x01,
2559*01826a49SYabin Cui 0x02, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0xde, 0x08,
2560*01826a49SYabin Cui 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
2561*01826a49SYabin Cui 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
2562*01826a49SYabin Cui 0x08, 0x08, 0x08, 0x08, 0xbc, 0xe1, 0x4b, 0x92, 0x0e, 0xb4, 0x7b, 0x18,
2563*01826a49SYabin Cui 0x86, 0x61, 0x18, 0xc6, 0x18, 0x63, 0x8c, 0x31, 0xc6, 0x18, 0x63, 0x8c,
2564*01826a49SYabin Cui 0x31, 0x66, 0x66, 0x66, 0x66, 0xb6, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x04,
2565*01826a49SYabin Cui 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x20, 0x73, 0x6f, 0x64, 0x61,
2566*01826a49SYabin Cui 0x6c, 0x65, 0x73, 0x20, 0x74, 0x6f, 0x72, 0x74, 0x6f, 0x72, 0x20, 0x65,
2567*01826a49SYabin Cui 0x6c, 0x65, 0x69, 0x66, 0x65, 0x6e, 0x64, 0x2e, 0x20, 0x41, 0x6c, 0x69};
2568*01826a49SYabin Cui
2569*01826a49SYabin Cui if (dictBuffer==NULL || samplesSizes==NULL) {
2570*01826a49SYabin Cui free(dictBuffer);
2571*01826a49SYabin Cui free(samplesSizes);
2572*01826a49SYabin Cui goto _output_error;
2573*01826a49SYabin Cui }
2574*01826a49SYabin Cui
2575*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : dictBuilder on cyclic data : ", testNb++);
2576*01826a49SYabin Cui assert(compressedBufferSize >= totalSampleSize);
2577*01826a49SYabin Cui { U32 u; for (u=0; u<totalSampleSize; u++) ((BYTE*)decodedBuffer)[u] = (BYTE)u; }
2578*01826a49SYabin Cui { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
2579*01826a49SYabin Cui { size_t const sDictSize = ZDICT_trainFromBuffer(dictBuffer, dictBufferCapacity,
2580*01826a49SYabin Cui decodedBuffer, samplesSizes, nbSamples);
2581*01826a49SYabin Cui if (ZDICT_isError(sDictSize)) goto _output_error;
2582*01826a49SYabin Cui DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)sDictSize);
2583*01826a49SYabin Cui }
2584*01826a49SYabin Cui
2585*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : dictBuilder : ", testNb++);
2586*01826a49SYabin Cui { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
2587*01826a49SYabin Cui dictSize = ZDICT_trainFromBuffer(dictBuffer, dictBufferCapacity,
2588*01826a49SYabin Cui CNBuffer, samplesSizes, nbSamples);
2589*01826a49SYabin Cui if (ZDICT_isError(dictSize)) goto _output_error;
2590*01826a49SYabin Cui DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)dictSize);
2591*01826a49SYabin Cui
2592*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Multithreaded COVER dictBuilder : ", testNb++);
2593*01826a49SYabin Cui { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
2594*01826a49SYabin Cui { ZDICT_cover_params_t coverParams;
2595*01826a49SYabin Cui memset(&coverParams, 0, sizeof(coverParams));
2596*01826a49SYabin Cui coverParams.steps = 8;
2597*01826a49SYabin Cui coverParams.nbThreads = 4;
2598*01826a49SYabin Cui dictSize = ZDICT_optimizeTrainFromBuffer_cover(
2599*01826a49SYabin Cui dictBuffer, dictBufferCapacity,
2600*01826a49SYabin Cui CNBuffer, samplesSizes, nbSamples/8, /* less samples for faster tests */
2601*01826a49SYabin Cui &coverParams);
2602*01826a49SYabin Cui if (ZDICT_isError(dictSize)) goto _output_error;
2603*01826a49SYabin Cui }
2604*01826a49SYabin Cui DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)dictSize);
2605*01826a49SYabin Cui
2606*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : COVER dictBuilder with shrinkDict: ", testNb++);
2607*01826a49SYabin Cui { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
2608*01826a49SYabin Cui { ZDICT_cover_params_t coverParams;
2609*01826a49SYabin Cui memset(&coverParams, 0, sizeof(coverParams));
2610*01826a49SYabin Cui coverParams.steps = 8;
2611*01826a49SYabin Cui coverParams.nbThreads = 4;
2612*01826a49SYabin Cui coverParams.shrinkDict = 1;
2613*01826a49SYabin Cui coverParams.shrinkDictMaxRegression = 1;
2614*01826a49SYabin Cui dictSize = ZDICT_optimizeTrainFromBuffer_cover(
2615*01826a49SYabin Cui dictBuffer, dictBufferCapacity,
2616*01826a49SYabin Cui CNBuffer, samplesSizes, nbSamples/8, /* less samples for faster tests */
2617*01826a49SYabin Cui &coverParams);
2618*01826a49SYabin Cui if (ZDICT_isError(dictSize)) goto _output_error;
2619*01826a49SYabin Cui }
2620*01826a49SYabin Cui DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)dictSize);
2621*01826a49SYabin Cui
2622*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Multithreaded FASTCOVER dictBuilder : ", testNb++);
2623*01826a49SYabin Cui { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
2624*01826a49SYabin Cui { ZDICT_fastCover_params_t fastCoverParams;
2625*01826a49SYabin Cui memset(&fastCoverParams, 0, sizeof(fastCoverParams));
2626*01826a49SYabin Cui fastCoverParams.steps = 8;
2627*01826a49SYabin Cui fastCoverParams.nbThreads = 4;
2628*01826a49SYabin Cui dictSize = ZDICT_optimizeTrainFromBuffer_fastCover(
2629*01826a49SYabin Cui dictBuffer, dictBufferCapacity,
2630*01826a49SYabin Cui CNBuffer, samplesSizes, nbSamples,
2631*01826a49SYabin Cui &fastCoverParams);
2632*01826a49SYabin Cui if (ZDICT_isError(dictSize)) goto _output_error;
2633*01826a49SYabin Cui }
2634*01826a49SYabin Cui DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)dictSize);
2635*01826a49SYabin Cui
2636*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : FASTCOVER dictBuilder with shrinkDict: ", testNb++);
2637*01826a49SYabin Cui { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
2638*01826a49SYabin Cui { ZDICT_fastCover_params_t fastCoverParams;
2639*01826a49SYabin Cui memset(&fastCoverParams, 0, sizeof(fastCoverParams));
2640*01826a49SYabin Cui fastCoverParams.steps = 8;
2641*01826a49SYabin Cui fastCoverParams.nbThreads = 4;
2642*01826a49SYabin Cui fastCoverParams.shrinkDict = 1;
2643*01826a49SYabin Cui fastCoverParams.shrinkDictMaxRegression = 1;
2644*01826a49SYabin Cui dictSize = ZDICT_optimizeTrainFromBuffer_fastCover(
2645*01826a49SYabin Cui dictBuffer, dictBufferCapacity,
2646*01826a49SYabin Cui CNBuffer, samplesSizes, nbSamples,
2647*01826a49SYabin Cui &fastCoverParams);
2648*01826a49SYabin Cui if (ZDICT_isError(dictSize)) goto _output_error;
2649*01826a49SYabin Cui }
2650*01826a49SYabin Cui DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)dictSize);
2651*01826a49SYabin Cui
2652*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : check dictID : ", testNb++);
2653*01826a49SYabin Cui dictID = ZDICT_getDictID(dictBuffer, dictSize);
2654*01826a49SYabin Cui if (dictID==0) goto _output_error;
2655*01826a49SYabin Cui DISPLAYLEVEL(3, "OK : %u \n", (unsigned)dictID);
2656*01826a49SYabin Cui
2657*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : check dict header size no error : ", testNb++);
2658*01826a49SYabin Cui dictHeaderSize = ZDICT_getDictHeaderSize(dictBuffer, dictSize);
2659*01826a49SYabin Cui if (dictHeaderSize==0) goto _output_error;
2660*01826a49SYabin Cui DISPLAYLEVEL(3, "OK : %u \n", (unsigned)dictHeaderSize);
2661*01826a49SYabin Cui
2662*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : check dict header size correctness : ", testNb++);
2663*01826a49SYabin Cui { dictHeaderSize = ZDICT_getDictHeaderSize(dictBufferFixed, dictBufferFixedSize);
2664*01826a49SYabin Cui if (dictHeaderSize != 115) goto _output_error;
2665*01826a49SYabin Cui }
2666*01826a49SYabin Cui DISPLAYLEVEL(3, "OK : %u \n", (unsigned)dictHeaderSize);
2667*01826a49SYabin Cui
2668*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress with dictionary : ", testNb++);
2669*01826a49SYabin Cui cSize = ZSTD_compress_usingDict(cctx, compressedBuffer, compressedBufferSize,
2670*01826a49SYabin Cui CNBuffer, CNBuffSize,
2671*01826a49SYabin Cui dictBuffer, dictSize, 4);
2672*01826a49SYabin Cui if (ZSTD_isError(cSize)) goto _output_error;
2673*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
2674*01826a49SYabin Cui
2675*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : retrieve dictID from dictionary : ", testNb++);
2676*01826a49SYabin Cui { U32 const did = ZSTD_getDictID_fromDict(dictBuffer, dictSize);
2677*01826a49SYabin Cui if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
2678*01826a49SYabin Cui }
2679*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2680*01826a49SYabin Cui
2681*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : retrieve dictID from frame : ", testNb++);
2682*01826a49SYabin Cui { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
2683*01826a49SYabin Cui if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
2684*01826a49SYabin Cui }
2685*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2686*01826a49SYabin Cui
2687*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : frame built with dictionary should be decompressible : ", testNb++);
2688*01826a49SYabin Cui { ZSTD_DCtx* const dctx = ZSTD_createDCtx(); assert(dctx != NULL);
2689*01826a49SYabin Cui CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
2690*01826a49SYabin Cui decodedBuffer, CNBuffSize,
2691*01826a49SYabin Cui compressedBuffer, cSize,
2692*01826a49SYabin Cui dictBuffer, dictSize),
2693*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error);
2694*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
2695*01826a49SYabin Cui }
2696*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2697*01826a49SYabin Cui
2698*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : estimate CDict size : ", testNb++);
2699*01826a49SYabin Cui { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
2700*01826a49SYabin Cui size_t const estimatedSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byRef);
2701*01826a49SYabin Cui DISPLAYLEVEL(3, "OK : %u \n", (unsigned)estimatedSize);
2702*01826a49SYabin Cui }
2703*01826a49SYabin Cui
2704*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress with CDict ", testNb++);
2705*01826a49SYabin Cui { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
2706*01826a49SYabin Cui ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize,
2707*01826a49SYabin Cui ZSTD_dlm_byRef, ZSTD_dct_auto,
2708*01826a49SYabin Cui cParams, ZSTD_defaultCMem);
2709*01826a49SYabin Cui assert(cdict != NULL);
2710*01826a49SYabin Cui DISPLAYLEVEL(3, "(size : %u) : ", (unsigned)ZSTD_sizeof_CDict(cdict));
2711*01826a49SYabin Cui assert(ZSTD_getDictID_fromDict(dictBuffer, dictSize) == ZSTD_getDictID_fromCDict(cdict));
2712*01826a49SYabin Cui cSize = ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize,
2713*01826a49SYabin Cui CNBuffer, CNBuffSize, cdict);
2714*01826a49SYabin Cui ZSTD_freeCDict(cdict);
2715*01826a49SYabin Cui if (ZSTD_isError(cSize)) goto _output_error;
2716*01826a49SYabin Cui }
2717*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
2718*01826a49SYabin Cui
2719*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : retrieve dictID from frame : ", testNb++);
2720*01826a49SYabin Cui { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
2721*01826a49SYabin Cui if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */
2722*01826a49SYabin Cui }
2723*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2724*01826a49SYabin Cui
2725*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : frame built with dictionary should be decompressible : ", testNb++);
2726*01826a49SYabin Cui { ZSTD_DCtx* const dctx = ZSTD_createDCtx(); assert(dctx != NULL);
2727*01826a49SYabin Cui CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
2728*01826a49SYabin Cui decodedBuffer, CNBuffSize,
2729*01826a49SYabin Cui compressedBuffer, cSize,
2730*01826a49SYabin Cui dictBuffer, dictSize),
2731*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error);
2732*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
2733*01826a49SYabin Cui }
2734*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2735*01826a49SYabin Cui
2736*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress with static CDict : ", testNb++);
2737*01826a49SYabin Cui { int const maxLevel = ZSTD_maxCLevel();
2738*01826a49SYabin Cui int level;
2739*01826a49SYabin Cui for (level = 1; level <= maxLevel; ++level) {
2740*01826a49SYabin Cui ZSTD_compressionParameters const cParams = ZSTD_getCParams(level, CNBuffSize, dictSize);
2741*01826a49SYabin Cui size_t const cdictSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy);
2742*01826a49SYabin Cui void* const cdictBuffer = malloc(cdictSize);
2743*01826a49SYabin Cui if (cdictBuffer==NULL) goto _output_error;
2744*01826a49SYabin Cui { const ZSTD_CDict* const cdict = ZSTD_initStaticCDict(
2745*01826a49SYabin Cui cdictBuffer, cdictSize,
2746*01826a49SYabin Cui dictBuffer, dictSize,
2747*01826a49SYabin Cui ZSTD_dlm_byCopy, ZSTD_dct_auto,
2748*01826a49SYabin Cui cParams);
2749*01826a49SYabin Cui if (cdict == NULL) {
2750*01826a49SYabin Cui DISPLAY("ZSTD_initStaticCDict failed ");
2751*01826a49SYabin Cui goto _output_error;
2752*01826a49SYabin Cui }
2753*01826a49SYabin Cui cSize = ZSTD_compress_usingCDict(cctx,
2754*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2755*01826a49SYabin Cui CNBuffer, MIN(10 KB, CNBuffSize), cdict);
2756*01826a49SYabin Cui if (ZSTD_isError(cSize)) {
2757*01826a49SYabin Cui DISPLAY("ZSTD_compress_usingCDict failed ");
2758*01826a49SYabin Cui goto _output_error;
2759*01826a49SYabin Cui } }
2760*01826a49SYabin Cui free(cdictBuffer);
2761*01826a49SYabin Cui } }
2762*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
2763*01826a49SYabin Cui
2764*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_compress_usingCDict_advanced, no contentSize, no dictID : ", testNb++);
2765*01826a49SYabin Cui { ZSTD_frameParameters const fParams = { 0 /* frameSize */, 1 /* checksum */, 1 /* noDictID*/ };
2766*01826a49SYabin Cui ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
2767*01826a49SYabin Cui ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, cParams, ZSTD_defaultCMem);
2768*01826a49SYabin Cui assert(cdict != NULL);
2769*01826a49SYabin Cui cSize = ZSTD_compress_usingCDict_advanced(cctx,
2770*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
2771*01826a49SYabin Cui CNBuffer, CNBuffSize,
2772*01826a49SYabin Cui cdict, fParams);
2773*01826a49SYabin Cui ZSTD_freeCDict(cdict);
2774*01826a49SYabin Cui if (ZSTD_isError(cSize)) goto _output_error;
2775*01826a49SYabin Cui }
2776*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
2777*01826a49SYabin Cui
2778*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : try retrieving contentSize from frame : ", testNb++);
2779*01826a49SYabin Cui { U64 const contentSize = ZSTD_getFrameContentSize(compressedBuffer, cSize);
2780*01826a49SYabin Cui if (contentSize != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
2781*01826a49SYabin Cui }
2782*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (unknown)\n");
2783*01826a49SYabin Cui
2784*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : frame built without dictID should be decompressible : ", testNb++);
2785*01826a49SYabin Cui { ZSTD_DCtx* const dctx = ZSTD_createDCtx();
2786*01826a49SYabin Cui assert(dctx != NULL);
2787*01826a49SYabin Cui CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
2788*01826a49SYabin Cui decodedBuffer, CNBuffSize,
2789*01826a49SYabin Cui compressedBuffer, cSize,
2790*01826a49SYabin Cui dictBuffer, dictSize),
2791*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error);
2792*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
2793*01826a49SYabin Cui }
2794*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2795*01826a49SYabin Cui
2796*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_compress_advanced, no dictID : ", testNb++);
2797*01826a49SYabin Cui { ZSTD_parameters p = ZSTD_getParams(3, CNBuffSize, dictSize);
2798*01826a49SYabin Cui p.fParams.noDictIDFlag = 1;
2799*01826a49SYabin Cui cSize = ZSTD_compress_advanced(cctx, compressedBuffer, compressedBufferSize,
2800*01826a49SYabin Cui CNBuffer, CNBuffSize,
2801*01826a49SYabin Cui dictBuffer, dictSize, p);
2802*01826a49SYabin Cui if (ZSTD_isError(cSize)) goto _output_error;
2803*01826a49SYabin Cui }
2804*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBuffSize*100);
2805*01826a49SYabin Cui
2806*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : frame built without dictID should be decompressible : ", testNb++);
2807*01826a49SYabin Cui { ZSTD_DCtx* const dctx = ZSTD_createDCtx(); assert(dctx != NULL);
2808*01826a49SYabin Cui CHECKPLUS(r, ZSTD_decompress_usingDict(dctx,
2809*01826a49SYabin Cui decodedBuffer, CNBuffSize,
2810*01826a49SYabin Cui compressedBuffer, cSize,
2811*01826a49SYabin Cui dictBuffer, dictSize),
2812*01826a49SYabin Cui if (r != CNBuffSize) goto _output_error);
2813*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
2814*01826a49SYabin Cui }
2815*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2816*01826a49SYabin Cui
2817*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : dictionary containing only header should return error : ", testNb++);
2818*01826a49SYabin Cui { ZSTD_DCtx* const dctx = ZSTD_createDCtx();
2819*01826a49SYabin Cui assert(dctx != NULL);
2820*01826a49SYabin Cui { const size_t ret = ZSTD_decompress_usingDict(
2821*01826a49SYabin Cui dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize,
2822*01826a49SYabin Cui "\x37\xa4\x30\xec\x11\x22\x33\x44", 8);
2823*01826a49SYabin Cui if (ZSTD_getErrorCode(ret) != ZSTD_error_dictionary_corrupted)
2824*01826a49SYabin Cui goto _output_error;
2825*01826a49SYabin Cui }
2826*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
2827*01826a49SYabin Cui }
2828*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2829*01826a49SYabin Cui
2830*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3d : bufferless api with cdict : ", testNb++);
2831*01826a49SYabin Cui { ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 1);
2832*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
2833*01826a49SYabin Cui ZSTD_frameParameters const fParams = { 0, 1, 0 };
2834*01826a49SYabin Cui size_t cBlockSize;
2835*01826a49SYabin Cui cSize = 0;
2836*01826a49SYabin Cui CHECK_Z(ZSTD_compressBegin_usingCDict_advanced(cctx, cdict, fParams, ZSTD_CONTENTSIZE_UNKNOWN));
2837*01826a49SYabin Cui cBlockSize = ZSTD_compressContinue(cctx, (char*)compressedBuffer + cSize, compressedBufferSize - cSize, CNBuffer, 1000);
2838*01826a49SYabin Cui CHECK_Z(cBlockSize);
2839*01826a49SYabin Cui cSize += cBlockSize;
2840*01826a49SYabin Cui cBlockSize = ZSTD_compressEnd(cctx, (char*)compressedBuffer + cSize, compressedBufferSize - cSize, (char const*)CNBuffer + 2000, 1000);
2841*01826a49SYabin Cui CHECK_Z(cBlockSize);
2842*01826a49SYabin Cui cSize += cBlockSize;
2843*01826a49SYabin Cui
2844*01826a49SYabin Cui CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, dictBuffer, dictSize));
2845*01826a49SYabin Cui
2846*01826a49SYabin Cui ZSTD_freeCDict(cdict);
2847*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
2848*01826a49SYabin Cui }
2849*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2850*01826a49SYabin Cui
2851*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Building cdict w/ ZSTD_dct_fullDict on a good dictionary : ", testNb++);
2852*01826a49SYabin Cui { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
2853*01826a49SYabin Cui ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_fullDict, cParams, ZSTD_defaultCMem);
2854*01826a49SYabin Cui if (cdict==NULL) goto _output_error;
2855*01826a49SYabin Cui ZSTD_freeCDict(cdict);
2856*01826a49SYabin Cui }
2857*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2858*01826a49SYabin Cui
2859*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Building cdict w/ ZSTD_dct_fullDict on a rawContent (must fail) : ", testNb++);
2860*01826a49SYabin Cui { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize);
2861*01826a49SYabin Cui ZSTD_CDict* const cdict = ZSTD_createCDict_advanced((const char*)dictBuffer+1, dictSize-1, ZSTD_dlm_byRef, ZSTD_dct_fullDict, cParams, ZSTD_defaultCMem);
2862*01826a49SYabin Cui if (cdict!=NULL) goto _output_error;
2863*01826a49SYabin Cui ZSTD_freeCDict(cdict);
2864*01826a49SYabin Cui }
2865*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2866*01826a49SYabin Cui
2867*01826a49SYabin Cui { char* rawDictBuffer = (char*)malloc(dictSize);
2868*01826a49SYabin Cui assert(rawDictBuffer);
2869*01826a49SYabin Cui memcpy(rawDictBuffer, (char*)dictBuffer + 2, dictSize - 2);
2870*01826a49SYabin Cui memset(rawDictBuffer + dictSize - 2, 0, 2);
2871*01826a49SYabin Cui MEM_writeLE32((char*)rawDictBuffer, ZSTD_MAGIC_DICTIONARY);
2872*01826a49SYabin Cui
2873*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Loading rawContent starting with dict header w/ ZSTD_dct_auto should fail : ", testNb++);
2874*01826a49SYabin Cui {
2875*01826a49SYabin Cui size_t ret;
2876*01826a49SYabin Cui /* Either operation is allowed to fail, but one must fail. */
2877*01826a49SYabin Cui ret = ZSTD_CCtx_loadDictionary_advanced(
2878*01826a49SYabin Cui cctx, (const char*)rawDictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto);
2879*01826a49SYabin Cui if (!ZSTD_isError(ret)) {
2880*01826a49SYabin Cui ret = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100));
2881*01826a49SYabin Cui if (!ZSTD_isError(ret)) goto _output_error;
2882*01826a49SYabin Cui }
2883*01826a49SYabin Cui }
2884*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2885*01826a49SYabin Cui
2886*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Loading rawContent starting with dict header w/ ZSTD_dct_rawContent should pass : ", testNb++);
2887*01826a49SYabin Cui {
2888*01826a49SYabin Cui size_t ret;
2889*01826a49SYabin Cui ret = ZSTD_CCtx_loadDictionary_advanced(
2890*01826a49SYabin Cui cctx, (const char*)rawDictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_rawContent);
2891*01826a49SYabin Cui if (ZSTD_isError(ret)) goto _output_error;
2892*01826a49SYabin Cui ret = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100));
2893*01826a49SYabin Cui if (ZSTD_isError(ret)) goto _output_error;
2894*01826a49SYabin Cui }
2895*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2896*01826a49SYabin Cui
2897*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Testing non-attached CDict with ZSTD_dct_rawContent : ", testNb++);
2898*01826a49SYabin Cui { size_t const srcSize = MIN(CNBuffSize, 100);
2899*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
2900*01826a49SYabin Cui /* Force the dictionary to be reloaded in raw content mode */
2901*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_forceAttachDict, ZSTD_dictForceLoad));
2902*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_loadDictionary_advanced(cctx, rawDictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_rawContent));
2903*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, srcSize);
2904*01826a49SYabin Cui CHECK_Z(cSize);
2905*01826a49SYabin Cui }
2906*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2907*01826a49SYabin Cui
2908*01826a49SYabin Cui free(rawDictBuffer);
2909*01826a49SYabin Cui }
2910*01826a49SYabin Cui
2911*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_CCtx_refCDict() then set parameters : ", testNb++);
2912*01826a49SYabin Cui { ZSTD_CDict* const cdict = ZSTD_createCDict(CNBuffer, dictSize, 1);
2913*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
2914*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 1) );
2915*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_hashLog, 12 ));
2916*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_refCDict(cctx, cdict) );
2917*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 1) );
2918*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_hashLog, 12 ));
2919*01826a49SYabin Cui ZSTD_freeCDict(cdict);
2920*01826a49SYabin Cui }
2921*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2922*01826a49SYabin Cui
2923*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Loading dictionary before setting parameters is the same as loading after : ", testNb++);
2924*01826a49SYabin Cui {
2925*01826a49SYabin Cui size_t size1, size2;
2926*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
2927*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 7) );
2928*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, CNBuffer, MIN(CNBuffSize, 10 KB)) );
2929*01826a49SYabin Cui size1 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
2930*01826a49SYabin Cui if (ZSTD_isError(size1)) goto _output_error;
2931*01826a49SYabin Cui
2932*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
2933*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, CNBuffer, MIN(CNBuffSize, 10 KB)) );
2934*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 7) );
2935*01826a49SYabin Cui size2 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
2936*01826a49SYabin Cui if (ZSTD_isError(size2)) goto _output_error;
2937*01826a49SYabin Cui
2938*01826a49SYabin Cui if (size1 != size2) goto _output_error;
2939*01826a49SYabin Cui }
2940*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2941*01826a49SYabin Cui
2942*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Loading a dictionary clears the prefix : ", testNb++);
2943*01826a49SYabin Cui {
2944*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_refPrefix(cctx, (const char*)dictBuffer, dictSize) );
2945*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, (const char*)dictBuffer, dictSize) );
2946*01826a49SYabin Cui CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100)) );
2947*01826a49SYabin Cui }
2948*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2949*01826a49SYabin Cui
2950*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Loading a dictionary clears the cdict : ", testNb++);
2951*01826a49SYabin Cui {
2952*01826a49SYabin Cui ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 1);
2953*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_refCDict(cctx, cdict) );
2954*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, (const char*)dictBuffer, dictSize) );
2955*01826a49SYabin Cui CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100)) );
2956*01826a49SYabin Cui ZSTD_freeCDict(cdict);
2957*01826a49SYabin Cui }
2958*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2959*01826a49SYabin Cui
2960*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Loading a cdict clears the prefix : ", testNb++);
2961*01826a49SYabin Cui {
2962*01826a49SYabin Cui ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 1);
2963*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_refPrefix(cctx, (const char*)dictBuffer, dictSize) );
2964*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_refCDict(cctx, cdict) );
2965*01826a49SYabin Cui CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100)) );
2966*01826a49SYabin Cui ZSTD_freeCDict(cdict);
2967*01826a49SYabin Cui }
2968*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2969*01826a49SYabin Cui
2970*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Loading a cdict clears the dictionary : ", testNb++);
2971*01826a49SYabin Cui {
2972*01826a49SYabin Cui ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 1);
2973*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, (const char*)dictBuffer, dictSize) );
2974*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_refCDict(cctx, cdict) );
2975*01826a49SYabin Cui CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100)) );
2976*01826a49SYabin Cui ZSTD_freeCDict(cdict);
2977*01826a49SYabin Cui }
2978*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2979*01826a49SYabin Cui
2980*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Loading a prefix clears the dictionary : ", testNb++);
2981*01826a49SYabin Cui {
2982*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, (const char*)dictBuffer, dictSize) );
2983*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_refPrefix(cctx, (const char*)dictBuffer, dictSize) );
2984*01826a49SYabin Cui CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100)) );
2985*01826a49SYabin Cui }
2986*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2987*01826a49SYabin Cui
2988*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Loading a prefix clears the cdict : ", testNb++);
2989*01826a49SYabin Cui {
2990*01826a49SYabin Cui ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 1);
2991*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_refCDict(cctx, cdict) );
2992*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_refPrefix(cctx, (const char*)dictBuffer, dictSize) );
2993*01826a49SYabin Cui CHECK_Z( ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100)) );
2994*01826a49SYabin Cui ZSTD_freeCDict(cdict);
2995*01826a49SYabin Cui }
2996*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
2997*01826a49SYabin Cui
2998*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Loaded dictionary persists across reset session : ", testNb++);
2999*01826a49SYabin Cui {
3000*01826a49SYabin Cui size_t size1, size2;
3001*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
3002*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, CNBuffer, MIN(CNBuffSize, 10 KB)) );
3003*01826a49SYabin Cui size1 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
3004*01826a49SYabin Cui if (ZSTD_isError(size1)) goto _output_error;
3005*01826a49SYabin Cui
3006*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
3007*01826a49SYabin Cui size2 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
3008*01826a49SYabin Cui if (ZSTD_isError(size2)) goto _output_error;
3009*01826a49SYabin Cui
3010*01826a49SYabin Cui if (size1 != size2) goto _output_error;
3011*01826a49SYabin Cui }
3012*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3013*01826a49SYabin Cui
3014*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Loaded dictionary is cleared after resetting parameters : ", testNb++);
3015*01826a49SYabin Cui {
3016*01826a49SYabin Cui size_t size1, size2;
3017*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
3018*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, CNBuffer, MIN(CNBuffSize, 10 KB)) );
3019*01826a49SYabin Cui size1 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
3020*01826a49SYabin Cui if (ZSTD_isError(size1)) goto _output_error;
3021*01826a49SYabin Cui
3022*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
3023*01826a49SYabin Cui size2 = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
3024*01826a49SYabin Cui if (ZSTD_isError(size2)) goto _output_error;
3025*01826a49SYabin Cui
3026*01826a49SYabin Cui if (size1 == size2) goto _output_error;
3027*01826a49SYabin Cui }
3028*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3029*01826a49SYabin Cui
3030*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
3031*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_loadDictionary(cctx, dictBuffer, dictSize) );
3032*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBuffSize, 100 KB));
3033*01826a49SYabin Cui CHECK_Z(cSize);
3034*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_decompressDCtx() with dictionary : ", testNb++);
3035*01826a49SYabin Cui {
3036*01826a49SYabin Cui ZSTD_DCtx* dctx = ZSTD_createDCtx();
3037*01826a49SYabin Cui size_t ret;
3038*01826a49SYabin Cui /* We should fail to decompress without a dictionary. */
3039*01826a49SYabin Cui ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
3040*01826a49SYabin Cui ret = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
3041*01826a49SYabin Cui if (!ZSTD_isError(ret)) goto _output_error;
3042*01826a49SYabin Cui /* We should succeed to decompress with the dictionary. */
3043*01826a49SYabin Cui ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
3044*01826a49SYabin Cui CHECK_Z( ZSTD_DCtx_loadDictionary(dctx, dictBuffer, dictSize) );
3045*01826a49SYabin Cui CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
3046*01826a49SYabin Cui /* The dictionary should persist across calls. */
3047*01826a49SYabin Cui CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
3048*01826a49SYabin Cui /* When we reset the context the dictionary is cleared. */
3049*01826a49SYabin Cui ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
3050*01826a49SYabin Cui ret = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
3051*01826a49SYabin Cui if (!ZSTD_isError(ret)) goto _output_error;
3052*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
3053*01826a49SYabin Cui }
3054*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3055*01826a49SYabin Cui
3056*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_decompressDCtx() with ddict : ", testNb++);
3057*01826a49SYabin Cui {
3058*01826a49SYabin Cui ZSTD_DCtx* dctx = ZSTD_createDCtx();
3059*01826a49SYabin Cui ZSTD_DDict* ddict = ZSTD_createDDict(dictBuffer, dictSize);
3060*01826a49SYabin Cui size_t ret;
3061*01826a49SYabin Cui /* We should succeed to decompress with the ddict. */
3062*01826a49SYabin Cui ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
3063*01826a49SYabin Cui CHECK_Z( ZSTD_DCtx_refDDict(dctx, ddict) );
3064*01826a49SYabin Cui CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
3065*01826a49SYabin Cui /* The ddict should persist across calls. */
3066*01826a49SYabin Cui CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
3067*01826a49SYabin Cui /* When we reset the context the ddict is cleared. */
3068*01826a49SYabin Cui ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
3069*01826a49SYabin Cui ret = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
3070*01826a49SYabin Cui if (!ZSTD_isError(ret)) goto _output_error;
3071*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
3072*01826a49SYabin Cui ZSTD_freeDDict(ddict);
3073*01826a49SYabin Cui }
3074*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3075*01826a49SYabin Cui
3076*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_decompressDCtx() with prefix : ", testNb++);
3077*01826a49SYabin Cui {
3078*01826a49SYabin Cui ZSTD_DCtx* dctx = ZSTD_createDCtx();
3079*01826a49SYabin Cui size_t ret;
3080*01826a49SYabin Cui /* We should succeed to decompress with the prefix. */
3081*01826a49SYabin Cui ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
3082*01826a49SYabin Cui CHECK_Z( ZSTD_DCtx_refPrefix_advanced(dctx, dictBuffer, dictSize, ZSTD_dct_auto) );
3083*01826a49SYabin Cui CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
3084*01826a49SYabin Cui /* The prefix should be cleared after the first compression. */
3085*01826a49SYabin Cui ret = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
3086*01826a49SYabin Cui if (!ZSTD_isError(ret)) goto _output_error;
3087*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
3088*01826a49SYabin Cui }
3089*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3090*01826a49SYabin Cui
3091*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_fast attach dictionary with hashLog = 25 and chainLog = 25 : ", testNb++);
3092*01826a49SYabin Cui {
3093*01826a49SYabin Cui ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams();
3094*01826a49SYabin Cui ZSTD_customMem customMem = {NULL, NULL, NULL};
3095*01826a49SYabin Cui ZSTD_DCtx* dctx = ZSTD_createDCtx();
3096*01826a49SYabin Cui ZSTD_CDict* cdict;
3097*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_strategy, ZSTD_fast));
3098*01826a49SYabin Cui /* Set windowLog to 25 so hash/chain logs don't get sized down */
3099*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_windowLog, 25));
3100*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_hashLog, 25));
3101*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_chainLog, 25));
3102*01826a49SYabin Cui /* Set srcSizeHint to 2^25 so hash/chain logs don't get sized down */
3103*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_srcSizeHint, 1u << 25));
3104*01826a49SYabin Cui cdict = ZSTD_createCDict_advanced2(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, cctxParams, customMem);
3105*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters));
3106*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_forceAttachDict, ZSTD_dictForceAttach));
3107*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
3108*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_refCDict(cctx, cdict));
3109*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
3110*01826a49SYabin Cui CHECK_Z(cSize);
3111*01826a49SYabin Cui CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, dictBuffer, dictSize));
3112*01826a49SYabin Cui ZSTD_freeCDict(cdict);
3113*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
3114*01826a49SYabin Cui ZSTD_freeCCtxParams(cctxParams);
3115*01826a49SYabin Cui }
3116*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3117*01826a49SYabin Cui
3118*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_dfast attach dictionary with hashLog = 25 and chainLog = 25 : ", testNb++);
3119*01826a49SYabin Cui {
3120*01826a49SYabin Cui ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams();
3121*01826a49SYabin Cui ZSTD_customMem customMem = {NULL, NULL, NULL};
3122*01826a49SYabin Cui ZSTD_DCtx* dctx = ZSTD_createDCtx();
3123*01826a49SYabin Cui ZSTD_CDict* cdict;
3124*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_strategy, ZSTD_dfast));
3125*01826a49SYabin Cui /* Set windowLog to 25 so hash/chain logs don't get sized down */
3126*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_windowLog, 25));
3127*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_hashLog, 25));
3128*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_chainLog, 25));
3129*01826a49SYabin Cui /* Set srcSizeHint to 2^25 so hash/chain logs don't get sized down */
3130*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_srcSizeHint, 1u << 25));
3131*01826a49SYabin Cui cdict = ZSTD_createCDict_advanced2(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, cctxParams, customMem);
3132*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters));
3133*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_forceAttachDict, ZSTD_dictForceAttach));
3134*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
3135*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_refCDict(cctx, cdict));
3136*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
3137*01826a49SYabin Cui CHECK_Z(cSize);
3138*01826a49SYabin Cui CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, dictBuffer, dictSize));
3139*01826a49SYabin Cui ZSTD_freeCDict(cdict);
3140*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
3141*01826a49SYabin Cui ZSTD_freeCCtxParams(cctxParams);
3142*01826a49SYabin Cui }
3143*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3144*01826a49SYabin Cui
3145*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_lazy attach dictionary with hashLog = 29 and searchLog = 4 : ", testNb++);
3146*01826a49SYabin Cui if (MEM_64bits()) {
3147*01826a49SYabin Cui ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams();
3148*01826a49SYabin Cui ZSTD_customMem customMem = {NULL, NULL, NULL};
3149*01826a49SYabin Cui ZSTD_DCtx* dctx = ZSTD_createDCtx();
3150*01826a49SYabin Cui ZSTD_CDict* cdict;
3151*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_strategy, ZSTD_lazy));
3152*01826a49SYabin Cui /* Force enable row based match finder, and disable dedicated dict search. */
3153*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_useRowMatchFinder, ZSTD_ps_enable));
3154*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_enableDedicatedDictSearch, 0));
3155*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_searchLog, 4));
3156*01826a49SYabin Cui /* Set windowLog to 29 so hash/chain logs don't get sized down */
3157*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_windowLog, 29));
3158*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_hashLog, 29));
3159*01826a49SYabin Cui /* Set srcSizeHint to 2^29 so hash/chain logs don't get sized down */
3160*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_srcSizeHint, 1u << 29));
3161*01826a49SYabin Cui cdict = ZSTD_createCDict_advanced2(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, cctxParams, customMem);
3162*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters));
3163*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_forceAttachDict, ZSTD_dictForceAttach));
3164*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
3165*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_refCDict(cctx, cdict));
3166*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
3167*01826a49SYabin Cui CHECK_Z(cSize);
3168*01826a49SYabin Cui CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, dictBuffer, dictSize));
3169*01826a49SYabin Cui ZSTD_freeCDict(cdict);
3170*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
3171*01826a49SYabin Cui ZSTD_freeCCtxParams(cctxParams);
3172*01826a49SYabin Cui }
3173*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3174*01826a49SYabin Cui
3175*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Dictionary with non-default repcodes : ", testNb++);
3176*01826a49SYabin Cui { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
3177*01826a49SYabin Cui dictSize = ZDICT_trainFromBuffer(dictBuffer, dictSize,
3178*01826a49SYabin Cui CNBuffer, samplesSizes, nbSamples);
3179*01826a49SYabin Cui if (ZDICT_isError(dictSize)) goto _output_error;
3180*01826a49SYabin Cui /* Set all the repcodes to non-default */
3181*01826a49SYabin Cui {
3182*01826a49SYabin Cui BYTE* dictPtr = (BYTE*)dictBuffer;
3183*01826a49SYabin Cui BYTE* dictLimit = dictPtr + dictSize - 12;
3184*01826a49SYabin Cui /* Find the repcodes */
3185*01826a49SYabin Cui while (dictPtr < dictLimit &&
3186*01826a49SYabin Cui (MEM_readLE32(dictPtr) != 1 || MEM_readLE32(dictPtr + 4) != 4 ||
3187*01826a49SYabin Cui MEM_readLE32(dictPtr + 8) != 8)) {
3188*01826a49SYabin Cui ++dictPtr;
3189*01826a49SYabin Cui }
3190*01826a49SYabin Cui if (dictPtr >= dictLimit) goto _output_error;
3191*01826a49SYabin Cui MEM_writeLE32(dictPtr + 0, 10);
3192*01826a49SYabin Cui MEM_writeLE32(dictPtr + 4, 10);
3193*01826a49SYabin Cui MEM_writeLE32(dictPtr + 8, 10);
3194*01826a49SYabin Cui /* Set the last 8 bytes to 'x' */
3195*01826a49SYabin Cui memset((BYTE*)dictBuffer + dictSize - 8, 'x', 8);
3196*01826a49SYabin Cui }
3197*01826a49SYabin Cui /* The optimal parser checks all the repcodes.
3198*01826a49SYabin Cui * Make sure at least one is a match >= targetLength so that it is
3199*01826a49SYabin Cui * immediately chosen. This will make sure that the compressor and
3200*01826a49SYabin Cui * decompressor agree on at least one of the repcodes.
3201*01826a49SYabin Cui */
3202*01826a49SYabin Cui { size_t dSize;
3203*01826a49SYabin Cui BYTE data[1024];
3204*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
3205*01826a49SYabin Cui ZSTD_compressionParameters const cParams = ZSTD_getCParams(19, CNBuffSize, dictSize);
3206*01826a49SYabin Cui ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize,
3207*01826a49SYabin Cui ZSTD_dlm_byRef, ZSTD_dct_auto,
3208*01826a49SYabin Cui cParams, ZSTD_defaultCMem);
3209*01826a49SYabin Cui assert(dctx != NULL); assert(cdict != NULL);
3210*01826a49SYabin Cui memset(data, 'x', sizeof(data));
3211*01826a49SYabin Cui cSize = ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize,
3212*01826a49SYabin Cui data, sizeof(data), cdict);
3213*01826a49SYabin Cui ZSTD_freeCDict(cdict);
3214*01826a49SYabin Cui if (ZSTD_isError(cSize)) { DISPLAYLEVEL(5, "Compression error %s : ", ZSTD_getErrorName(cSize)); goto _output_error; }
3215*01826a49SYabin Cui dSize = ZSTD_decompress_usingDict(dctx, decodedBuffer, sizeof(data), compressedBuffer, cSize, dictBuffer, dictSize);
3216*01826a49SYabin Cui if (ZSTD_isError(dSize)) { DISPLAYLEVEL(5, "Decompression error %s : ", ZSTD_getErrorName(dSize)); goto _output_error; }
3217*01826a49SYabin Cui if (memcmp(data, decodedBuffer, sizeof(data))) { DISPLAYLEVEL(5, "Data corruption : "); goto _output_error; }
3218*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
3219*01826a49SYabin Cui }
3220*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3221*01826a49SYabin Cui
3222*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_decompressDCtx() with multiple ddicts : ", testNb++);
3223*01826a49SYabin Cui {
3224*01826a49SYabin Cui const size_t numDicts = 128;
3225*01826a49SYabin Cui const size_t numFrames = 4;
3226*01826a49SYabin Cui size_t i;
3227*01826a49SYabin Cui ZSTD_DCtx* dctx = ZSTD_createDCtx();
3228*01826a49SYabin Cui ZSTD_DDict** ddictTable = (ZSTD_DDict**)malloc(sizeof(ZSTD_DDict*)*numDicts);
3229*01826a49SYabin Cui ZSTD_CDict** cdictTable = (ZSTD_CDict**)malloc(sizeof(ZSTD_CDict*)*numDicts);
3230*01826a49SYabin Cui U32 dictIDSeed = seed;
3231*01826a49SYabin Cui /* Create new compressed buffer that will hold frames with differing dictIDs */
3232*01826a49SYabin Cui char* dictBufferMulti = (char*)malloc(sizeof(char) * dictBufferFixedSize); /* Modifiable copy of fixed full dict buffer */
3233*01826a49SYabin Cui
3234*01826a49SYabin Cui ZSTD_memcpy(dictBufferMulti, dictBufferFixed, dictBufferFixedSize);
3235*01826a49SYabin Cui /* Create a bunch of DDicts with random dict IDs */
3236*01826a49SYabin Cui for (i = 0; i < numDicts; ++i) {
3237*01826a49SYabin Cui U32 currDictID = FUZ_rand(&dictIDSeed);
3238*01826a49SYabin Cui MEM_writeLE32(dictBufferMulti+ZSTD_FRAMEIDSIZE, currDictID);
3239*01826a49SYabin Cui ddictTable[i] = ZSTD_createDDict(dictBufferMulti, dictBufferFixedSize);
3240*01826a49SYabin Cui cdictTable[i] = ZSTD_createCDict(dictBufferMulti, dictBufferFixedSize, 3);
3241*01826a49SYabin Cui if (!ddictTable[i] || !cdictTable[i] || ZSTD_getDictID_fromCDict(cdictTable[i]) != ZSTD_getDictID_fromDDict(ddictTable[i])) {
3242*01826a49SYabin Cui goto _output_error;
3243*01826a49SYabin Cui }
3244*01826a49SYabin Cui }
3245*01826a49SYabin Cui /* Compress a few frames using random CDicts */
3246*01826a49SYabin Cui {
3247*01826a49SYabin Cui size_t off = 0;
3248*01826a49SYabin Cui /* only use the first half so we don't push against size limit of compressedBuffer */
3249*01826a49SYabin Cui size_t const segSize = (CNBuffSize / 2) / numFrames;
3250*01826a49SYabin Cui for (i = 0; i < numFrames; i++) {
3251*01826a49SYabin Cui size_t dictIdx = FUZ_rand(&dictIDSeed) % numDicts;
3252*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
3253*01826a49SYabin Cui { CHECK_NEWV(r, ZSTD_compress_usingCDict(cctx,
3254*01826a49SYabin Cui (BYTE*)compressedBuffer + off, CNBuffSize - off,
3255*01826a49SYabin Cui (BYTE*)CNBuffer + segSize * (size_t)i, segSize,
3256*01826a49SYabin Cui cdictTable[dictIdx]));
3257*01826a49SYabin Cui off += r;
3258*01826a49SYabin Cui }
3259*01826a49SYabin Cui }
3260*01826a49SYabin Cui cSize = off;
3261*01826a49SYabin Cui }
3262*01826a49SYabin Cui
3263*01826a49SYabin Cui /* We should succeed to decompression even though different dicts were used on different frames */
3264*01826a49SYabin Cui ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
3265*01826a49SYabin Cui ZSTD_DCtx_setParameter(dctx, ZSTD_d_refMultipleDDicts, ZSTD_rmd_refMultipleDDicts);
3266*01826a49SYabin Cui /* Reference every single ddict we made */
3267*01826a49SYabin Cui for (i = 0; i < numDicts; ++i) {
3268*01826a49SYabin Cui CHECK_Z( ZSTD_DCtx_refDDict(dctx, ddictTable[i]));
3269*01826a49SYabin Cui }
3270*01826a49SYabin Cui CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
3271*01826a49SYabin Cui /* Streaming decompression should also work */
3272*01826a49SYabin Cui {
3273*01826a49SYabin Cui ZSTD_inBuffer in = {compressedBuffer, cSize, 0};
3274*01826a49SYabin Cui ZSTD_outBuffer out = {decodedBuffer, CNBuffSize, 0};
3275*01826a49SYabin Cui while (in.pos < in.size) {
3276*01826a49SYabin Cui CHECK_Z(ZSTD_decompressStream(dctx, &out, &in));
3277*01826a49SYabin Cui }
3278*01826a49SYabin Cui }
3279*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
3280*01826a49SYabin Cui for (i = 0; i < numDicts; ++i) {
3281*01826a49SYabin Cui ZSTD_freeCDict(cdictTable[i]);
3282*01826a49SYabin Cui ZSTD_freeDDict(ddictTable[i]);
3283*01826a49SYabin Cui }
3284*01826a49SYabin Cui free(dictBufferMulti);
3285*01826a49SYabin Cui free(ddictTable);
3286*01826a49SYabin Cui free(cdictTable);
3287*01826a49SYabin Cui }
3288*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3289*01826a49SYabin Cui
3290*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3291*01826a49SYabin Cui free(dictBuffer);
3292*01826a49SYabin Cui free(samplesSizes);
3293*01826a49SYabin Cui }
3294*01826a49SYabin Cui
3295*01826a49SYabin Cui /* COVER dictionary builder tests */
3296*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
3297*01826a49SYabin Cui size_t dictSize = 16 KB;
3298*01826a49SYabin Cui size_t optDictSize = dictSize;
3299*01826a49SYabin Cui void* dictBuffer = malloc(dictSize);
3300*01826a49SYabin Cui size_t const totalSampleSize = 1 MB;
3301*01826a49SYabin Cui size_t const sampleUnitSize = 8 KB;
3302*01826a49SYabin Cui U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
3303*01826a49SYabin Cui size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
3304*01826a49SYabin Cui U32 seed32 = seed;
3305*01826a49SYabin Cui ZDICT_cover_params_t params;
3306*01826a49SYabin Cui U32 dictID;
3307*01826a49SYabin Cui
3308*01826a49SYabin Cui if (dictBuffer==NULL || samplesSizes==NULL) {
3309*01826a49SYabin Cui free(dictBuffer);
3310*01826a49SYabin Cui free(samplesSizes);
3311*01826a49SYabin Cui goto _output_error;
3312*01826a49SYabin Cui }
3313*01826a49SYabin Cui
3314*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZDICT_trainFromBuffer_cover : ", testNb++);
3315*01826a49SYabin Cui { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
3316*01826a49SYabin Cui memset(¶ms, 0, sizeof(params));
3317*01826a49SYabin Cui params.d = 1 + (FUZ_rand(&seed32) % 16);
3318*01826a49SYabin Cui params.k = params.d + (FUZ_rand(&seed32) % 256);
3319*01826a49SYabin Cui dictSize = ZDICT_trainFromBuffer_cover(dictBuffer, dictSize,
3320*01826a49SYabin Cui CNBuffer, samplesSizes, nbSamples,
3321*01826a49SYabin Cui params);
3322*01826a49SYabin Cui if (ZDICT_isError(dictSize)) goto _output_error;
3323*01826a49SYabin Cui DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)dictSize);
3324*01826a49SYabin Cui
3325*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : check dictID : ", testNb++);
3326*01826a49SYabin Cui dictID = ZDICT_getDictID(dictBuffer, dictSize);
3327*01826a49SYabin Cui if (dictID==0) goto _output_error;
3328*01826a49SYabin Cui DISPLAYLEVEL(3, "OK : %u \n", (unsigned)dictID);
3329*01826a49SYabin Cui
3330*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZDICT_optimizeTrainFromBuffer_cover : ", testNb++);
3331*01826a49SYabin Cui memset(¶ms, 0, sizeof(params));
3332*01826a49SYabin Cui params.steps = 4;
3333*01826a49SYabin Cui optDictSize = ZDICT_optimizeTrainFromBuffer_cover(dictBuffer, optDictSize,
3334*01826a49SYabin Cui CNBuffer, samplesSizes,
3335*01826a49SYabin Cui nbSamples / 4, ¶ms);
3336*01826a49SYabin Cui if (ZDICT_isError(optDictSize)) goto _output_error;
3337*01826a49SYabin Cui DISPLAYLEVEL(3, "OK, created dictionary of size %u \n", (unsigned)optDictSize);
3338*01826a49SYabin Cui
3339*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : check dictID : ", testNb++);
3340*01826a49SYabin Cui dictID = ZDICT_getDictID(dictBuffer, optDictSize);
3341*01826a49SYabin Cui if (dictID==0) goto _output_error;
3342*01826a49SYabin Cui DISPLAYLEVEL(3, "OK : %u \n", (unsigned)dictID);
3343*01826a49SYabin Cui
3344*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3345*01826a49SYabin Cui free(dictBuffer);
3346*01826a49SYabin Cui free(samplesSizes);
3347*01826a49SYabin Cui }
3348*01826a49SYabin Cui
3349*01826a49SYabin Cui /* Decompression defense tests */
3350*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Check input length for magic number : ", testNb++);
3351*01826a49SYabin Cui { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 3); /* too small input */
3352*01826a49SYabin Cui if (!ZSTD_isError(r)) goto _output_error;
3353*01826a49SYabin Cui if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
3354*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3355*01826a49SYabin Cui
3356*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Check magic Number : ", testNb++);
3357*01826a49SYabin Cui ((char*)(CNBuffer))[0] = 1;
3358*01826a49SYabin Cui { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 4);
3359*01826a49SYabin Cui if (!ZSTD_isError(r)) goto _output_error; }
3360*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3361*01826a49SYabin Cui
3362*01826a49SYabin Cui /* content size verification test */
3363*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Content size verification : ", testNb++);
3364*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
3365*01826a49SYabin Cui size_t const srcSize = 5000;
3366*01826a49SYabin Cui size_t const wrongSrcSize = (srcSize + 1000);
3367*01826a49SYabin Cui ZSTD_parameters params = ZSTD_getParams(1, wrongSrcSize, 0);
3368*01826a49SYabin Cui params.fParams.contentSizeFlag = 1;
3369*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin_advanced(cctx, NULL, 0, params, wrongSrcSize) );
3370*01826a49SYabin Cui { size_t const result = ZSTD_compressEnd(cctx, decodedBuffer, CNBuffSize, CNBuffer, srcSize);
3371*01826a49SYabin Cui if (!ZSTD_isError(result)) goto _output_error;
3372*01826a49SYabin Cui if (ZSTD_getErrorCode(result) != ZSTD_error_srcSize_wrong) goto _output_error;
3373*01826a49SYabin Cui DISPLAYLEVEL(3, "OK : %s \n", ZSTD_getErrorName(result));
3374*01826a49SYabin Cui }
3375*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3376*01826a49SYabin Cui }
3377*01826a49SYabin Cui
3378*01826a49SYabin Cui /* negative compression level test : ensure simple API and advanced API produce same result */
3379*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : negative compression level : ", testNb++);
3380*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
3381*01826a49SYabin Cui size_t const srcSize = CNBuffSize / 5;
3382*01826a49SYabin Cui int const compressionLevel = -1;
3383*01826a49SYabin Cui
3384*01826a49SYabin Cui assert(cctx != NULL);
3385*01826a49SYabin Cui { size_t const cSize_1pass = ZSTD_compress(compressedBuffer, compressedBufferSize,
3386*01826a49SYabin Cui CNBuffer, srcSize, compressionLevel);
3387*01826a49SYabin Cui if (ZSTD_isError(cSize_1pass)) goto _output_error;
3388*01826a49SYabin Cui
3389*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionLevel) );
3390*01826a49SYabin Cui { size_t const compressionResult = ZSTD_compress2(cctx,
3391*01826a49SYabin Cui compressedBuffer, compressedBufferSize,
3392*01826a49SYabin Cui CNBuffer, srcSize);
3393*01826a49SYabin Cui DISPLAYLEVEL(5, "simple=%zu vs %zu=advanced : ", cSize_1pass, compressionResult);
3394*01826a49SYabin Cui if (ZSTD_isError(compressionResult)) goto _output_error;
3395*01826a49SYabin Cui if (compressionResult != cSize_1pass) goto _output_error;
3396*01826a49SYabin Cui } }
3397*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3398*01826a49SYabin Cui }
3399*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3400*01826a49SYabin Cui
3401*01826a49SYabin Cui /* parameters order test */
3402*01826a49SYabin Cui { size_t const inputSize = CNBuffSize / 2;
3403*01826a49SYabin Cui U64 xxh64;
3404*01826a49SYabin Cui
3405*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
3406*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : parameters in order : ", testNb++);
3407*01826a49SYabin Cui assert(cctx != NULL);
3408*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 2) );
3409*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_enable) );
3410*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 18) );
3411*01826a49SYabin Cui { size_t const compressedSize = ZSTD_compress2(cctx,
3412*01826a49SYabin Cui compressedBuffer, ZSTD_compressBound(inputSize),
3413*01826a49SYabin Cui CNBuffer, inputSize);
3414*01826a49SYabin Cui CHECK_Z(compressedSize);
3415*01826a49SYabin Cui cSize = compressedSize;
3416*01826a49SYabin Cui xxh64 = XXH64(compressedBuffer, compressedSize, 0);
3417*01826a49SYabin Cui }
3418*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (compress : %u -> %u bytes)\n", (unsigned)inputSize, (unsigned)cSize);
3419*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3420*01826a49SYabin Cui }
3421*01826a49SYabin Cui
3422*01826a49SYabin Cui { ZSTD_CCtx* cctx = ZSTD_createCCtx();
3423*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : parameters disordered : ", testNb++);
3424*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 18) );
3425*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_enable) );
3426*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 2) );
3427*01826a49SYabin Cui { size_t const result = ZSTD_compress2(cctx,
3428*01826a49SYabin Cui compressedBuffer, ZSTD_compressBound(inputSize),
3429*01826a49SYabin Cui CNBuffer, inputSize);
3430*01826a49SYabin Cui CHECK_Z(result);
3431*01826a49SYabin Cui if (result != cSize) goto _output_error; /* must result in same compressed result, hence same size */
3432*01826a49SYabin Cui if (XXH64(compressedBuffer, result, 0) != xxh64) goto _output_error; /* must result in exactly same content, hence same hash */
3433*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (compress : %u -> %u bytes)\n", (unsigned)inputSize, (unsigned)result);
3434*01826a49SYabin Cui }
3435*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3436*01826a49SYabin Cui }
3437*01826a49SYabin Cui }
3438*01826a49SYabin Cui
3439*01826a49SYabin Cui /* advanced parameters for decompression */
3440*01826a49SYabin Cui { ZSTD_DCtx* const dctx = ZSTD_createDCtx();
3441*01826a49SYabin Cui assert(dctx != NULL);
3442*01826a49SYabin Cui
3443*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : get dParameter bounds ", testNb++);
3444*01826a49SYabin Cui { ZSTD_bounds const bounds = ZSTD_dParam_getBounds(ZSTD_d_windowLogMax);
3445*01826a49SYabin Cui CHECK_Z(bounds.error);
3446*01826a49SYabin Cui }
3447*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3448*01826a49SYabin Cui
3449*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : wrong dParameter : ", testNb++);
3450*01826a49SYabin Cui { size_t const sr = ZSTD_DCtx_setParameter(dctx, (ZSTD_dParameter)999999, 0);
3451*01826a49SYabin Cui if (!ZSTD_isError(sr)) goto _output_error;
3452*01826a49SYabin Cui }
3453*01826a49SYabin Cui { ZSTD_bounds const bounds = ZSTD_dParam_getBounds((ZSTD_dParameter)999998);
3454*01826a49SYabin Cui if (!ZSTD_isError(bounds.error)) goto _output_error;
3455*01826a49SYabin Cui }
3456*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3457*01826a49SYabin Cui
3458*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : out of bound dParameter : ", testNb++);
3459*01826a49SYabin Cui { size_t const sr = ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, 9999);
3460*01826a49SYabin Cui if (!ZSTD_isError(sr)) goto _output_error;
3461*01826a49SYabin Cui }
3462*01826a49SYabin Cui { size_t const sr = ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, (ZSTD_format_e)888);
3463*01826a49SYabin Cui if (!ZSTD_isError(sr)) goto _output_error;
3464*01826a49SYabin Cui }
3465*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3466*01826a49SYabin Cui
3467*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
3468*01826a49SYabin Cui }
3469*01826a49SYabin Cui
3470*01826a49SYabin Cui
3471*01826a49SYabin Cui /* custom formats tests */
3472*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
3473*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
3474*01826a49SYabin Cui size_t const inputSize = CNBuffSize / 2; /* won't cause pb with small dict size */
3475*01826a49SYabin Cui assert(dctx != NULL); assert(cctx != NULL);
3476*01826a49SYabin Cui
3477*01826a49SYabin Cui /* basic block compression */
3478*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : magic-less format test : ", testNb++);
3479*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_format, ZSTD_f_zstd1_magicless) );
3480*01826a49SYabin Cui { ZSTD_inBuffer in = { CNBuffer, inputSize, 0 };
3481*01826a49SYabin Cui ZSTD_outBuffer out = { compressedBuffer, ZSTD_compressBound(inputSize), 0 };
3482*01826a49SYabin Cui size_t const result = ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
3483*01826a49SYabin Cui if (result != 0) goto _output_error;
3484*01826a49SYabin Cui if (in.pos != in.size) goto _output_error;
3485*01826a49SYabin Cui cSize = out.pos;
3486*01826a49SYabin Cui }
3487*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (compress : %u -> %u bytes)\n", (unsigned)inputSize, (unsigned)cSize);
3488*01826a49SYabin Cui
3489*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress normally (should fail) : ", testNb++);
3490*01826a49SYabin Cui { size_t const decodeResult = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
3491*01826a49SYabin Cui if (ZSTD_getErrorCode(decodeResult) != ZSTD_error_prefix_unknown) goto _output_error;
3492*01826a49SYabin Cui DISPLAYLEVEL(3, "OK : %s \n", ZSTD_getErrorName(decodeResult));
3493*01826a49SYabin Cui }
3494*01826a49SYabin Cui
3495*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress of magic-less frame : ", testNb++);
3496*01826a49SYabin Cui ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
3497*01826a49SYabin Cui CHECK_Z( ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, ZSTD_f_zstd1_magicless) );
3498*01826a49SYabin Cui { ZSTD_frameHeader zfh;
3499*01826a49SYabin Cui size_t const zfhrt = ZSTD_getFrameHeader_advanced(&zfh, compressedBuffer, cSize, ZSTD_f_zstd1_magicless);
3500*01826a49SYabin Cui if (zfhrt != 0) goto _output_error;
3501*01826a49SYabin Cui }
3502*01826a49SYabin Cui /* one shot */
3503*01826a49SYabin Cui { size_t const result = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
3504*01826a49SYabin Cui if (result != inputSize) goto _output_error;
3505*01826a49SYabin Cui DISPLAYLEVEL(3, "one-shot OK, ");
3506*01826a49SYabin Cui }
3507*01826a49SYabin Cui /* streaming */
3508*01826a49SYabin Cui { ZSTD_inBuffer in = { compressedBuffer, cSize, 0 };
3509*01826a49SYabin Cui ZSTD_outBuffer out = { decodedBuffer, CNBuffSize, 0 };
3510*01826a49SYabin Cui size_t const result = ZSTD_decompressStream(dctx, &out, &in);
3511*01826a49SYabin Cui if (result != 0) goto _output_error;
3512*01826a49SYabin Cui if (in.pos != in.size) goto _output_error;
3513*01826a49SYabin Cui if (out.pos != inputSize) goto _output_error;
3514*01826a49SYabin Cui DISPLAYLEVEL(3, "streaming OK : regenerated %u bytes \n", (unsigned)out.pos);
3515*01826a49SYabin Cui }
3516*01826a49SYabin Cui
3517*01826a49SYabin Cui /* basic block compression */
3518*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : empty magic-less format test : ", testNb++);
3519*01826a49SYabin Cui CHECK_Z( ZSTD_CCtx_setParameter(cctx, ZSTD_c_format, ZSTD_f_zstd1_magicless) );
3520*01826a49SYabin Cui { ZSTD_inBuffer in = { CNBuffer, 0, 0 };
3521*01826a49SYabin Cui ZSTD_outBuffer out = { compressedBuffer, ZSTD_compressBound(0), 0 };
3522*01826a49SYabin Cui size_t const result = ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
3523*01826a49SYabin Cui if (result != 0) goto _output_error;
3524*01826a49SYabin Cui if (in.pos != in.size) goto _output_error;
3525*01826a49SYabin Cui cSize = out.pos;
3526*01826a49SYabin Cui }
3527*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (compress : %u -> %u bytes)\n", (unsigned)0, (unsigned)cSize);
3528*01826a49SYabin Cui
3529*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress of empty magic-less frame : ", testNb++);
3530*01826a49SYabin Cui ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
3531*01826a49SYabin Cui CHECK_Z( ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, ZSTD_f_zstd1_magicless) );
3532*01826a49SYabin Cui /* one shot */
3533*01826a49SYabin Cui { size_t const result = ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize);
3534*01826a49SYabin Cui if (result != 0) goto _output_error;
3535*01826a49SYabin Cui DISPLAYLEVEL(3, "one-shot OK, ");
3536*01826a49SYabin Cui }
3537*01826a49SYabin Cui /* streaming */
3538*01826a49SYabin Cui { ZSTD_inBuffer in = { compressedBuffer, cSize, 0 };
3539*01826a49SYabin Cui ZSTD_outBuffer out = { decodedBuffer, CNBuffSize, 0 };
3540*01826a49SYabin Cui size_t const result = ZSTD_decompressStream(dctx, &out, &in);
3541*01826a49SYabin Cui if (result != 0) goto _output_error;
3542*01826a49SYabin Cui if (in.pos != in.size) goto _output_error;
3543*01826a49SYabin Cui if (out.pos != 0) goto _output_error;
3544*01826a49SYabin Cui DISPLAYLEVEL(3, "streaming OK : regenerated %u bytes \n", (unsigned)out.pos);
3545*01826a49SYabin Cui }
3546*01826a49SYabin Cui
3547*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3548*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
3549*01826a49SYabin Cui }
3550*01826a49SYabin Cui
3551*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Decompression parameter reset test : ", testNb++);
3552*01826a49SYabin Cui {
3553*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
3554*01826a49SYabin Cui /* Attempt to future proof this to new parameters. */
3555*01826a49SYabin Cui int const maxParam = 2000;
3556*01826a49SYabin Cui int param;
3557*01826a49SYabin Cui if (ZSTD_d_experimentalParam3 > maxParam) goto _output_error;
3558*01826a49SYabin Cui for (param = 0; param < maxParam; ++param) {
3559*01826a49SYabin Cui ZSTD_dParameter dParam = (ZSTD_dParameter)param;
3560*01826a49SYabin Cui ZSTD_bounds bounds = ZSTD_dParam_getBounds(dParam);
3561*01826a49SYabin Cui int value1;
3562*01826a49SYabin Cui int value2;
3563*01826a49SYabin Cui int check;
3564*01826a49SYabin Cui if (ZSTD_isError(bounds.error))
3565*01826a49SYabin Cui continue;
3566*01826a49SYabin Cui CHECK_Z(ZSTD_DCtx_getParameter(dctx, dParam, &value1));
3567*01826a49SYabin Cui value2 = (value1 != bounds.lowerBound) ? bounds.lowerBound : bounds.upperBound;
3568*01826a49SYabin Cui CHECK_Z(ZSTD_DCtx_setParameter(dctx, dParam, value2));
3569*01826a49SYabin Cui CHECK_Z(ZSTD_DCtx_getParameter(dctx, dParam, &check));
3570*01826a49SYabin Cui if (check != value2) goto _output_error;
3571*01826a49SYabin Cui CHECK_Z(ZSTD_DCtx_reset(dctx, ZSTD_reset_parameters));
3572*01826a49SYabin Cui CHECK_Z(ZSTD_DCtx_getParameter(dctx, dParam, &check));
3573*01826a49SYabin Cui if (check != value1) goto _output_error;
3574*01826a49SYabin Cui }
3575*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
3576*01826a49SYabin Cui }
3577*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3578*01826a49SYabin Cui
3579*01826a49SYabin Cui /* block API tests */
3580*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
3581*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
3582*01826a49SYabin Cui static const size_t dictSize = 65 KB;
3583*01826a49SYabin Cui static const size_t blockSize = 100 KB; /* won't cause pb with small dict size */
3584*01826a49SYabin Cui size_t cSize2;
3585*01826a49SYabin Cui assert(cctx != NULL); assert(dctx != NULL);
3586*01826a49SYabin Cui
3587*01826a49SYabin Cui /* basic block compression */
3588*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Block compression test : ", testNb++);
3589*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin(cctx, 5) );
3590*01826a49SYabin Cui CHECK_Z( ZSTD_getBlockSize(cctx) >= blockSize);
3591*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize) );
3592*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3593*01826a49SYabin Cui
3594*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Block decompression test : ", testNb++);
3595*01826a49SYabin Cui CHECK_Z( ZSTD_decompressBegin(dctx) );
3596*01826a49SYabin Cui { CHECK_NEWV(r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) );
3597*01826a49SYabin Cui if (r != blockSize) goto _output_error; }
3598*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3599*01826a49SYabin Cui
3600*01826a49SYabin Cui /* very long stream of block compression */
3601*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Huge block streaming compression test : ", testNb++);
3602*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin(cctx, -199) ); /* we just want to quickly overflow internal U32 index */
3603*01826a49SYabin Cui CHECK_Z( ZSTD_getBlockSize(cctx) >= blockSize);
3604*01826a49SYabin Cui { U64 const toCompress = 5000000000ULL; /* > 4 GB */
3605*01826a49SYabin Cui U64 compressed = 0;
3606*01826a49SYabin Cui while (compressed < toCompress) {
3607*01826a49SYabin Cui size_t const blockCSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize);
3608*01826a49SYabin Cui assert(blockCSize != 0);
3609*01826a49SYabin Cui if (ZSTD_isError(blockCSize)) goto _output_error;
3610*01826a49SYabin Cui compressed += blockCSize;
3611*01826a49SYabin Cui } }
3612*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3613*01826a49SYabin Cui
3614*01826a49SYabin Cui /* dictionary block compression */
3615*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Dictionary Block compression test : ", testNb++);
3616*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5) );
3617*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize));
3618*01826a49SYabin Cui RDG_genBuffer((char*)CNBuffer+dictSize+blockSize, blockSize, 0.0, 0.0, seed); /* create a non-compressible second block */
3619*01826a49SYabin Cui { CHECK_NEWV(r, ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize+blockSize, blockSize) ); /* for cctx history consistency */
3620*01826a49SYabin Cui assert(r == 0); /* non-compressible block */ }
3621*01826a49SYabin Cui memcpy((char*)compressedBuffer+cSize, (char*)CNBuffer+dictSize+blockSize, blockSize); /* send non-compressed block (without header) */
3622*01826a49SYabin Cui CHECK_VAR(cSize2, ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize+blockSize, ZSTD_compressBound(blockSize),
3623*01826a49SYabin Cui (char*)CNBuffer+dictSize+2*blockSize, blockSize));
3624*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3625*01826a49SYabin Cui
3626*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Dictionary Block decompression test : ", testNb++);
3627*01826a49SYabin Cui CHECK_Z( ZSTD_decompressBegin_usingDict(dctx, CNBuffer, dictSize) );
3628*01826a49SYabin Cui { CHECK_NEWV( r, ZSTD_decompressBlock(dctx, decodedBuffer, blockSize, compressedBuffer, cSize) );
3629*01826a49SYabin Cui if (r != blockSize) {
3630*01826a49SYabin Cui DISPLAYLEVEL(1, "ZSTD_decompressBlock() with _usingDict() fails : %u, instead of %u expected \n", (unsigned)r, (unsigned)blockSize);
3631*01826a49SYabin Cui goto _output_error;
3632*01826a49SYabin Cui } }
3633*01826a49SYabin Cui memcpy((char*)decodedBuffer+blockSize, (char*)compressedBuffer+cSize, blockSize);
3634*01826a49SYabin Cui ZSTD_insertBlock(dctx, (char*)decodedBuffer+blockSize, blockSize); /* insert non-compressed block into dctx history */
3635*01826a49SYabin Cui { CHECK_NEWV( r, ZSTD_decompressBlock(dctx, (char*)decodedBuffer+2*blockSize, blockSize, (char*)compressedBuffer+cSize+blockSize, cSize2) );
3636*01826a49SYabin Cui if (r != blockSize) {
3637*01826a49SYabin Cui DISPLAYLEVEL(1, "ZSTD_decompressBlock() with _usingDict() and after insertBlock() fails : %u, instead of %u expected \n", (unsigned)r, (unsigned)blockSize);
3638*01826a49SYabin Cui goto _output_error;
3639*01826a49SYabin Cui } }
3640*01826a49SYabin Cui assert(memcpy((char*)CNBuffer+dictSize, decodedBuffer, blockSize*3)); /* ensure regenerated content is identical to origin */
3641*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3642*01826a49SYabin Cui
3643*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Block compression with CDict : ", testNb++);
3644*01826a49SYabin Cui { ZSTD_CDict* const cdict = ZSTD_createCDict(CNBuffer, dictSize, 3);
3645*01826a49SYabin Cui if (cdict==NULL) goto _output_error;
3646*01826a49SYabin Cui CHECK_Z( ZSTD_compressBegin_usingCDict(cctx, cdict) );
3647*01826a49SYabin Cui CHECK_Z( ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize) );
3648*01826a49SYabin Cui ZSTD_freeCDict(cdict);
3649*01826a49SYabin Cui }
3650*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3651*01826a49SYabin Cui
3652*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3653*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
3654*01826a49SYabin Cui }
3655*01826a49SYabin Cui
3656*01826a49SYabin Cui /* long rle test */
3657*01826a49SYabin Cui { size_t sampleSize = 0;
3658*01826a49SYabin Cui size_t expectedCompressedSize = 39; /* block 1, 2: compressed, block 3: RLE, zstd 1.4.4 */
3659*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : Long RLE test : ", testNb++);
3660*01826a49SYabin Cui memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1);
3661*01826a49SYabin Cui sampleSize += 256 KB - 1;
3662*01826a49SYabin Cui memset((char*)CNBuffer+sampleSize, 'A', 96 KB);
3663*01826a49SYabin Cui sampleSize += 96 KB;
3664*01826a49SYabin Cui cSize = ZSTD_compress(compressedBuffer, ZSTD_compressBound(sampleSize), CNBuffer, sampleSize, 1);
3665*01826a49SYabin Cui if (ZSTD_isError(cSize) || cSize > expectedCompressedSize) goto _output_error;
3666*01826a49SYabin Cui { CHECK_NEWV(regenSize, ZSTD_decompress(decodedBuffer, sampleSize, compressedBuffer, cSize));
3667*01826a49SYabin Cui if (regenSize!=sampleSize) goto _output_error; }
3668*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3669*01826a49SYabin Cui }
3670*01826a49SYabin Cui
3671*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_generateSequences decode from sequences test : ", testNb++);
3672*01826a49SYabin Cui {
3673*01826a49SYabin Cui size_t srcSize = 150 KB;
3674*01826a49SYabin Cui BYTE* src = (BYTE*)CNBuffer;
3675*01826a49SYabin Cui BYTE* decoded = (BYTE*)compressedBuffer;
3676*01826a49SYabin Cui
3677*01826a49SYabin Cui ZSTD_CCtx* cctx = ZSTD_createCCtx();
3678*01826a49SYabin Cui ZSTD_Sequence* seqs = (ZSTD_Sequence*)malloc(srcSize * sizeof(ZSTD_Sequence));
3679*01826a49SYabin Cui size_t seqsSize;
3680*01826a49SYabin Cui
3681*01826a49SYabin Cui if (seqs == NULL) goto _output_error;
3682*01826a49SYabin Cui assert(cctx != NULL);
3683*01826a49SYabin Cui ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 19);
3684*01826a49SYabin Cui /* Populate src with random data */
3685*01826a49SYabin Cui RDG_genBuffer(CNBuffer, srcSize, compressibility, 0.5, seed);
3686*01826a49SYabin Cui
3687*01826a49SYabin Cui /* Test with block delimiters roundtrip */
3688*01826a49SYabin Cui seqsSize = ZSTD_generateSequences(cctx, seqs, srcSize, src, srcSize);
3689*01826a49SYabin Cui CHECK_Z(seqsSize);
3690*01826a49SYabin Cui FUZ_decodeSequences(decoded, seqs, seqsSize, src, srcSize, ZSTD_sf_explicitBlockDelimiters);
3691*01826a49SYabin Cui assert(!memcmp(CNBuffer, compressedBuffer, srcSize));
3692*01826a49SYabin Cui
3693*01826a49SYabin Cui /* Test no block delimiters roundtrip */
3694*01826a49SYabin Cui seqsSize = ZSTD_mergeBlockDelimiters(seqs, seqsSize);
3695*01826a49SYabin Cui CHECK_Z(seqsSize);
3696*01826a49SYabin Cui FUZ_decodeSequences(decoded, seqs, seqsSize, src, srcSize, ZSTD_sf_noBlockDelimiters);
3697*01826a49SYabin Cui assert(!memcmp(CNBuffer, compressedBuffer, srcSize));
3698*01826a49SYabin Cui
3699*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3700*01826a49SYabin Cui free(seqs);
3701*01826a49SYabin Cui }
3702*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3703*01826a49SYabin Cui
3704*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_generateSequences too small output buffer : ", testNb++);
3705*01826a49SYabin Cui {
3706*01826a49SYabin Cui const size_t seqsCapacity = 10;
3707*01826a49SYabin Cui const size_t srcSize = 150 KB;
3708*01826a49SYabin Cui const BYTE* src = (BYTE*)CNBuffer;
3709*01826a49SYabin Cui
3710*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
3711*01826a49SYabin Cui ZSTD_Sequence* const seqs = (ZSTD_Sequence*)malloc(seqsCapacity * sizeof(ZSTD_Sequence));
3712*01826a49SYabin Cui
3713*01826a49SYabin Cui if (seqs == NULL) goto _output_error;
3714*01826a49SYabin Cui if (cctx == NULL) goto _output_error;
3715*01826a49SYabin Cui /* Populate src with random data */
3716*01826a49SYabin Cui RDG_genBuffer(CNBuffer, srcSize, compressibility, 0.5, seed);
3717*01826a49SYabin Cui
3718*01826a49SYabin Cui /* Test with block delimiters roundtrip */
3719*01826a49SYabin Cui {
3720*01826a49SYabin Cui size_t const seqsSize = ZSTD_generateSequences(cctx, seqs, seqsCapacity, src, srcSize);
3721*01826a49SYabin Cui if (!ZSTD_isError(seqsSize)) goto _output_error;
3722*01826a49SYabin Cui }
3723*01826a49SYabin Cui
3724*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3725*01826a49SYabin Cui free(seqs);
3726*01826a49SYabin Cui }
3727*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3728*01826a49SYabin Cui
3729*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_getSequences followed by ZSTD_compressSequences : ", testNb++);
3730*01826a49SYabin Cui {
3731*01826a49SYabin Cui const size_t srcSize = 500 KB;
3732*01826a49SYabin Cui const BYTE* const src = (BYTE*)CNBuffer;
3733*01826a49SYabin Cui BYTE* const dst = (BYTE*)compressedBuffer;
3734*01826a49SYabin Cui const size_t dstCapacity = ZSTD_compressBound(srcSize);
3735*01826a49SYabin Cui const size_t decompressSize = srcSize;
3736*01826a49SYabin Cui char* const decompressBuffer = (char*)malloc(decompressSize);
3737*01826a49SYabin Cui size_t compressedSize;
3738*01826a49SYabin Cui
3739*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
3740*01826a49SYabin Cui ZSTD_Sequence* const seqs = (ZSTD_Sequence*)malloc(srcSize * sizeof(ZSTD_Sequence));
3741*01826a49SYabin Cui size_t nbSeqs;
3742*01826a49SYabin Cui
3743*01826a49SYabin Cui if (seqs == NULL) goto _output_error;
3744*01826a49SYabin Cui assert(cctx != NULL);
3745*01826a49SYabin Cui
3746*01826a49SYabin Cui /* Populate src with random data */
3747*01826a49SYabin Cui RDG_genBuffer(CNBuffer, srcSize, compressibility, 0., seed);
3748*01826a49SYabin Cui
3749*01826a49SYabin Cui /* Roundtrip Test with block delimiters generated by ZSTD_generateSequences() */
3750*01826a49SYabin Cui nbSeqs = ZSTD_generateSequences(cctx, seqs, srcSize, src, srcSize);
3751*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
3752*01826a49SYabin Cui ZSTD_CCtx_setParameter(cctx, ZSTD_c_blockDelimiters, ZSTD_sf_explicitBlockDelimiters);
3753*01826a49SYabin Cui compressedSize = ZSTD_compressSequences(cctx, dst, dstCapacity, seqs, nbSeqs, src, srcSize);
3754*01826a49SYabin Cui if (ZSTD_isError(compressedSize)) {
3755*01826a49SYabin Cui DISPLAY("Error in sequence compression with block delims\n");
3756*01826a49SYabin Cui goto _output_error;
3757*01826a49SYabin Cui }
3758*01826a49SYabin Cui { size_t const dSize = ZSTD_decompress(decompressBuffer, decompressSize, dst, compressedSize);
3759*01826a49SYabin Cui if (ZSTD_isError(dSize)) {
3760*01826a49SYabin Cui DISPLAY("Error in sequence compression roundtrip with block delims\n");
3761*01826a49SYabin Cui goto _output_error;
3762*01826a49SYabin Cui } }
3763*01826a49SYabin Cui assert(!memcmp(decompressBuffer, src, srcSize));
3764*01826a49SYabin Cui
3765*01826a49SYabin Cui /* Roundtrip Test with no block delimiters */
3766*01826a49SYabin Cui { size_t const nbSeqsAfterMerge = ZSTD_mergeBlockDelimiters(seqs, nbSeqs);
3767*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
3768*01826a49SYabin Cui ZSTD_CCtx_setParameter(cctx, ZSTD_c_blockDelimiters, ZSTD_sf_noBlockDelimiters);
3769*01826a49SYabin Cui compressedSize = ZSTD_compressSequences(cctx, dst, dstCapacity, seqs, nbSeqsAfterMerge, src, srcSize);
3770*01826a49SYabin Cui }
3771*01826a49SYabin Cui if (ZSTD_isError(compressedSize)) {
3772*01826a49SYabin Cui DISPLAY("Error in sequence compression with no block delims\n");
3773*01826a49SYabin Cui goto _output_error;
3774*01826a49SYabin Cui }
3775*01826a49SYabin Cui { size_t const dSize = ZSTD_decompress(decompressBuffer, decompressSize, dst, compressedSize);
3776*01826a49SYabin Cui if (ZSTD_isError(dSize)) {
3777*01826a49SYabin Cui DISPLAY("Error in sequence compression roundtrip with no block delims\n");
3778*01826a49SYabin Cui goto _output_error;
3779*01826a49SYabin Cui } }
3780*01826a49SYabin Cui assert(!memcmp(decompressBuffer, src, srcSize));
3781*01826a49SYabin Cui
3782*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3783*01826a49SYabin Cui free(decompressBuffer);
3784*01826a49SYabin Cui free(seqs);
3785*01826a49SYabin Cui }
3786*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3787*01826a49SYabin Cui
3788*01826a49SYabin Cui /* Multiple blocks of zeros test */
3789*01826a49SYabin Cui #define LONGZEROSLENGTH 1000000 /* 1MB of zeros */
3790*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress %u zeroes : ", testNb++, LONGZEROSLENGTH);
3791*01826a49SYabin Cui memset(CNBuffer, 0, LONGZEROSLENGTH);
3792*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compress(compressedBuffer, ZSTD_compressBound(LONGZEROSLENGTH), CNBuffer, LONGZEROSLENGTH, 1) );
3793*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/LONGZEROSLENGTH*100);
3794*01826a49SYabin Cui
3795*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress %u zeroes : ", testNb++, LONGZEROSLENGTH);
3796*01826a49SYabin Cui { CHECK_NEWV(r, ZSTD_decompress(decodedBuffer, LONGZEROSLENGTH, compressedBuffer, cSize) );
3797*01826a49SYabin Cui if (r != LONGZEROSLENGTH) goto _output_error; }
3798*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3799*01826a49SYabin Cui
3800*01826a49SYabin Cui /* All zeroes test (test bug #137) */
3801*01826a49SYabin Cui #define ZEROESLENGTH 100
3802*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH);
3803*01826a49SYabin Cui memset(CNBuffer, 0, ZEROESLENGTH);
3804*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1) );
3805*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/ZEROESLENGTH*100);
3806*01826a49SYabin Cui
3807*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH);
3808*01826a49SYabin Cui { CHECK_NEWV(r, ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize) );
3809*01826a49SYabin Cui if (r != ZEROESLENGTH) goto _output_error; }
3810*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3811*01826a49SYabin Cui
3812*01826a49SYabin Cui /* nbSeq limit test */
3813*01826a49SYabin Cui #define _3BYTESTESTLENGTH 131000
3814*01826a49SYabin Cui #define NB3BYTESSEQLOG 9
3815*01826a49SYabin Cui #define NB3BYTESSEQ (1 << NB3BYTESSEQLOG)
3816*01826a49SYabin Cui #define NB3BYTESSEQMASK (NB3BYTESSEQ-1)
3817*01826a49SYabin Cui /* creates a buffer full of 3-bytes sequences */
3818*01826a49SYabin Cui { BYTE _3BytesSeqs[NB3BYTESSEQ][3];
3819*01826a49SYabin Cui U32 rSeed = 1;
3820*01826a49SYabin Cui
3821*01826a49SYabin Cui /* create batch of 3-bytes sequences */
3822*01826a49SYabin Cui { int i;
3823*01826a49SYabin Cui for (i=0; i < NB3BYTESSEQ; i++) {
3824*01826a49SYabin Cui _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&rSeed) & 255);
3825*01826a49SYabin Cui _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&rSeed) & 255);
3826*01826a49SYabin Cui _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&rSeed) & 255);
3827*01826a49SYabin Cui } }
3828*01826a49SYabin Cui
3829*01826a49SYabin Cui /* randomly fills CNBuffer with prepared 3-bytes sequences */
3830*01826a49SYabin Cui { int i;
3831*01826a49SYabin Cui for (i=0; i < _3BYTESTESTLENGTH; i += 3) { /* note : CNBuffer size > _3BYTESTESTLENGTH+3 */
3832*01826a49SYabin Cui U32 const id = FUZ_rand(&rSeed) & NB3BYTESSEQMASK;
3833*01826a49SYabin Cui ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0];
3834*01826a49SYabin Cui ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1];
3835*01826a49SYabin Cui ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2];
3836*01826a49SYabin Cui } } }
3837*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : growing nbSeq : ", testNb++);
3838*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
3839*01826a49SYabin Cui size_t const maxNbSeq = _3BYTESTESTLENGTH / 3;
3840*01826a49SYabin Cui size_t const bound = ZSTD_compressBound(_3BYTESTESTLENGTH);
3841*01826a49SYabin Cui size_t nbSeq = 1;
3842*01826a49SYabin Cui while (nbSeq <= maxNbSeq) {
3843*01826a49SYabin Cui CHECK_Z(ZSTD_compressCCtx(cctx, compressedBuffer, bound, CNBuffer, nbSeq * 3, 19));
3844*01826a49SYabin Cui /* Check every sequence for the first 100, then skip more rapidly. */
3845*01826a49SYabin Cui if (nbSeq < 100) {
3846*01826a49SYabin Cui ++nbSeq;
3847*01826a49SYabin Cui } else {
3848*01826a49SYabin Cui nbSeq += (nbSeq >> 2);
3849*01826a49SYabin Cui }
3850*01826a49SYabin Cui }
3851*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3852*01826a49SYabin Cui }
3853*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3854*01826a49SYabin Cui
3855*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : compress lots 3-bytes sequences : ", testNb++);
3856*01826a49SYabin Cui CHECK_VAR(cSize, ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH),
3857*01826a49SYabin Cui CNBuffer, _3BYTESTESTLENGTH, 19) );
3858*01826a49SYabin Cui DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/_3BYTESTESTLENGTH*100);
3859*01826a49SYabin Cui
3860*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : decompress lots 3-bytes sequence : ", testNb++);
3861*01826a49SYabin Cui { CHECK_NEWV(r, ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize) );
3862*01826a49SYabin Cui if (r != _3BYTESTESTLENGTH) goto _output_error; }
3863*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3864*01826a49SYabin Cui
3865*01826a49SYabin Cui
3866*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : growing literals buffer : ", testNb++);
3867*01826a49SYabin Cui RDG_genBuffer(CNBuffer, CNBuffSize, 0.0, 0.1, seed);
3868*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
3869*01826a49SYabin Cui size_t const bound = ZSTD_compressBound(CNBuffSize);
3870*01826a49SYabin Cui size_t size = 1;
3871*01826a49SYabin Cui while (size <= CNBuffSize) {
3872*01826a49SYabin Cui CHECK_Z(ZSTD_compressCCtx(cctx, compressedBuffer, bound, CNBuffer, size, 3));
3873*01826a49SYabin Cui /* Check every size for the first 100, then skip more rapidly. */
3874*01826a49SYabin Cui if (size < 100) {
3875*01826a49SYabin Cui ++size;
3876*01826a49SYabin Cui } else {
3877*01826a49SYabin Cui size += (size >> 2);
3878*01826a49SYabin Cui }
3879*01826a49SYabin Cui }
3880*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3881*01826a49SYabin Cui }
3882*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3883*01826a49SYabin Cui
3884*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : incompressible data and ill suited dictionary : ", testNb++);
3885*01826a49SYabin Cui { /* Train a dictionary on low characters */
3886*01826a49SYabin Cui size_t dictSize = 16 KB;
3887*01826a49SYabin Cui void* const dictBuffer = malloc(dictSize);
3888*01826a49SYabin Cui size_t const totalSampleSize = 1 MB;
3889*01826a49SYabin Cui size_t const sampleUnitSize = 8 KB;
3890*01826a49SYabin Cui U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize);
3891*01826a49SYabin Cui size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t));
3892*01826a49SYabin Cui if (!dictBuffer || !samplesSizes) goto _output_error;
3893*01826a49SYabin Cui { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }
3894*01826a49SYabin Cui dictSize = ZDICT_trainFromBuffer(dictBuffer, dictSize, CNBuffer, samplesSizes, nbSamples);
3895*01826a49SYabin Cui if (ZDICT_isError(dictSize)) goto _output_error;
3896*01826a49SYabin Cui /* Reverse the characters to make the dictionary ill suited */
3897*01826a49SYabin Cui { U32 u;
3898*01826a49SYabin Cui for (u = 0; u < CNBuffSize; ++u) {
3899*01826a49SYabin Cui ((BYTE*)CNBuffer)[u] = 255 - ((BYTE*)CNBuffer)[u];
3900*01826a49SYabin Cui } }
3901*01826a49SYabin Cui { /* Compress the data */
3902*01826a49SYabin Cui size_t const inputSize = 500;
3903*01826a49SYabin Cui size_t const outputSize = ZSTD_compressBound(inputSize);
3904*01826a49SYabin Cui void* const outputBuffer = malloc(outputSize);
3905*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
3906*01826a49SYabin Cui if (!outputBuffer || !cctx) goto _output_error;
3907*01826a49SYabin Cui CHECK_Z(ZSTD_compress_usingDict(cctx, outputBuffer, outputSize, CNBuffer, inputSize, dictBuffer, dictSize, 1));
3908*01826a49SYabin Cui free(outputBuffer);
3909*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3910*01826a49SYabin Cui }
3911*01826a49SYabin Cui
3912*01826a49SYabin Cui free(dictBuffer);
3913*01826a49SYabin Cui free(samplesSizes);
3914*01826a49SYabin Cui }
3915*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3916*01826a49SYabin Cui
3917*01826a49SYabin Cui
3918*01826a49SYabin Cui /* findFrameCompressedSize on skippable frames */
3919*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : frame compressed size of skippable frame : ", testNb++);
3920*01826a49SYabin Cui { const char* frame = "\x50\x2a\x4d\x18\x05\x0\x0\0abcde";
3921*01826a49SYabin Cui size_t const frameSrcSize = 13;
3922*01826a49SYabin Cui if (ZSTD_findFrameCompressedSize(frame, frameSrcSize) != frameSrcSize) goto _output_error; }
3923*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3924*01826a49SYabin Cui
3925*01826a49SYabin Cui /* error string tests */
3926*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : testing ZSTD error code strings : ", testNb++);
3927*01826a49SYabin Cui if (strcmp("No error detected", ZSTD_getErrorName((ZSTD_ErrorCode)(0-ZSTD_error_no_error))) != 0) goto _output_error;
3928*01826a49SYabin Cui if (strcmp("No error detected", ZSTD_getErrorString(ZSTD_error_no_error)) != 0) goto _output_error;
3929*01826a49SYabin Cui if (strcmp("Unspecified error code", ZSTD_getErrorString((ZSTD_ErrorCode)(0-ZSTD_error_GENERIC))) != 0) goto _output_error;
3930*01826a49SYabin Cui if (strcmp("Error (generic)", ZSTD_getErrorName((size_t)0-ZSTD_error_GENERIC)) != 0) goto _output_error;
3931*01826a49SYabin Cui if (strcmp("Error (generic)", ZSTD_getErrorString(ZSTD_error_GENERIC)) != 0) goto _output_error;
3932*01826a49SYabin Cui if (strcmp("No error detected", ZSTD_getErrorName(ZSTD_error_GENERIC)) != 0) goto _output_error;
3933*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3934*01826a49SYabin Cui
3935*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : testing ZSTD dictionary sizes : ", testNb++);
3936*01826a49SYabin Cui RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed);
3937*01826a49SYabin Cui {
3938*01826a49SYabin Cui size_t const size = MIN(128 KB, CNBuffSize);
3939*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
3940*01826a49SYabin Cui ZSTD_CDict* const lgCDict = ZSTD_createCDict(CNBuffer, size, 1);
3941*01826a49SYabin Cui ZSTD_CDict* const smCDict = ZSTD_createCDict(CNBuffer, 1 KB, 1);
3942*01826a49SYabin Cui ZSTD_frameHeader lgHeader;
3943*01826a49SYabin Cui ZSTD_frameHeader smHeader;
3944*01826a49SYabin Cui
3945*01826a49SYabin Cui CHECK_Z(ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize, CNBuffer, size, lgCDict));
3946*01826a49SYabin Cui CHECK_Z(ZSTD_getFrameHeader(&lgHeader, compressedBuffer, compressedBufferSize));
3947*01826a49SYabin Cui CHECK_Z(ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize, CNBuffer, size, smCDict));
3948*01826a49SYabin Cui CHECK_Z(ZSTD_getFrameHeader(&smHeader, compressedBuffer, compressedBufferSize));
3949*01826a49SYabin Cui
3950*01826a49SYabin Cui if (lgHeader.windowSize != smHeader.windowSize) goto _output_error;
3951*01826a49SYabin Cui
3952*01826a49SYabin Cui ZSTD_freeCDict(smCDict);
3953*01826a49SYabin Cui ZSTD_freeCDict(lgCDict);
3954*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
3955*01826a49SYabin Cui }
3956*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3957*01826a49SYabin Cui
3958*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : testing FSE_normalizeCount() PR#1255: ", testNb++);
3959*01826a49SYabin Cui {
3960*01826a49SYabin Cui short norm[32];
3961*01826a49SYabin Cui unsigned count[32];
3962*01826a49SYabin Cui unsigned const tableLog = 5;
3963*01826a49SYabin Cui size_t const nbSeq = 32;
3964*01826a49SYabin Cui unsigned const maxSymbolValue = 31;
3965*01826a49SYabin Cui size_t i;
3966*01826a49SYabin Cui
3967*01826a49SYabin Cui for (i = 0; i < 32; ++i)
3968*01826a49SYabin Cui count[i] = 1;
3969*01826a49SYabin Cui /* Calling FSE_normalizeCount() on a uniform distribution should not
3970*01826a49SYabin Cui * cause a division by zero.
3971*01826a49SYabin Cui */
3972*01826a49SYabin Cui FSE_normalizeCount(norm, tableLog, count, nbSeq, maxSymbolValue, /* useLowProbCount */ 1);
3973*01826a49SYabin Cui }
3974*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3975*01826a49SYabin Cui
3976*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : testing FSE_writeNCount() PR#2779: ", testNb++);
3977*01826a49SYabin Cui {
3978*01826a49SYabin Cui size_t const outBufSize = 9;
3979*01826a49SYabin Cui short const count[11] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 9, 18};
3980*01826a49SYabin Cui unsigned const tableLog = 5;
3981*01826a49SYabin Cui unsigned const maxSymbolValue = 10;
3982*01826a49SYabin Cui BYTE* outBuf = (BYTE*)malloc(outBufSize*sizeof(BYTE));
3983*01826a49SYabin Cui
3984*01826a49SYabin Cui /* Ensure that this write doesn't write out of bounds, and that
3985*01826a49SYabin Cui * FSE_writeNCount_generic() is *not* called with writeIsSafe == 1.
3986*01826a49SYabin Cui */
3987*01826a49SYabin Cui FSE_writeNCount(outBuf, outBufSize, count, maxSymbolValue, tableLog);
3988*01826a49SYabin Cui free(outBuf);
3989*01826a49SYabin Cui }
3990*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
3991*01826a49SYabin Cui
3992*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : testing bitwise intrinsics PR#3045: ", testNb++);
3993*01826a49SYabin Cui {
3994*01826a49SYabin Cui U32 seed_copy = seed; /* need non-const seed to avoid compiler warning for FUZ_rand(&seed) */
3995*01826a49SYabin Cui U32 rand32 = FUZ_rand(&seed_copy);
3996*01826a49SYabin Cui U64 rand64 = ((U64)FUZ_rand(&seed_copy) << 32) | FUZ_rand(&seed_copy);
3997*01826a49SYabin Cui U32 lowbit_only_32 = 1;
3998*01826a49SYabin Cui U64 lowbit_only_64 = 1;
3999*01826a49SYabin Cui U32 highbit_only_32 = (U32)1 << 31;
4000*01826a49SYabin Cui U64 highbit_only_64 = (U64)1 << 63;
4001*01826a49SYabin Cui U32 i;
4002*01826a49SYabin Cui if (rand32 == 0) rand32 = 1; /* CLZ and CTZ are undefined on 0 */
4003*01826a49SYabin Cui if (rand64 == 0) rand64 = 1; /* CLZ and CTZ are undefined on 0 */
4004*01826a49SYabin Cui
4005*01826a49SYabin Cui /* Test ZSTD_countTrailingZeros32 */
4006*01826a49SYabin Cui CHECK_EQ(ZSTD_countTrailingZeros32(lowbit_only_32), 0u);
4007*01826a49SYabin Cui CHECK_EQ(ZSTD_countTrailingZeros32(highbit_only_32), 31u);
4008*01826a49SYabin Cui CHECK_EQ(ZSTD_countTrailingZeros32(rand32), ZSTD_countTrailingZeros32_fallback(rand32));
4009*01826a49SYabin Cui
4010*01826a49SYabin Cui /* Test ZSTD_countLeadingZeros32 */
4011*01826a49SYabin Cui CHECK_EQ(ZSTD_countLeadingZeros32(lowbit_only_32), 31u);
4012*01826a49SYabin Cui CHECK_EQ(ZSTD_countLeadingZeros32(highbit_only_32), 0u);
4013*01826a49SYabin Cui CHECK_EQ(ZSTD_countLeadingZeros32(rand32), ZSTD_countLeadingZeros32_fallback(rand32));
4014*01826a49SYabin Cui
4015*01826a49SYabin Cui /* Test ZSTD_countTrailingZeros64 */
4016*01826a49SYabin Cui CHECK_EQ(ZSTD_countTrailingZeros64(lowbit_only_64), 0u);
4017*01826a49SYabin Cui CHECK_EQ(ZSTD_countTrailingZeros64(highbit_only_64), 63u);
4018*01826a49SYabin Cui
4019*01826a49SYabin Cui /* Test ZSTD_countLeadingZeros64 */
4020*01826a49SYabin Cui CHECK_EQ(ZSTD_countLeadingZeros64(lowbit_only_64), 63u);
4021*01826a49SYabin Cui CHECK_EQ(ZSTD_countLeadingZeros64(highbit_only_64), 0u);
4022*01826a49SYabin Cui
4023*01826a49SYabin Cui /* Test ZSTD_highbit32 */
4024*01826a49SYabin Cui CHECK_EQ(ZSTD_highbit32(lowbit_only_32), 0u);
4025*01826a49SYabin Cui CHECK_EQ(ZSTD_highbit32(highbit_only_32), 31u);
4026*01826a49SYabin Cui
4027*01826a49SYabin Cui /* Test ZSTD_NbCommonBytes */
4028*01826a49SYabin Cui if (MEM_isLittleEndian()) {
4029*01826a49SYabin Cui if (MEM_64bits()) {
4030*01826a49SYabin Cui CHECK_EQ(ZSTD_NbCommonBytes(lowbit_only_32), 0u);
4031*01826a49SYabin Cui CHECK_EQ(ZSTD_NbCommonBytes(highbit_only_32), 3u);
4032*01826a49SYabin Cui } else {
4033*01826a49SYabin Cui CHECK_EQ(ZSTD_NbCommonBytes(lowbit_only_32), 0u);
4034*01826a49SYabin Cui CHECK_EQ(ZSTD_NbCommonBytes(highbit_only_32), 3u);
4035*01826a49SYabin Cui }
4036*01826a49SYabin Cui } else {
4037*01826a49SYabin Cui if (MEM_64bits()) {
4038*01826a49SYabin Cui CHECK_EQ(ZSTD_NbCommonBytes(lowbit_only_32), 7u);
4039*01826a49SYabin Cui CHECK_EQ(ZSTD_NbCommonBytes(highbit_only_32), 4u);
4040*01826a49SYabin Cui } else {
4041*01826a49SYabin Cui CHECK_EQ(ZSTD_NbCommonBytes(lowbit_only_32), 3u);
4042*01826a49SYabin Cui CHECK_EQ(ZSTD_NbCommonBytes(highbit_only_32), 0u);
4043*01826a49SYabin Cui }
4044*01826a49SYabin Cui }
4045*01826a49SYabin Cui
4046*01826a49SYabin Cui /* Test MEM_ intrinsics */
4047*01826a49SYabin Cui CHECK_EQ(MEM_swap32(rand32), MEM_swap32_fallback(rand32));
4048*01826a49SYabin Cui CHECK_EQ(MEM_swap64(rand64), MEM_swap64_fallback(rand64));
4049*01826a49SYabin Cui
4050*01826a49SYabin Cui /* Test fallbacks vs intrinsics on a range of small integers */
4051*01826a49SYabin Cui for (i=1; i <= 1000; i++) {
4052*01826a49SYabin Cui CHECK_EQ(MEM_swap32(i), MEM_swap32_fallback(i));
4053*01826a49SYabin Cui CHECK_EQ(MEM_swap64((U64)i), MEM_swap64_fallback((U64)i));
4054*01826a49SYabin Cui CHECK_EQ(ZSTD_countTrailingZeros32(i), ZSTD_countTrailingZeros32_fallback(i));
4055*01826a49SYabin Cui CHECK_EQ(ZSTD_countLeadingZeros32(i), ZSTD_countLeadingZeros32_fallback(i));
4056*01826a49SYabin Cui }
4057*01826a49SYabin Cui }
4058*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
4059*01826a49SYabin Cui
4060*01826a49SYabin Cui #ifdef ZSTD_MULTITHREAD
4061*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : passing wrong full dict should fail on compressStream2 refPrefix ", testNb++);
4062*01826a49SYabin Cui { ZSTD_CCtx* cctx = ZSTD_createCCtx();
4063*01826a49SYabin Cui size_t const srcSize = 1 MB + 5; /* A little more than ZSTDMT_JOBSIZE_MIN */
4064*01826a49SYabin Cui size_t const dstSize = ZSTD_compressBound(srcSize);
4065*01826a49SYabin Cui void* const src = CNBuffer;
4066*01826a49SYabin Cui void* const dst = compressedBuffer;
4067*01826a49SYabin Cui void* dict = (void*)malloc(srcSize);
4068*01826a49SYabin Cui
4069*01826a49SYabin Cui RDG_genBuffer(src, srcSize, compressibility, 0.5, seed);
4070*01826a49SYabin Cui RDG_genBuffer(dict, srcSize, compressibility, 0., seed);
4071*01826a49SYabin Cui
4072*01826a49SYabin Cui /* Make sure there is no ZSTD_MAGIC_NUMBER */
4073*01826a49SYabin Cui memset(dict, 0, sizeof(U32));
4074*01826a49SYabin Cui
4075*01826a49SYabin Cui /* something more than 1 */
4076*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 2));
4077*01826a49SYabin Cui /* lie and claim this is a full dict */
4078*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_refPrefix_advanced(cctx, dict, srcSize, ZSTD_dct_fullDict));
4079*01826a49SYabin Cui
4080*01826a49SYabin Cui { ZSTD_outBuffer out = {dst, dstSize, 0};
4081*01826a49SYabin Cui ZSTD_inBuffer in = {src, srcSize, 0};
4082*01826a49SYabin Cui /* should fail because its not a full dict like we said it was */
4083*01826a49SYabin Cui assert(ZSTD_isError(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush)));
4084*01826a49SYabin Cui }
4085*01826a49SYabin Cui
4086*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
4087*01826a49SYabin Cui free(dict);
4088*01826a49SYabin Cui }
4089*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
4090*01826a49SYabin Cui
4091*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : small dictionary with multithreading and LDM ", testNb++);
4092*01826a49SYabin Cui { ZSTD_CCtx* cctx = ZSTD_createCCtx();
4093*01826a49SYabin Cui size_t const srcSize = 1 MB + 5; /* A little more than ZSTDMT_JOBSIZE_MIN */
4094*01826a49SYabin Cui size_t const dictSize = 10;
4095*01826a49SYabin Cui size_t const dstSize = ZSTD_compressBound(srcSize);
4096*01826a49SYabin Cui void* const src = CNBuffer;
4097*01826a49SYabin Cui void* const dst = compressedBuffer;
4098*01826a49SYabin Cui void* dict = (void*)malloc(dictSize);
4099*01826a49SYabin Cui
4100*01826a49SYabin Cui RDG_genBuffer(src, srcSize, compressibility, 0.5, seed);
4101*01826a49SYabin Cui RDG_genBuffer(dict, dictSize, compressibility, 0., seed);
4102*01826a49SYabin Cui
4103*01826a49SYabin Cui /* Make sure there is no ZSTD_MAGIC_NUMBER */
4104*01826a49SYabin Cui memset(dict, 0, sizeof(U32));
4105*01826a49SYabin Cui
4106*01826a49SYabin Cui /* Enable MT, LDM, and use refPrefix() for a small dict */
4107*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 2));
4108*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_enable));
4109*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_refPrefix(cctx, dict, dictSize));
4110*01826a49SYabin Cui
4111*01826a49SYabin Cui CHECK_Z(ZSTD_compress2(cctx, dst, dstSize, src, srcSize));
4112*01826a49SYabin Cui
4113*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
4114*01826a49SYabin Cui free(dict);
4115*01826a49SYabin Cui }
4116*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
4117*01826a49SYabin Cui
4118*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_getCParams() + dictionary ", testNb++);
4119*01826a49SYabin Cui {
4120*01826a49SYabin Cui ZSTD_compressionParameters const medium = ZSTD_getCParams(1, 16*1024-1, 0);
4121*01826a49SYabin Cui ZSTD_compressionParameters const large = ZSTD_getCParams(1, 128*1024-1, 0);
4122*01826a49SYabin Cui ZSTD_compressionParameters const smallDict = ZSTD_getCParams(1, 0, 400);
4123*01826a49SYabin Cui ZSTD_compressionParameters const mediumDict = ZSTD_getCParams(1, 0, 10000);
4124*01826a49SYabin Cui ZSTD_compressionParameters const largeDict = ZSTD_getCParams(1, 0, 100000);
4125*01826a49SYabin Cui
4126*01826a49SYabin Cui assert(!memcmp(&smallDict, &mediumDict, sizeof(smallDict)));
4127*01826a49SYabin Cui assert(!memcmp(&medium, &mediumDict, sizeof(medium)));
4128*01826a49SYabin Cui assert(!memcmp(&large, &largeDict, sizeof(large)));
4129*01826a49SYabin Cui }
4130*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
4131*01826a49SYabin Cui
4132*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : ZSTD_adjustCParams() + dictionary ", testNb++);
4133*01826a49SYabin Cui {
4134*01826a49SYabin Cui ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, 0, 0);
4135*01826a49SYabin Cui ZSTD_compressionParameters const smallDict = ZSTD_adjustCParams(cParams, 0, 400);
4136*01826a49SYabin Cui ZSTD_compressionParameters const smallSrcAndDict = ZSTD_adjustCParams(cParams, 500, 400);
4137*01826a49SYabin Cui
4138*01826a49SYabin Cui assert(smallSrcAndDict.windowLog == 10);
4139*01826a49SYabin Cui assert(!memcmp(&cParams, &smallDict, sizeof(cParams)));
4140*01826a49SYabin Cui }
4141*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
4142*01826a49SYabin Cui
4143*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : check compression mem usage monotonicity over levels for estimateCCtxSize() : ", testNb++);
4144*01826a49SYabin Cui {
4145*01826a49SYabin Cui int level = 1;
4146*01826a49SYabin Cui size_t prevSize = 0;
4147*01826a49SYabin Cui for (; level < ZSTD_maxCLevel(); ++level) {
4148*01826a49SYabin Cui size_t const currSize = ZSTD_estimateCCtxSize(level);
4149*01826a49SYabin Cui if (prevSize > currSize) {
4150*01826a49SYabin Cui DISPLAYLEVEL(3, "Error! previous cctx size: %zu at level: %d is larger than current cctx size: %zu at level: %d",
4151*01826a49SYabin Cui prevSize, level-1, currSize, level);
4152*01826a49SYabin Cui goto _output_error;
4153*01826a49SYabin Cui }
4154*01826a49SYabin Cui prevSize = currSize;
4155*01826a49SYabin Cui }
4156*01826a49SYabin Cui }
4157*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
4158*01826a49SYabin Cui
4159*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : check estimateCCtxSize() always larger or equal to ZSTD_estimateCCtxSize_usingCParams() : ", testNb++);
4160*01826a49SYabin Cui {
4161*01826a49SYabin Cui size_t const kSizeIncrement = 2 KB;
4162*01826a49SYabin Cui int level = -3;
4163*01826a49SYabin Cui
4164*01826a49SYabin Cui for (; level <= ZSTD_maxCLevel(); ++level) {
4165*01826a49SYabin Cui size_t dictSize = 0;
4166*01826a49SYabin Cui for (; dictSize <= 256 KB; dictSize += 8 * kSizeIncrement) {
4167*01826a49SYabin Cui size_t srcSize = 2 KB;
4168*01826a49SYabin Cui for (; srcSize < 300 KB; srcSize += kSizeIncrement) {
4169*01826a49SYabin Cui ZSTD_compressionParameters const cParams = ZSTD_getCParams(level, srcSize, dictSize);
4170*01826a49SYabin Cui size_t const cctxSizeUsingCParams = ZSTD_estimateCCtxSize_usingCParams(cParams);
4171*01826a49SYabin Cui size_t const cctxSizeUsingLevel = ZSTD_estimateCCtxSize(level);
4172*01826a49SYabin Cui if (cctxSizeUsingLevel < cctxSizeUsingCParams
4173*01826a49SYabin Cui || ZSTD_isError(cctxSizeUsingCParams)
4174*01826a49SYabin Cui || ZSTD_isError(cctxSizeUsingLevel)) {
4175*01826a49SYabin Cui DISPLAYLEVEL(3, "error! l: %d dict: %zu srcSize: %zu cctx size cpar: %zu, cctx size level: %zu\n",
4176*01826a49SYabin Cui level, dictSize, srcSize, cctxSizeUsingCParams, cctxSizeUsingLevel);
4177*01826a49SYabin Cui goto _output_error;
4178*01826a49SYabin Cui } } } } }
4179*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
4180*01826a49SYabin Cui
4181*01826a49SYabin Cui DISPLAYLEVEL(3, "test%3i : thread pool API tests : \n", testNb++)
4182*01826a49SYabin Cui {
4183*01826a49SYabin Cui int const threadPoolTestResult = threadPoolTests();
4184*01826a49SYabin Cui if (threadPoolTestResult) {
4185*01826a49SYabin Cui goto _output_error;
4186*01826a49SYabin Cui }
4187*01826a49SYabin Cui }
4188*01826a49SYabin Cui DISPLAYLEVEL(3, "thread pool tests OK \n");
4189*01826a49SYabin Cui
4190*01826a49SYabin Cui #endif /* ZSTD_MULTITHREAD */
4191*01826a49SYabin Cui
4192*01826a49SYabin Cui _end:
4193*01826a49SYabin Cui free(CNBuffer);
4194*01826a49SYabin Cui free(compressedBuffer);
4195*01826a49SYabin Cui free(decodedBuffer);
4196*01826a49SYabin Cui return testResult;
4197*01826a49SYabin Cui
4198*01826a49SYabin Cui _output_error:
4199*01826a49SYabin Cui testResult = 1;
4200*01826a49SYabin Cui DISPLAY("Error detected in Unit tests ! \n");
4201*01826a49SYabin Cui goto _end;
4202*01826a49SYabin Cui }
4203*01826a49SYabin Cui
longUnitTests(U32 const seed,double compressibility)4204*01826a49SYabin Cui static int longUnitTests(U32 const seed, double compressibility)
4205*01826a49SYabin Cui {
4206*01826a49SYabin Cui size_t const CNBuffSize = 5 MB;
4207*01826a49SYabin Cui void* const CNBuffer = malloc(CNBuffSize);
4208*01826a49SYabin Cui size_t const compressedBufferSize = ZSTD_compressBound(CNBuffSize);
4209*01826a49SYabin Cui void* const compressedBuffer = malloc(compressedBufferSize);
4210*01826a49SYabin Cui void* const decodedBuffer = malloc(CNBuffSize);
4211*01826a49SYabin Cui int testResult = 0;
4212*01826a49SYabin Cui unsigned testNb=0;
4213*01826a49SYabin Cui size_t cSize;
4214*01826a49SYabin Cui
4215*01826a49SYabin Cui /* Create compressible noise */
4216*01826a49SYabin Cui if (!CNBuffer || !compressedBuffer || !decodedBuffer) {
4217*01826a49SYabin Cui DISPLAY("Not enough memory, aborting\n");
4218*01826a49SYabin Cui testResult = 1;
4219*01826a49SYabin Cui goto _end;
4220*01826a49SYabin Cui }
4221*01826a49SYabin Cui RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed);
4222*01826a49SYabin Cui
4223*01826a49SYabin Cui /* note : this test is rather long, it would be great to find a way to speed up its execution */
4224*01826a49SYabin Cui DISPLAYLEVEL(3, "longtest%3i : table cleanliness through index reduction : ", testNb++);
4225*01826a49SYabin Cui { int cLevel;
4226*01826a49SYabin Cui size_t approxIndex = 0;
4227*01826a49SYabin Cui size_t maxIndex = ((3U << 29) + (1U << ZSTD_WINDOWLOG_MAX)); /* ZSTD_CURRENT_MAX from zstd_compress_internal.h */
4228*01826a49SYabin Cui
4229*01826a49SYabin Cui /* Provision enough space in a static context so that we can do all
4230*01826a49SYabin Cui * this without ever reallocating, which would reset the indices. */
4231*01826a49SYabin Cui size_t const staticCCtxSize = ZSTD_estimateCStreamSize(22);
4232*01826a49SYabin Cui void* const staticCCtxBuffer = malloc(staticCCtxSize);
4233*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_initStaticCCtx(staticCCtxBuffer, staticCCtxSize);
4234*01826a49SYabin Cui
4235*01826a49SYabin Cui /* bump the indices so the following compressions happen at high
4236*01826a49SYabin Cui * indices. */
4237*01826a49SYabin Cui { ZSTD_outBuffer out = { compressedBuffer, compressedBufferSize, 0 };
4238*01826a49SYabin Cui ZSTD_inBuffer in = { CNBuffer, CNBuffSize, 0 };
4239*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
4240*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, -500));
4241*01826a49SYabin Cui while (approxIndex <= (maxIndex / 4) * 3) {
4242*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
4243*01826a49SYabin Cui approxIndex += in.pos;
4244*01826a49SYabin Cui CHECK_Z(in.pos == in.size);
4245*01826a49SYabin Cui in.pos = 0;
4246*01826a49SYabin Cui out.pos = 0;
4247*01826a49SYabin Cui }
4248*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
4249*01826a49SYabin Cui }
4250*01826a49SYabin Cui
4251*01826a49SYabin Cui /* spew a bunch of stuff into the table area */
4252*01826a49SYabin Cui for (cLevel = 1; cLevel <= 22; cLevel++) {
4253*01826a49SYabin Cui ZSTD_outBuffer out = { compressedBuffer, compressedBufferSize / (unsigned)cLevel, 0 };
4254*01826a49SYabin Cui ZSTD_inBuffer in = { CNBuffer, CNBuffSize, 0 };
4255*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
4256*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel));
4257*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
4258*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
4259*01826a49SYabin Cui approxIndex += in.pos;
4260*01826a49SYabin Cui }
4261*01826a49SYabin Cui
4262*01826a49SYabin Cui /* now crank the indices so we overflow */
4263*01826a49SYabin Cui { ZSTD_outBuffer out = { compressedBuffer, compressedBufferSize, 0 };
4264*01826a49SYabin Cui ZSTD_inBuffer in = { CNBuffer, CNBuffSize, 0 };
4265*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
4266*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, -500));
4267*01826a49SYabin Cui while (approxIndex <= maxIndex) {
4268*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
4269*01826a49SYabin Cui approxIndex += in.pos;
4270*01826a49SYabin Cui CHECK_Z(in.pos == in.size);
4271*01826a49SYabin Cui in.pos = 0;
4272*01826a49SYabin Cui out.pos = 0;
4273*01826a49SYabin Cui }
4274*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
4275*01826a49SYabin Cui }
4276*01826a49SYabin Cui
4277*01826a49SYabin Cui /* do a bunch of compressions again in low indices and ensure we don't
4278*01826a49SYabin Cui * hit untracked invalid indices */
4279*01826a49SYabin Cui for (cLevel = 1; cLevel <= 22; cLevel++) {
4280*01826a49SYabin Cui ZSTD_outBuffer out = { compressedBuffer, compressedBufferSize / (unsigned)cLevel, 0 };
4281*01826a49SYabin Cui ZSTD_inBuffer in = { CNBuffer, CNBuffSize, 0 };
4282*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
4283*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel));
4284*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush));
4285*01826a49SYabin Cui CHECK_Z(ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end));
4286*01826a49SYabin Cui approxIndex += in.pos;
4287*01826a49SYabin Cui }
4288*01826a49SYabin Cui
4289*01826a49SYabin Cui free(staticCCtxBuffer);
4290*01826a49SYabin Cui }
4291*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
4292*01826a49SYabin Cui
4293*01826a49SYabin Cui DISPLAYLEVEL(3, "longtest%3i : testing ldm no regressions in size for opt parser : ", testNb++);
4294*01826a49SYabin Cui { size_t cSizeLdm;
4295*01826a49SYabin Cui size_t cSizeNoLdm;
4296*01826a49SYabin Cui ZSTD_CCtx* const cctx = ZSTD_createCCtx();
4297*01826a49SYabin Cui
4298*01826a49SYabin Cui RDG_genBuffer(CNBuffer, CNBuffSize, 0.5, 0.5, seed);
4299*01826a49SYabin Cui
4300*01826a49SYabin Cui /* Enable checksum to verify round trip. */
4301*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
4302*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_enable));
4303*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 19));
4304*01826a49SYabin Cui
4305*01826a49SYabin Cui /* Round trip once with ldm. */
4306*01826a49SYabin Cui cSizeLdm = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
4307*01826a49SYabin Cui CHECK_Z(cSizeLdm);
4308*01826a49SYabin Cui CHECK_Z(ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSizeLdm));
4309*01826a49SYabin Cui
4310*01826a49SYabin Cui ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters);
4311*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1));
4312*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_disable));
4313*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 19));
4314*01826a49SYabin Cui
4315*01826a49SYabin Cui /* Round trip once without ldm. */
4316*01826a49SYabin Cui cSizeNoLdm = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
4317*01826a49SYabin Cui CHECK_Z(cSizeNoLdm);
4318*01826a49SYabin Cui CHECK_Z(ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSizeNoLdm));
4319*01826a49SYabin Cui
4320*01826a49SYabin Cui if (cSizeLdm > cSizeNoLdm) {
4321*01826a49SYabin Cui DISPLAY("Using long mode should not cause regressions for btopt+\n");
4322*01826a49SYabin Cui testResult = 1;
4323*01826a49SYabin Cui goto _end;
4324*01826a49SYabin Cui }
4325*01826a49SYabin Cui
4326*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
4327*01826a49SYabin Cui }
4328*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
4329*01826a49SYabin Cui
4330*01826a49SYabin Cui DISPLAYLEVEL(3, "longtest%3i : testing cdict compression with different attachment strategies : ", testNb++);
4331*01826a49SYabin Cui { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
4332*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
4333*01826a49SYabin Cui size_t dictSize = CNBuffSize;
4334*01826a49SYabin Cui void* dict = (void*)malloc(dictSize);
4335*01826a49SYabin Cui ZSTD_CCtx_params* cctx_params = ZSTD_createCCtxParams();
4336*01826a49SYabin Cui ZSTD_dictAttachPref_e const attachPrefs[] = {
4337*01826a49SYabin Cui ZSTD_dictDefaultAttach,
4338*01826a49SYabin Cui ZSTD_dictForceAttach,
4339*01826a49SYabin Cui ZSTD_dictForceCopy,
4340*01826a49SYabin Cui ZSTD_dictForceLoad,
4341*01826a49SYabin Cui ZSTD_dictDefaultAttach,
4342*01826a49SYabin Cui ZSTD_dictForceAttach,
4343*01826a49SYabin Cui ZSTD_dictForceCopy,
4344*01826a49SYabin Cui ZSTD_dictForceLoad
4345*01826a49SYabin Cui };
4346*01826a49SYabin Cui int const enableDedicatedDictSearch[] = {0, 0, 0, 0, 1, 1, 1, 1};
4347*01826a49SYabin Cui int cLevel;
4348*01826a49SYabin Cui int i;
4349*01826a49SYabin Cui
4350*01826a49SYabin Cui RDG_genBuffer(dict, dictSize, 0.5, 0.5, seed);
4351*01826a49SYabin Cui RDG_genBuffer(CNBuffer, CNBuffSize, 0.6, 0.6, seed);
4352*01826a49SYabin Cui
4353*01826a49SYabin Cui CHECK_Z(cctx_params != NULL);
4354*01826a49SYabin Cui
4355*01826a49SYabin Cui for (dictSize = CNBuffSize; dictSize; dictSize = dictSize >> 3) {
4356*01826a49SYabin Cui DISPLAYLEVEL(3, "\n Testing with dictSize %u ", (U32)dictSize);
4357*01826a49SYabin Cui for (cLevel = 4; cLevel < 13; cLevel++) {
4358*01826a49SYabin Cui for (i = 0; i < 8; ++i) {
4359*01826a49SYabin Cui ZSTD_dictAttachPref_e const attachPref = attachPrefs[i];
4360*01826a49SYabin Cui int const enableDDS = enableDedicatedDictSearch[i];
4361*01826a49SYabin Cui ZSTD_CDict* cdict;
4362*01826a49SYabin Cui
4363*01826a49SYabin Cui DISPLAYLEVEL(5, "\n dictSize %u cLevel %d iter %d ", (U32)dictSize, cLevel, i);
4364*01826a49SYabin Cui
4365*01826a49SYabin Cui ZSTD_CCtxParams_init(cctx_params, cLevel);
4366*01826a49SYabin Cui CHECK_Z(ZSTD_CCtxParams_setParameter(cctx_params, ZSTD_c_enableDedicatedDictSearch, enableDDS));
4367*01826a49SYabin Cui
4368*01826a49SYabin Cui cdict = ZSTD_createCDict_advanced2(dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, cctx_params, ZSTD_defaultCMem);
4369*01826a49SYabin Cui CHECK(cdict != NULL);
4370*01826a49SYabin Cui
4371*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_refCDict(cctx, cdict));
4372*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_forceAttachDict, (int)attachPref));
4373*01826a49SYabin Cui
4374*01826a49SYabin Cui cSize = ZSTD_compress2(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize);
4375*01826a49SYabin Cui CHECK_Z(cSize);
4376*01826a49SYabin Cui CHECK_Z(ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, dict, dictSize));
4377*01826a49SYabin Cui
4378*01826a49SYabin Cui DISPLAYLEVEL(5, "compressed to %u bytes ", (U32)cSize);
4379*01826a49SYabin Cui
4380*01826a49SYabin Cui CHECK_Z(ZSTD_CCtx_reset(cctx, ZSTD_reset_session_and_parameters));
4381*01826a49SYabin Cui ZSTD_freeCDict(cdict);
4382*01826a49SYabin Cui } } }
4383*01826a49SYabin Cui
4384*01826a49SYabin Cui ZSTD_freeCCtx(cctx);
4385*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
4386*01826a49SYabin Cui ZSTD_freeCCtxParams(cctx_params);
4387*01826a49SYabin Cui free(dict);
4388*01826a49SYabin Cui }
4389*01826a49SYabin Cui DISPLAYLEVEL(3, "OK \n");
4390*01826a49SYabin Cui
4391*01826a49SYabin Cui _end:
4392*01826a49SYabin Cui free(CNBuffer);
4393*01826a49SYabin Cui free(compressedBuffer);
4394*01826a49SYabin Cui free(decodedBuffer);
4395*01826a49SYabin Cui return testResult;
4396*01826a49SYabin Cui }
4397*01826a49SYabin Cui
4398*01826a49SYabin Cui
findDiff(const void * buf1,const void * buf2,size_t max)4399*01826a49SYabin Cui static size_t findDiff(const void* buf1, const void* buf2, size_t max)
4400*01826a49SYabin Cui {
4401*01826a49SYabin Cui const BYTE* b1 = (const BYTE*)buf1;
4402*01826a49SYabin Cui const BYTE* b2 = (const BYTE*)buf2;
4403*01826a49SYabin Cui size_t u;
4404*01826a49SYabin Cui for (u=0; u<max; u++) {
4405*01826a49SYabin Cui if (b1[u] != b2[u]) break;
4406*01826a49SYabin Cui }
4407*01826a49SYabin Cui return u;
4408*01826a49SYabin Cui }
4409*01826a49SYabin Cui
4410*01826a49SYabin Cui
FUZ_makeParams(ZSTD_compressionParameters cParams,ZSTD_frameParameters fParams)4411*01826a49SYabin Cui static ZSTD_parameters FUZ_makeParams(ZSTD_compressionParameters cParams, ZSTD_frameParameters fParams)
4412*01826a49SYabin Cui {
4413*01826a49SYabin Cui ZSTD_parameters params;
4414*01826a49SYabin Cui params.cParams = cParams;
4415*01826a49SYabin Cui params.fParams = fParams;
4416*01826a49SYabin Cui return params;
4417*01826a49SYabin Cui }
4418*01826a49SYabin Cui
FUZ_rLogLength(U32 * seed,U32 logLength)4419*01826a49SYabin Cui static size_t FUZ_rLogLength(U32* seed, U32 logLength)
4420*01826a49SYabin Cui {
4421*01826a49SYabin Cui size_t const lengthMask = ((size_t)1 << logLength) - 1;
4422*01826a49SYabin Cui return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
4423*01826a49SYabin Cui }
4424*01826a49SYabin Cui
FUZ_randomLength(U32 * seed,U32 maxLog)4425*01826a49SYabin Cui static size_t FUZ_randomLength(U32* seed, U32 maxLog)
4426*01826a49SYabin Cui {
4427*01826a49SYabin Cui U32 const logLength = FUZ_rand(seed) % maxLog;
4428*01826a49SYabin Cui return FUZ_rLogLength(seed, logLength);
4429*01826a49SYabin Cui }
4430*01826a49SYabin Cui
4431*01826a49SYabin Cui #undef CHECK
4432*01826a49SYabin Cui #define CHECK(cond, ...) { \
4433*01826a49SYabin Cui if (cond) { \
4434*01826a49SYabin Cui DISPLAY("Error => "); \
4435*01826a49SYabin Cui DISPLAY(__VA_ARGS__); \
4436*01826a49SYabin Cui DISPLAY(" (seed %u, test nb %u) \n", (unsigned)seed, testNb); \
4437*01826a49SYabin Cui goto _output_error; \
4438*01826a49SYabin Cui } }
4439*01826a49SYabin Cui
4440*01826a49SYabin Cui #undef CHECK_Z
4441*01826a49SYabin Cui #define CHECK_Z(f) { \
4442*01826a49SYabin Cui size_t const err = f; \
4443*01826a49SYabin Cui if (ZSTD_isError(err)) { \
4444*01826a49SYabin Cui DISPLAY("Error => %s : %s ", \
4445*01826a49SYabin Cui #f, ZSTD_getErrorName(err)); \
4446*01826a49SYabin Cui DISPLAY(" (seed %u, test nb %u) \n", (unsigned)seed, testNb); \
4447*01826a49SYabin Cui goto _output_error; \
4448*01826a49SYabin Cui } }
4449*01826a49SYabin Cui
4450*01826a49SYabin Cui
fuzzerTests(U32 seed,unsigned nbTests,unsigned startTest,U32 const maxDurationS,double compressibility,int bigTests)4451*01826a49SYabin Cui static int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, U32 const maxDurationS, double compressibility, int bigTests)
4452*01826a49SYabin Cui {
4453*01826a49SYabin Cui static const U32 maxSrcLog = 23;
4454*01826a49SYabin Cui static const U32 maxSampleLog = 22;
4455*01826a49SYabin Cui size_t const srcBufferSize = (size_t)1<<maxSrcLog;
4456*01826a49SYabin Cui size_t const dstBufferSize = (size_t)1<<maxSampleLog;
4457*01826a49SYabin Cui size_t const cBufferSize = ZSTD_compressBound(dstBufferSize);
4458*01826a49SYabin Cui BYTE* cNoiseBuffer[5];
4459*01826a49SYabin Cui BYTE* const cBuffer = (BYTE*) malloc (cBufferSize);
4460*01826a49SYabin Cui BYTE* const dstBuffer = (BYTE*) malloc (dstBufferSize);
4461*01826a49SYabin Cui BYTE* const mirrorBuffer = (BYTE*) malloc (dstBufferSize);
4462*01826a49SYabin Cui ZSTD_CCtx* const refCtx = ZSTD_createCCtx();
4463*01826a49SYabin Cui ZSTD_CCtx* const ctx = ZSTD_createCCtx();
4464*01826a49SYabin Cui ZSTD_DCtx* const dctx = ZSTD_createDCtx();
4465*01826a49SYabin Cui U32 result = 0;
4466*01826a49SYabin Cui unsigned testNb = 0;
4467*01826a49SYabin Cui U32 coreSeed = seed;
4468*01826a49SYabin Cui UTIL_time_t const startClock = UTIL_getTime();
4469*01826a49SYabin Cui U64 const maxClockSpan = maxDurationS * SEC_TO_MICRO;
4470*01826a49SYabin Cui int const cLevelLimiter = bigTests ? 3 : 2;
4471*01826a49SYabin Cui
4472*01826a49SYabin Cui /* allocation */
4473*01826a49SYabin Cui cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
4474*01826a49SYabin Cui cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
4475*01826a49SYabin Cui cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
4476*01826a49SYabin Cui cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
4477*01826a49SYabin Cui cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
4478*01826a49SYabin Cui CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4]
4479*01826a49SYabin Cui || !dstBuffer || !mirrorBuffer || !cBuffer || !refCtx || !ctx || !dctx,
4480*01826a49SYabin Cui "Not enough memory, fuzzer tests cancelled");
4481*01826a49SYabin Cui
4482*01826a49SYabin Cui /* Create initial samples */
4483*01826a49SYabin Cui RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
4484*01826a49SYabin Cui RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
4485*01826a49SYabin Cui RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
4486*01826a49SYabin Cui RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
4487*01826a49SYabin Cui RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
4488*01826a49SYabin Cui
4489*01826a49SYabin Cui /* catch up testNb */
4490*01826a49SYabin Cui for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed);
4491*01826a49SYabin Cui
4492*01826a49SYabin Cui /* main test loop */
4493*01826a49SYabin Cui for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < maxClockSpan); testNb++ ) {
4494*01826a49SYabin Cui BYTE* srcBuffer; /* jumping pointer */
4495*01826a49SYabin Cui U32 lseed;
4496*01826a49SYabin Cui size_t sampleSize, maxTestSize, totalTestSize;
4497*01826a49SYabin Cui size_t cSize, totalCSize, totalGenSize;
4498*01826a49SYabin Cui U64 crcOrig;
4499*01826a49SYabin Cui BYTE* sampleBuffer;
4500*01826a49SYabin Cui const BYTE* dict;
4501*01826a49SYabin Cui size_t dictSize;
4502*01826a49SYabin Cui
4503*01826a49SYabin Cui /* notification */
4504*01826a49SYabin Cui if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
4505*01826a49SYabin Cui else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
4506*01826a49SYabin Cui
4507*01826a49SYabin Cui FUZ_rand(&coreSeed);
4508*01826a49SYabin Cui { U32 const prime1 = 2654435761U; lseed = coreSeed ^ prime1; }
4509*01826a49SYabin Cui
4510*01826a49SYabin Cui /* srcBuffer selection [0-4] */
4511*01826a49SYabin Cui { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
4512*01826a49SYabin Cui if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
4513*01826a49SYabin Cui else {
4514*01826a49SYabin Cui buffNb >>= 3;
4515*01826a49SYabin Cui if (buffNb & 7) {
4516*01826a49SYabin Cui const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
4517*01826a49SYabin Cui buffNb = tnb[buffNb >> 3];
4518*01826a49SYabin Cui } else {
4519*01826a49SYabin Cui const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
4520*01826a49SYabin Cui buffNb = tnb[buffNb >> 3];
4521*01826a49SYabin Cui } }
4522*01826a49SYabin Cui srcBuffer = cNoiseBuffer[buffNb];
4523*01826a49SYabin Cui }
4524*01826a49SYabin Cui
4525*01826a49SYabin Cui /* select src segment */
4526*01826a49SYabin Cui sampleSize = FUZ_randomLength(&lseed, maxSampleLog);
4527*01826a49SYabin Cui
4528*01826a49SYabin Cui /* create sample buffer (to catch read error with valgrind & sanitizers) */
4529*01826a49SYabin Cui sampleBuffer = (BYTE*)malloc(sampleSize);
4530*01826a49SYabin Cui CHECK(sampleBuffer==NULL, "not enough memory for sample buffer");
4531*01826a49SYabin Cui { size_t const sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
4532*01826a49SYabin Cui memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); }
4533*01826a49SYabin Cui crcOrig = XXH64(sampleBuffer, sampleSize, 0);
4534*01826a49SYabin Cui
4535*01826a49SYabin Cui /* compression tests */
4536*01826a49SYabin Cui { int const cLevelPositive = (int)
4537*01826a49SYabin Cui ( FUZ_rand(&lseed) %
4538*01826a49SYabin Cui ((U32)ZSTD_maxCLevel() - (FUZ_highbit32((U32)sampleSize) / (U32)cLevelLimiter)) )
4539*01826a49SYabin Cui + 1;
4540*01826a49SYabin Cui int const cLevel = ((FUZ_rand(&lseed) & 15) == 3) ?
4541*01826a49SYabin Cui - (int)((FUZ_rand(&lseed) & 7) + 1) : /* test negative cLevel */
4542*01826a49SYabin Cui cLevelPositive;
4543*01826a49SYabin Cui DISPLAYLEVEL(5, "fuzzer t%u: Simple compression test (level %i) \n", testNb, cLevel);
4544*01826a49SYabin Cui cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel);
4545*01826a49SYabin Cui CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed : %s", ZSTD_getErrorName(cSize));
4546*01826a49SYabin Cui
4547*01826a49SYabin Cui /* compression failure test : too small dest buffer */
4548*01826a49SYabin Cui assert(cSize > 3);
4549*01826a49SYabin Cui { const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1;
4550*01826a49SYabin Cui const size_t tooSmallSize = cSize - missing;
4551*01826a49SYabin Cui const unsigned endMark = 0x4DC2B1A9;
4552*01826a49SYabin Cui memcpy(dstBuffer+tooSmallSize, &endMark, sizeof(endMark));
4553*01826a49SYabin Cui DISPLAYLEVEL(5, "fuzzer t%u: compress into too small buffer of size %u (missing %u bytes) \n",
4554*01826a49SYabin Cui testNb, (unsigned)tooSmallSize, (unsigned)missing);
4555*01826a49SYabin Cui { size_t const errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel);
4556*01826a49SYabin Cui CHECK(ZSTD_getErrorCode(errorCode) != ZSTD_error_dstSize_tooSmall, "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (unsigned)tooSmallSize, (unsigned)cSize); }
4557*01826a49SYabin Cui { unsigned endCheck; memcpy(&endCheck, dstBuffer+tooSmallSize, sizeof(endCheck));
4558*01826a49SYabin Cui CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow (check.%08X != %08X.mark)", endCheck, endMark); }
4559*01826a49SYabin Cui } }
4560*01826a49SYabin Cui
4561*01826a49SYabin Cui /* frame header decompression test */
4562*01826a49SYabin Cui { ZSTD_frameHeader zfh;
4563*01826a49SYabin Cui CHECK_Z( ZSTD_getFrameHeader(&zfh, cBuffer, cSize) );
4564*01826a49SYabin Cui CHECK(zfh.frameContentSize != sampleSize, "Frame content size incorrect");
4565*01826a49SYabin Cui }
4566*01826a49SYabin Cui
4567*01826a49SYabin Cui /* Decompressed size test */
4568*01826a49SYabin Cui { unsigned long long const rSize = ZSTD_findDecompressedSize(cBuffer, cSize);
4569*01826a49SYabin Cui CHECK(rSize != sampleSize, "decompressed size incorrect");
4570*01826a49SYabin Cui }
4571*01826a49SYabin Cui
4572*01826a49SYabin Cui /* successful decompression test */
4573*01826a49SYabin Cui DISPLAYLEVEL(5, "fuzzer t%u: simple decompression test \n", testNb);
4574*01826a49SYabin Cui { size_t const margin = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1;
4575*01826a49SYabin Cui size_t const dSize = ZSTD_decompress(dstBuffer, sampleSize + margin, cBuffer, cSize);
4576*01826a49SYabin Cui CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s) (srcSize : %u ; cSize : %u)", ZSTD_getErrorName(dSize), (unsigned)sampleSize, (unsigned)cSize);
4577*01826a49SYabin Cui { U64 const crcDest = XXH64(dstBuffer, sampleSize, 0);
4578*01826a49SYabin Cui CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (unsigned)findDiff(sampleBuffer, dstBuffer, sampleSize), (unsigned)sampleSize);
4579*01826a49SYabin Cui } }
4580*01826a49SYabin Cui
4581*01826a49SYabin Cui free(sampleBuffer); /* no longer useful after this point */
4582*01826a49SYabin Cui
4583*01826a49SYabin Cui /* truncated src decompression test */
4584*01826a49SYabin Cui DISPLAYLEVEL(5, "fuzzer t%u: decompression of truncated source \n", testNb);
4585*01826a49SYabin Cui { size_t const missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
4586*01826a49SYabin Cui size_t const tooSmallSize = cSize - missing;
4587*01826a49SYabin Cui void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch read overflows */
4588*01826a49SYabin Cui CHECK(cBufferTooSmall == NULL, "not enough memory !");
4589*01826a49SYabin Cui memcpy(cBufferTooSmall, cBuffer, tooSmallSize);
4590*01826a49SYabin Cui { size_t const errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize);
4591*01826a49SYabin Cui CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); }
4592*01826a49SYabin Cui free(cBufferTooSmall);
4593*01826a49SYabin Cui }
4594*01826a49SYabin Cui
4595*01826a49SYabin Cui /* too small dst decompression test */
4596*01826a49SYabin Cui DISPLAYLEVEL(5, "fuzzer t%u: decompress into too small dst buffer \n", testNb);
4597*01826a49SYabin Cui if (sampleSize > 3) {
4598*01826a49SYabin Cui size_t const missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
4599*01826a49SYabin Cui size_t const tooSmallSize = sampleSize - missing;
4600*01826a49SYabin Cui static const BYTE token = 0xA9;
4601*01826a49SYabin Cui dstBuffer[tooSmallSize] = token;
4602*01826a49SYabin Cui { size_t const errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize);
4603*01826a49SYabin Cui CHECK(ZSTD_getErrorCode(errorCode) != ZSTD_error_dstSize_tooSmall, "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (unsigned)errorCode, (unsigned)tooSmallSize); }
4604*01826a49SYabin Cui CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow");
4605*01826a49SYabin Cui }
4606*01826a49SYabin Cui
4607*01826a49SYabin Cui /* noisy src decompression test */
4608*01826a49SYabin Cui if (cSize > 6) {
4609*01826a49SYabin Cui /* insert noise into src */
4610*01826a49SYabin Cui { U32 const maxNbBits = FUZ_highbit32((U32)(cSize-4));
4611*01826a49SYabin Cui size_t pos = 4; /* preserve magic number (too easy to detect) */
4612*01826a49SYabin Cui for (;;) {
4613*01826a49SYabin Cui /* keep some original src */
4614*01826a49SYabin Cui { U32 const nbBits = FUZ_rand(&lseed) % maxNbBits;
4615*01826a49SYabin Cui size_t const mask = (1<<nbBits) - 1;
4616*01826a49SYabin Cui size_t const skipLength = FUZ_rand(&lseed) & mask;
4617*01826a49SYabin Cui pos += skipLength;
4618*01826a49SYabin Cui }
4619*01826a49SYabin Cui if (pos >= cSize) break;
4620*01826a49SYabin Cui /* add noise */
4621*01826a49SYabin Cui { U32 const nbBitsCodes = FUZ_rand(&lseed) % maxNbBits;
4622*01826a49SYabin Cui U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0;
4623*01826a49SYabin Cui size_t const mask = (1<<nbBits) - 1;
4624*01826a49SYabin Cui size_t const rNoiseLength = (FUZ_rand(&lseed) & mask) + 1;
4625*01826a49SYabin Cui size_t const noiseLength = MIN(rNoiseLength, cSize-pos);
4626*01826a49SYabin Cui size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength);
4627*01826a49SYabin Cui memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength);
4628*01826a49SYabin Cui pos += noiseLength;
4629*01826a49SYabin Cui } } }
4630*01826a49SYabin Cui
4631*01826a49SYabin Cui /* decompress noisy source */
4632*01826a49SYabin Cui DISPLAYLEVEL(5, "fuzzer t%u: decompress noisy source \n", testNb);
4633*01826a49SYabin Cui { U32 const endMark = 0xA9B1C3D6;
4634*01826a49SYabin Cui memcpy(dstBuffer+sampleSize, &endMark, 4);
4635*01826a49SYabin Cui { size_t const decompressResult = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize);
4636*01826a49SYabin Cui /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */
4637*01826a49SYabin Cui CHECK((!ZSTD_isError(decompressResult)) && (decompressResult>sampleSize),
4638*01826a49SYabin Cui "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (unsigned)decompressResult, (unsigned)sampleSize);
4639*01826a49SYabin Cui }
4640*01826a49SYabin Cui { U32 endCheck; memcpy(&endCheck, dstBuffer+sampleSize, 4);
4641*01826a49SYabin Cui CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow");
4642*01826a49SYabin Cui } } } /* noisy src decompression test */
4643*01826a49SYabin Cui
4644*01826a49SYabin Cui /*===== Bufferless streaming compression test, scattered segments and dictionary =====*/
4645*01826a49SYabin Cui DISPLAYLEVEL(5, "fuzzer t%u: Bufferless streaming compression test \n", testNb);
4646*01826a49SYabin Cui { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
4647*01826a49SYabin Cui U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
4648*01826a49SYabin Cui int const cLevel = (int)(FUZ_rand(&lseed) %
4649*01826a49SYabin Cui ((U32)ZSTD_maxCLevel() -
4650*01826a49SYabin Cui (MAX(testLog, dictLog) / (U32)cLevelLimiter))) +
4651*01826a49SYabin Cui 1;
4652*01826a49SYabin Cui maxTestSize = FUZ_rLogLength(&lseed, testLog);
4653*01826a49SYabin Cui if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1;
4654*01826a49SYabin Cui
4655*01826a49SYabin Cui dictSize = FUZ_rLogLength(&lseed, dictLog); /* needed also for decompression */
4656*01826a49SYabin Cui dict = srcBuffer + (FUZ_rand(&lseed) % (srcBufferSize - dictSize));
4657*01826a49SYabin Cui
4658*01826a49SYabin Cui DISPLAYLEVEL(6, "fuzzer t%u: Compressing up to <=%u bytes at level %i with dictionary size %u \n",
4659*01826a49SYabin Cui testNb, (unsigned)maxTestSize, cLevel, (unsigned)dictSize);
4660*01826a49SYabin Cui
4661*01826a49SYabin Cui if (FUZ_rand(&lseed) & 0xF) {
4662*01826a49SYabin Cui CHECK_Z ( ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, cLevel) );
4663*01826a49SYabin Cui } else {
4664*01826a49SYabin Cui ZSTD_compressionParameters const cPar = ZSTD_getCParams(cLevel, ZSTD_CONTENTSIZE_UNKNOWN, dictSize);
4665*01826a49SYabin Cui ZSTD_frameParameters const fPar = { FUZ_rand(&lseed)&1 /* contentSizeFlag */,
4666*01826a49SYabin Cui !(FUZ_rand(&lseed)&3) /* contentChecksumFlag*/,
4667*01826a49SYabin Cui 0 /*NodictID*/ }; /* note : since dictionary is fake, dictIDflag has no impact */
4668*01826a49SYabin Cui ZSTD_parameters const p = FUZ_makeParams(cPar, fPar);
4669*01826a49SYabin Cui CHECK_Z ( ZSTD_compressBegin_advanced(refCtx, dict, dictSize, p, 0) );
4670*01826a49SYabin Cui }
4671*01826a49SYabin Cui CHECK_Z( ZSTD_copyCCtx(ctx, refCtx, 0) );
4672*01826a49SYabin Cui }
4673*01826a49SYabin Cui
4674*01826a49SYabin Cui { U32 const nbChunks = (FUZ_rand(&lseed) & 127) + 2;
4675*01826a49SYabin Cui U32 n;
4676*01826a49SYabin Cui XXH64_state_t xxhState;
4677*01826a49SYabin Cui XXH64_reset(&xxhState, 0);
4678*01826a49SYabin Cui for (totalTestSize=0, cSize=0, n=0 ; n<nbChunks ; n++) {
4679*01826a49SYabin Cui size_t const segmentSize = FUZ_randomLength(&lseed, maxSampleLog);
4680*01826a49SYabin Cui size_t const segmentStart = FUZ_rand(&lseed) % (srcBufferSize - segmentSize);
4681*01826a49SYabin Cui
4682*01826a49SYabin Cui if (cBufferSize-cSize < ZSTD_compressBound(segmentSize)) break; /* avoid invalid dstBufferTooSmall */
4683*01826a49SYabin Cui if (totalTestSize+segmentSize > maxTestSize) break;
4684*01826a49SYabin Cui
4685*01826a49SYabin Cui { size_t const compressResult = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+segmentStart, segmentSize);
4686*01826a49SYabin Cui CHECK (ZSTD_isError(compressResult), "multi-segments compression error : %s", ZSTD_getErrorName(compressResult));
4687*01826a49SYabin Cui cSize += compressResult;
4688*01826a49SYabin Cui }
4689*01826a49SYabin Cui XXH64_update(&xxhState, srcBuffer+segmentStart, segmentSize);
4690*01826a49SYabin Cui memcpy(mirrorBuffer + totalTestSize, srcBuffer+segmentStart, segmentSize);
4691*01826a49SYabin Cui totalTestSize += segmentSize;
4692*01826a49SYabin Cui }
4693*01826a49SYabin Cui
4694*01826a49SYabin Cui { size_t const flushResult = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize, NULL, 0);
4695*01826a49SYabin Cui CHECK (ZSTD_isError(flushResult), "multi-segments epilogue error : %s", ZSTD_getErrorName(flushResult));
4696*01826a49SYabin Cui cSize += flushResult;
4697*01826a49SYabin Cui }
4698*01826a49SYabin Cui crcOrig = XXH64_digest(&xxhState);
4699*01826a49SYabin Cui }
4700*01826a49SYabin Cui
4701*01826a49SYabin Cui /* streaming decompression test */
4702*01826a49SYabin Cui DISPLAYLEVEL(5, "fuzzer t%u: Bufferless streaming decompression test \n", testNb);
4703*01826a49SYabin Cui /* ensure memory requirement is good enough (should always be true) */
4704*01826a49SYabin Cui { ZSTD_frameHeader zfh;
4705*01826a49SYabin Cui CHECK( ZSTD_getFrameHeader(&zfh, cBuffer, ZSTD_FRAMEHEADERSIZE_MAX),
4706*01826a49SYabin Cui "ZSTD_getFrameHeader(): error retrieving frame information");
4707*01826a49SYabin Cui { size_t const roundBuffSize = ZSTD_decodingBufferSize_min(zfh.windowSize, zfh.frameContentSize);
4708*01826a49SYabin Cui CHECK_Z(roundBuffSize);
4709*01826a49SYabin Cui CHECK((roundBuffSize > totalTestSize) && (zfh.frameContentSize!=ZSTD_CONTENTSIZE_UNKNOWN),
4710*01826a49SYabin Cui "ZSTD_decodingBufferSize_min() requires more memory (%u) than necessary (%u)",
4711*01826a49SYabin Cui (unsigned)roundBuffSize, (unsigned)totalTestSize );
4712*01826a49SYabin Cui } }
4713*01826a49SYabin Cui if (dictSize<8) dictSize=0, dict=NULL; /* disable dictionary */
4714*01826a49SYabin Cui CHECK_Z( ZSTD_decompressBegin_usingDict(dctx, dict, dictSize) );
4715*01826a49SYabin Cui totalCSize = 0;
4716*01826a49SYabin Cui totalGenSize = 0;
4717*01826a49SYabin Cui while (totalCSize < cSize) {
4718*01826a49SYabin Cui size_t const inSize = ZSTD_nextSrcSizeToDecompress(dctx);
4719*01826a49SYabin Cui size_t const genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize);
4720*01826a49SYabin Cui CHECK (ZSTD_isError(genSize), "ZSTD_decompressContinue error : %s", ZSTD_getErrorName(genSize));
4721*01826a49SYabin Cui totalGenSize += genSize;
4722*01826a49SYabin Cui totalCSize += inSize;
4723*01826a49SYabin Cui }
4724*01826a49SYabin Cui CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded");
4725*01826a49SYabin Cui CHECK (totalGenSize != totalTestSize, "streaming decompressed data : wrong size")
4726*01826a49SYabin Cui CHECK (totalCSize != cSize, "compressed data should be fully read")
4727*01826a49SYabin Cui { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
4728*01826a49SYabin Cui CHECK(crcOrig != crcDest, "streaming decompressed data corrupted (pos %u / %u)",
4729*01826a49SYabin Cui (unsigned)findDiff(mirrorBuffer, dstBuffer, totalTestSize), (unsigned)totalTestSize);
4730*01826a49SYabin Cui }
4731*01826a49SYabin Cui } /* for ( ; (testNb <= nbTests) */
4732*01826a49SYabin Cui DISPLAY("\r%u fuzzer tests completed \n", testNb-1);
4733*01826a49SYabin Cui
4734*01826a49SYabin Cui _cleanup:
4735*01826a49SYabin Cui ZSTD_freeCCtx(refCtx);
4736*01826a49SYabin Cui ZSTD_freeCCtx(ctx);
4737*01826a49SYabin Cui ZSTD_freeDCtx(dctx);
4738*01826a49SYabin Cui free(cNoiseBuffer[0]);
4739*01826a49SYabin Cui free(cNoiseBuffer[1]);
4740*01826a49SYabin Cui free(cNoiseBuffer[2]);
4741*01826a49SYabin Cui free(cNoiseBuffer[3]);
4742*01826a49SYabin Cui free(cNoiseBuffer[4]);
4743*01826a49SYabin Cui free(cBuffer);
4744*01826a49SYabin Cui free(dstBuffer);
4745*01826a49SYabin Cui free(mirrorBuffer);
4746*01826a49SYabin Cui return (int)result;
4747*01826a49SYabin Cui
4748*01826a49SYabin Cui _output_error:
4749*01826a49SYabin Cui result = 1;
4750*01826a49SYabin Cui goto _cleanup;
4751*01826a49SYabin Cui }
4752*01826a49SYabin Cui
4753*01826a49SYabin Cui
4754*01826a49SYabin Cui /*_*******************************************************
4755*01826a49SYabin Cui * Command line
4756*01826a49SYabin Cui *********************************************************/
FUZ_usage(const char * programName)4757*01826a49SYabin Cui static int FUZ_usage(const char* programName)
4758*01826a49SYabin Cui {
4759*01826a49SYabin Cui DISPLAY( "Usage :\n");
4760*01826a49SYabin Cui DISPLAY( " %s [args]\n", programName);
4761*01826a49SYabin Cui DISPLAY( "\n");
4762*01826a49SYabin Cui DISPLAY( "Arguments :\n");
4763*01826a49SYabin Cui DISPLAY( " -i# : Number of tests (default:%i)\n", nbTestsDefault);
4764*01826a49SYabin Cui DISPLAY( " -T# : Max duration to run for. Overrides number of tests. (e.g. -T1m or -T60s for one minute)\n");
4765*01826a49SYabin Cui DISPLAY( " -s# : Select seed (default:prompt user)\n");
4766*01826a49SYabin Cui DISPLAY( " -t# : Select starting test number (default:0)\n");
4767*01826a49SYabin Cui DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_compressibility_default);
4768*01826a49SYabin Cui DISPLAY( " -v : verbose\n");
4769*01826a49SYabin Cui DISPLAY( " -p : pause at the end\n");
4770*01826a49SYabin Cui DISPLAY( " -h : display help and exit\n");
4771*01826a49SYabin Cui return 0;
4772*01826a49SYabin Cui }
4773*01826a49SYabin Cui
4774*01826a49SYabin Cui /*! readU32FromChar() :
4775*01826a49SYabin Cui @return : unsigned integer value read from input in `char` format
4776*01826a49SYabin Cui allows and interprets K, KB, KiB, M, MB and MiB suffix.
4777*01826a49SYabin Cui Will also modify `*stringPtr`, advancing it to position where it stopped reading.
4778*01826a49SYabin Cui Note : function result can overflow if digit string > MAX_UINT */
readU32FromChar(const char ** stringPtr)4779*01826a49SYabin Cui static unsigned readU32FromChar(const char** stringPtr)
4780*01826a49SYabin Cui {
4781*01826a49SYabin Cui unsigned result = 0;
4782*01826a49SYabin Cui while ((**stringPtr >='0') && (**stringPtr <='9'))
4783*01826a49SYabin Cui result *= 10, result += (unsigned)(**stringPtr - '0'), (*stringPtr)++ ;
4784*01826a49SYabin Cui if ((**stringPtr=='K') || (**stringPtr=='M')) {
4785*01826a49SYabin Cui result <<= 10;
4786*01826a49SYabin Cui if (**stringPtr=='M') result <<= 10;
4787*01826a49SYabin Cui (*stringPtr)++ ;
4788*01826a49SYabin Cui if (**stringPtr=='i') (*stringPtr)++;
4789*01826a49SYabin Cui if (**stringPtr=='B') (*stringPtr)++;
4790*01826a49SYabin Cui }
4791*01826a49SYabin Cui return result;
4792*01826a49SYabin Cui }
4793*01826a49SYabin Cui
4794*01826a49SYabin Cui /** longCommandWArg() :
4795*01826a49SYabin Cui * check if *stringPtr is the same as longCommand.
4796*01826a49SYabin Cui * If yes, @return 1 and advances *stringPtr to the position which immediately follows longCommand.
4797*01826a49SYabin Cui * @return 0 and doesn't modify *stringPtr otherwise.
4798*01826a49SYabin Cui */
longCommandWArg(const char ** stringPtr,const char * longCommand)4799*01826a49SYabin Cui static int longCommandWArg(const char** stringPtr, const char* longCommand)
4800*01826a49SYabin Cui {
4801*01826a49SYabin Cui size_t const comSize = strlen(longCommand);
4802*01826a49SYabin Cui int const result = !strncmp(*stringPtr, longCommand, comSize);
4803*01826a49SYabin Cui if (result) *stringPtr += comSize;
4804*01826a49SYabin Cui return result;
4805*01826a49SYabin Cui }
4806*01826a49SYabin Cui
main(int argc,const char ** argv)4807*01826a49SYabin Cui int main(int argc, const char** argv)
4808*01826a49SYabin Cui {
4809*01826a49SYabin Cui U32 seed = 0;
4810*01826a49SYabin Cui int seedset = 0;
4811*01826a49SYabin Cui int argNb;
4812*01826a49SYabin Cui int nbTests = nbTestsDefault;
4813*01826a49SYabin Cui int testNb = 0;
4814*01826a49SYabin Cui int proba = FUZ_compressibility_default;
4815*01826a49SYabin Cui double probfloat;
4816*01826a49SYabin Cui int result = 0;
4817*01826a49SYabin Cui U32 mainPause = 0;
4818*01826a49SYabin Cui U32 maxDuration = 0;
4819*01826a49SYabin Cui int bigTests = 1;
4820*01826a49SYabin Cui int longTests = 0;
4821*01826a49SYabin Cui U32 memTestsOnly = 0;
4822*01826a49SYabin Cui const char* const programName = argv[0];
4823*01826a49SYabin Cui
4824*01826a49SYabin Cui /* Check command line */
4825*01826a49SYabin Cui for (argNb=1; argNb<argc; argNb++) {
4826*01826a49SYabin Cui const char* argument = argv[argNb];
4827*01826a49SYabin Cui if(!argument) continue; /* Protection if argument empty */
4828*01826a49SYabin Cui
4829*01826a49SYabin Cui /* Handle commands. Aggregated commands are allowed */
4830*01826a49SYabin Cui if (argument[0]=='-') {
4831*01826a49SYabin Cui
4832*01826a49SYabin Cui if (longCommandWArg(&argument, "--memtest=")) { memTestsOnly = readU32FromChar(&argument); continue; }
4833*01826a49SYabin Cui
4834*01826a49SYabin Cui if (!strcmp(argument, "--memtest")) { memTestsOnly=1; continue; }
4835*01826a49SYabin Cui if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
4836*01826a49SYabin Cui if (!strcmp(argument, "--long-tests")) { longTests=1; continue; }
4837*01826a49SYabin Cui if (!strcmp(argument, "--no-long-tests")) { longTests=0; continue; }
4838*01826a49SYabin Cui
4839*01826a49SYabin Cui argument++;
4840*01826a49SYabin Cui while (*argument!=0) {
4841*01826a49SYabin Cui switch(*argument)
4842*01826a49SYabin Cui {
4843*01826a49SYabin Cui case 'h':
4844*01826a49SYabin Cui return FUZ_usage(programName);
4845*01826a49SYabin Cui
4846*01826a49SYabin Cui case 'v':
4847*01826a49SYabin Cui argument++;
4848*01826a49SYabin Cui g_displayLevel++;
4849*01826a49SYabin Cui break;
4850*01826a49SYabin Cui
4851*01826a49SYabin Cui case 'q':
4852*01826a49SYabin Cui argument++;
4853*01826a49SYabin Cui g_displayLevel--;
4854*01826a49SYabin Cui break;
4855*01826a49SYabin Cui
4856*01826a49SYabin Cui case 'p': /* pause at the end */
4857*01826a49SYabin Cui argument++;
4858*01826a49SYabin Cui mainPause = 1;
4859*01826a49SYabin Cui break;
4860*01826a49SYabin Cui
4861*01826a49SYabin Cui case 'i':
4862*01826a49SYabin Cui argument++; maxDuration = 0;
4863*01826a49SYabin Cui nbTests = (int)readU32FromChar(&argument);
4864*01826a49SYabin Cui break;
4865*01826a49SYabin Cui
4866*01826a49SYabin Cui case 'T':
4867*01826a49SYabin Cui argument++;
4868*01826a49SYabin Cui nbTests = 0;
4869*01826a49SYabin Cui maxDuration = readU32FromChar(&argument);
4870*01826a49SYabin Cui if (*argument=='s') argument++; /* seconds */
4871*01826a49SYabin Cui if (*argument=='m') maxDuration *= 60, argument++; /* minutes */
4872*01826a49SYabin Cui if (*argument=='n') argument++;
4873*01826a49SYabin Cui break;
4874*01826a49SYabin Cui
4875*01826a49SYabin Cui case 's':
4876*01826a49SYabin Cui argument++;
4877*01826a49SYabin Cui seedset = 1;
4878*01826a49SYabin Cui seed = readU32FromChar(&argument);
4879*01826a49SYabin Cui break;
4880*01826a49SYabin Cui
4881*01826a49SYabin Cui case 't':
4882*01826a49SYabin Cui argument++;
4883*01826a49SYabin Cui testNb = (int)readU32FromChar(&argument);
4884*01826a49SYabin Cui break;
4885*01826a49SYabin Cui
4886*01826a49SYabin Cui case 'P': /* compressibility % */
4887*01826a49SYabin Cui argument++;
4888*01826a49SYabin Cui proba = (int)readU32FromChar(&argument);
4889*01826a49SYabin Cui if (proba>100) proba = 100;
4890*01826a49SYabin Cui break;
4891*01826a49SYabin Cui
4892*01826a49SYabin Cui default:
4893*01826a49SYabin Cui return (FUZ_usage(programName), 1);
4894*01826a49SYabin Cui } } } } /* for (argNb=1; argNb<argc; argNb++) */
4895*01826a49SYabin Cui
4896*01826a49SYabin Cui /* Get Seed */
4897*01826a49SYabin Cui DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
4898*01826a49SYabin Cui
4899*01826a49SYabin Cui if (!seedset) {
4900*01826a49SYabin Cui time_t const t = time(NULL);
4901*01826a49SYabin Cui U32 const h = XXH32(&t, sizeof(t), 1);
4902*01826a49SYabin Cui seed = h % 10000;
4903*01826a49SYabin Cui }
4904*01826a49SYabin Cui
4905*01826a49SYabin Cui DISPLAY("Seed = %u\n", (unsigned)seed);
4906*01826a49SYabin Cui if (proba!=FUZ_compressibility_default) DISPLAY("Compressibility : %i%%\n", proba);
4907*01826a49SYabin Cui
4908*01826a49SYabin Cui probfloat = ((double)proba) / 100;
4909*01826a49SYabin Cui
4910*01826a49SYabin Cui if (memTestsOnly) {
4911*01826a49SYabin Cui g_displayLevel = MAX(3, g_displayLevel);
4912*01826a49SYabin Cui return FUZ_mallocTests(seed, probfloat, memTestsOnly);
4913*01826a49SYabin Cui }
4914*01826a49SYabin Cui
4915*01826a49SYabin Cui if (nbTests < testNb) nbTests = testNb;
4916*01826a49SYabin Cui
4917*01826a49SYabin Cui if (testNb==0) {
4918*01826a49SYabin Cui result = basicUnitTests(0, probfloat); /* constant seed for predictability */
4919*01826a49SYabin Cui
4920*01826a49SYabin Cui if (!result && longTests) {
4921*01826a49SYabin Cui result = longUnitTests(0, probfloat);
4922*01826a49SYabin Cui }
4923*01826a49SYabin Cui }
4924*01826a49SYabin Cui if (!result)
4925*01826a49SYabin Cui result = fuzzerTests(seed, (unsigned)nbTests, (unsigned)testNb, maxDuration, ((double)proba) / 100, bigTests);
4926*01826a49SYabin Cui if (mainPause) {
4927*01826a49SYabin Cui int unused;
4928*01826a49SYabin Cui DISPLAY("Press Enter \n");
4929*01826a49SYabin Cui unused = getchar();
4930*01826a49SYabin Cui (void)unused;
4931*01826a49SYabin Cui }
4932*01826a49SYabin Cui return result;
4933*01826a49SYabin Cui }
4934