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 | 57827f9 | 2017-05-25 15:44:06 -0700 | [diff] [blame] | 408 | { ZSTD_DDict* const ddict = ZSTD_createDDict(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 | 57827f9 | 2017-05-25 15:44:06 -0700 | [diff] [blame] | 415 | DISPLAYLEVEL(4, "test%3i : decompress with static DDict : ", testNb++); |
| 416 | { size_t const ddictBufferSize = ZSTD_estimateDDictSize(dictSize, 0); |
| 417 | void* ddictBuffer = malloc(ddictBufferSize); |
| 418 | if (ddictBuffer == NULL) goto _output_error; |
| 419 | { ZSTD_DDict* const ddict = ZSTD_initStaticDDict(ddictBuffer, ddictBufferSize, CNBuffer, dictSize, 0); |
| 420 | size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict); |
| 421 | if (r != CNBuffSize - dictSize) goto _output_error; |
| 422 | } |
Yann Collet | ff8f83b | 2017-06-20 12:17:32 -0700 | [diff] [blame] | 423 | free(ddictBuffer); |
Yann Collet | 57827f9 | 2017-05-25 15:44:06 -0700 | [diff] [blame] | 424 | DISPLAYLEVEL(4, "OK (size of static DDict : %u) \n", (U32)ddictBufferSize); |
| 425 | } |
| 426 | |
Yann Collet | 541dc7c | 2016-04-12 18:00:20 +0200 | [diff] [blame] | 427 | DISPLAYLEVEL(4, "test%3i : check content size on duplicated context : ", testNb++); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 428 | { size_t const testSize = CNBuffSize / 3; |
Yann Collet | 6c6e175 | 2016-06-27 15:28:45 +0200 | [diff] [blame] | 429 | { ZSTD_parameters p = ZSTD_getParams(2, testSize, dictSize); |
| 430 | p.fParams.contentSizeFlag = 1; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 431 | CHECK( ZSTD_compressBegin_advanced(ctxOrig, CNBuffer, dictSize, p, testSize-1) ); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 432 | } |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 433 | CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, testSize) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 434 | |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 435 | CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(testSize), |
| 436 | (const char*)CNBuffer + dictSize, testSize), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 437 | cSize = r); |
Yann Collet | 542c9df | 2017-05-09 15:46:07 -0700 | [diff] [blame] | 438 | { ZSTD_frameHeader fp; |
| 439 | if (ZSTD_getFrameHeader(&fp, compressedBuffer, cSize)) goto _output_error; |
Yann Collet | 541dc7c | 2016-04-12 18:00:20 +0200 | [diff] [blame] | 440 | if ((fp.frameContentSize != testSize) && (fp.frameContentSize != 0)) goto _output_error; |
| 441 | } } |
| 442 | DISPLAYLEVEL(4, "OK \n"); |
| 443 | |
| 444 | ZSTD_freeCCtx(ctxOrig); |
| 445 | ZSTD_freeCCtx(ctxDuplicated); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 448 | /* Dictionary and dictBuilder tests */ |
| 449 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 450 | size_t dictSize = 16 KB; |
| 451 | void* dictBuffer = malloc(dictSize); |
| 452 | size_t const totalSampleSize = 1 MB; |
| 453 | size_t const sampleUnitSize = 8 KB; |
Yann Collet | b81cbba | 2016-05-30 22:29:45 +0200 | [diff] [blame] | 454 | U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 455 | size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t)); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 456 | U32 dictID; |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 457 | |
| 458 | if (dictBuffer==NULL || samplesSizes==NULL) { |
| 459 | free(dictBuffer); |
| 460 | free(samplesSizes); |
| 461 | goto _output_error; |
| 462 | } |
| 463 | |
| 464 | DISPLAYLEVEL(4, "test%3i : dictBuilder : ", testNb++); |
| 465 | { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; } |
| 466 | dictSize = ZDICT_trainFromBuffer(dictBuffer, dictSize, |
| 467 | CNBuffer, samplesSizes, nbSamples); |
| 468 | if (ZDICT_isError(dictSize)) goto _output_error; |
| 469 | DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize); |
| 470 | |
Yann Collet | da3fbcb | 2016-08-19 14:23:58 +0200 | [diff] [blame] | 471 | DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 472 | dictID = ZDICT_getDictID(dictBuffer, dictSize); |
| 473 | if (dictID==0) goto _output_error; |
| 474 | DISPLAYLEVEL(4, "OK : %u \n", dictID); |
Yann Collet | da3fbcb | 2016-08-19 14:23:58 +0200 | [diff] [blame] | 475 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 476 | DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++); |
| 477 | cSize = ZSTD_compress_usingDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 478 | CNBuffer, CNBuffSize, |
| 479 | dictBuffer, dictSize, 4); |
| 480 | if (ZSTD_isError(cSize)) goto _output_error; |
| 481 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 482 | |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 483 | DISPLAYLEVEL(4, "test%3i : retrieve dictID from dictionary : ", testNb++); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 484 | { U32 const did = ZSTD_getDictID_fromDict(dictBuffer, dictSize); |
| 485 | if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */ |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 486 | } |
| 487 | DISPLAYLEVEL(4, "OK \n"); |
| 488 | |
| 489 | DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 490 | { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize); |
| 491 | if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */ |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 492 | } |
| 493 | DISPLAYLEVEL(4, "OK \n"); |
| 494 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 495 | DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++); |
| 496 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
| 497 | decodedBuffer, CNBuffSize, |
| 498 | compressedBuffer, cSize, |
| 499 | dictBuffer, dictSize), |
| 500 | if (r != CNBuffSize) goto _output_error); |
| 501 | DISPLAYLEVEL(4, "OK \n"); |
| 502 | |
Yann Collet | a1d6704 | 2017-05-08 17:51:49 -0700 | [diff] [blame] | 503 | DISPLAYLEVEL(4, "test%3i : estimate CDict size : ", testNb++); |
| 504 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 505 | size_t const estimatedSize = ZSTD_estimateCDictSize(cParams, dictSize, 1 /*byReference*/); |
Yann Collet | a1d6704 | 2017-05-08 17:51:49 -0700 | [diff] [blame] | 506 | DISPLAYLEVEL(4, "OK : %u \n", (U32)estimatedSize); |
| 507 | } |
| 508 | |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 509 | DISPLAYLEVEL(4, "test%3i : compress with CDict ", testNb++); |
Yann Collet | 2f73427 | 2017-04-27 14:39:39 -0700 | [diff] [blame] | 510 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); |
Yann Collet | 25989e3 | 2017-05-25 15:07:37 -0700 | [diff] [blame] | 511 | ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 512 | 1 /* byReference */, ZSTD_dm_auto, |
| 513 | cParams, ZSTD_defaultCMem); |
| 514 | DISPLAYLEVEL(4, "(size : %u) : ", (U32)ZSTD_sizeof_CDict(cdict)); |
Yann Collet | 2f73427 | 2017-04-27 14:39:39 -0700 | [diff] [blame] | 515 | cSize = ZSTD_compress_usingCDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
Nick Terrell | 39a6cc5 | 2017-04-03 21:00:44 -0700 | [diff] [blame] | 516 | CNBuffer, CNBuffSize, cdict); |
Yann Collet | 2f73427 | 2017-04-27 14:39:39 -0700 | [diff] [blame] | 517 | ZSTD_freeCDict(cdict); |
| 518 | if (ZSTD_isError(cSize)) goto _output_error; |
Nick Terrell | 39a6cc5 | 2017-04-03 21:00:44 -0700 | [diff] [blame] | 519 | } |
| 520 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 521 | |
| 522 | DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++); |
| 523 | { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize); |
| 524 | if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */ |
| 525 | } |
| 526 | DISPLAYLEVEL(4, "OK \n"); |
| 527 | |
Nick Terrell | 39a6cc5 | 2017-04-03 21:00:44 -0700 | [diff] [blame] | 528 | DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++); |
| 529 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
| 530 | decodedBuffer, CNBuffSize, |
| 531 | compressedBuffer, cSize, |
| 532 | dictBuffer, dictSize), |
| 533 | if (r != CNBuffSize) goto _output_error); |
| 534 | DISPLAYLEVEL(4, "OK \n"); |
| 535 | |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 536 | DISPLAYLEVEL(4, "test%3i : compress with static CDict : ", testNb++); |
| 537 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); |
| 538 | size_t const cdictSize = ZSTD_estimateCDictSize(cParams, dictSize, 0); |
| 539 | void* const cdictBuffer = malloc(cdictSize); |
| 540 | if (cdictBuffer==NULL) goto _output_error; |
| 541 | { ZSTD_CDict* const cdict = ZSTD_initStaticCDict(cdictBuffer, cdictSize, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 542 | dictBuffer, dictSize, |
| 543 | 0 /* by Reference */, ZSTD_dm_auto, |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 544 | cParams); |
| 545 | if (cdict == NULL) { |
| 546 | DISPLAY("ZSTD_initStaticCDict failed "); |
| 547 | goto _output_error; |
| 548 | } |
| 549 | cSize = ZSTD_compress_usingCDict(cctx, |
| 550 | compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 551 | CNBuffer, CNBuffSize, cdict); |
| 552 | if (ZSTD_isError(cSize)) { |
| 553 | DISPLAY("ZSTD_compress_usingCDict failed "); |
| 554 | goto _output_error; |
| 555 | } } |
| 556 | free(cdictBuffer); |
| 557 | } |
| 558 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 559 | |
Yann Collet | 2f73427 | 2017-04-27 14:39:39 -0700 | [diff] [blame] | 560 | DISPLAYLEVEL(4, "test%3i : ZSTD_compress_usingCDict_advanced, no contentSize, no dictID : ", testNb++); |
| 561 | { ZSTD_frameParameters const fParams = { 0 /* frameSize */, 1 /* checksum */, 1 /* noDictID*/ }; |
| 562 | ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 563 | ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, 1 /*byRef*/, ZSTD_dm_auto, cParams, ZSTD_defaultCMem); |
Yann Collet | 2f73427 | 2017-04-27 14:39:39 -0700 | [diff] [blame] | 564 | cSize = ZSTD_compress_usingCDict_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 565 | CNBuffer, CNBuffSize, cdict, fParams); |
| 566 | ZSTD_freeCDict(cdict); |
| 567 | if (ZSTD_isError(cSize)) goto _output_error; |
| 568 | } |
| 569 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 570 | |
| 571 | DISPLAYLEVEL(4, "test%3i : try retrieving contentSize from frame : ", testNb++); |
| 572 | { U64 const contentSize = ZSTD_getFrameContentSize(compressedBuffer, cSize); |
| 573 | if (contentSize != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error; |
| 574 | } |
| 575 | DISPLAYLEVEL(4, "OK (unknown)\n"); |
| 576 | |
| 577 | DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++); |
| 578 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
| 579 | decodedBuffer, CNBuffSize, |
| 580 | compressedBuffer, cSize, |
| 581 | dictBuffer, dictSize), |
| 582 | if (r != CNBuffSize) goto _output_error); |
| 583 | DISPLAYLEVEL(4, "OK \n"); |
| 584 | |
| 585 | DISPLAYLEVEL(4, "test%3i : ZSTD_compress_advanced, no dictID : ", testNb++); |
Yann Collet | 6c6e175 | 2016-06-27 15:28:45 +0200 | [diff] [blame] | 586 | { ZSTD_parameters p = ZSTD_getParams(3, CNBuffSize, dictSize); |
| 587 | p.fParams.noDictIDFlag = 1; |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 588 | cSize = ZSTD_compress_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 589 | CNBuffer, CNBuffSize, |
| 590 | dictBuffer, dictSize, p); |
| 591 | if (ZSTD_isError(cSize)) goto _output_error; |
| 592 | } |
| 593 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 594 | |
| 595 | DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++); |
| 596 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
| 597 | decodedBuffer, CNBuffSize, |
| 598 | compressedBuffer, cSize, |
| 599 | dictBuffer, dictSize), |
| 600 | if (r != CNBuffSize) goto _output_error); |
| 601 | DISPLAYLEVEL(4, "OK \n"); |
| 602 | |
Sean Purcell | 834ab50 | 2017-01-11 17:31:06 -0800 | [diff] [blame] | 603 | DISPLAYLEVEL(4, "test%3i : dictionary containing only header should return error : ", testNb++); |
| 604 | { |
| 605 | const size_t ret = ZSTD_decompress_usingDict( |
| 606 | dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, |
| 607 | "\x37\xa4\x30\xec\x11\x22\x33\x44", 8); |
| 608 | if (ZSTD_getErrorCode(ret) != ZSTD_error_dictionary_corrupted) goto _output_error; |
| 609 | } |
Sean Purcell | c44c4d5 | 2017-01-12 09:38:29 -0800 | [diff] [blame] | 610 | DISPLAYLEVEL(4, "OK \n"); |
Sean Purcell | 834ab50 | 2017-01-11 17:31:06 -0800 | [diff] [blame] | 611 | |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 612 | DISPLAYLEVEL(4, "test%3i : Building cdict w/ ZSTD_dm_fullDict on a good dictionary : ", testNb++); |
| 613 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); |
| 614 | ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictSize, 1 /*byRef*/, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem); |
| 615 | if (cdict==NULL) goto _output_error; |
| 616 | ZSTD_freeCDict(cdict); |
| 617 | } |
| 618 | DISPLAYLEVEL(4, "OK \n"); |
| 619 | |
| 620 | DISPLAYLEVEL(4, "test%3i : Building cdict w/ ZSTD_dm_fullDict on a rawContent (must fail) : ", testNb++); |
| 621 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBuffSize, dictSize); |
| 622 | ZSTD_CDict* const cdict = ZSTD_createCDict_advanced((const char*)dictBuffer+1, dictSize-1, 1 /*byRef*/, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem); |
| 623 | if (cdict!=NULL) goto _output_error; |
| 624 | ZSTD_freeCDict(cdict); |
| 625 | } |
| 626 | DISPLAYLEVEL(4, "OK \n"); |
| 627 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 628 | ZSTD_freeCCtx(cctx); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 629 | free(dictBuffer); |
| 630 | free(samplesSizes); |
| 631 | } |
| 632 | |
Nick Terrell | a8b4fe0 | 2017-01-02 18:45:19 -0800 | [diff] [blame] | 633 | /* COVER dictionary builder tests */ |
| 634 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
Nick Terrell | a8b4fe0 | 2017-01-02 18:45:19 -0800 | [diff] [blame] | 635 | size_t dictSize = 16 KB; |
| 636 | size_t optDictSize = dictSize; |
| 637 | void* dictBuffer = malloc(dictSize); |
| 638 | size_t const totalSampleSize = 1 MB; |
| 639 | size_t const sampleUnitSize = 8 KB; |
| 640 | U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize); |
| 641 | size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t)); |
| 642 | COVER_params_t params; |
| 643 | U32 dictID; |
| 644 | |
| 645 | if (dictBuffer==NULL || samplesSizes==NULL) { |
| 646 | free(dictBuffer); |
| 647 | free(samplesSizes); |
| 648 | goto _output_error; |
| 649 | } |
| 650 | |
| 651 | DISPLAYLEVEL(4, "test%3i : COVER_trainFromBuffer : ", testNb++); |
| 652 | { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; } |
| 653 | memset(¶ms, 0, sizeof(params)); |
| 654 | params.d = 1 + (FUZ_rand(&seed) % 16); |
| 655 | params.k = params.d + (FUZ_rand(&seed) % 256); |
| 656 | dictSize = COVER_trainFromBuffer(dictBuffer, dictSize, |
| 657 | CNBuffer, samplesSizes, nbSamples, |
| 658 | params); |
| 659 | if (ZDICT_isError(dictSize)) goto _output_error; |
| 660 | DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize); |
| 661 | |
| 662 | DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++); |
| 663 | dictID = ZDICT_getDictID(dictBuffer, dictSize); |
| 664 | if (dictID==0) goto _output_error; |
| 665 | DISPLAYLEVEL(4, "OK : %u \n", dictID); |
| 666 | |
| 667 | DISPLAYLEVEL(4, "test%3i : COVER_optimizeTrainFromBuffer : ", testNb++); |
| 668 | memset(¶ms, 0, sizeof(params)); |
| 669 | params.steps = 4; |
| 670 | optDictSize = COVER_optimizeTrainFromBuffer(dictBuffer, optDictSize, |
Yann Collet | c0b1731 | 2017-02-28 01:02:46 -0800 | [diff] [blame] | 671 | CNBuffer, samplesSizes, nbSamples / 4, |
Nick Terrell | a8b4fe0 | 2017-01-02 18:45:19 -0800 | [diff] [blame] | 672 | ¶ms); |
| 673 | if (ZDICT_isError(optDictSize)) goto _output_error; |
| 674 | DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)optDictSize); |
| 675 | |
| 676 | DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++); |
| 677 | dictID = ZDICT_getDictID(dictBuffer, optDictSize); |
| 678 | if (dictID==0) goto _output_error; |
| 679 | DISPLAYLEVEL(4, "OK : %u \n", dictID); |
| 680 | |
| 681 | ZSTD_freeCCtx(cctx); |
Nick Terrell | a8b4fe0 | 2017-01-02 18:45:19 -0800 | [diff] [blame] | 682 | free(dictBuffer); |
| 683 | free(samplesSizes); |
| 684 | } |
| 685 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 686 | /* Decompression defense tests */ |
| 687 | DISPLAYLEVEL(4, "test%3i : Check input length for magic number : ", testNb++); |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 688 | { 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] | 689 | if (!ZSTD_isError(r)) goto _output_error; |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 690 | if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 691 | DISPLAYLEVEL(4, "OK \n"); |
| 692 | |
| 693 | DISPLAYLEVEL(4, "test%3i : Check magic Number : ", testNb++); |
| 694 | ((char*)(CNBuffer))[0] = 1; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 695 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 4); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 696 | if (!ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 697 | DISPLAYLEVEL(4, "OK \n"); |
| 698 | |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 699 | /* content size verification test */ |
| 700 | DISPLAYLEVEL(4, "test%3i : Content size verification : ", testNb++); |
| 701 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
| 702 | size_t const srcSize = 5000; |
| 703 | size_t const wrongSrcSize = (srcSize + 1000); |
| 704 | ZSTD_parameters params = ZSTD_getParams(1, wrongSrcSize, 0); |
| 705 | params.fParams.contentSizeFlag = 1; |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 706 | CHECK( ZSTD_compressBegin_advanced(cctx, NULL, 0, params, wrongSrcSize) ); |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 707 | { size_t const result = ZSTD_compressEnd(cctx, decodedBuffer, CNBuffSize, CNBuffer, srcSize); |
| 708 | if (!ZSTD_isError(result)) goto _output_error; |
| 709 | if (ZSTD_getErrorCode(result) != ZSTD_error_srcSize_wrong) goto _output_error; |
| 710 | DISPLAYLEVEL(4, "OK : %s \n", ZSTD_getErrorName(result)); |
Yann Collet | f913cbe | 2017-04-13 22:46:41 -0700 | [diff] [blame] | 711 | } |
| 712 | ZSTD_freeCCtx(cctx); |
| 713 | } |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 714 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 715 | /* block API tests */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 716 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
Yann Collet | d3d2db5 | 2016-07-15 12:20:26 +0200 | [diff] [blame] | 717 | static const size_t dictSize = 65 KB; |
| 718 | 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] | 719 | size_t cSize2; |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 720 | |
| 721 | /* basic block compression */ |
| 722 | DISPLAYLEVEL(4, "test%3i : Block compression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 723 | CHECK( ZSTD_compressBegin(cctx, 5) ); |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 724 | CHECK( ZSTD_getBlockSize(cctx) >= blockSize); |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 725 | cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize); |
| 726 | if (ZSTD_isError(cSize)) goto _output_error; |
| 727 | DISPLAYLEVEL(4, "OK \n"); |
| 728 | |
| 729 | DISPLAYLEVEL(4, "test%3i : Block decompression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 730 | CHECK( ZSTD_decompressBegin(dctx) ); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 731 | { CHECK_V(r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) ); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 732 | if (r != blockSize) goto _output_error; } |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 733 | DISPLAYLEVEL(4, "OK \n"); |
| 734 | |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 735 | /* dictionary block compression */ |
| 736 | DISPLAYLEVEL(4, "test%3i : Dictionary Block compression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 737 | CHECK( ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5) ); |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 738 | cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize); |
| 739 | if (ZSTD_isError(cSize)) goto _output_error; |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 740 | cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize+blockSize, blockSize); |
| 741 | if (ZSTD_isError(cSize2)) goto _output_error; |
Yann Collet | aa2628d | 2016-07-07 15:28:41 +0200 | [diff] [blame] | 742 | memcpy((char*)compressedBuffer+cSize, (char*)CNBuffer+dictSize+blockSize, blockSize); /* fake non-compressed block */ |
| 743 | cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize+blockSize, ZSTD_compressBound(blockSize), |
| 744 | (char*)CNBuffer+dictSize+2*blockSize, blockSize); |
| 745 | if (ZSTD_isError(cSize2)) goto _output_error; |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 746 | DISPLAYLEVEL(4, "OK \n"); |
| 747 | |
| 748 | DISPLAYLEVEL(4, "test%3i : Dictionary Block decompression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 749 | CHECK( ZSTD_decompressBegin_usingDict(dctx, CNBuffer, dictSize) ); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 750 | { CHECK_V( r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) ); |
| 751 | if (r != blockSize) goto _output_error; } |
Yann Collet | aa2628d | 2016-07-07 15:28:41 +0200 | [diff] [blame] | 752 | ZSTD_insertBlock(dctx, (char*)decodedBuffer+blockSize, blockSize); /* insert non-compressed block into dctx history */ |
| 753 | { CHECK_V( r, ZSTD_decompressBlock(dctx, (char*)decodedBuffer+2*blockSize, CNBuffSize, (char*)compressedBuffer+cSize+blockSize, cSize2) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 754 | if (r != blockSize) goto _output_error; } |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 755 | DISPLAYLEVEL(4, "OK \n"); |
| 756 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 757 | ZSTD_freeCCtx(cctx); |
| 758 | ZSTD_freeDCtx(dctx); |
| 759 | } |
| 760 | |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 761 | /* long rle test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 762 | { size_t sampleSize = 0; |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 763 | DISPLAYLEVEL(4, "test%3i : Long RLE test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 764 | RDG_genBuffer(CNBuffer, sampleSize, compressibility, 0., seed+1); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 765 | memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1); |
| 766 | sampleSize += 256 KB - 1; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 767 | RDG_genBuffer((char*)CNBuffer+sampleSize, 96 KB, compressibility, 0., seed+2); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 768 | sampleSize += 96 KB; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 769 | cSize = ZSTD_compress(compressedBuffer, ZSTD_compressBound(sampleSize), CNBuffer, sampleSize, 1); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 770 | if (ZSTD_isError(cSize)) goto _output_error; |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 771 | { CHECK_V(regenSize, ZSTD_decompress(decodedBuffer, sampleSize, compressedBuffer, cSize)); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 772 | if (regenSize!=sampleSize) goto _output_error; } |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 773 | DISPLAYLEVEL(4, "OK \n"); |
| 774 | } |
| 775 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 776 | /* All zeroes test (test bug #137) */ |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 777 | #define ZEROESLENGTH 100 |
| 778 | DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH); |
| 779 | memset(CNBuffer, 0, ZEROESLENGTH); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 780 | { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 781 | cSize = r; } |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 782 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100); |
| 783 | |
| 784 | DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 785 | { CHECK_V(r, ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 786 | if (r != ZEROESLENGTH) goto _output_error; } |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 787 | DISPLAYLEVEL(4, "OK \n"); |
| 788 | |
| 789 | /* nbSeq limit test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 790 | #define _3BYTESTESTLENGTH 131000 |
| 791 | #define NB3BYTESSEQLOG 9 |
| 792 | #define NB3BYTESSEQ (1 << NB3BYTESSEQLOG) |
| 793 | #define NB3BYTESSEQMASK (NB3BYTESSEQ-1) |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 794 | /* creates a buffer full of 3-bytes sequences */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 795 | { BYTE _3BytesSeqs[NB3BYTESSEQ][3]; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 796 | U32 rSeed = 1; |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 797 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 798 | /* create batch of 3-bytes sequences */ |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 799 | { int i; |
| 800 | for (i=0; i < NB3BYTESSEQ; i++) { |
| 801 | _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 802 | _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 803 | _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 804 | } } |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 805 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 806 | /* randomly fills CNBuffer with prepared 3-bytes sequences */ |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 807 | { int i; |
| 808 | for (i=0; i < _3BYTESTESTLENGTH; i += 3) { /* note : CNBuffer size > _3BYTESTESTLENGTH+3 */ |
| 809 | U32 const id = FUZ_rand(&rSeed) & NB3BYTESSEQMASK; |
| 810 | ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0]; |
| 811 | ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1]; |
| 812 | ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2]; |
| 813 | } } } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 814 | DISPLAYLEVEL(4, "test%3i : compress lots 3-bytes sequences : ", testNb++); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 815 | { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 816 | CNBuffer, _3BYTESTESTLENGTH, 19) ); |
| 817 | cSize = r; } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 818 | 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] | 819 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 820 | DISPLAYLEVEL(4, "test%3i : decompress lots 3-bytes sequence : ", testNb++); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 821 | { CHECK_V(r, ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 822 | if (r != _3BYTESTESTLENGTH) goto _output_error; } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 823 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 824 | |
Sean Purcell | 9050e19 | 2017-02-22 12:12:32 -0800 | [diff] [blame] | 825 | /* findFrameCompressedSize on skippable frames */ |
| 826 | DISPLAYLEVEL(4, "test%3i : frame compressed size of skippable frame : ", testNb++); |
| 827 | { const char* frame = "\x50\x2a\x4d\x18\x05\x0\x0\0abcde"; |
| 828 | size_t const frameSrcSize = 13; |
| 829 | if (ZSTD_findFrameCompressedSize(frame, frameSrcSize) != frameSrcSize) goto _output_error; } |
| 830 | DISPLAYLEVEL(4, "OK \n"); |
| 831 | |
Sean Purcell | e0b3265 | 2017-02-08 15:31:47 -0800 | [diff] [blame] | 832 | /* error string tests */ |
| 833 | DISPLAYLEVEL(4, "test%3i : testing ZSTD error code strings : ", testNb++); |
| 834 | if (strcmp("No error detected", ZSTD_getErrorName((ZSTD_ErrorCode)(0-ZSTD_error_no_error))) != 0) goto _output_error; |
| 835 | if (strcmp("No error detected", ZSTD_getErrorString(ZSTD_error_no_error)) != 0) goto _output_error; |
| 836 | if (strcmp("Unspecified error code", ZSTD_getErrorString((ZSTD_ErrorCode)(0-ZSTD_error_GENERIC))) != 0) goto _output_error; |
| 837 | if (strcmp("Error (generic)", ZSTD_getErrorName((size_t)0-ZSTD_error_GENERIC)) != 0) goto _output_error; |
| 838 | if (strcmp("Error (generic)", ZSTD_getErrorString(ZSTD_error_GENERIC)) != 0) goto _output_error; |
| 839 | if (strcmp("No error detected", ZSTD_getErrorName(ZSTD_error_GENERIC)) != 0) goto _output_error; |
| 840 | DISPLAYLEVEL(4, "OK \n"); |
| 841 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 842 | _end: |
| 843 | free(CNBuffer); |
| 844 | free(compressedBuffer); |
| 845 | free(decodedBuffer); |
| 846 | return testResult; |
| 847 | |
| 848 | _output_error: |
| 849 | testResult = 1; |
| 850 | DISPLAY("Error detected in Unit tests ! \n"); |
| 851 | goto _end; |
| 852 | } |
| 853 | |
| 854 | |
| 855 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 856 | { |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 857 | const BYTE* b1 = (const BYTE*)buf1; |
| 858 | const BYTE* b2 = (const BYTE*)buf2; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 859 | size_t u; |
| 860 | for (u=0; u<max; u++) { |
| 861 | if (b1[u] != b2[u]) break; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 862 | } |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 863 | return u; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 864 | } |
| 865 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 866 | |
Yann Collet | 7321345 | 2017-04-27 14:19:34 -0700 | [diff] [blame] | 867 | static ZSTD_parameters FUZ_makeParams(ZSTD_compressionParameters cParams, ZSTD_frameParameters fParams) |
| 868 | { |
| 869 | ZSTD_parameters params; |
| 870 | params.cParams = cParams; |
| 871 | params.fParams = fParams; |
| 872 | return params; |
| 873 | } |
| 874 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 875 | static size_t FUZ_rLogLength(U32* seed, U32 logLength) |
| 876 | { |
| 877 | size_t const lengthMask = ((size_t)1 << logLength) - 1; |
| 878 | return (lengthMask+1) + (FUZ_rand(seed) & lengthMask); |
| 879 | } |
| 880 | |
| 881 | static size_t FUZ_randomLength(U32* seed, U32 maxLog) |
| 882 | { |
| 883 | U32 const logLength = FUZ_rand(seed) % maxLog; |
| 884 | return FUZ_rLogLength(seed, logLength); |
| 885 | } |
| 886 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 887 | #undef CHECK |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 888 | #define CHECK(cond, ...) { \ |
| 889 | if (cond) { \ |
| 890 | DISPLAY("Error => "); \ |
| 891 | DISPLAY(__VA_ARGS__); \ |
| 892 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); \ |
| 893 | goto _output_error; \ |
| 894 | } } |
| 895 | |
| 896 | #define CHECK_Z(f) { \ |
| 897 | size_t const err = f; \ |
| 898 | if (ZSTD_isError(err)) { \ |
| 899 | DISPLAY("Error => %s : %s ", \ |
| 900 | #f, ZSTD_getErrorName(err)); \ |
| 901 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); \ |
| 902 | goto _output_error; \ |
| 903 | } } |
| 904 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 905 | |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 906 | 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] | 907 | { |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 908 | static const U32 maxSrcLog = 23; |
| 909 | static const U32 maxSampleLog = 22; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 910 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
| 911 | size_t const dstBufferSize = (size_t)1<<maxSampleLog; |
| 912 | size_t const cBufferSize = ZSTD_compressBound(dstBufferSize); |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 913 | BYTE* cNoiseBuffer[5]; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 914 | BYTE* srcBuffer; /* jumping pointer */ |
| 915 | BYTE* const cBuffer = (BYTE*) malloc (cBufferSize); |
| 916 | BYTE* const dstBuffer = (BYTE*) malloc (dstBufferSize); |
| 917 | BYTE* const mirrorBuffer = (BYTE*) malloc (dstBufferSize); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 918 | ZSTD_CCtx* const refCtx = ZSTD_createCCtx(); |
| 919 | ZSTD_CCtx* const ctx = ZSTD_createCCtx(); |
| 920 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 921 | U32 result = 0; |
| 922 | U32 testNb = 0; |
| 923 | U32 coreSeed = seed, lseed = 0; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 924 | clock_t const startClock = clock(); |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 925 | clock_t const maxClockSpan = maxDurationS * CLOCKS_PER_SEC; |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 926 | int const cLevelLimiter = bigTests ? 3 : 2; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 927 | |
| 928 | /* allocation */ |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 929 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 930 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 931 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 932 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 933 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 934 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 935 | || !dstBuffer || !mirrorBuffer || !cBuffer || !refCtx || !ctx || !dctx, |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 936 | "Not enough memory, fuzzer tests cancelled"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 937 | |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 938 | /* Create initial samples */ |
| 939 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 940 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 941 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 942 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 943 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 944 | srcBuffer = cNoiseBuffer[2]; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 945 | |
| 946 | /* catch up testNb */ |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 947 | for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 948 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 949 | /* main test loop */ |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 950 | for ( ; (testNb <= nbTests) || (FUZ_clockSpan(startClock) < maxClockSpan); testNb++ ) { |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 951 | size_t sampleSize, maxTestSize, totalTestSize; |
| 952 | size_t cSize, totalCSize, totalGenSize; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 953 | U64 crcOrig; |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 954 | BYTE* sampleBuffer; |
Yann Collet | 4bfe415 | 2015-12-06 13:18:37 +0100 | [diff] [blame] | 955 | const BYTE* dict; |
| 956 | size_t dictSize; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 957 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 958 | /* notification */ |
| 959 | if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } |
Yann Collet | 4c0b44f | 2016-11-01 11:13:22 -0700 | [diff] [blame] | 960 | else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
Yann Collet | 1c2ddba | 2015-12-04 17:45:35 +0100 | [diff] [blame] | 961 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 962 | FUZ_rand(&coreSeed); |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 963 | { U32 const prime1 = 2654435761U; lseed = coreSeed ^ prime1; } |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 964 | |
| 965 | /* srcBuffer selection [0-4] */ |
| 966 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 967 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 968 | else { |
| 969 | buffNb >>= 3; |
| 970 | if (buffNb & 7) { |
| 971 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 972 | buffNb = tnb[buffNb >> 3]; |
| 973 | } else { |
| 974 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 975 | buffNb = tnb[buffNb >> 3]; |
| 976 | } } |
| 977 | srcBuffer = cNoiseBuffer[buffNb]; |
| 978 | } |
| 979 | |
| 980 | /* select src segment */ |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 981 | sampleSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 982 | |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 983 | /* create sample buffer (to catch read error with valgrind & sanitizers) */ |
| 984 | sampleBuffer = (BYTE*)malloc(sampleSize); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 985 | CHECK(sampleBuffer==NULL, "not enough memory for sample buffer"); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 986 | { size_t const sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); |
| 987 | memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); } |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 988 | crcOrig = XXH64(sampleBuffer, sampleSize, 0); |
| 989 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 990 | /* compression tests */ |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 991 | { unsigned const cLevel = |
| 992 | ( FUZ_rand(&lseed) % |
| 993 | (ZSTD_maxCLevel() - (FUZ_highbit32((U32)sampleSize) / cLevelLimiter)) ) |
| 994 | + 1; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 995 | cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 996 | CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed : %s", ZSTD_getErrorName(cSize)); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 997 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 998 | /* compression failure test : too small dest buffer */ |
| 999 | if (cSize > 3) { |
| 1000 | const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 1001 | const size_t tooSmallSize = cSize - missing; |
| 1002 | const U32 endMark = 0x4DC2B1A9; |
| 1003 | memcpy(dstBuffer+tooSmallSize, &endMark, 4); |
| 1004 | { size_t const errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel); |
| 1005 | CHECK(!ZSTD_isError(errorCode), "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (U32)tooSmallSize, (U32)cSize); } |
| 1006 | { U32 endCheck; memcpy(&endCheck, dstBuffer+tooSmallSize, 4); |
| 1007 | CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow"); } |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1008 | } } |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1009 | |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 1010 | /* Decompressed size test */ |
Sean Purcell | 4e70971 | 2017-02-07 13:50:09 -0800 | [diff] [blame] | 1011 | { unsigned long long const rSize = ZSTD_findDecompressedSize(cBuffer, cSize); |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 1012 | CHECK(rSize != sampleSize, "decompressed size incorrect"); |
| 1013 | } |
| 1014 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 1015 | /* frame header decompression test */ |
Yann Collet | 542c9df | 2017-05-09 15:46:07 -0700 | [diff] [blame] | 1016 | { ZSTD_frameHeader dParams; |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 1017 | CHECK_Z( ZSTD_getFrameHeader(&dParams, cBuffer, cSize) ); |
Yann Collet | 346bffb | 2016-03-15 15:24:52 +0100 | [diff] [blame] | 1018 | CHECK(dParams.frameContentSize != sampleSize, "Frame content size incorrect"); |
| 1019 | } |
| 1020 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 1021 | /* successful decompression test */ |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1022 | { 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] | 1023 | size_t const dSize = ZSTD_decompress(dstBuffer, sampleSize + margin, cBuffer, cSize); |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 1024 | 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] | 1025 | { U64 const crcDest = XXH64(dstBuffer, sampleSize, 0); |
| 1026 | CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (U32)findDiff(sampleBuffer, dstBuffer, sampleSize), (U32)sampleSize); |
| 1027 | } } |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 1028 | |
| 1029 | free(sampleBuffer); /* no longer useful after this point */ |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 1030 | |
| 1031 | /* truncated src decompression test */ |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1032 | { size_t const missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 1033 | size_t const tooSmallSize = cSize - missing; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1034 | void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch read overflows */ |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 1035 | CHECK(cBufferTooSmall == NULL, "not enough memory !"); |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 1036 | memcpy(cBufferTooSmall, cBuffer, tooSmallSize); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1037 | { size_t const errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize); |
| 1038 | CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); } |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 1039 | free(cBufferTooSmall); |
| 1040 | } |
| 1041 | |
| 1042 | /* too small dst decompression test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1043 | if (sampleSize > 3) { |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 1044 | size_t const missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 1045 | size_t const tooSmallSize = sampleSize - missing; |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 1046 | static const BYTE token = 0xA9; |
| 1047 | dstBuffer[tooSmallSize] = token; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1048 | { size_t const errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize); |
| 1049 | CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (U32)errorCode, (U32)tooSmallSize); } |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 1050 | CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow"); |
| 1051 | } |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 1052 | |
| 1053 | /* noisy src decompression test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1054 | if (cSize > 6) { |
| 1055 | /* insert noise into src */ |
| 1056 | { U32 const maxNbBits = FUZ_highbit32((U32)(cSize-4)); |
| 1057 | size_t pos = 4; /* preserve magic number (too easy to detect) */ |
| 1058 | for (;;) { |
| 1059 | /* keep some original src */ |
| 1060 | { U32 const nbBits = FUZ_rand(&lseed) % maxNbBits; |
| 1061 | size_t const mask = (1<<nbBits) - 1; |
| 1062 | size_t const skipLength = FUZ_rand(&lseed) & mask; |
| 1063 | pos += skipLength; |
| 1064 | } |
| 1065 | if (pos <= cSize) break; |
| 1066 | /* add noise */ |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 1067 | { U32 const nbBitsCodes = FUZ_rand(&lseed) % maxNbBits; |
| 1068 | U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0; |
| 1069 | size_t const mask = (1<<nbBits) - 1; |
| 1070 | size_t const rNoiseLength = (FUZ_rand(&lseed) & mask) + 1; |
| 1071 | size_t const noiseLength = MIN(rNoiseLength, cSize-pos); |
| 1072 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength); |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1073 | memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength); |
| 1074 | pos += noiseLength; |
| 1075 | } } } |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 1076 | |
| 1077 | /* decompress noisy source */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1078 | { U32 const endMark = 0xA9B1C3D6; |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 1079 | memcpy(dstBuffer+sampleSize, &endMark, 4); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1080 | { size_t const decompressResult = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize); |
| 1081 | /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */ |
| 1082 | CHECK((!ZSTD_isError(decompressResult)) && (decompressResult>sampleSize), |
| 1083 | "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (U32)decompressResult, (U32)sampleSize); |
| 1084 | } |
| 1085 | { U32 endCheck; memcpy(&endCheck, dstBuffer+sampleSize, 4); |
| 1086 | CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow"); |
| 1087 | } } } /* noisy src decompression test */ |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1088 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1089 | /*===== Streaming compression test, scattered segments and dictionary =====*/ |
| 1090 | |
| 1091 | { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 1092 | U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 1093 | int const cLevel = (FUZ_rand(&lseed) % |
| 1094 | (ZSTD_maxCLevel() - |
| 1095 | (MAX(testLog, dictLog) / cLevelLimiter))) + |
| 1096 | 1; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1097 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
| 1098 | if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1; |
| 1099 | |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 1100 | dictSize = FUZ_rLogLength(&lseed, dictLog); /* needed also for decompression */ |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 1101 | dict = srcBuffer + (FUZ_rand(&lseed) % (srcBufferSize - dictSize)); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1102 | |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1103 | if (FUZ_rand(&lseed) & 0xF) { |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 1104 | CHECK_Z ( ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, cLevel) ); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 1105 | } else { |
cyan | b880631 | 2016-05-30 18:20:46 +0200 | [diff] [blame] | 1106 | ZSTD_compressionParameters const cPar = ZSTD_getCParams(cLevel, 0, dictSize); |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 1107 | ZSTD_frameParameters const fPar = { FUZ_rand(&lseed)&1 /* contentSizeFlag */, |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1108 | !(FUZ_rand(&lseed)&3) /* contentChecksumFlag*/, |
| 1109 | 0 /*NodictID*/ }; /* note : since dictionary is fake, dictIDflag has no impact */ |
Yann Collet | 7321345 | 2017-04-27 14:19:34 -0700 | [diff] [blame] | 1110 | ZSTD_parameters const p = FUZ_makeParams(cPar, fPar); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 1111 | CHECK_Z ( ZSTD_compressBegin_advanced(refCtx, dict, dictSize, p, 0) ); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 1112 | } |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 1113 | CHECK_Z( ZSTD_copyCCtx(ctx, refCtx, 0) ); |
| 1114 | } |
Yann Collet | 06e7697 | 2017-01-25 16:39:03 -0800 | [diff] [blame] | 1115 | ZSTD_setCCtxParameter(ctx, ZSTD_p_forceWindow, FUZ_rand(&lseed) & 1); |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 1116 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1117 | { U32 const nbChunks = (FUZ_rand(&lseed) & 127) + 2; |
| 1118 | U32 n; |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 1119 | XXH64_state_t xxhState; |
| 1120 | XXH64_reset(&xxhState, 0); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1121 | for (totalTestSize=0, cSize=0, n=0 ; n<nbChunks ; n++) { |
| 1122 | size_t const segmentSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1123 | size_t const segmentStart = FUZ_rand(&lseed) % (srcBufferSize - segmentSize); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1124 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1125 | if (cBufferSize-cSize < ZSTD_compressBound(segmentSize)) break; /* avoid invalid dstBufferTooSmall */ |
| 1126 | if (totalTestSize+segmentSize > maxTestSize) break; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1127 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1128 | { size_t const compressResult = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+segmentStart, segmentSize); |
| 1129 | CHECK (ZSTD_isError(compressResult), "multi-segments compression error : %s", ZSTD_getErrorName(compressResult)); |
| 1130 | cSize += compressResult; |
| 1131 | } |
| 1132 | XXH64_update(&xxhState, srcBuffer+segmentStart, segmentSize); |
| 1133 | memcpy(mirrorBuffer + totalTestSize, srcBuffer+segmentStart, segmentSize); |
| 1134 | totalTestSize += segmentSize; |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 1135 | } |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1136 | |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 1137 | { size_t const flushResult = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize, NULL, 0); |
| 1138 | CHECK (ZSTD_isError(flushResult), "multi-segments epilogue error : %s", ZSTD_getErrorName(flushResult)); |
| 1139 | cSize += flushResult; |
| 1140 | } |
| 1141 | crcOrig = XXH64_digest(&xxhState); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1142 | } |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1143 | |
| 1144 | /* streaming decompression test */ |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 1145 | if (dictSize<8) dictSize=0, dict=NULL; /* disable dictionary */ |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 1146 | CHECK_Z( ZSTD_decompressBegin_usingDict(dctx, dict, dictSize) ); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1147 | totalCSize = 0; |
| 1148 | totalGenSize = 0; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1149 | while (totalCSize < cSize) { |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1150 | size_t const inSize = ZSTD_nextSrcSizeToDecompress(dctx); |
| 1151 | 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] | 1152 | CHECK (ZSTD_isError(genSize), "ZSTD_decompressContinue error : %s", ZSTD_getErrorName(genSize)); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1153 | totalGenSize += genSize; |
| 1154 | totalCSize += inSize; |
| 1155 | } |
| 1156 | CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded"); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 1157 | CHECK (totalGenSize != totalTestSize, "streaming decompressed data : wrong size") |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1158 | CHECK (totalCSize != cSize, "compressed data should be fully read") |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1159 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 1160 | if (crcDest!=crcOrig) { |
| 1161 | size_t const errorPos = findDiff(mirrorBuffer, dstBuffer, totalTestSize); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 1162 | CHECK (1, "streaming decompressed data corrupted : byte %u / %u (%02X!=%02X)", |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 1163 | (U32)errorPos, (U32)totalTestSize, dstBuffer[errorPos], mirrorBuffer[errorPos]); |
| 1164 | } } |
| 1165 | } /* for ( ; (testNb <= nbTests) */ |
Yann Collet | 1c2ddba | 2015-12-04 17:45:35 +0100 | [diff] [blame] | 1166 | DISPLAY("\r%u fuzzer tests completed \n", testNb-1); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1167 | |
| 1168 | _cleanup: |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 1169 | ZSTD_freeCCtx(refCtx); |
Yann Collet | 2f648e5 | 2015-10-29 18:23:38 +0100 | [diff] [blame] | 1170 | ZSTD_freeCCtx(ctx); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1171 | ZSTD_freeDCtx(dctx); |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 1172 | free(cNoiseBuffer[0]); |
| 1173 | free(cNoiseBuffer[1]); |
| 1174 | free(cNoiseBuffer[2]); |
| 1175 | free(cNoiseBuffer[3]); |
| 1176 | free(cNoiseBuffer[4]); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1177 | free(cBuffer); |
| 1178 | free(dstBuffer); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1179 | free(mirrorBuffer); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1180 | return result; |
| 1181 | |
| 1182 | _output_error: |
| 1183 | result = 1; |
| 1184 | goto _cleanup; |
| 1185 | } |
| 1186 | |
| 1187 | |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1188 | /*_******************************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1189 | * Command line |
| 1190 | *********************************************************/ |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1191 | static int FUZ_usage(const char* programName) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1192 | { |
| 1193 | DISPLAY( "Usage :\n"); |
| 1194 | DISPLAY( " %s [args]\n", programName); |
| 1195 | DISPLAY( "\n"); |
| 1196 | DISPLAY( "Arguments :\n"); |
| 1197 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 1198 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 1199 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 1200 | DISPLAY( " -P# : Select compressibility in %% (default:%u%%)\n", FUZ_compressibility_default); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1201 | DISPLAY( " -v : verbose\n"); |
Yann Collet | e9853b2 | 2015-08-07 19:07:32 +0100 | [diff] [blame] | 1202 | DISPLAY( " -p : pause at the end\n"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1203 | DISPLAY( " -h : display help and exit\n"); |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1207 | /*! readU32FromChar() : |
| 1208 | @return : unsigned integer value read from input in `char` format |
| 1209 | allows and interprets K, KB, KiB, M, MB and MiB suffix. |
| 1210 | Will also modify `*stringPtr`, advancing it to position where it stopped reading. |
| 1211 | Note : function result can overflow if digit string > MAX_UINT */ |
| 1212 | static unsigned readU32FromChar(const char** stringPtr) |
| 1213 | { |
| 1214 | unsigned result = 0; |
| 1215 | while ((**stringPtr >='0') && (**stringPtr <='9')) |
| 1216 | result *= 10, result += **stringPtr - '0', (*stringPtr)++ ; |
| 1217 | if ((**stringPtr=='K') || (**stringPtr=='M')) { |
| 1218 | result <<= 10; |
| 1219 | if (**stringPtr=='M') result <<= 10; |
| 1220 | (*stringPtr)++ ; |
| 1221 | if (**stringPtr=='i') (*stringPtr)++; |
| 1222 | if (**stringPtr=='B') (*stringPtr)++; |
| 1223 | } |
| 1224 | return result; |
| 1225 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1226 | |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1227 | int main(int argc, const char** argv) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1228 | { |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1229 | U32 seed = 0; |
| 1230 | int seedset = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1231 | int argNb; |
| 1232 | int nbTests = nbTestsDefault; |
| 1233 | int testNb = 0; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 1234 | U32 proba = FUZ_compressibility_default; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1235 | int result = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1236 | U32 mainPause = 0; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 1237 | U32 maxDuration = 0; |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 1238 | int bigTests = 1; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1239 | const char* const programName = argv[0]; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1240 | |
| 1241 | /* Check command line */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1242 | for (argNb=1; argNb<argc; argNb++) { |
| 1243 | const char* argument = argv[argNb]; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1244 | if(!argument) continue; /* Protection if argument empty */ |
| 1245 | |
| 1246 | /* Handle commands. Aggregated commands are allowed */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1247 | if (argument[0]=='-') { |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 1248 | |
| 1249 | if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; } |
| 1250 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1251 | argument++; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1252 | while (*argument!=0) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1253 | switch(*argument) |
| 1254 | { |
| 1255 | case 'h': |
| 1256 | return FUZ_usage(programName); |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1257 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1258 | case 'v': |
| 1259 | argument++; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1260 | g_displayLevel = 4; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1261 | break; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1262 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1263 | case 'q': |
| 1264 | argument++; |
| 1265 | g_displayLevel--; |
| 1266 | break; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1267 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1268 | case 'p': /* pause at the end */ |
| 1269 | argument++; |
| 1270 | mainPause = 1; |
| 1271 | break; |
| 1272 | |
| 1273 | case 'i': |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1274 | argument++; maxDuration = 0; |
| 1275 | nbTests = readU32FromChar(&argument); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1276 | break; |
| 1277 | |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 1278 | case 'T': |
| 1279 | argument++; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1280 | nbTests = 0; |
| 1281 | maxDuration = readU32FromChar(&argument); |
| 1282 | if (*argument=='s') argument++; /* seconds */ |
| 1283 | if (*argument=='m') maxDuration *= 60, argument++; /* minutes */ |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 1284 | if (*argument=='n') argument++; |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 1285 | break; |
| 1286 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1287 | case 's': |
| 1288 | argument++; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1289 | seedset = 1; |
| 1290 | seed = readU32FromChar(&argument); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1291 | break; |
| 1292 | |
| 1293 | case 't': |
| 1294 | argument++; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1295 | testNb = readU32FromChar(&argument); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1296 | break; |
| 1297 | |
| 1298 | case 'P': /* compressibility % */ |
| 1299 | argument++; |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1300 | proba = readU32FromChar(&argument); |
| 1301 | if (proba>100) proba = 100; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1302 | break; |
| 1303 | |
| 1304 | default: |
Yann Collet | c691542 | 2017-04-27 16:24:53 -0700 | [diff] [blame] | 1305 | return (FUZ_usage(programName), 1); |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1306 | } } } } /* for (argNb=1; argNb<argc; argNb++) */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1307 | |
| 1308 | /* Get Seed */ |
Yann Collet | 45f84ab | 2016-05-20 12:34:40 +0200 | [diff] [blame] | 1309 | 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] | 1310 | |
Yann Collet | 3f01c88 | 2016-06-16 13:38:10 +0200 | [diff] [blame] | 1311 | if (!seedset) { |
| 1312 | time_t const t = time(NULL); |
| 1313 | U32 const h = XXH32(&t, sizeof(t), 1); |
| 1314 | seed = h % 10000; |
| 1315 | } |
| 1316 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1317 | DISPLAY("Seed = %u\n", seed); |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 1318 | if (proba!=FUZ_compressibility_default) DISPLAY("Compressibility : %u%%\n", proba); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1319 | |
Yann Collet | 803c05e | 2016-06-16 11:32:57 +0200 | [diff] [blame] | 1320 | if (nbTests < testNb) nbTests = testNb; |
| 1321 | |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1322 | if (testNb==0) |
| 1323 | result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1324 | if (!result) |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 1325 | result = fuzzerTests(seed, nbTests, testNb, maxDuration, ((double)proba) / 100, bigTests); |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1326 | if (mainPause) { |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 1327 | int unused; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1328 | DISPLAY("Press Enter \n"); |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 1329 | unused = getchar(); |
| 1330 | (void)unused; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1331 | } |
| 1332 | return result; |
| 1333 | } |