Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under the BSD-style license found in the |
| 6 | * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 9 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 10 | |
| 11 | /*-************************************ |
| 12 | * Compiler specific |
| 13 | **************************************/ |
| 14 | #ifdef _MSC_VER /* Visual Studio */ |
| 15 | # define _CRT_SECURE_NO_WARNINGS /* fgets */ |
| 16 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 17 | # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ |
| 18 | #endif |
| 19 | |
| 20 | |
| 21 | /*-************************************ |
| 22 | * Includes |
| 23 | **************************************/ |
| 24 | #include <stdlib.h> /* free */ |
| 25 | #include <stdio.h> /* fgets, sscanf */ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 26 | #include <time.h> /* clock_t, clock() */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 27 | #include <string.h> /* strcmp */ |
| 28 | #include "mem.h" |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 29 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 30 | #include "zstd.h" /* ZSTD_compressBound */ |
| 31 | #include "datagen.h" /* RDG_genBuffer */ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 32 | #define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 33 | #include "xxhash.h" /* XXH64_* */ |
| 34 | |
| 35 | |
| 36 | /*-************************************ |
| 37 | * Constants |
| 38 | **************************************/ |
| 39 | #define KB *(1U<<10) |
| 40 | #define MB *(1U<<20) |
| 41 | #define GB *(1U<<30) |
| 42 | |
| 43 | static const U32 nbTestsDefault = 10000; |
| 44 | #define COMPRESSIBLE_NOISE_LENGTH (10 MB) |
| 45 | #define FUZ_COMPRESSIBILITY_DEFAULT 50 |
| 46 | static const U32 prime1 = 2654435761U; |
| 47 | static const U32 prime2 = 2246822519U; |
| 48 | |
| 49 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 50 | /*-************************************ |
| 51 | * Display Macros |
| 52 | **************************************/ |
| 53 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 54 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 55 | static U32 g_displayLevel = 2; |
| 56 | |
| 57 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 58 | if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ |
| 59 | { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 60 | if (g_displayLevel>=4) fflush(stdout); } } |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 61 | static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 62 | static clock_t g_displayClock = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 63 | |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 64 | static clock_t g_clockTime = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 65 | |
| 66 | |
| 67 | /*-******************************************************* |
| 68 | * Fuzzer functions |
| 69 | *********************************************************/ |
| 70 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
| 71 | |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 72 | static clock_t FUZ_GetClockSpan(clock_t clockStart) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 73 | { |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 74 | return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /*! FUZ_rand() : |
| 78 | @return : a 27 bits random value, from a 32-bits `seed`. |
| 79 | `seed` is also modified */ |
| 80 | # define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 81 | unsigned int FUZ_rand(unsigned int* seedPtr) |
| 82 | { |
| 83 | U32 rand32 = *seedPtr; |
| 84 | rand32 *= prime1; |
| 85 | rand32 += prime2; |
| 86 | rand32 = FUZ_rotl32(rand32, 13); |
| 87 | *seedPtr = rand32; |
| 88 | return rand32 >> 5; |
| 89 | } |
| 90 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 91 | /* |
| 92 | static unsigned FUZ_highbit32(U32 v32) |
| 93 | { |
| 94 | unsigned nbBits = 0; |
| 95 | if (v32==0) return 0; |
| 96 | for ( ; v32 ; v32>>=1) nbBits++; |
| 97 | return nbBits; |
| 98 | } |
| 99 | */ |
| 100 | |
| 101 | static void* allocFunction(void* opaque, size_t size) |
| 102 | { |
| 103 | void* address = malloc(size); |
| 104 | (void)opaque; |
| 105 | return address; |
| 106 | } |
| 107 | |
| 108 | static void freeFunction(void* opaque, void* address) |
| 109 | { |
| 110 | (void)opaque; |
| 111 | free(address); |
| 112 | } |
| 113 | |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 114 | |
| 115 | /*====================================================== |
| 116 | * Basic Unit tests |
| 117 | ======================================================*/ |
| 118 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 119 | static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem) |
| 120 | { |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 121 | size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 122 | void* CNBuffer = malloc(CNBufferSize); |
| 123 | size_t const skippableFrameSize = 11; |
| 124 | size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH); |
| 125 | void* compressedBuffer = malloc(compressedBufferSize); |
| 126 | size_t const decodedBufferSize = CNBufferSize; |
| 127 | void* decodedBuffer = malloc(decodedBufferSize); |
| 128 | size_t cSize; |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 129 | int testResult = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 130 | U32 testNb=0; |
| 131 | ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem); |
| 132 | ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 133 | ZSTD_inBuffer inBuff; |
| 134 | ZSTD_outBuffer outBuff; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 135 | |
| 136 | /* Create compressible test buffer */ |
| 137 | if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) { |
| 138 | DISPLAY("Not enough memory, aborting\n"); |
| 139 | goto _output_error; |
| 140 | } |
| 141 | RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed); |
| 142 | |
| 143 | /* generate skippable frame */ |
| 144 | MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START); |
| 145 | MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize); |
| 146 | cSize = skippableFrameSize + 8; |
| 147 | |
| 148 | /* Basic compression test */ |
| 149 | DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
| 150 | ZSTD_initCStream_usingDict(zc, CNBuffer, 128 KB, 1); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 151 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 152 | outBuff.size = compressedBufferSize; |
| 153 | outBuff.pos = 0; |
| 154 | inBuff.src = CNBuffer; |
| 155 | inBuff.size = CNBufferSize; |
| 156 | inBuff.pos = 0; |
| 157 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 158 | if (ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 159 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 160 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
Yann Collet | 9a021c1 | 2016-08-26 09:05:06 +0200 | [diff] [blame] | 161 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 162 | cSize += outBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 163 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
| 164 | |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 165 | DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++); |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 166 | { size_t const s = ZSTD_sizeof_CStream(zc); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 167 | if (ZSTD_isError(s)) goto _output_error; |
| 168 | DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s); |
| 169 | } |
| 170 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 171 | /* skippable frame test */ |
| 172 | DISPLAYLEVEL(4, "test%3i : decompress skippable frame : ", testNb++); |
| 173 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 174 | inBuff.src = compressedBuffer; |
| 175 | inBuff.size = cSize; |
| 176 | inBuff.pos = 0; |
| 177 | outBuff.dst = decodedBuffer; |
| 178 | outBuff.size = CNBufferSize; |
| 179 | outBuff.pos = 0; |
| 180 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 181 | if (r != 0) goto _output_error; } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 182 | if (outBuff.pos != 0) goto _output_error; /* skippable frame len is 0 */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 183 | DISPLAYLEVEL(4, "OK \n"); |
| 184 | |
| 185 | /* Basic decompression test */ |
| 186 | DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
| 187 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 188 | { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000000000); /* large limit */ |
| 189 | if (ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 190 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 191 | if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 192 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 193 | if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 194 | DISPLAYLEVEL(4, "OK \n"); |
| 195 | |
| 196 | /* check regenerated data is byte exact */ |
| 197 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 198 | { size_t i; |
| 199 | for (i=0; i<CNBufferSize; i++) { |
| 200 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error; |
| 201 | } } |
| 202 | DISPLAYLEVEL(4, "OK \n"); |
| 203 | |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 204 | DISPLAYLEVEL(4, "test%3i : check DStream size : ", testNb++); |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 205 | { size_t const s = ZSTD_sizeof_DStream(zd); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 206 | if (ZSTD_isError(s)) goto _output_error; |
| 207 | DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s); |
| 208 | } |
| 209 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 210 | /* Byte-by-byte decompression test */ |
| 211 | DISPLAYLEVEL(4, "test%3i : decompress byte-by-byte : ", testNb++); |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame^] | 212 | { /* skippable frame */ |
| 213 | size_t r = 1; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 214 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 215 | inBuff.src = compressedBuffer; |
| 216 | outBuff.dst = decodedBuffer; |
| 217 | inBuff.pos = 0; |
| 218 | outBuff.pos = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 219 | while (r) { /* skippable frame */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 220 | inBuff.size = inBuff.pos + 1; |
| 221 | outBuff.size = outBuff.pos + 1; |
| 222 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 223 | if (ZSTD_isError(r)) goto _output_error; |
| 224 | } |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame^] | 225 | /* normal frame */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 226 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
| 227 | r=1; |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame^] | 228 | while (r) { |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 229 | inBuff.size = inBuff.pos + 1; |
| 230 | outBuff.size = outBuff.pos + 1; |
| 231 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 232 | if (ZSTD_isError(r)) goto _output_error; |
| 233 | } |
| 234 | } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 235 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 236 | if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 237 | DISPLAYLEVEL(4, "OK \n"); |
| 238 | |
| 239 | /* check regenerated data is byte exact */ |
| 240 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 241 | { size_t i; |
| 242 | for (i=0; i<CNBufferSize; i++) { |
| 243 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; |
| 244 | } } |
| 245 | DISPLAYLEVEL(4, "OK \n"); |
| 246 | |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 247 | /* Complex context re-use scenario */ |
| 248 | DISPLAYLEVEL(4, "test%3i : context re-use : ", testNb++); |
| 249 | ZSTD_freeCStream(zc); |
| 250 | zc = ZSTD_createCStream_advanced(customMem); |
| 251 | if (zc==NULL) goto _output_error; /* memory allocation issue */ |
| 252 | /* use 1 */ |
| 253 | { size_t const inSize = 513; |
| 254 | ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */ |
| 255 | inBuff.src = CNBuffer; |
| 256 | inBuff.size = inSize; |
| 257 | inBuff.pos = 0; |
| 258 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 259 | outBuff.size = ZSTD_compressBound(inSize); |
| 260 | outBuff.pos = 0; |
| 261 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 262 | if (ZSTD_isError(r)) goto _output_error; } |
| 263 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 264 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 265 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 266 | } |
| 267 | /* use 2 */ |
| 268 | { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */ |
| 269 | ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */ |
| 270 | inBuff.src = CNBuffer; |
| 271 | inBuff.size = inSize; |
| 272 | inBuff.pos = 0; |
| 273 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 274 | outBuff.size = ZSTD_compressBound(inSize); |
| 275 | outBuff.pos = 0; |
| 276 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 277 | if (ZSTD_isError(r)) goto _output_error; } |
| 278 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 279 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 280 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 281 | } |
| 282 | DISPLAYLEVEL(4, "OK \n"); |
| 283 | |
| 284 | DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++); |
| 285 | { size_t const s = ZSTD_sizeof_CStream(zc); |
| 286 | if (ZSTD_isError(s)) goto _output_error; |
| 287 | DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s); |
| 288 | } |
| 289 | |
| 290 | /* test ZSTD_setDStreamParameter() resilience */ |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 291 | DISPLAYLEVEL(4, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++); |
| 292 | { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */ |
| 293 | if (!ZSTD_isError(r)) goto _output_error; } |
| 294 | DISPLAYLEVEL(4, "OK \n"); |
| 295 | |
| 296 | /* Memory restriction */ |
| 297 | DISPLAYLEVEL(4, "test%3i : maxWindowSize < frame requirement : ", testNb++); |
| 298 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
| 299 | { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000); /* too small limit */ |
| 300 | if (ZSTD_isError(r)) goto _output_error; } |
| 301 | inBuff.src = compressedBuffer; |
| 302 | inBuff.size = cSize; |
| 303 | inBuff.pos = 0; |
| 304 | outBuff.dst = decodedBuffer; |
| 305 | outBuff.size = CNBufferSize; |
| 306 | outBuff.pos = 0; |
| 307 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 308 | if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */ |
| 309 | DISPLAYLEVEL(4, "OK (%s)\n", ZSTD_getErrorName(r)); } |
| 310 | |
| 311 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 312 | _end: |
| 313 | ZSTD_freeCStream(zc); |
| 314 | ZSTD_freeDStream(zd); |
| 315 | free(CNBuffer); |
| 316 | free(compressedBuffer); |
| 317 | free(decodedBuffer); |
| 318 | return testResult; |
| 319 | |
| 320 | _output_error: |
| 321 | testResult = 1; |
| 322 | DISPLAY("Error detected in Unit tests ! \n"); |
| 323 | goto _end; |
| 324 | } |
| 325 | |
| 326 | |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame^] | 327 | /* ====== Fuzzer tests ====== */ |
| 328 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 329 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 330 | { |
| 331 | const BYTE* b1 = (const BYTE*)buf1; |
| 332 | const BYTE* b2 = (const BYTE*)buf2; |
| 333 | size_t u; |
| 334 | for (u=0; u<max; u++) { |
| 335 | if (b1[u] != b2[u]) break; |
| 336 | } |
| 337 | return u; |
| 338 | } |
| 339 | |
| 340 | static size_t FUZ_rLogLength(U32* seed, U32 logLength) |
| 341 | { |
| 342 | size_t const lengthMask = ((size_t)1 << logLength) - 1; |
| 343 | return (lengthMask+1) + (FUZ_rand(seed) & lengthMask); |
| 344 | } |
| 345 | |
| 346 | static size_t FUZ_randomLength(U32* seed, U32 maxLog) |
| 347 | { |
| 348 | U32 const logLength = FUZ_rand(seed) % maxLog; |
| 349 | return FUZ_rLogLength(seed, logLength); |
| 350 | } |
| 351 | |
| 352 | #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) |
| 353 | |
| 354 | #define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ |
| 355 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } |
| 356 | |
| 357 | static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility) |
| 358 | { |
| 359 | static const U32 maxSrcLog = 24; |
| 360 | static const U32 maxSampleLog = 19; |
| 361 | BYTE* cNoiseBuffer[5]; |
| 362 | size_t srcBufferSize = (size_t)1<<maxSrcLog; |
| 363 | BYTE* copyBuffer; |
| 364 | size_t copyBufferSize= srcBufferSize + (1<<maxSampleLog); |
| 365 | BYTE* cBuffer; |
| 366 | size_t cBufferSize = ZSTD_compressBound(srcBufferSize); |
| 367 | BYTE* dstBuffer; |
| 368 | size_t dstBufferSize = srcBufferSize; |
| 369 | U32 result = 0; |
| 370 | U32 testNb = 0; |
| 371 | U32 coreSeed = seed; |
| 372 | ZSTD_CStream* zc; |
| 373 | ZSTD_DStream* zd; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 374 | clock_t startClock = clock(); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 375 | |
| 376 | /* allocations */ |
| 377 | zc = ZSTD_createCStream(); |
| 378 | zd = ZSTD_createDStream(); |
| 379 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 380 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 381 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 382 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 383 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
| 384 | copyBuffer= (BYTE*)malloc (copyBufferSize); |
| 385 | dstBuffer = (BYTE*)malloc (dstBufferSize); |
| 386 | cBuffer = (BYTE*)malloc (cBufferSize); |
| 387 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
| 388 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd, |
| 389 | "Not enough memory, fuzzer tests cancelled"); |
| 390 | |
| 391 | /* Create initial samples */ |
| 392 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 393 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 394 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 395 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 396 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 397 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
| 398 | |
| 399 | /* catch up testNb */ |
| 400 | for (testNb=1; testNb < startTest; testNb++) |
| 401 | FUZ_rand(&coreSeed); |
| 402 | |
| 403 | /* test loop */ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 404 | for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 405 | U32 lseed; |
| 406 | const BYTE* srcBuffer; |
| 407 | const BYTE* dict; |
| 408 | size_t maxTestSize, dictSize; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 409 | size_t cSize, totalTestSize, totalGenSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 410 | U32 n, nbChunks; |
| 411 | XXH64_state_t xxhState; |
| 412 | U64 crcOrig; |
| 413 | |
| 414 | /* init */ |
| 415 | DISPLAYUPDATE(2, "\r%6u", testNb); |
| 416 | if (nbTests >= testNb) DISPLAYUPDATE(2, "/%6u ", nbTests); |
| 417 | FUZ_rand(&coreSeed); |
| 418 | lseed = coreSeed ^ prime1; |
| 419 | |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame^] | 420 | /* states full reset (deliberately not synchronized) */ |
| 421 | /* some issues can only happen when reusing states */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 422 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); } |
| 423 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZSTD_freeDStream(zd); zd = ZSTD_createDStream(); } |
| 424 | |
| 425 | /* srcBuffer selection [0-4] */ |
| 426 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 427 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 428 | else { |
| 429 | buffNb >>= 3; |
| 430 | if (buffNb & 7) { |
| 431 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 432 | buffNb = tnb[buffNb >> 3]; |
| 433 | } else { |
| 434 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 435 | buffNb = tnb[buffNb >> 3]; |
| 436 | } } |
| 437 | srcBuffer = cNoiseBuffer[buffNb]; |
| 438 | } |
| 439 | |
| 440 | /* compression init */ |
| 441 | { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
| 442 | U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1; |
| 443 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 444 | dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 445 | /* random dictionary selection */ |
| 446 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 447 | dict = srcBuffer + dictStart; |
| 448 | } |
| 449 | { ZSTD_parameters params = ZSTD_getParams(cLevel, 0, dictSize); |
| 450 | params.fParams.checksumFlag = FUZ_rand(&lseed) & 1; |
| 451 | params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1; |
| 452 | { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, 0); |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 453 | CHECK (ZSTD_isError(initError),"ZSTD_initCStream_advanced error : %s", ZSTD_getErrorName(initError)); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 454 | } } } |
| 455 | |
| 456 | /* multi-segments compression test */ |
| 457 | XXH64_reset(&xxhState, 0); |
| 458 | nbChunks = (FUZ_rand(&lseed) & 127) + 2; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 459 | { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 460 | for (n=0, cSize=0, totalTestSize=0 ; (n<nbChunks) && (totalTestSize < maxTestSize) ; n++) { |
| 461 | /* compress random chunk into random size dst buffer */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 462 | { size_t const readChunkSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 463 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - readChunkSize); |
| 464 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 465 | size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 466 | ZSTD_inBuffer inBuff = { srcBuffer+srcStart, readChunkSize, 0 }; |
| 467 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 468 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 469 | { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 470 | CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 471 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 472 | XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); |
| 473 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); |
| 474 | totalTestSize += inBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* random flush operation, to mess around */ |
| 478 | if ((FUZ_rand(&lseed) & 15) == 0) { |
| 479 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 480 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 481 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 482 | { size_t const flushError = ZSTD_flushStream(zc, &outBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 483 | CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError)); |
| 484 | } } } |
| 485 | |
| 486 | /* final frame epilogue */ |
| 487 | { size_t remainingToFlush = (size_t)(-1); |
| 488 | while (remainingToFlush) { |
| 489 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 490 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 491 | U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 492 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 493 | remainingToFlush = ZSTD_endStream(zc, &outBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 494 | CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush)); |
| 495 | CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush); |
| 496 | } } |
| 497 | crcOrig = XXH64_digest(&xxhState); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 498 | cSize = outBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* multi - fragments decompression test */ |
| 502 | ZSTD_initDStream_usingDict(zd, dict, dictSize); |
| 503 | { size_t decompressionResult = 1; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 504 | ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 505 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 506 | for (totalGenSize = 0 ; decompressionResult ; ) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 507 | size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 508 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 509 | size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 510 | inBuff.size = inBuff.pos + readCSrcSize; |
| 511 | outBuff.size = inBuff.pos + dstBuffSize; |
| 512 | decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 513 | CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult)); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 514 | } |
| 515 | CHECK (decompressionResult != 0, "frame not fully decoded"); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 516 | CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size") |
| 517 | CHECK (inBuff.pos != cSize, "compressed data should be fully read") |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 518 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 519 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 520 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); |
| 521 | } } |
| 522 | |
| 523 | /*===== noisy/erroneous src decompression test =====*/ |
| 524 | |
| 525 | /* add some noise */ |
| 526 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 527 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
| 528 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 529 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
| 530 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 531 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 532 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 533 | } } |
| 534 | |
| 535 | /* try decompression on noisy data */ |
| 536 | ZSTD_initDStream(zd); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 537 | { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 538 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 539 | while (outBuff.pos < dstBufferSize) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 540 | size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 541 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 542 | size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize); |
| 543 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 544 | inBuff.size = inBuff.pos + randomCSrcSize; |
| 545 | { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 546 | if (ZSTD_isError(decompressError)) break; /* error correctly detected */ |
| 547 | } } } } |
| 548 | DISPLAY("\r%u fuzzer tests completed \n", testNb); |
| 549 | |
| 550 | _cleanup: |
| 551 | ZSTD_freeCStream(zc); |
| 552 | ZSTD_freeDStream(zd); |
| 553 | free(cNoiseBuffer[0]); |
| 554 | free(cNoiseBuffer[1]); |
| 555 | free(cNoiseBuffer[2]); |
| 556 | free(cNoiseBuffer[3]); |
| 557 | free(cNoiseBuffer[4]); |
| 558 | free(copyBuffer); |
| 559 | free(cBuffer); |
| 560 | free(dstBuffer); |
| 561 | return result; |
| 562 | |
| 563 | _output_error: |
| 564 | result = 1; |
| 565 | goto _cleanup; |
| 566 | } |
| 567 | |
| 568 | |
| 569 | /*-******************************************************* |
| 570 | * Command line |
| 571 | *********************************************************/ |
| 572 | int FUZ_usage(const char* programName) |
| 573 | { |
| 574 | DISPLAY( "Usage :\n"); |
| 575 | DISPLAY( " %s [args]\n", programName); |
| 576 | DISPLAY( "\n"); |
| 577 | DISPLAY( "Arguments :\n"); |
| 578 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 579 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 580 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
| 581 | DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); |
| 582 | DISPLAY( " -v : verbose\n"); |
| 583 | DISPLAY( " -p : pause at the end\n"); |
| 584 | DISPLAY( " -h : display help and exit\n"); |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | |
| 589 | int main(int argc, const char** argv) |
| 590 | { |
| 591 | U32 seed=0; |
| 592 | int seedset=0; |
| 593 | int argNb; |
| 594 | int nbTests = nbTestsDefault; |
| 595 | int testNb = 0; |
| 596 | int proba = FUZ_COMPRESSIBILITY_DEFAULT; |
| 597 | int result=0; |
| 598 | U32 mainPause = 0; |
| 599 | const char* programName = argv[0]; |
| 600 | ZSTD_customMem customMem = { allocFunction, freeFunction, NULL }; |
| 601 | ZSTD_customMem customNULL = { NULL, NULL, NULL }; |
| 602 | |
| 603 | /* Check command line */ |
| 604 | for(argNb=1; argNb<argc; argNb++) { |
| 605 | const char* argument = argv[argNb]; |
| 606 | if(!argument) continue; /* Protection if argument empty */ |
| 607 | |
| 608 | /* Parsing commands. Aggregated commands are allowed */ |
| 609 | if (argument[0]=='-') { |
| 610 | argument++; |
| 611 | |
| 612 | while (*argument!=0) { |
| 613 | switch(*argument) |
| 614 | { |
| 615 | case 'h': |
| 616 | return FUZ_usage(programName); |
| 617 | case 'v': |
| 618 | argument++; |
| 619 | g_displayLevel=4; |
| 620 | break; |
| 621 | case 'q': |
| 622 | argument++; |
| 623 | g_displayLevel--; |
| 624 | break; |
| 625 | case 'p': /* pause at the end */ |
| 626 | argument++; |
| 627 | mainPause = 1; |
| 628 | break; |
| 629 | |
| 630 | case 'i': |
| 631 | argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 632 | nbTests=0; g_clockTime=0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 633 | while ((*argument>='0') && (*argument<='9')) { |
| 634 | nbTests *= 10; |
| 635 | nbTests += *argument - '0'; |
| 636 | argument++; |
| 637 | } |
| 638 | break; |
| 639 | |
| 640 | case 'T': |
| 641 | argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 642 | nbTests=0; g_clockTime=0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 643 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 644 | g_clockTime *= 10; |
| 645 | g_clockTime += *argument - '0'; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 646 | argument++; |
| 647 | } |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 648 | if (*argument=='m') g_clockTime *=60, argument++; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 649 | if (*argument=='n') argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 650 | g_clockTime *= CLOCKS_PER_SEC; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 651 | break; |
| 652 | |
| 653 | case 's': |
| 654 | argument++; |
| 655 | seed=0; |
| 656 | seedset=1; |
| 657 | while ((*argument>='0') && (*argument<='9')) { |
| 658 | seed *= 10; |
| 659 | seed += *argument - '0'; |
| 660 | argument++; |
| 661 | } |
| 662 | break; |
| 663 | |
| 664 | case 't': |
| 665 | argument++; |
| 666 | testNb=0; |
| 667 | while ((*argument>='0') && (*argument<='9')) { |
| 668 | testNb *= 10; |
| 669 | testNb += *argument - '0'; |
| 670 | argument++; |
| 671 | } |
| 672 | break; |
| 673 | |
| 674 | case 'P': /* compressibility % */ |
| 675 | argument++; |
| 676 | proba=0; |
| 677 | while ((*argument>='0') && (*argument<='9')) { |
| 678 | proba *= 10; |
| 679 | proba += *argument - '0'; |
| 680 | argument++; |
| 681 | } |
| 682 | if (proba<0) proba=0; |
| 683 | if (proba>100) proba=100; |
| 684 | break; |
| 685 | |
| 686 | default: |
| 687 | return FUZ_usage(programName); |
| 688 | } |
| 689 | } } } /* for(argNb=1; argNb<argc; argNb++) */ |
| 690 | |
| 691 | /* Get Seed */ |
Yann Collet | bb85581 | 2016-08-25 19:11:11 +0200 | [diff] [blame] | 692 | DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 693 | |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 694 | if (!seedset) { |
| 695 | time_t const t = time(NULL); |
| 696 | U32 const h = XXH32(&t, sizeof(t), 1); |
| 697 | seed = h % 10000; |
| 698 | } |
| 699 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 700 | DISPLAY("Seed = %u\n", seed); |
| 701 | if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba); |
| 702 | |
| 703 | if (nbTests<=0) nbTests=1; |
| 704 | |
| 705 | if (testNb==0) { |
| 706 | result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */ |
| 707 | if (!result) { |
| 708 | DISPLAYLEVEL(4, "Unit tests using customMem :\n") |
| 709 | result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */ |
| 710 | } } |
| 711 | |
| 712 | if (!result) |
| 713 | result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100); |
| 714 | |
| 715 | if (mainPause) { |
| 716 | int unused; |
| 717 | DISPLAY("Press Enter \n"); |
| 718 | unused = getchar(); |
| 719 | (void)unused; |
| 720 | } |
| 721 | return result; |
| 722 | } |