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 | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 9 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 10 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 11 | /*-************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 12 | * Compiler specific |
| 13 | **************************************/ |
| 14 | #ifdef _MSC_VER /* Visual Studio */ |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 15 | # define _CRT_SECURE_NO_WARNINGS /* fgets */ |
| 16 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 17 | # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 18 | #endif |
| 19 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 20 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 21 | /*-************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 22 | * Includes |
| 23 | **************************************/ |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 24 | #include <stdlib.h> /* free */ |
| 25 | #include <stdio.h> /* fgets, sscanf */ |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 26 | #include <string.h> /* strcmp */ |
| 27 | #include <time.h> /* clock_t */ |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 28 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressContinue, ZSTD_compressBlock */ |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 29 | #include "zstd.h" /* ZSTD_VERSION_STRING */ |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 30 | #include "zstd_errors.h" /* ZSTD_getErrorCode */ |
Yann Collet | 0ef6803 | 2017-03-29 16:58:57 -0700 | [diff] [blame] | 31 | #include "zstdmt_compress.h" |
Nick Terrell | a8b4fe0 | 2017-01-02 18:45:19 -0800 | [diff] [blame] | 32 | #define ZDICT_STATIC_LINKING_ONLY |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 33 | #include "zdict.h" /* ZDICT_trainFromBuffer */ |
| 34 | #include "datagen.h" /* RDG_genBuffer */ |
Yann Collet | 353c5d2 | 2015-10-21 14:39:26 +0100 | [diff] [blame] | 35 | #include "mem.h" |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 36 | #define XXH_STATIC_LINKING_ONLY |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 37 | #include "xxhash.h" /* XXH64 */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 38 | |
| 39 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 40 | /*-************************************ |
| 41 | * Constants |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 42 | **************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 43 | #define KB *(1U<<10) |
| 44 | #define MB *(1U<<20) |
| 45 | #define GB *(1U<<30) |
| 46 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 47 | static const U32 FUZ_compressibility_default = 50; |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 48 | static const U32 nbTestsDefault = 30000; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 49 | |
| 50 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 51 | /*-************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 52 | * Display Macros |
| 53 | **************************************/ |
| 54 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 55 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 56 | static U32 g_displayLevel = 2; |
| 57 | |
| 58 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 59 | if ((FUZ_clockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ |
| 60 | { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \ |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 61 | if (g_displayLevel>=4) fflush(stderr); } } |
Yann Collet | 64deef3 | 2016-09-14 00:16:07 +0200 | [diff] [blame] | 62 | static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6; |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 63 | static clock_t g_displayClock = 0; |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 64 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 65 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 66 | /*-******************************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 67 | * Fuzzer functions |
| 68 | *********************************************************/ |
Yann Collet | e93add0 | 2016-02-15 17:44:14 +0100 | [diff] [blame] | 69 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 70 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 71 | |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 72 | static clock_t FUZ_clockSpan(clock_t cStart) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 73 | { |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 74 | return clock() - cStart; /* works even when overflow; max span ~ 30mn */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 77 | #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 78 | static unsigned FUZ_rand(unsigned* src) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 79 | { |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 80 | static const U32 prime1 = 2654435761U; |
| 81 | static const U32 prime2 = 2246822519U; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 82 | U32 rand32 = *src; |
| 83 | rand32 *= prime1; |
| 84 | rand32 += prime2; |
| 85 | rand32 = FUZ_rotl32(rand32, 13); |
| 86 | *src = rand32; |
| 87 | return rand32 >> 5; |
| 88 | } |
| 89 | |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 90 | static unsigned FUZ_highbit32(U32 v32) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 91 | { |
| 92 | unsigned nbBits = 0; |
| 93 | if (v32==0) return 0; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 94 | while (v32) v32 >>= 1, nbBits++; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 95 | return nbBits; |
| 96 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 97 | |
| 98 | |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 99 | /*============================================= |
| 100 | * Basic Unit tests |
| 101 | =============================================*/ |
| 102 | |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 103 | #define CHECK_V(var, fn) size_t const var = fn; if (ZSTD_isError(var)) goto _output_error |
| 104 | #define CHECK(fn) { CHECK_V(err, fn); } |
| 105 | #define CHECKPLUS(var, fn, more) { CHECK_V(var, fn); more; } |
Yann Collet | 0fdc71c | 2017-05-24 17:41:41 -0700 | [diff] [blame] | 106 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 107 | static int basicUnitTests(U32 seed, double compressibility) |
| 108 | { |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 109 | size_t const CNBuffSize = 5 MB; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 110 | void* const CNBuffer = malloc(CNBuffSize); |
| 111 | void* const compressedBuffer = malloc(ZSTD_compressBound(CNBuffSize)); |
| 112 | void* const decodedBuffer = malloc(CNBuffSize); |
Yann Collet | c0b1731 | 2017-02-28 01:02:46 -0800 | [diff] [blame] | 113 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 114 | int testResult = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 115 | U32 testNb=0; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 116 | size_t cSize; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 117 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 118 | /* Create compressible noise */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 119 | if (!CNBuffer || !compressedBuffer || !decodedBuffer) { |
Yann Collet | 94f998b | 2015-07-04 23:10:40 -0800 | [diff] [blame] | 120 | DISPLAY("Not enough memory, aborting\n"); |
| 121 | testResult = 1; |
| 122 | goto _end; |
| 123 | } |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 124 | RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 125 | |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 126 | /* Basic tests */ |
Yann Collet | 9a69ec4 | 2016-08-01 16:25:58 +0200 | [diff] [blame] | 127 | DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName : ", testNb++); |
| 128 | { const char* errorString = ZSTD_getErrorName(0); |
| 129 | DISPLAYLEVEL(4, "OK : %s \n", errorString); |
| 130 | } |
| 131 | |
| 132 | DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName with wrong value : ", testNb++); |
| 133 | { const char* errorString = ZSTD_getErrorName(499); |
| 134 | DISPLAYLEVEL(4, "OK : %s \n", errorString); |
| 135 | } |
| 136 | |
Yann Collet | 0ef6803 | 2017-03-29 16:58:57 -0700 | [diff] [blame] | 137 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 138 | DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, (U32)CNBuffSize); |
| 139 | CHECKPLUS(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 140 | CNBuffer, CNBuffSize, 1), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 141 | cSize=r ); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 142 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 143 | |
Yann Collet | 0ef6803 | 2017-03-29 16:58:57 -0700 | [diff] [blame] | 144 | |
| 145 | DISPLAYLEVEL(4, "test%3i : ZSTD_getFrameContentSize test : ", testNb++); |
| 146 | { unsigned long long const rSize = ZSTD_getFrameContentSize(compressedBuffer, cSize); |
| 147 | if (rSize != CNBuffSize) goto _output_error; |
| 148 | } |
| 149 | DISPLAYLEVEL(4, "OK \n"); |
| 150 | |
| 151 | DISPLAYLEVEL(4, "test%3i : ZSTD_findDecompressedSize test : ", testNb++); |
Sean Purcell | 4e70971 | 2017-02-07 13:50:09 -0800 | [diff] [blame] | 152 | { unsigned long long const rSize = ZSTD_findDecompressedSize(compressedBuffer, cSize); |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 153 | if (rSize != CNBuffSize) goto _output_error; |
| 154 | } |
| 155 | DISPLAYLEVEL(4, "OK \n"); |
| 156 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 157 | DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)CNBuffSize); |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 158 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize); |
| 159 | if (r != CNBuffSize) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 160 | DISPLAYLEVEL(4, "OK \n"); |
| 161 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 162 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 163 | { size_t u; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 164 | for (u=0; u<CNBuffSize; u++) { |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 165 | if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;; |
| 166 | } } |
| 167 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 168 | |
Yann Collet | 0ef6803 | 2017-03-29 16:58:57 -0700 | [diff] [blame] | 169 | |
Yann Collet | c0b1731 | 2017-02-28 01:02:46 -0800 | [diff] [blame] | 170 | DISPLAYLEVEL(4, "test%3i : decompress with null dict : ", testNb++); |
| 171 | { size_t const r = ZSTD_decompress_usingDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, NULL, 0); |
| 172 | if (r != CNBuffSize) goto _output_error; } |
| 173 | DISPLAYLEVEL(4, "OK \n"); |
| 174 | |
| 175 | DISPLAYLEVEL(4, "test%3i : decompress with null DDict : ", testNb++); |
| 176 | { size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, NULL); |
| 177 | if (r != CNBuffSize) goto _output_error; } |
| 178 | DISPLAYLEVEL(4, "OK \n"); |
| 179 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 180 | DISPLAYLEVEL(4, "test%3i : decompress with 1 missing byte : ", testNb++); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 181 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize-1); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 182 | if (!ZSTD_isError(r)) goto _output_error; |
| 183 | if (ZSTD_getErrorCode((size_t)r) != ZSTD_error_srcSize_wrong) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 184 | DISPLAYLEVEL(4, "OK \n"); |
| 185 | |
| 186 | DISPLAYLEVEL(4, "test%3i : decompress with 1 too much byte : ", testNb++); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 187 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize+1); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 188 | if (!ZSTD_isError(r)) goto _output_error; |
| 189 | if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 190 | DISPLAYLEVEL(4, "OK \n"); |
| 191 | |
Yann Collet | 0ef6803 | 2017-03-29 16:58:57 -0700 | [diff] [blame] | 192 | |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 193 | /* Static CCtx tests */ |
| 194 | #define STATIC_CCTX_LEVEL 3 |
| 195 | DISPLAYLEVEL(4, "test%3i : create static CCtx for level %u :", testNb++, STATIC_CCTX_LEVEL); |
| 196 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(STATIC_CCTX_LEVEL, 0, 0); |
| 197 | size_t const staticCCtxSize = ZSTD_estimateCStreamSize(cParams); |
Yann Collet | 0fdc71c | 2017-05-24 17:41:41 -0700 | [diff] [blame] | 198 | void* const staticCCtxBuffer = malloc(staticCCtxSize); |
| 199 | size_t const staticDCtxSize = ZSTD_estimateDCtxSize(); |
| 200 | void* const staticDCtxBuffer = malloc(staticDCtxSize); |
| 201 | if (staticCCtxBuffer==NULL || staticDCtxBuffer==NULL) { |
| 202 | free(staticCCtxBuffer); |
| 203 | free(staticDCtxBuffer); |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 204 | DISPLAY("Not enough memory, aborting\n"); |
| 205 | testResult = 1; |
| 206 | goto _end; |
| 207 | } |
| 208 | { ZSTD_CCtx* staticCCtx = ZSTD_initStaticCCtx(staticCCtxBuffer, staticCCtxSize); |
Yann Collet | 0fdc71c | 2017-05-24 17:41:41 -0700 | [diff] [blame] | 209 | ZSTD_DCtx* staticDCtx = ZSTD_initStaticDCtx(staticDCtxBuffer, staticDCtxSize); |
| 210 | if ((staticCCtx==NULL) || (staticDCtx==NULL)) goto _output_error; |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 211 | DISPLAYLEVEL(4, "OK \n"); |
| 212 | |
| 213 | DISPLAYLEVEL(4, "test%3i : init CCtx for level %u : ", testNb++, STATIC_CCTX_LEVEL); |
| 214 | { size_t const r = ZSTD_compressBegin(staticCCtx, STATIC_CCTX_LEVEL); |
| 215 | if (ZSTD_isError(r)) goto _output_error; } |
| 216 | DISPLAYLEVEL(4, "OK \n"); |
| 217 | |
| 218 | DISPLAYLEVEL(4, "test%3i : simple compression test with static CCtx : ", testNb++); |
| 219 | CHECKPLUS(r, ZSTD_compressCCtx(staticCCtx, |
| 220 | compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 221 | CNBuffer, CNBuffSize, STATIC_CCTX_LEVEL), |
| 222 | cSize=r ); |
Yann Collet | 0fdc71c | 2017-05-24 17:41:41 -0700 | [diff] [blame] | 223 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", |
| 224 | (U32)cSize, (double)cSize/CNBuffSize*100); |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 225 | |
Yann Collet | 0fdc71c | 2017-05-24 17:41:41 -0700 | [diff] [blame] | 226 | DISPLAYLEVEL(4, "test%3i : simple decompression test with static DCtx : ", testNb++); |
| 227 | { size_t const r = ZSTD_decompressDCtx(staticDCtx, |
| 228 | decodedBuffer, CNBuffSize, |
| 229 | compressedBuffer, cSize); |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 230 | if (r != CNBuffSize) goto _output_error; } |
| 231 | DISPLAYLEVEL(4, "OK \n"); |
| 232 | |
Yann Collet | 0fdc71c | 2017-05-24 17:41:41 -0700 | [diff] [blame] | 233 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 234 | { size_t u; |
| 235 | for (u=0; u<CNBuffSize; u++) { |
| 236 | if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) |
| 237 | goto _output_error;; |
| 238 | } } |
| 239 | DISPLAYLEVEL(4, "OK \n"); |
| 240 | |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 241 | DISPLAYLEVEL(4, "test%3i : init CCtx for too large level (must fail) : ", testNb++); |
| 242 | { size_t const r = ZSTD_compressBegin(staticCCtx, ZSTD_maxCLevel()); |
| 243 | if (!ZSTD_isError(r)) goto _output_error; } |
| 244 | DISPLAYLEVEL(4, "OK \n"); |
| 245 | |
| 246 | DISPLAYLEVEL(4, "test%3i : init CCtx for small level %u (should work again) : ", testNb++, 1); |
| 247 | { size_t const r = ZSTD_compressBegin(staticCCtx, 1); |
| 248 | if (ZSTD_isError(r)) goto _output_error; } |
| 249 | DISPLAYLEVEL(4, "OK \n"); |
| 250 | |
| 251 | DISPLAYLEVEL(4, "test%3i : init CStream for small level %u : ", testNb++, 1); |
| 252 | { size_t const r = ZSTD_initCStream(staticCCtx, 1); |
| 253 | if (ZSTD_isError(r)) goto _output_error; } |
| 254 | DISPLAYLEVEL(4, "OK \n"); |
| 255 | |
| 256 | DISPLAYLEVEL(4, "test%3i : init CStream with dictionary (should fail) : ", testNb++); |
| 257 | { size_t const r = ZSTD_initCStream_usingDict(staticCCtx, CNBuffer, 64 KB, 1); |
| 258 | if (!ZSTD_isError(r)) goto _output_error; } |
| 259 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | 0fdc71c | 2017-05-24 17:41:41 -0700 | [diff] [blame] | 260 | |
| 261 | DISPLAYLEVEL(4, "test%3i : init DStream (should fail) : ", testNb++); |
| 262 | { size_t const r = ZSTD_initDStream(staticDCtx); |
| 263 | if (ZSTD_isError(r)) goto _output_error; } |
| 264 | { ZSTD_outBuffer output = { decodedBuffer, CNBuffSize, 0 }; |
| 265 | ZSTD_inBuffer input = { compressedBuffer, ZSTD_FRAMEHEADERSIZE_MAX+1, 0 }; |
| 266 | size_t const r = ZSTD_decompressStream(staticDCtx, &output, &input); |
| 267 | if (!ZSTD_isError(r)) goto _output_error; |
| 268 | } |
| 269 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 270 | } |
| 271 | free(staticCCtxBuffer); |
Yann Collet | 0fdc71c | 2017-05-24 17:41:41 -0700 | [diff] [blame] | 272 | free(staticDCtxBuffer); |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
| 276 | |
Yann Collet | 1e1e26f | 2017-03-29 17:09:59 -0700 | [diff] [blame] | 277 | /* ZSTDMT simple MT compression test */ |
Yann Collet | 0ef6803 | 2017-03-29 16:58:57 -0700 | [diff] [blame] | 278 | DISPLAYLEVEL(4, "test%3i : create ZSTDMT CCtx : ", testNb++); |
| 279 | { ZSTDMT_CCtx* mtctx = ZSTDMT_createCCtx(2); |
| 280 | if (mtctx==NULL) { |
| 281 | DISPLAY("mtctx : mot enough memory, aborting \n"); |
| 282 | testResult = 1; |
| 283 | goto _end; |
| 284 | } |
| 285 | DISPLAYLEVEL(4, "OK \n"); |
| 286 | |
| 287 | DISPLAYLEVEL(4, "test%3i : compress %u bytes with 2 threads : ", testNb++, (U32)CNBuffSize); |
| 288 | CHECKPLUS(r, ZSTDMT_compressCCtx(mtctx, |
| 289 | compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 290 | CNBuffer, CNBuffSize, |
| 291 | 1), |
| 292 | cSize=r ); |
| 293 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 294 | |
| 295 | DISPLAYLEVEL(4, "test%3i : decompressed size test : ", testNb++); |
| 296 | { unsigned long long const rSize = ZSTD_getFrameContentSize(compressedBuffer, cSize); |
| 297 | if (rSize != CNBuffSize) { |
| 298 | DISPLAY("ZSTD_getFrameContentSize incorrect : %u != %u \n", (U32)rSize, (U32)CNBuffSize); |
| 299 | goto _output_error; |
| 300 | } } |
| 301 | DISPLAYLEVEL(4, "OK \n"); |
| 302 | |
| 303 | DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)CNBuffSize); |
| 304 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize); |
| 305 | if (r != CNBuffSize) goto _output_error; } |
| 306 | DISPLAYLEVEL(4, "OK \n"); |
| 307 | |
| 308 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 309 | { size_t u; |
| 310 | for (u=0; u<CNBuffSize; u++) { |
| 311 | if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;; |
| 312 | } } |
| 313 | DISPLAYLEVEL(4, "OK \n"); |
| 314 | |
| 315 | ZSTDMT_freeCCtx(mtctx); |
| 316 | } |
| 317 | |
| 318 | |
Sean Purcell | ba2ad9f | 2017-02-07 16:16:55 -0800 | [diff] [blame] | 319 | /* Simple API multiframe test */ |
| 320 | DISPLAYLEVEL(4, "test%3i : compress multiple frames : ", testNb++); |
| 321 | { size_t off = 0; |
| 322 | int i; |
| 323 | int const segs = 4; |
| 324 | /* only use the first half so we don't push against size limit of compressedBuffer */ |
| 325 | size_t const segSize = (CNBuffSize / 2) / segs; |
| 326 | for (i = 0; i < segs; i++) { |
| 327 | CHECK_V(r, |
| 328 | ZSTD_compress( |
| 329 | (BYTE *)compressedBuffer + off, CNBuffSize - off, |
| 330 | (BYTE *)CNBuffer + segSize * i, |
| 331 | segSize, 5)); |
| 332 | off += r; |
| 333 | if (i == segs/2) { |
| 334 | /* insert skippable frame */ |
| 335 | const U32 skipLen = 128 KB; |
| 336 | MEM_writeLE32((BYTE*)compressedBuffer + off, ZSTD_MAGIC_SKIPPABLE_START); |
| 337 | MEM_writeLE32((BYTE*)compressedBuffer + off + 4, skipLen); |
| 338 | off += skipLen + ZSTD_skippableHeaderSize; |
| 339 | } |
| 340 | } |
| 341 | cSize = off; |
| 342 | } |
| 343 | DISPLAYLEVEL(4, "OK \n"); |
| 344 | |
| 345 | DISPLAYLEVEL(4, "test%3i : get decompressed size of multiple frames : ", testNb++); |
| 346 | { unsigned long long const r = ZSTD_findDecompressedSize(compressedBuffer, cSize); |
| 347 | if (r != CNBuffSize / 2) goto _output_error; } |
| 348 | DISPLAYLEVEL(4, "OK \n"); |
| 349 | |
| 350 | DISPLAYLEVEL(4, "test%3i : decompress multiple frames : ", testNb++); |
| 351 | { CHECK_V(r, ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize)); |
| 352 | if (r != CNBuffSize / 2) goto _output_error; } |
| 353 | DISPLAYLEVEL(4, "OK \n"); |
| 354 | |
| 355 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 356 | if (memcmp(decodedBuffer, CNBuffer, CNBuffSize / 2) != 0) goto _output_error; |
| 357 | DISPLAYLEVEL(4, "OK \n"); |
| 358 | |
Yann Collet | 389648c | 2016-04-12 19:13:08 +0200 | [diff] [blame] | 359 | /* Dictionary and CCtx Duplication tests */ |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 360 | { ZSTD_CCtx* const ctxOrig = ZSTD_createCCtx(); |
| 361 | ZSTD_CCtx* const ctxDuplicated = ZSTD_createCCtx(); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 362 | static const size_t dictSize = 551; |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 363 | |
Yann Collet | 887e7da | 2016-04-11 20:12:27 +0200 | [diff] [blame] | 364 | DISPLAYLEVEL(4, "test%3i : copy context too soon : ", testNb++); |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 365 | { size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 366 | if (!ZSTD_isError(copyResult)) goto _output_error; } /* error must be detected */ |
Yann Collet | 887e7da | 2016-04-11 20:12:27 +0200 | [diff] [blame] | 367 | DISPLAYLEVEL(4, "OK \n"); |
| 368 | |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 369 | DISPLAYLEVEL(4, "test%3i : load dictionary into context : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 370 | CHECK( ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2) ); |
Sean Purcell | 84b37cc | 2017-02-09 12:27:32 -0800 | [diff] [blame] | 371 | CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0) ); /* Begin_usingDict implies unknown srcSize, so match that */ |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 372 | DISPLAYLEVEL(4, "OK \n"); |
| 373 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 374 | DISPLAYLEVEL(4, "test%3i : compress with flat dictionary : ", testNb++); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 375 | cSize = 0; |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 376 | CHECKPLUS(r, ZSTD_compressEnd(ctxOrig, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 377 | (const char*)CNBuffer + dictSize, CNBuffSize - dictSize), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 378 | cSize += r); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 379 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 380 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 381 | DISPLAYLEVEL(4, "test%3i : frame built with flat dictionary should be decompressible : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 382 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 383 | decodedBuffer, CNBuffSize, |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 384 | compressedBuffer, cSize, |
| 385 | CNBuffer, dictSize), |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 386 | if (r != CNBuffSize - dictSize) goto _output_error); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 387 | DISPLAYLEVEL(4, "OK \n"); |
| 388 | |
| 389 | DISPLAYLEVEL(4, "test%3i : compress with duplicated context : ", testNb++); |
Yann Collet | 389648c | 2016-04-12 19:13:08 +0200 | [diff] [blame] | 390 | { size_t const cSizeOrig = cSize; |
| 391 | cSize = 0; |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 392 | CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 393 | (const char*)CNBuffer + dictSize, CNBuffSize - dictSize), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 394 | cSize += r); |
Yann Collet | 227cc39 | 2016-07-15 11:27:09 +0200 | [diff] [blame] | 395 | if (cSize != cSizeOrig) goto _output_error; /* should be identical ==> same size */ |
Yann Collet | 389648c | 2016-04-12 19:13:08 +0200 | [diff] [blame] | 396 | } |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 397 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 398 | |
| 399 | DISPLAYLEVEL(4, "test%3i : frame built with duplicated context should be decompressible : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 400 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 401 | decodedBuffer, CNBuffSize, |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 402 | compressedBuffer, cSize, |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 403 | CNBuffer, dictSize), |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 404 | if (r != CNBuffSize - dictSize) goto _output_error); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 405 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | 541dc7c | 2016-04-12 18:00:20 +0200 | [diff] [blame] | 406 | |
Yann Collet | 8dff956 | 2017-02-25 10:11:15 -0800 | [diff] [blame] | 407 | DISPLAYLEVEL(4, "test%3i : decompress with DDict : ", testNb++); |
Yann Collet | bd7fa21 | 2017-02-26 14:43:07 -0800 | [diff] [blame] | 408 | { ZSTD_DDict* const ddict = ZSTD_createDDict_byReference(CNBuffer, dictSize); |
Yann Collet | 8dff956 | 2017-02-25 10:11:15 -0800 | [diff] [blame] | 409 | size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict); |
| 410 | if (r != CNBuffSize - dictSize) goto _output_error; |
| 411 | DISPLAYLEVEL(4, "OK (size of DDict : %u) \n", (U32)ZSTD_sizeof_DDict(ddict)); |
| 412 | ZSTD_freeDDict(ddict); |
| 413 | } |
| 414 | |
Yann Collet | 541dc7c | 2016-04-12 18:00:20 +0200 | [diff] [blame] | 415 | DISPLAYLEVEL(4, "test%3i : check content size on duplicated context : ", testNb++); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 416 | { size_t const testSize = CNBuffSize / 3; |
Yann Collet | 6c6e175 | 2016-06-27 15:28:45 +0200 | [diff] [blame] | 417 | { ZSTD_parameters p = ZSTD_getParams(2, testSize, dictSize); |
| 418 | p.fParams.contentSizeFlag = 1; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 419 | CHECK( ZSTD_compressBegin_advanced(ctxOrig, CNBuffer, dictSize, p, testSize-1) ); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 420 | } |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 421 | CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, testSize) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 422 | |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 423 | CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(testSize), |
| 424 | (const char*)CNBuffer + dictSize, testSize), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 425 | cSize = r); |
Yann Collet | 542c9df | 2017-05-09 15:46:07 -0700 | [diff] [blame] | 426 | { ZSTD_frameHeader fp; |
| 427 | if (ZSTD_getFrameHeader(&fp, compressedBuffer, cSize)) goto _output_error; |
Yann Collet | 541dc7c | 2016-04-12 18:00:20 +0200 | [diff] [blame] | 428 | if ((fp.frameContentSize != testSize) && (fp.frameContentSize != 0)) goto _output_error; |
| 429 | } } |
| 430 | DISPLAYLEVEL(4, "OK \n"); |
| 431 | |
| 432 | ZSTD_freeCCtx(ctxOrig); |
| 433 | ZSTD_freeCCtx(ctxDuplicated); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 436 | /* Dictionary and dictBuilder tests */ |
| 437 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 438 | size_t dictSize = 16 KB; |
| 439 | void* dictBuffer = malloc(dictSize); |
| 440 | size_t const totalSampleSize = 1 MB; |
| 441 | size_t const sampleUnitSize = 8 KB; |
Yann Collet | b81cbba | 2016-05-30 22:29:45 +0200 | [diff] [blame] | 442 | U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 443 | size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t)); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 444 | U32 dictID; |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 445 | |
| 446 | if (dictBuffer==NULL || samplesSizes==NULL) { |
| 447 | free(dictBuffer); |
| 448 | free(samplesSizes); |
| 449 | goto _output_error; |
| 450 | } |
| 451 | |
| 452 | DISPLAYLEVEL(4, "test%3i : dictBuilder : ", testNb++); |
| 453 | { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; } |
| 454 | dictSize = ZDICT_trainFromBuffer(dictBuffer, dictSize, |
| 455 | CNBuffer, samplesSizes, nbSamples); |
| 456 | if (ZDICT_isError(dictSize)) goto _output_error; |
| 457 | DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize); |
| 458 | |
Yann Collet | da3fbcb | 2016-08-19 14:23:58 +0200 | [diff] [blame] | 459 | DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 460 | dictID = ZDICT_getDictID(dictBuffer, dictSize); |
| 461 | if (dictID==0) goto _output_error; |
| 462 | DISPLAYLEVEL(4, "OK : %u \n", dictID); |
Yann Collet | da3fbcb | 2016-08-19 14:23:58 +0200 | [diff] [blame] | 463 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 464 | DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++); |
| 465 | cSize = ZSTD_compress_usingDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 466 | CNBuffer, CNBuffSize, |
| 467 | dictBuffer, dictSize, 4); |
| 468 | if (ZSTD_isError(cSize)) goto _output_error; |
| 469 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 470 | |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 471 | DISPLAYLEVEL(4, "test%3i : retrieve dictID from dictionary : ", testNb++); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 472 | { U32 const did = ZSTD_getDictID_fromDict(dictBuffer, dictSize); |
| 473 | if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */ |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 474 | } |
| 475 | DISPLAYLEVEL(4, "OK \n"); |
| 476 | |
| 477 | DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 478 | { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize); |
| 479 | if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */ |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 480 | } |
| 481 | DISPLAYLEVEL(4, "OK \n"); |
| 482 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 483 | DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++); |
| 484 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
| 485 | decodedBuffer, CNBuffSize, |
| 486 | compressedBuffer, cSize, |
| 487 | dictBuffer, dictSize), |
| 488 | if (r != CNBuffSize) goto _output_error); |
| 489 | DISPLAYLEVEL(4, "OK \n"); |
| 490 | |
Yann Collet | a1d6704 | 2017-05-08 17:51:49 -0700 | [diff] [blame] | 491 | DISPLAYLEVEL(4, "test%3i : estimate CDict size : ", testNb++); |
| 492 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); |
Yann Collet | 25989e3 | 2017-05-25 15:07:37 -0700 | [diff] [blame^] | 493 | size_t const estimatedSize = ZSTD_estimateCDictSize(cParams, dictSize, 1); |
Yann Collet | a1d6704 | 2017-05-08 17:51:49 -0700 | [diff] [blame] | 494 | DISPLAYLEVEL(4, "OK : %u \n", (U32)estimatedSize); |
| 495 | } |
| 496 | |
Nick Terrell | 39a6cc5 | 2017-04-03 21:00:44 -0700 | [diff] [blame] | 497 | DISPLAYLEVEL(4, "test%3i : compress with preprocessed dictionary : ", testNb++); |
Yann Collet | 2f73427 | 2017-04-27 14:39:39 -0700 | [diff] [blame] | 498 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); |
Yann Collet | 25989e3 | 2017-05-25 15:07:37 -0700 | [diff] [blame^] | 499 | ZSTD_customMem const customMem = { NULL, NULL, NULL }; |
| 500 | ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, |
| 501 | 1 /* by Referece */, cParams, customMem); |
Yann Collet | 2f73427 | 2017-04-27 14:39:39 -0700 | [diff] [blame] | 502 | cSize = ZSTD_compress_usingCDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
Nick Terrell | 39a6cc5 | 2017-04-03 21:00:44 -0700 | [diff] [blame] | 503 | CNBuffer, CNBuffSize, cdict); |
Yann Collet | 2f73427 | 2017-04-27 14:39:39 -0700 | [diff] [blame] | 504 | ZSTD_freeCDict(cdict); |
| 505 | if (ZSTD_isError(cSize)) goto _output_error; |
Nick Terrell | 39a6cc5 | 2017-04-03 21:00:44 -0700 | [diff] [blame] | 506 | } |
| 507 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 508 | |
| 509 | DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++); |
| 510 | { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize); |
| 511 | if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */ |
| 512 | } |
| 513 | DISPLAYLEVEL(4, "OK \n"); |
| 514 | |
Nick Terrell | 39a6cc5 | 2017-04-03 21:00:44 -0700 | [diff] [blame] | 515 | DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++); |
| 516 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
| 517 | decodedBuffer, CNBuffSize, |
| 518 | compressedBuffer, cSize, |
| 519 | dictBuffer, dictSize), |
| 520 | if (r != CNBuffSize) goto _output_error); |
| 521 | DISPLAYLEVEL(4, "OK \n"); |
| 522 | |
Yann Collet | 2f73427 | 2017-04-27 14:39:39 -0700 | [diff] [blame] | 523 | DISPLAYLEVEL(4, "test%3i : ZSTD_compress_usingCDict_advanced, no contentSize, no dictID : ", testNb++); |
| 524 | { ZSTD_frameParameters const fParams = { 0 /* frameSize */, 1 /* checksum */, 1 /* noDictID*/ }; |
| 525 | ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); |
| 526 | ZSTD_customMem const customMem = { NULL, NULL, NULL }; |
| 527 | ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, 1, cParams, customMem); |
| 528 | cSize = ZSTD_compress_usingCDict_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 529 | CNBuffer, CNBuffSize, cdict, fParams); |
| 530 | ZSTD_freeCDict(cdict); |
| 531 | if (ZSTD_isError(cSize)) goto _output_error; |
| 532 | } |
| 533 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 534 | |
| 535 | DISPLAYLEVEL(4, "test%3i : try retrieving contentSize from frame : ", testNb++); |
| 536 | { U64 const contentSize = ZSTD_getFrameContentSize(compressedBuffer, cSize); |
| 537 | if (contentSize != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error; |
| 538 | } |
| 539 | DISPLAYLEVEL(4, "OK (unknown)\n"); |
| 540 | |
| 541 | DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++); |
| 542 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
| 543 | decodedBuffer, CNBuffSize, |
| 544 | compressedBuffer, cSize, |
| 545 | dictBuffer, dictSize), |
| 546 | if (r != CNBuffSize) goto _output_error); |
| 547 | DISPLAYLEVEL(4, "OK \n"); |
| 548 | |
| 549 | DISPLAYLEVEL(4, "test%3i : ZSTD_compress_advanced, no dictID : ", testNb++); |
Yann Collet | 6c6e175 | 2016-06-27 15:28:45 +0200 | [diff] [blame] | 550 | { ZSTD_parameters p = ZSTD_getParams(3, CNBuffSize, dictSize); |
| 551 | p.fParams.noDictIDFlag = 1; |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 552 | cSize = ZSTD_compress_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 553 | CNBuffer, CNBuffSize, |
| 554 | dictBuffer, dictSize, p); |
| 555 | if (ZSTD_isError(cSize)) goto _output_error; |
| 556 | } |
| 557 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 558 | |
| 559 | DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++); |
| 560 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
| 561 | decodedBuffer, CNBuffSize, |
| 562 | compressedBuffer, cSize, |
| 563 | dictBuffer, dictSize), |
| 564 | if (r != CNBuffSize) goto _output_error); |
| 565 | DISPLAYLEVEL(4, "OK \n"); |
| 566 | |
Sean Purcell | 834ab50 | 2017-01-11 17:31:06 -0800 | [diff] [blame] | 567 | DISPLAYLEVEL(4, "test%3i : dictionary containing only header should return error : ", testNb++); |
| 568 | { |
| 569 | const size_t ret = ZSTD_decompress_usingDict( |
| 570 | dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, |
| 571 | "\x37\xa4\x30\xec\x11\x22\x33\x44", 8); |
| 572 | if (ZSTD_getErrorCode(ret) != ZSTD_error_dictionary_corrupted) goto _output_error; |
| 573 | } |
Sean Purcell | c44c4d5 | 2017-01-12 09:38:29 -0800 | [diff] [blame] | 574 | DISPLAYLEVEL(4, "OK \n"); |
Sean Purcell | 834ab50 | 2017-01-11 17:31:06 -0800 | [diff] [blame] | 575 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 576 | ZSTD_freeCCtx(cctx); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 577 | free(dictBuffer); |
| 578 | free(samplesSizes); |
| 579 | } |
| 580 | |
Nick Terrell | a8b4fe0 | 2017-01-02 18:45:19 -0800 | [diff] [blame] | 581 | /* COVER dictionary builder tests */ |
| 582 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
Nick Terrell | a8b4fe0 | 2017-01-02 18:45:19 -0800 | [diff] [blame] | 583 | size_t dictSize = 16 KB; |
| 584 | size_t optDictSize = dictSize; |
| 585 | void* dictBuffer = malloc(dictSize); |
| 586 | size_t const totalSampleSize = 1 MB; |
| 587 | size_t const sampleUnitSize = 8 KB; |
| 588 | U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize); |
| 589 | size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t)); |
| 590 | COVER_params_t params; |
| 591 | U32 dictID; |
| 592 | |
| 593 | if (dictBuffer==NULL || samplesSizes==NULL) { |
| 594 | free(dictBuffer); |
| 595 | free(samplesSizes); |
| 596 | goto _output_error; |
| 597 | } |
| 598 | |
| 599 | DISPLAYLEVEL(4, "test%3i : COVER_trainFromBuffer : ", testNb++); |
| 600 | { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; } |
| 601 | memset(¶ms, 0, sizeof(params)); |
| 602 | params.d = 1 + (FUZ_rand(&seed) % 16); |
| 603 | params.k = params.d + (FUZ_rand(&seed) % 256); |
| 604 | dictSize = COVER_trainFromBuffer(dictBuffer, dictSize, |
| 605 | CNBuffer, samplesSizes, nbSamples, |
| 606 | params); |
| 607 | if (ZDICT_isError(dictSize)) goto _output_error; |
| 608 | DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize); |
| 609 | |
| 610 | DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++); |
| 611 | dictID = ZDICT_getDictID(dictBuffer, dictSize); |
| 612 | if (dictID==0) goto _output_error; |
| 613 | DISPLAYLEVEL(4, "OK : %u \n", dictID); |
| 614 | |
| 615 | DISPLAYLEVEL(4, "test%3i : COVER_optimizeTrainFromBuffer : ", testNb++); |
| 616 | memset(¶ms, 0, sizeof(params)); |
| 617 | params.steps = 4; |
| 618 | optDictSize = COVER_optimizeTrainFromBuffer(dictBuffer, optDictSize, |
Yann Collet | c0b1731 | 2017-02-28 01:02:46 -0800 | [diff] [blame] | 619 | CNBuffer, samplesSizes, nbSamples / 4, |
Nick Terrell | a8b4fe0 | 2017-01-02 18:45:19 -0800 | [diff] [blame] | 620 | ¶ms); |
| 621 | if (ZDICT_isError(optDictSize)) goto _output_error; |
| 622 | DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)optDictSize); |
| 623 | |
| 624 | DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++); |
| 625 | dictID = ZDICT_getDictID(dictBuffer, optDictSize); |
| 626 | if (dictID==0) goto _output_error; |
| 627 | DISPLAYLEVEL(4, "OK : %u \n", dictID); |
| 628 | |
| 629 | ZSTD_freeCCtx(cctx); |
Nick Terrell | a8b4fe0 | 2017-01-02 18:45:19 -0800 | [diff] [blame] | 630 | free(dictBuffer); |
| 631 | free(samplesSizes); |
| 632 | } |
| 633 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 634 | /* Decompression defense tests */ |
| 635 | DISPLAYLEVEL(4, "test%3i : Check input length for magic number : ", testNb++); |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 636 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 3); /* too small input */ |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 637 | if (!ZSTD_isError(r)) goto _output_error; |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 638 | if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 639 | DISPLAYLEVEL(4, "OK \n"); |
| 640 | |
| 641 | DISPLAYLEVEL(4, "test%3i : Check magic Number : ", testNb++); |
| 642 | ((char*)(CNBuffer))[0] = 1; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 643 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 4); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 644 | if (!ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 645 | DISPLAYLEVEL(4, "OK \n"); |
| 646 | |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 647 | /* content size verification test */ |
| 648 | DISPLAYLEVEL(4, "test%3i : Content size verification : ", testNb++); |
| 649 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
| 650 | size_t const srcSize = 5000; |
| 651 | size_t const wrongSrcSize = (srcSize + 1000); |
| 652 | ZSTD_parameters params = ZSTD_getParams(1, wrongSrcSize, 0); |
| 653 | params.fParams.contentSizeFlag = 1; |
| 654 | { size_t const result = ZSTD_compressBegin_advanced(cctx, NULL, 0, params, wrongSrcSize); |
| 655 | if (ZSTD_isError(result)) goto _output_error; |
| 656 | } |
| 657 | { size_t const result = ZSTD_compressEnd(cctx, decodedBuffer, CNBuffSize, CNBuffer, srcSize); |
| 658 | if (!ZSTD_isError(result)) goto _output_error; |
| 659 | if (ZSTD_getErrorCode(result) != ZSTD_error_srcSize_wrong) goto _output_error; |
| 660 | DISPLAYLEVEL(4, "OK : %s \n", ZSTD_getErrorName(result)); |
Yann Collet | f913cbe | 2017-04-13 22:46:41 -0700 | [diff] [blame] | 661 | } |
| 662 | ZSTD_freeCCtx(cctx); |
| 663 | } |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 664 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 665 | /* block API tests */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 666 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
Yann Collet | d3d2db5 | 2016-07-15 12:20:26 +0200 | [diff] [blame] | 667 | static const size_t dictSize = 65 KB; |
| 668 | static const size_t blockSize = 100 KB; /* won't cause pb with small dict size */ |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 669 | size_t cSize2; |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 670 | |
| 671 | /* basic block compression */ |
| 672 | DISPLAYLEVEL(4, "test%3i : Block compression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 673 | CHECK( ZSTD_compressBegin(cctx, 5) ); |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 674 | CHECK( ZSTD_getBlockSize(cctx) >= blockSize); |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 675 | cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize); |
| 676 | if (ZSTD_isError(cSize)) goto _output_error; |
| 677 | DISPLAYLEVEL(4, "OK \n"); |
| 678 | |
| 679 | DISPLAYLEVEL(4, "test%3i : Block decompression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 680 | CHECK( ZSTD_decompressBegin(dctx) ); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 681 | { CHECK_V(r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) ); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 682 | if (r != blockSize) goto _output_error; } |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 683 | DISPLAYLEVEL(4, "OK \n"); |
| 684 | |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 685 | /* dictionary block compression */ |
| 686 | DISPLAYLEVEL(4, "test%3i : Dictionary Block compression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 687 | CHECK( ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5) ); |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 688 | cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize); |
| 689 | if (ZSTD_isError(cSize)) goto _output_error; |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 690 | cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize+blockSize, blockSize); |
| 691 | if (ZSTD_isError(cSize2)) goto _output_error; |
Yann Collet | aa2628d | 2016-07-07 15:28:41 +0200 | [diff] [blame] | 692 | memcpy((char*)compressedBuffer+cSize, (char*)CNBuffer+dictSize+blockSize, blockSize); /* fake non-compressed block */ |
| 693 | cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize+blockSize, ZSTD_compressBound(blockSize), |
| 694 | (char*)CNBuffer+dictSize+2*blockSize, blockSize); |
| 695 | if (ZSTD_isError(cSize2)) goto _output_error; |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 696 | DISPLAYLEVEL(4, "OK \n"); |
| 697 | |
| 698 | DISPLAYLEVEL(4, "test%3i : Dictionary Block decompression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 699 | CHECK( ZSTD_decompressBegin_usingDict(dctx, CNBuffer, dictSize) ); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 700 | { CHECK_V( r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) ); |
| 701 | if (r != blockSize) goto _output_error; } |
Yann Collet | aa2628d | 2016-07-07 15:28:41 +0200 | [diff] [blame] | 702 | ZSTD_insertBlock(dctx, (char*)decodedBuffer+blockSize, blockSize); /* insert non-compressed block into dctx history */ |
| 703 | { CHECK_V( r, ZSTD_decompressBlock(dctx, (char*)decodedBuffer+2*blockSize, CNBuffSize, (char*)compressedBuffer+cSize+blockSize, cSize2) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 704 | if (r != blockSize) goto _output_error; } |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 705 | DISPLAYLEVEL(4, "OK \n"); |
| 706 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 707 | ZSTD_freeCCtx(cctx); |
| 708 | ZSTD_freeDCtx(dctx); |
| 709 | } |
| 710 | |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 711 | /* long rle test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 712 | { size_t sampleSize = 0; |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 713 | DISPLAYLEVEL(4, "test%3i : Long RLE test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 714 | RDG_genBuffer(CNBuffer, sampleSize, compressibility, 0., seed+1); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 715 | memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1); |
| 716 | sampleSize += 256 KB - 1; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 717 | RDG_genBuffer((char*)CNBuffer+sampleSize, 96 KB, compressibility, 0., seed+2); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 718 | sampleSize += 96 KB; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 719 | cSize = ZSTD_compress(compressedBuffer, ZSTD_compressBound(sampleSize), CNBuffer, sampleSize, 1); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 720 | if (ZSTD_isError(cSize)) goto _output_error; |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 721 | { CHECK_V(regenSize, ZSTD_decompress(decodedBuffer, sampleSize, compressedBuffer, cSize)); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 722 | if (regenSize!=sampleSize) goto _output_error; } |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 723 | DISPLAYLEVEL(4, "OK \n"); |
| 724 | } |
| 725 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 726 | /* All zeroes test (test bug #137) */ |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 727 | #define ZEROESLENGTH 100 |
| 728 | DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH); |
| 729 | memset(CNBuffer, 0, ZEROESLENGTH); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 730 | { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 731 | cSize = r; } |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 732 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100); |
| 733 | |
| 734 | DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 735 | { CHECK_V(r, ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 736 | if (r != ZEROESLENGTH) goto _output_error; } |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 737 | DISPLAYLEVEL(4, "OK \n"); |
| 738 | |
| 739 | /* nbSeq limit test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 740 | #define _3BYTESTESTLENGTH 131000 |
| 741 | #define NB3BYTESSEQLOG 9 |
| 742 | #define NB3BYTESSEQ (1 << NB3BYTESSEQLOG) |
| 743 | #define NB3BYTESSEQMASK (NB3BYTESSEQ-1) |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 744 | /* creates a buffer full of 3-bytes sequences */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 745 | { BYTE _3BytesSeqs[NB3BYTESSEQ][3]; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 746 | U32 rSeed = 1; |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 747 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 748 | /* create batch of 3-bytes sequences */ |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 749 | { int i; |
| 750 | for (i=0; i < NB3BYTESSEQ; i++) { |
| 751 | _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 752 | _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 753 | _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 754 | } } |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 755 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 756 | /* randomly fills CNBuffer with prepared 3-bytes sequences */ |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 757 | { int i; |
| 758 | for (i=0; i < _3BYTESTESTLENGTH; i += 3) { /* note : CNBuffer size > _3BYTESTESTLENGTH+3 */ |
| 759 | U32 const id = FUZ_rand(&rSeed) & NB3BYTESSEQMASK; |
| 760 | ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0]; |
| 761 | ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1]; |
| 762 | ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2]; |
| 763 | } } } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 764 | DISPLAYLEVEL(4, "test%3i : compress lots 3-bytes sequences : ", testNb++); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 765 | { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 766 | CNBuffer, _3BYTESTESTLENGTH, 19) ); |
| 767 | cSize = r; } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 768 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/_3BYTESTESTLENGTH*100); |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 769 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 770 | DISPLAYLEVEL(4, "test%3i : decompress lots 3-bytes sequence : ", testNb++); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 771 | { CHECK_V(r, ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 772 | if (r != _3BYTESTESTLENGTH) goto _output_error; } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 773 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 774 | |
Sean Purcell | 9050e19 | 2017-02-22 12:12:32 -0800 | [diff] [blame] | 775 | /* findFrameCompressedSize on skippable frames */ |
| 776 | DISPLAYLEVEL(4, "test%3i : frame compressed size of skippable frame : ", testNb++); |
| 777 | { const char* frame = "\x50\x2a\x4d\x18\x05\x0\x0\0abcde"; |
| 778 | size_t const frameSrcSize = 13; |
| 779 | if (ZSTD_findFrameCompressedSize(frame, frameSrcSize) != frameSrcSize) goto _output_error; } |
| 780 | DISPLAYLEVEL(4, "OK \n"); |
| 781 | |
Sean Purcell | e0b3265 | 2017-02-08 15:31:47 -0800 | [diff] [blame] | 782 | /* error string tests */ |
| 783 | DISPLAYLEVEL(4, "test%3i : testing ZSTD error code strings : ", testNb++); |
| 784 | if (strcmp("No error detected", ZSTD_getErrorName((ZSTD_ErrorCode)(0-ZSTD_error_no_error))) != 0) goto _output_error; |
| 785 | if (strcmp("No error detected", ZSTD_getErrorString(ZSTD_error_no_error)) != 0) goto _output_error; |
| 786 | if (strcmp("Unspecified error code", ZSTD_getErrorString((ZSTD_ErrorCode)(0-ZSTD_error_GENERIC))) != 0) goto _output_error; |
| 787 | if (strcmp("Error (generic)", ZSTD_getErrorName((size_t)0-ZSTD_error_GENERIC)) != 0) goto _output_error; |
| 788 | if (strcmp("Error (generic)", ZSTD_getErrorString(ZSTD_error_GENERIC)) != 0) goto _output_error; |
| 789 | if (strcmp("No error detected", ZSTD_getErrorName(ZSTD_error_GENERIC)) != 0) goto _output_error; |
| 790 | DISPLAYLEVEL(4, "OK \n"); |
| 791 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 792 | _end: |
| 793 | free(CNBuffer); |
| 794 | free(compressedBuffer); |
| 795 | free(decodedBuffer); |
| 796 | return testResult; |
| 797 | |
| 798 | _output_error: |
| 799 | testResult = 1; |
| 800 | DISPLAY("Error detected in Unit tests ! \n"); |
| 801 | goto _end; |
| 802 | } |
| 803 | |
| 804 | |
| 805 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 806 | { |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 807 | const BYTE* b1 = (const BYTE*)buf1; |
| 808 | const BYTE* b2 = (const BYTE*)buf2; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 809 | size_t u; |
| 810 | for (u=0; u<max; u++) { |
| 811 | if (b1[u] != b2[u]) break; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 812 | } |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 813 | return u; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 814 | } |
| 815 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 816 | |
Yann Collet | 7321345 | 2017-04-27 14:19:34 -0700 | [diff] [blame] | 817 | static ZSTD_parameters FUZ_makeParams(ZSTD_compressionParameters cParams, ZSTD_frameParameters fParams) |
| 818 | { |
| 819 | ZSTD_parameters params; |
| 820 | params.cParams = cParams; |
| 821 | params.fParams = fParams; |
| 822 | return params; |
| 823 | } |
| 824 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 825 | static size_t FUZ_rLogLength(U32* seed, U32 logLength) |
| 826 | { |
| 827 | size_t const lengthMask = ((size_t)1 << logLength) - 1; |
| 828 | return (lengthMask+1) + (FUZ_rand(seed) & lengthMask); |
| 829 | } |
| 830 | |
| 831 | static size_t FUZ_randomLength(U32* seed, U32 maxLog) |
| 832 | { |
| 833 | U32 const logLength = FUZ_rand(seed) % maxLog; |
| 834 | return FUZ_rLogLength(seed, logLength); |
| 835 | } |
| 836 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 837 | #undef CHECK |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 838 | #define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ |
| 839 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 840 | |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 841 | static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxDurationS, double compressibility, int bigTests) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 842 | { |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 843 | static const U32 maxSrcLog = 23; |
| 844 | static const U32 maxSampleLog = 22; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 845 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
| 846 | size_t const dstBufferSize = (size_t)1<<maxSampleLog; |
| 847 | size_t const cBufferSize = ZSTD_compressBound(dstBufferSize); |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 848 | BYTE* cNoiseBuffer[5]; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 849 | BYTE* srcBuffer; /* jumping pointer */ |
| 850 | BYTE* const cBuffer = (BYTE*) malloc (cBufferSize); |
| 851 | BYTE* const dstBuffer = (BYTE*) malloc (dstBufferSize); |
| 852 | BYTE* const mirrorBuffer = (BYTE*) malloc (dstBufferSize); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 853 | ZSTD_CCtx* const refCtx = ZSTD_createCCtx(); |
| 854 | ZSTD_CCtx* const ctx = ZSTD_createCCtx(); |
| 855 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 856 | U32 result = 0; |
| 857 | U32 testNb = 0; |
| 858 | U32 coreSeed = seed, lseed = 0; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 859 | clock_t const startClock = clock(); |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 860 | clock_t const maxClockSpan = maxDurationS * CLOCKS_PER_SEC; |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 861 | int const cLevelLimiter = bigTests ? 3 : 2; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 862 | |
| 863 | /* allocation */ |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 864 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 865 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 866 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 867 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 868 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 869 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 870 | || !dstBuffer || !mirrorBuffer || !cBuffer || !refCtx || !ctx || !dctx, |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 871 | "Not enough memory, fuzzer tests cancelled"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 872 | |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 873 | /* Create initial samples */ |
| 874 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 875 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 876 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 877 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 878 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 879 | srcBuffer = cNoiseBuffer[2]; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 880 | |
| 881 | /* catch up testNb */ |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 882 | for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 883 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 884 | /* main test loop */ |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 885 | for ( ; (testNb <= nbTests) || (FUZ_clockSpan(startClock) < maxClockSpan); testNb++ ) { |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 886 | size_t sampleSize, maxTestSize, totalTestSize; |
| 887 | size_t cSize, totalCSize, totalGenSize; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 888 | U64 crcOrig; |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 889 | BYTE* sampleBuffer; |
Yann Collet | 4bfe415 | 2015-12-06 13:18:37 +0100 | [diff] [blame] | 890 | const BYTE* dict; |
| 891 | size_t dictSize; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 892 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 893 | /* notification */ |
| 894 | if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } |
Yann Collet | 4c0b44f | 2016-11-01 11:13:22 -0700 | [diff] [blame] | 895 | else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
Yann Collet | 1c2ddba | 2015-12-04 17:45:35 +0100 | [diff] [blame] | 896 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 897 | FUZ_rand(&coreSeed); |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 898 | { U32 const prime1 = 2654435761U; lseed = coreSeed ^ prime1; } |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 899 | |
| 900 | /* srcBuffer selection [0-4] */ |
| 901 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 902 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 903 | else { |
| 904 | buffNb >>= 3; |
| 905 | if (buffNb & 7) { |
| 906 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 907 | buffNb = tnb[buffNb >> 3]; |
| 908 | } else { |
| 909 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 910 | buffNb = tnb[buffNb >> 3]; |
| 911 | } } |
| 912 | srcBuffer = cNoiseBuffer[buffNb]; |
| 913 | } |
| 914 | |
| 915 | /* select src segment */ |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 916 | sampleSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 917 | |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 918 | /* create sample buffer (to catch read error with valgrind & sanitizers) */ |
| 919 | sampleBuffer = (BYTE*)malloc(sampleSize); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 920 | CHECK(sampleBuffer==NULL, "not enough memory for sample buffer"); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 921 | { size_t const sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); |
| 922 | memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); } |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 923 | crcOrig = XXH64(sampleBuffer, sampleSize, 0); |
| 924 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 925 | /* compression tests */ |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 926 | { unsigned const cLevel = |
| 927 | ( FUZ_rand(&lseed) % |
| 928 | (ZSTD_maxCLevel() - (FUZ_highbit32((U32)sampleSize) / cLevelLimiter)) ) |
| 929 | + 1; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 930 | cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 931 | CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed : %s", ZSTD_getErrorName(cSize)); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 932 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 933 | /* compression failure test : too small dest buffer */ |
| 934 | if (cSize > 3) { |
| 935 | const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 936 | const size_t tooSmallSize = cSize - missing; |
| 937 | const U32 endMark = 0x4DC2B1A9; |
| 938 | memcpy(dstBuffer+tooSmallSize, &endMark, 4); |
| 939 | { size_t const errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel); |
| 940 | CHECK(!ZSTD_isError(errorCode), "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (U32)tooSmallSize, (U32)cSize); } |
| 941 | { U32 endCheck; memcpy(&endCheck, dstBuffer+tooSmallSize, 4); |
| 942 | CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow"); } |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 943 | } } |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 944 | |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 945 | /* Decompressed size test */ |
Sean Purcell | 4e70971 | 2017-02-07 13:50:09 -0800 | [diff] [blame] | 946 | { unsigned long long const rSize = ZSTD_findDecompressedSize(cBuffer, cSize); |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 947 | CHECK(rSize != sampleSize, "decompressed size incorrect"); |
| 948 | } |
| 949 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 950 | /* frame header decompression test */ |
Yann Collet | 542c9df | 2017-05-09 15:46:07 -0700 | [diff] [blame] | 951 | { ZSTD_frameHeader dParams; |
| 952 | size_t const check = ZSTD_getFrameHeader(&dParams, cBuffer, cSize); |
Yann Collet | 346bffb | 2016-03-15 15:24:52 +0100 | [diff] [blame] | 953 | CHECK(ZSTD_isError(check), "Frame Parameters extraction failed"); |
| 954 | CHECK(dParams.frameContentSize != sampleSize, "Frame content size incorrect"); |
| 955 | } |
| 956 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 957 | /* successful decompression test */ |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 958 | { size_t const margin = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 959 | size_t const dSize = ZSTD_decompress(dstBuffer, sampleSize + margin, cBuffer, cSize); |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 960 | CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s) (srcSize : %u ; cSize : %u)", ZSTD_getErrorName(dSize), (U32)sampleSize, (U32)cSize); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 961 | { U64 const crcDest = XXH64(dstBuffer, sampleSize, 0); |
| 962 | CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (U32)findDiff(sampleBuffer, dstBuffer, sampleSize), (U32)sampleSize); |
| 963 | } } |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 964 | |
| 965 | free(sampleBuffer); /* no longer useful after this point */ |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 966 | |
| 967 | /* truncated src decompression test */ |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 968 | { size_t const missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 969 | size_t const tooSmallSize = cSize - missing; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 970 | void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch read overflows */ |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 971 | CHECK(cBufferTooSmall == NULL, "not enough memory !"); |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 972 | memcpy(cBufferTooSmall, cBuffer, tooSmallSize); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 973 | { size_t const errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize); |
| 974 | CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); } |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 975 | free(cBufferTooSmall); |
| 976 | } |
| 977 | |
| 978 | /* too small dst decompression test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 979 | if (sampleSize > 3) { |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 980 | size_t const missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 981 | size_t const tooSmallSize = sampleSize - missing; |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 982 | static const BYTE token = 0xA9; |
| 983 | dstBuffer[tooSmallSize] = token; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 984 | { size_t const errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize); |
| 985 | CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (U32)errorCode, (U32)tooSmallSize); } |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 986 | CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow"); |
| 987 | } |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 988 | |
| 989 | /* noisy src decompression test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 990 | if (cSize > 6) { |
| 991 | /* insert noise into src */ |
| 992 | { U32 const maxNbBits = FUZ_highbit32((U32)(cSize-4)); |
| 993 | size_t pos = 4; /* preserve magic number (too easy to detect) */ |
| 994 | for (;;) { |
| 995 | /* keep some original src */ |
| 996 | { U32 const nbBits = FUZ_rand(&lseed) % maxNbBits; |
| 997 | size_t const mask = (1<<nbBits) - 1; |
| 998 | size_t const skipLength = FUZ_rand(&lseed) & mask; |
| 999 | pos += skipLength; |
| 1000 | } |
| 1001 | if (pos <= cSize) break; |
| 1002 | /* add noise */ |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 1003 | { U32 const nbBitsCodes = FUZ_rand(&lseed) % maxNbBits; |
| 1004 | U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0; |
| 1005 | size_t const mask = (1<<nbBits) - 1; |
| 1006 | size_t const rNoiseLength = (FUZ_rand(&lseed) & mask) + 1; |
| 1007 | size_t const noiseLength = MIN(rNoiseLength, cSize-pos); |
| 1008 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength); |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1009 | memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength); |
| 1010 | pos += noiseLength; |
| 1011 | } } } |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 1012 | |
| 1013 | /* decompress noisy source */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1014 | { U32 const endMark = 0xA9B1C3D6; |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 1015 | memcpy(dstBuffer+sampleSize, &endMark, 4); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1016 | { size_t const decompressResult = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize); |
| 1017 | /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */ |
| 1018 | CHECK((!ZSTD_isError(decompressResult)) && (decompressResult>sampleSize), |
| 1019 | "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (U32)decompressResult, (U32)sampleSize); |
| 1020 | } |
| 1021 | { U32 endCheck; memcpy(&endCheck, dstBuffer+sampleSize, 4); |
| 1022 | CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow"); |
| 1023 | } } } /* noisy src decompression test */ |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1024 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1025 | /*===== Streaming compression test, scattered segments and dictionary =====*/ |
| 1026 | |
| 1027 | { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 1028 | U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 1029 | int const cLevel = (FUZ_rand(&lseed) % |
| 1030 | (ZSTD_maxCLevel() - |
| 1031 | (MAX(testLog, dictLog) / cLevelLimiter))) + |
| 1032 | 1; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1033 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
| 1034 | if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1; |
| 1035 | |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 1036 | dictSize = FUZ_rLogLength(&lseed, dictLog); /* needed also for decompression */ |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 1037 | dict = srcBuffer + (FUZ_rand(&lseed) % (srcBufferSize - dictSize)); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1038 | |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1039 | if (FUZ_rand(&lseed) & 0xF) { |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 1040 | size_t const errorCode = ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, cLevel); |
| 1041 | CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode)); |
| 1042 | } else { |
cyan | b880631 | 2016-05-30 18:20:46 +0200 | [diff] [blame] | 1043 | ZSTD_compressionParameters const cPar = ZSTD_getCParams(cLevel, 0, dictSize); |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 1044 | ZSTD_frameParameters const fPar = { FUZ_rand(&lseed)&1 /* contentSizeFlag */, |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1045 | !(FUZ_rand(&lseed)&3) /* contentChecksumFlag*/, |
| 1046 | 0 /*NodictID*/ }; /* note : since dictionary is fake, dictIDflag has no impact */ |
Yann Collet | 7321345 | 2017-04-27 14:19:34 -0700 | [diff] [blame] | 1047 | ZSTD_parameters const p = FUZ_makeParams(cPar, fPar); |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 1048 | size_t const errorCode = ZSTD_compressBegin_advanced(refCtx, dict, dictSize, p, 0); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 1049 | CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_advanced error : %s", ZSTD_getErrorName(errorCode)); |
| 1050 | } |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 1051 | { size_t const errorCode = ZSTD_copyCCtx(ctx, refCtx, 0); |
| 1052 | CHECK (ZSTD_isError(errorCode), "ZSTD_copyCCtx error : %s", ZSTD_getErrorName(errorCode)); |
| 1053 | } } |
Yann Collet | 06e7697 | 2017-01-25 16:39:03 -0800 | [diff] [blame] | 1054 | ZSTD_setCCtxParameter(ctx, ZSTD_p_forceWindow, FUZ_rand(&lseed) & 1); |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 1055 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1056 | { U32 const nbChunks = (FUZ_rand(&lseed) & 127) + 2; |
| 1057 | U32 n; |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 1058 | XXH64_state_t xxhState; |
| 1059 | XXH64_reset(&xxhState, 0); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1060 | for (totalTestSize=0, cSize=0, n=0 ; n<nbChunks ; n++) { |
| 1061 | size_t const segmentSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1062 | size_t const segmentStart = FUZ_rand(&lseed) % (srcBufferSize - segmentSize); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1063 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1064 | if (cBufferSize-cSize < ZSTD_compressBound(segmentSize)) break; /* avoid invalid dstBufferTooSmall */ |
| 1065 | if (totalTestSize+segmentSize > maxTestSize) break; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1066 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1067 | { size_t const compressResult = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+segmentStart, segmentSize); |
| 1068 | CHECK (ZSTD_isError(compressResult), "multi-segments compression error : %s", ZSTD_getErrorName(compressResult)); |
| 1069 | cSize += compressResult; |
| 1070 | } |
| 1071 | XXH64_update(&xxhState, srcBuffer+segmentStart, segmentSize); |
| 1072 | memcpy(mirrorBuffer + totalTestSize, srcBuffer+segmentStart, segmentSize); |
| 1073 | totalTestSize += segmentSize; |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 1074 | } |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1075 | |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 1076 | { size_t const flushResult = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize, NULL, 0); |
| 1077 | CHECK (ZSTD_isError(flushResult), "multi-segments epilogue error : %s", ZSTD_getErrorName(flushResult)); |
| 1078 | cSize += flushResult; |
| 1079 | } |
| 1080 | crcOrig = XXH64_digest(&xxhState); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1081 | } |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1082 | |
| 1083 | /* streaming decompression test */ |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 1084 | if (dictSize<8) dictSize=0, dict=NULL; /* disable dictionary */ |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1085 | { size_t const errorCode = ZSTD_decompressBegin_usingDict(dctx, dict, dictSize); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 1086 | CHECK (ZSTD_isError(errorCode), "ZSTD_decompressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode)); } |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1087 | totalCSize = 0; |
| 1088 | totalGenSize = 0; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1089 | while (totalCSize < cSize) { |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1090 | size_t const inSize = ZSTD_nextSrcSizeToDecompress(dctx); |
| 1091 | size_t const genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize); |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1092 | CHECK (ZSTD_isError(genSize), "ZSTD_decompressContinue error : %s", ZSTD_getErrorName(genSize)); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1093 | totalGenSize += genSize; |
| 1094 | totalCSize += inSize; |
| 1095 | } |
| 1096 | CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded"); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 1097 | CHECK (totalGenSize != totalTestSize, "streaming decompressed data : wrong size") |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1098 | CHECK (totalCSize != cSize, "compressed data should be fully read") |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1099 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 1100 | if (crcDest!=crcOrig) { |
| 1101 | size_t const errorPos = findDiff(mirrorBuffer, dstBuffer, totalTestSize); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1102 | CHECK (1, "streaming decompressed data corrupted : byte %u / %u (%02X!=%02X)", |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1103 | (U32)errorPos, (U32)totalTestSize, dstBuffer[errorPos], mirrorBuffer[errorPos]); |
| 1104 | } } |
| 1105 | } /* for ( ; (testNb <= nbTests) */ |
Yann Collet | 1c2ddba | 2015-12-04 17:45:35 +0100 | [diff] [blame] | 1106 | DISPLAY("\r%u fuzzer tests completed \n", testNb-1); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1107 | |
| 1108 | _cleanup: |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 1109 | ZSTD_freeCCtx(refCtx); |
Yann Collet | 2f648e5 | 2015-10-29 18:23:38 +0100 | [diff] [blame] | 1110 | ZSTD_freeCCtx(ctx); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1111 | ZSTD_freeDCtx(dctx); |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 1112 | free(cNoiseBuffer[0]); |
| 1113 | free(cNoiseBuffer[1]); |
| 1114 | free(cNoiseBuffer[2]); |
| 1115 | free(cNoiseBuffer[3]); |
| 1116 | free(cNoiseBuffer[4]); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1117 | free(cBuffer); |
| 1118 | free(dstBuffer); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1119 | free(mirrorBuffer); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1120 | return result; |
| 1121 | |
| 1122 | _output_error: |
| 1123 | result = 1; |
| 1124 | goto _cleanup; |
| 1125 | } |
| 1126 | |
| 1127 | |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1128 | /*_******************************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1129 | * Command line |
| 1130 | *********************************************************/ |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1131 | static int FUZ_usage(const char* programName) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1132 | { |
| 1133 | DISPLAY( "Usage :\n"); |
| 1134 | DISPLAY( " %s [args]\n", programName); |
| 1135 | DISPLAY( "\n"); |
| 1136 | DISPLAY( "Arguments :\n"); |
| 1137 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 1138 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 1139 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 1140 | DISPLAY( " -P# : Select compressibility in %% (default:%u%%)\n", FUZ_compressibility_default); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1141 | DISPLAY( " -v : verbose\n"); |
Yann Collet | e9853b2 | 2015-08-07 19:07:32 +0100 | [diff] [blame] | 1142 | DISPLAY( " -p : pause at the end\n"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1143 | DISPLAY( " -h : display help and exit\n"); |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1147 | /*! readU32FromChar() : |
| 1148 | @return : unsigned integer value read from input in `char` format |
| 1149 | allows and interprets K, KB, KiB, M, MB and MiB suffix. |
| 1150 | Will also modify `*stringPtr`, advancing it to position where it stopped reading. |
| 1151 | Note : function result can overflow if digit string > MAX_UINT */ |
| 1152 | static unsigned readU32FromChar(const char** stringPtr) |
| 1153 | { |
| 1154 | unsigned result = 0; |
| 1155 | while ((**stringPtr >='0') && (**stringPtr <='9')) |
| 1156 | result *= 10, result += **stringPtr - '0', (*stringPtr)++ ; |
| 1157 | if ((**stringPtr=='K') || (**stringPtr=='M')) { |
| 1158 | result <<= 10; |
| 1159 | if (**stringPtr=='M') result <<= 10; |
| 1160 | (*stringPtr)++ ; |
| 1161 | if (**stringPtr=='i') (*stringPtr)++; |
| 1162 | if (**stringPtr=='B') (*stringPtr)++; |
| 1163 | } |
| 1164 | return result; |
| 1165 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1166 | |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1167 | int main(int argc, const char** argv) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1168 | { |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1169 | U32 seed = 0; |
| 1170 | int seedset = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1171 | int argNb; |
| 1172 | int nbTests = nbTestsDefault; |
| 1173 | int testNb = 0; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 1174 | U32 proba = FUZ_compressibility_default; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1175 | int result = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1176 | U32 mainPause = 0; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 1177 | U32 maxDuration = 0; |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 1178 | int bigTests = 1; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1179 | const char* const programName = argv[0]; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1180 | |
| 1181 | /* Check command line */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1182 | for (argNb=1; argNb<argc; argNb++) { |
| 1183 | const char* argument = argv[argNb]; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1184 | if(!argument) continue; /* Protection if argument empty */ |
| 1185 | |
| 1186 | /* Handle commands. Aggregated commands are allowed */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1187 | if (argument[0]=='-') { |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 1188 | |
| 1189 | if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; } |
| 1190 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1191 | argument++; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1192 | while (*argument!=0) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1193 | switch(*argument) |
| 1194 | { |
| 1195 | case 'h': |
| 1196 | return FUZ_usage(programName); |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1197 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1198 | case 'v': |
| 1199 | argument++; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1200 | g_displayLevel = 4; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1201 | break; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1202 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1203 | case 'q': |
| 1204 | argument++; |
| 1205 | g_displayLevel--; |
| 1206 | break; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1207 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1208 | case 'p': /* pause at the end */ |
| 1209 | argument++; |
| 1210 | mainPause = 1; |
| 1211 | break; |
| 1212 | |
| 1213 | case 'i': |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1214 | argument++; maxDuration = 0; |
| 1215 | nbTests = readU32FromChar(&argument); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1216 | break; |
| 1217 | |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 1218 | case 'T': |
| 1219 | argument++; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1220 | nbTests = 0; |
| 1221 | maxDuration = readU32FromChar(&argument); |
| 1222 | if (*argument=='s') argument++; /* seconds */ |
| 1223 | if (*argument=='m') maxDuration *= 60, argument++; /* minutes */ |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 1224 | if (*argument=='n') argument++; |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 1225 | break; |
| 1226 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1227 | case 's': |
| 1228 | argument++; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1229 | seedset = 1; |
| 1230 | seed = readU32FromChar(&argument); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1231 | break; |
| 1232 | |
| 1233 | case 't': |
| 1234 | argument++; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1235 | testNb = readU32FromChar(&argument); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1236 | break; |
| 1237 | |
| 1238 | case 'P': /* compressibility % */ |
| 1239 | argument++; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1240 | proba = readU32FromChar(&argument); |
| 1241 | if (proba>100) proba = 100; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1242 | break; |
| 1243 | |
| 1244 | default: |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1245 | return (FUZ_usage(programName), 1); |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1246 | } } } } /* for (argNb=1; argNb<argc; argNb++) */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1247 | |
| 1248 | /* Get Seed */ |
Yann Collet | 45f84ab | 2016-05-20 12:34:40 +0200 | [diff] [blame] | 1249 | DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1250 | |
Yann Collet | 3f01c88 | 2016-06-16 13:38:10 +0200 | [diff] [blame] | 1251 | if (!seedset) { |
| 1252 | time_t const t = time(NULL); |
| 1253 | U32 const h = XXH32(&t, sizeof(t), 1); |
| 1254 | seed = h % 10000; |
| 1255 | } |
| 1256 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1257 | DISPLAY("Seed = %u\n", seed); |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 1258 | if (proba!=FUZ_compressibility_default) DISPLAY("Compressibility : %u%%\n", proba); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1259 | |
Yann Collet | 803c05e | 2016-06-16 11:32:57 +0200 | [diff] [blame] | 1260 | if (nbTests < testNb) nbTests = testNb; |
| 1261 | |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1262 | if (testNb==0) |
| 1263 | result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1264 | if (!result) |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 1265 | result = fuzzerTests(seed, nbTests, testNb, maxDuration, ((double)proba) / 100, bigTests); |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1266 | if (mainPause) { |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 1267 | int unused; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1268 | DISPLAY("Press Enter \n"); |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 1269 | unused = getchar(); |
| 1270 | (void)unused; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1271 | } |
| 1272 | return result; |
| 1273 | } |