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 | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 9 | |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 10 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 11 | /*-************************************ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 12 | * Compiler specific |
| 13 | **************************************/ |
| 14 | #ifdef _MSC_VER /* Visual Studio */ |
Przemyslaw Skibinski | 97a258d | 2016-12-21 14:00:41 +0100 | [diff] [blame] | 15 | # define _CRT_SECURE_NO_WARNINGS /* fgets */ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 16 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 17 | # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ |
| 18 | #endif |
| 19 | |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 20 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 21 | /*-************************************ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 22 | * Includes |
| 23 | **************************************/ |
inikep | e75909e | 2016-05-25 11:31:16 +0200 | [diff] [blame] | 24 | #include <stdlib.h> /* free */ |
| 25 | #include <stdio.h> /* fgets, sscanf */ |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 26 | #include <time.h> /* clock_t, clock() */ |
inikep | e75909e | 2016-05-25 11:31:16 +0200 | [diff] [blame] | 27 | #include <string.h> /* strcmp */ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 28 | #include "mem.h" |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 29 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel */ |
| 30 | #include "zstd.h" /* ZSTD_compressBound */ |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 31 | #define ZBUFF_STATIC_LINKING_ONLY /* ZBUFF_createCCtx_advanced */ |
| 32 | #include "zbuff.h" /* ZBUFF_isError */ |
inikep | e75909e | 2016-05-25 11:31:16 +0200 | [diff] [blame] | 33 | #include "datagen.h" /* RDG_genBuffer */ |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 34 | #define XXH_STATIC_LINKING_ONLY |
Yann Collet | 5347aee | 2016-06-04 19:12:48 +0200 | [diff] [blame] | 35 | #include "xxhash.h" /* XXH64_* */ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 36 | |
| 37 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 38 | /*-************************************ |
| 39 | * Constants |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 40 | **************************************/ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 41 | #define KB *(1U<<10) |
| 42 | #define MB *(1U<<20) |
| 43 | #define GB *(1U<<30) |
| 44 | |
Yann Collet | 5f2ec63 | 2015-11-26 10:32:17 +0100 | [diff] [blame] | 45 | static const U32 nbTestsDefault = 10000; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 46 | #define COMPRESSIBLE_NOISE_LENGTH (10 MB) |
| 47 | #define FUZ_COMPRESSIBILITY_DEFAULT 50 |
| 48 | static const U32 prime1 = 2654435761U; |
| 49 | static const U32 prime2 = 2246822519U; |
| 50 | |
| 51 | |
| 52 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 53 | /*-************************************ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 54 | * Display Macros |
| 55 | **************************************/ |
| 56 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 57 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 58 | static U32 g_displayLevel = 2; |
| 59 | |
| 60 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 61 | if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ |
| 62 | { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 63 | if (g_displayLevel>=4) fflush(stdout); } } |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 64 | static const clock_t g_refreshRate = CLOCKS_PER_SEC * 15 / 100; |
| 65 | static clock_t g_displayClock = 0; |
Yann Collet | 7447ee9 | 2015-11-26 10:52:30 +0100 | [diff] [blame] | 66 | |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 67 | static clock_t g_clockTime = 0; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 68 | |
| 69 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 70 | /*-******************************************************* |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 71 | * Fuzzer functions |
| 72 | *********************************************************/ |
| 73 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
| 74 | |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 75 | static clock_t FUZ_GetClockSpan(clock_t clockStart) |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 76 | { |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 77 | return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 80 | /*! FUZ_rand() : |
| 81 | @return : a 27 bits random value, from a 32-bits `seed`. |
| 82 | `seed` is also modified */ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 83 | # define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 84 | unsigned int FUZ_rand(unsigned int* seedPtr) |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 85 | { |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 86 | U32 rand32 = *seedPtr; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 87 | rand32 *= prime1; |
| 88 | rand32 += prime2; |
| 89 | rand32 = FUZ_rotl32(rand32, 13); |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 90 | *seedPtr = rand32; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 91 | return rand32 >> 5; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /* |
| 96 | static unsigned FUZ_highbit32(U32 v32) |
| 97 | { |
| 98 | unsigned nbBits = 0; |
| 99 | if (v32==0) return 0; |
| 100 | for ( ; v32 ; v32>>=1) nbBits++; |
| 101 | return nbBits; |
| 102 | } |
| 103 | */ |
| 104 | |
inikep | 7cab86f | 2016-06-02 18:24:07 +0200 | [diff] [blame] | 105 | static void* ZBUFF_allocFunction(void* opaque, size_t size) |
inikep | a1653fb | 2016-05-24 15:35:48 +0200 | [diff] [blame] | 106 | { |
| 107 | void* address = malloc(size); |
inikep | ff2d189 | 2016-06-02 22:15:09 +0200 | [diff] [blame] | 108 | (void)opaque; |
inikep | ff9114a | 2016-06-02 16:52:36 +0200 | [diff] [blame] | 109 | /* DISPLAYLEVEL(4, "alloc %p, %d opaque=%p \n", address, (int)size, opaque); */ |
inikep | a1653fb | 2016-05-24 15:35:48 +0200 | [diff] [blame] | 110 | return address; |
| 111 | } |
| 112 | |
inikep | 7cab86f | 2016-06-02 18:24:07 +0200 | [diff] [blame] | 113 | static void ZBUFF_freeFunction(void* opaque, void* address) |
inikep | a1653fb | 2016-05-24 15:35:48 +0200 | [diff] [blame] | 114 | { |
inikep | 2866951 | 2016-06-02 13:04:18 +0200 | [diff] [blame] | 115 | (void)opaque; |
inikep | ff9114a | 2016-06-02 16:52:36 +0200 | [diff] [blame] | 116 | /* if (address) DISPLAYLEVEL(4, "free %p opaque=%p \n", address, opaque); */ |
inikep | a1653fb | 2016-05-24 15:35:48 +0200 | [diff] [blame] | 117 | free(address); |
| 118 | } |
| 119 | |
| 120 | static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem) |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 121 | { |
| 122 | int testResult = 0; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 123 | size_t CNBufferSize = COMPRESSIBLE_NOISE_LENGTH; |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 124 | void* CNBuffer = malloc(CNBufferSize); |
inikep | f772bf5 | 2016-05-31 12:43:46 +0200 | [diff] [blame] | 125 | size_t const skippableFrameSize = 11; |
| 126 | size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH); |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 127 | void* compressedBuffer = malloc(compressedBufferSize); |
| 128 | size_t const decodedBufferSize = CNBufferSize; |
| 129 | void* decodedBuffer = malloc(decodedBufferSize); |
inikep | 5c27717 | 2016-06-01 09:16:11 +0200 | [diff] [blame] | 130 | size_t cSize, readSize, readSkipSize, genSize; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 131 | U32 testNb=0; |
inikep | a1653fb | 2016-05-24 15:35:48 +0200 | [diff] [blame] | 132 | ZBUFF_CCtx* zc = ZBUFF_createCCtx_advanced(customMem); |
| 133 | ZBUFF_DCtx* zd = ZBUFF_createDCtx_advanced(customMem); |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 134 | |
| 135 | /* Create compressible test buffer */ |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 136 | if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) { |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 137 | DISPLAY("Not enough memory, aborting\n"); |
| 138 | goto _output_error; |
| 139 | } |
Yann Collet | bde926f | 2016-05-18 17:18:48 +0200 | [diff] [blame] | 140 | RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed); |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 141 | |
inikep | f772bf5 | 2016-05-31 12:43:46 +0200 | [diff] [blame] | 142 | /* generate skippable frame */ |
| 143 | MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START); |
inikep | 5bee32e | 2016-05-31 13:36:14 +0200 | [diff] [blame] | 144 | MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize); |
inikep | f772bf5 | 2016-05-31 12:43:46 +0200 | [diff] [blame] | 145 | cSize = skippableFrameSize + 8; |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 146 | |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 147 | /* Basic compression test */ |
| 148 | DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 149 | ZBUFF_compressInitDictionary(zc, CNBuffer, 128 KB, 1); |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 150 | readSize = CNBufferSize; |
| 151 | genSize = compressedBufferSize; |
inikep | 5c27717 | 2016-06-01 09:16:11 +0200 | [diff] [blame] | 152 | { size_t const r = ZBUFF_compressContinue(zc, ((char*)compressedBuffer)+cSize, &genSize, CNBuffer, &readSize); |
Yann Collet | 202844e | 2016-06-01 00:44:36 +0200 | [diff] [blame] | 153 | if (ZBUFF_isError(r)) goto _output_error; } |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 154 | if (readSize != CNBufferSize) goto _output_error; /* entire input should be consumed */ |
inikep | f772bf5 | 2016-05-31 12:43:46 +0200 | [diff] [blame] | 155 | cSize += genSize; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 156 | genSize = compressedBufferSize - cSize; |
Yann Collet | 202844e | 2016-06-01 00:44:36 +0200 | [diff] [blame] | 157 | { size_t const r = ZBUFF_compressEnd(zc, ((char*)compressedBuffer)+cSize, &genSize); |
Yann Collet | 9a021c1 | 2016-08-26 09:05:06 +0200 | [diff] [blame] | 158 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 159 | cSize += genSize; |
| 160 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
| 161 | |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 162 | /* skippable frame test */ |
| 163 | DISPLAYLEVEL(4, "test%3i : decompress skippable frame : ", testNb++); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 164 | ZBUFF_decompressInitDictionary(zd, CNBuffer, 128 KB); |
inikep | 43aa9fe | 2016-05-31 19:36:51 +0200 | [diff] [blame] | 165 | readSkipSize = cSize; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 166 | genSize = CNBufferSize; |
inikep | 5c27717 | 2016-06-01 09:16:11 +0200 | [diff] [blame] | 167 | { size_t const r = ZBUFF_decompressContinue(zd, decodedBuffer, &genSize, compressedBuffer, &readSkipSize); |
| 168 | if (r != 0) goto _output_error; } |
| 169 | if (genSize != 0) goto _output_error; /* skippable frame len is 0 */ |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 170 | DISPLAYLEVEL(4, "OK \n"); |
| 171 | |
| 172 | /* Basic decompression test */ |
| 173 | DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
inikep | 43aa9fe | 2016-05-31 19:36:51 +0200 | [diff] [blame] | 174 | ZBUFF_decompressInitDictionary(zd, CNBuffer, 128 KB); |
| 175 | readSize = cSize - readSkipSize; |
| 176 | genSize = CNBufferSize; |
inikep | 5c27717 | 2016-06-01 09:16:11 +0200 | [diff] [blame] | 177 | { size_t const r = ZBUFF_decompressContinue(zd, decodedBuffer, &genSize, ((char*)compressedBuffer)+readSkipSize, &readSize); |
Yann Collet | 202844e | 2016-06-01 00:44:36 +0200 | [diff] [blame] | 178 | if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 179 | if (genSize != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
inikep | 43aa9fe | 2016-05-31 19:36:51 +0200 | [diff] [blame] | 180 | if (readSize+readSkipSize != cSize) goto _output_error; /* should have read the entire frame */ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 181 | DISPLAYLEVEL(4, "OK \n"); |
| 182 | |
| 183 | /* check regenerated data is byte exact */ |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 184 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 185 | { size_t i; |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 186 | for (i=0; i<CNBufferSize; i++) { |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 187 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 188 | } } |
| 189 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 190 | |
Yann Collet | bd39d54 | 2016-05-10 14:14:19 +0200 | [diff] [blame] | 191 | /* Byte-by-byte decompression test */ |
Yann Collet | b12b369 | 2016-05-10 15:30:43 +0200 | [diff] [blame] | 192 | DISPLAYLEVEL(4, "test%3i : decompress byte-by-byte : ", testNb++); |
inikep | 43aa9fe | 2016-05-31 19:36:51 +0200 | [diff] [blame] | 193 | { size_t r, pIn=0, pOut=0; |
Yann Collet | 83c3f44 | 2016-06-01 17:44:53 +0200 | [diff] [blame] | 194 | do |
inikep | 43aa9fe | 2016-05-31 19:36:51 +0200 | [diff] [blame] | 195 | { ZBUFF_decompressInitDictionary(zd, CNBuffer, 128 KB); |
| 196 | r = 1; |
| 197 | while (r) { |
| 198 | size_t inS = 1; |
| 199 | size_t outS = 1; |
| 200 | r = ZBUFF_decompressContinue(zd, ((BYTE*)decodedBuffer)+pOut, &outS, ((BYTE*)compressedBuffer)+pIn, &inS); |
| 201 | pIn += inS; |
| 202 | pOut += outS; |
| 203 | } |
| 204 | readSize = pIn; |
| 205 | genSize = pOut; |
| 206 | } while (genSize==0); |
Yann Collet | bd39d54 | 2016-05-10 14:14:19 +0200 | [diff] [blame] | 207 | } |
| 208 | if (genSize != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 209 | if (readSize != cSize) goto _output_error; /* should have read the entire frame */ |
| 210 | DISPLAYLEVEL(4, "OK \n"); |
| 211 | |
| 212 | /* check regenerated data is byte exact */ |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 213 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
Yann Collet | bd39d54 | 2016-05-10 14:14:19 +0200 | [diff] [blame] | 214 | { size_t i; |
Yann Collet | bd39d54 | 2016-05-10 14:14:19 +0200 | [diff] [blame] | 215 | for (i=0; i<CNBufferSize; i++) { |
| 216 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 217 | } } |
| 218 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | bd39d54 | 2016-05-10 14:14:19 +0200 | [diff] [blame] | 219 | |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 220 | _end: |
| 221 | ZBUFF_freeCCtx(zc); |
| 222 | ZBUFF_freeDCtx(zd); |
| 223 | free(CNBuffer); |
| 224 | free(compressedBuffer); |
| 225 | free(decodedBuffer); |
| 226 | return testResult; |
| 227 | |
| 228 | _output_error: |
| 229 | testResult = 1; |
| 230 | DISPLAY("Error detected in Unit tests ! \n"); |
| 231 | goto _end; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 236 | { |
| 237 | const BYTE* b1 = (const BYTE*)buf1; |
| 238 | const BYTE* b2 = (const BYTE*)buf2; |
Yann Collet | bde926f | 2016-05-18 17:18:48 +0200 | [diff] [blame] | 239 | size_t u; |
| 240 | for (u=0; u<max; u++) { |
| 241 | if (b1[u] != b2[u]) break; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 242 | } |
Yann Collet | bde926f | 2016-05-18 17:18:48 +0200 | [diff] [blame] | 243 | return u; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 246 | static size_t FUZ_rLogLength(U32* seed, U32 logLength) |
| 247 | { |
| 248 | size_t const lengthMask = ((size_t)1 << logLength) - 1; |
| 249 | return (lengthMask+1) + (FUZ_rand(seed) & lengthMask); |
| 250 | } |
| 251 | |
| 252 | static size_t FUZ_randomLength(U32* seed, U32 maxLog) |
| 253 | { |
| 254 | U32 const logLength = FUZ_rand(seed) % maxLog; |
| 255 | return FUZ_rLogLength(seed, logLength); |
| 256 | } |
| 257 | |
inikep | 957823f | 2016-05-25 15:30:55 +0200 | [diff] [blame] | 258 | #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) |
| 259 | |
Yann Collet | bde926f | 2016-05-18 17:18:48 +0200 | [diff] [blame] | 260 | #define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ |
| 261 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } |
| 262 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 263 | static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility) |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 264 | { |
Yann Collet | efb1830 | 2016-04-01 18:54:13 +0200 | [diff] [blame] | 265 | static const U32 maxSrcLog = 24; |
| 266 | static const U32 maxSampleLog = 19; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 267 | BYTE* cNoiseBuffer[5]; |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 268 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 269 | BYTE* copyBuffer; |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 270 | size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog); |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 271 | BYTE* cBuffer; |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 272 | size_t const cBufferSize = ZSTD_compressBound(srcBufferSize); |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 273 | BYTE* dstBuffer; |
| 274 | size_t dstBufferSize = srcBufferSize; |
| 275 | U32 result = 0; |
| 276 | U32 testNb = 0; |
Yann Collet | efb1830 | 2016-04-01 18:54:13 +0200 | [diff] [blame] | 277 | U32 coreSeed = seed; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 278 | ZBUFF_CCtx* zc; |
| 279 | ZBUFF_DCtx* zd; |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 280 | clock_t startClock = clock(); |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 281 | |
Yann Collet | efb1830 | 2016-04-01 18:54:13 +0200 | [diff] [blame] | 282 | /* allocations */ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 283 | zc = ZBUFF_createCCtx(); |
| 284 | zd = ZBUFF_createDCtx(); |
| 285 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 286 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 287 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 288 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 289 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
| 290 | copyBuffer= (BYTE*)malloc (copyBufferSize); |
| 291 | dstBuffer = (BYTE*)malloc (dstBufferSize); |
| 292 | cBuffer = (BYTE*)malloc (cBufferSize); |
| 293 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
| 294 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd, |
| 295 | "Not enough memory, fuzzer tests cancelled"); |
| 296 | |
| 297 | /* Create initial samples */ |
| 298 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 299 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 300 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 301 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 302 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
Yann Collet | 541cf19 | 2016-04-04 01:49:30 +0200 | [diff] [blame] | 303 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 304 | |
| 305 | /* catch up testNb */ |
| 306 | for (testNb=1; testNb < startTest; testNb++) |
| 307 | FUZ_rand(&coreSeed); |
| 308 | |
| 309 | /* test loop */ |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 310 | for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) { |
Yann Collet | efb1830 | 2016-04-01 18:54:13 +0200 | [diff] [blame] | 311 | U32 lseed; |
Yann Collet | 541cf19 | 2016-04-04 01:49:30 +0200 | [diff] [blame] | 312 | const BYTE* srcBuffer; |
Yann Collet | 3a061a4 | 2015-12-12 11:17:42 +0100 | [diff] [blame] | 313 | const BYTE* dict; |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 314 | size_t maxTestSize, dictSize; |
| 315 | size_t cSize, totalTestSize, totalCSize, totalGenSize; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 316 | size_t errorCode; |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 317 | U32 n, nbChunks; |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 318 | XXH64_state_t xxhState; |
Yann Collet | efb1830 | 2016-04-01 18:54:13 +0200 | [diff] [blame] | 319 | U64 crcOrig; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 320 | |
| 321 | /* init */ |
Yann Collet | 26415d3 | 2015-11-26 12:43:28 +0100 | [diff] [blame] | 322 | DISPLAYUPDATE(2, "\r%6u", testNb); |
| 323 | if (nbTests >= testNb) DISPLAYUPDATE(2, "/%6u ", nbTests); |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 324 | FUZ_rand(&coreSeed); |
| 325 | lseed = coreSeed ^ prime1; |
Yann Collet | 15bc943 | 2016-04-04 01:22:26 +0200 | [diff] [blame] | 326 | |
Yann Collet | 83c3f44 | 2016-06-01 17:44:53 +0200 | [diff] [blame] | 327 | /* states full reset (unsynchronized) */ |
| 328 | /* some issues only happen when reusing states in a specific sequence of parameters */ |
Yann Collet | 15bc943 | 2016-04-04 01:22:26 +0200 | [diff] [blame] | 329 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZBUFF_freeCCtx(zc); zc = ZBUFF_createCCtx(); } |
| 330 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZBUFF_freeDCtx(zd); zd = ZBUFF_createDCtx(); } |
| 331 | |
Yann Collet | 9bf7016 | 2016-04-04 02:53:27 +0200 | [diff] [blame] | 332 | /* srcBuffer selection [0-4] */ |
Yann Collet | 541cf19 | 2016-04-04 01:49:30 +0200 | [diff] [blame] | 333 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 334 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
Yann Collet | 541cf19 | 2016-04-04 01:49:30 +0200 | [diff] [blame] | 335 | else { |
| 336 | buffNb >>= 3; |
| 337 | if (buffNb & 7) { |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 338 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
Yann Collet | 541cf19 | 2016-04-04 01:49:30 +0200 | [diff] [blame] | 339 | buffNb = tnb[buffNb >> 3]; |
| 340 | } else { |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 341 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
Yann Collet | 541cf19 | 2016-04-04 01:49:30 +0200 | [diff] [blame] | 342 | buffNb = tnb[buffNb >> 3]; |
| 343 | } } |
| 344 | srcBuffer = cNoiseBuffer[buffNb]; |
| 345 | } |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 346 | |
Yann Collet | 9bf7016 | 2016-04-04 02:53:27 +0200 | [diff] [blame] | 347 | /* compression init */ |
| 348 | { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 349 | U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1; |
Yann Collet | 9bf7016 | 2016-04-04 02:53:27 +0200 | [diff] [blame] | 350 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
Yann Collet | 83c3f44 | 2016-06-01 17:44:53 +0200 | [diff] [blame] | 351 | dictSize = (FUZ_rand(&lseed)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0; |
Yann Collet | 9bf7016 | 2016-04-04 02:53:27 +0200 | [diff] [blame] | 352 | /* random dictionary selection */ |
Yann Collet | 83c3f44 | 2016-06-01 17:44:53 +0200 | [diff] [blame] | 353 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 354 | dict = srcBuffer + dictStart; |
Yann Collet | 9bf7016 | 2016-04-04 02:53:27 +0200 | [diff] [blame] | 355 | } |
Yann Collet | 6c6e175 | 2016-06-27 15:28:45 +0200 | [diff] [blame] | 356 | { ZSTD_parameters params = ZSTD_getParams(cLevel, 0, dictSize); |
| 357 | params.fParams.checksumFlag = FUZ_rand(&lseed) & 1; |
| 358 | params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1; |
Yann Collet | 83c3f44 | 2016-06-01 17:44:53 +0200 | [diff] [blame] | 359 | { size_t const initError = ZBUFF_compressInit_advanced(zc, dict, dictSize, params, 0); |
| 360 | CHECK (ZBUFF_isError(initError),"init error : %s", ZBUFF_getErrorName(initError)); |
| 361 | } } } |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 362 | |
| 363 | /* multi-segments compression test */ |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 364 | XXH64_reset(&xxhState, 0); |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 365 | nbChunks = (FUZ_rand(&lseed) & 127) + 2; |
| 366 | for (n=0, cSize=0, totalTestSize=0 ; (n<nbChunks) && (totalTestSize < maxTestSize) ; n++) { |
| 367 | /* compress random chunk into random size dst buffer */ |
| 368 | { size_t readChunkSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 369 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 370 | size_t dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 371 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - readChunkSize); |
Yann Collet | 3a061a4 | 2015-12-12 11:17:42 +0100 | [diff] [blame] | 372 | |
Yann Collet | 9bf7016 | 2016-04-04 02:53:27 +0200 | [diff] [blame] | 373 | size_t const compressionError = ZBUFF_compressContinue(zc, cBuffer+cSize, &dstBuffSize, srcBuffer+srcStart, &readChunkSize); |
| 374 | CHECK (ZBUFF_isError(compressionError), "compression error : %s", ZBUFF_getErrorName(compressionError)); |
Yann Collet | 31d1806 | 2015-11-27 14:07:36 +0100 | [diff] [blame] | 375 | |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 376 | XXH64_update(&xxhState, srcBuffer+srcStart, readChunkSize); |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 377 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, readChunkSize); |
Yann Collet | 541cf19 | 2016-04-04 01:49:30 +0200 | [diff] [blame] | 378 | cSize += dstBuffSize; |
| 379 | totalTestSize += readChunkSize; |
| 380 | } |
Yann Collet | 6bcdeac | 2015-11-26 11:43:00 +0100 | [diff] [blame] | 381 | |
Yann Collet | efb1830 | 2016-04-01 18:54:13 +0200 | [diff] [blame] | 382 | /* random flush operation, to mess around */ |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 383 | if ((FUZ_rand(&lseed) & 15) == 0) { |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 384 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 385 | size_t dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 386 | size_t const flushError = ZBUFF_compressFlush(zc, cBuffer+cSize, &dstBuffSize); |
| 387 | CHECK (ZBUFF_isError(flushError), "flush error : %s", ZBUFF_getErrorName(flushError)); |
Yann Collet | 541cf19 | 2016-04-04 01:49:30 +0200 | [diff] [blame] | 388 | cSize += dstBuffSize; |
| 389 | } } |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 390 | |
| 391 | /* final frame epilogue */ |
Yann Collet | 446d442 | 2016-06-21 14:14:02 +0200 | [diff] [blame] | 392 | { size_t remainingToFlush = (size_t)(-1); |
Yann Collet | 2565971 | 2016-06-21 12:17:26 +0200 | [diff] [blame] | 393 | while (remainingToFlush) { |
| 394 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 395 | size_t dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 446d442 | 2016-06-21 14:14:02 +0200 | [diff] [blame] | 396 | U32 const enoughDstSize = dstBuffSize >= remainingToFlush; |
Yann Collet | 2565971 | 2016-06-21 12:17:26 +0200 | [diff] [blame] | 397 | remainingToFlush = ZBUFF_compressEnd(zc, cBuffer+cSize, &dstBuffSize); |
| 398 | CHECK (ZBUFF_isError(remainingToFlush), "flush error : %s", ZBUFF_getErrorName(remainingToFlush)); |
Yann Collet | 60ba31c | 2016-07-28 19:55:09 +0200 | [diff] [blame] | 399 | CHECK (enoughDstSize && remainingToFlush, "ZBUFF_compressEnd() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush); |
Yann Collet | 2565971 | 2016-06-21 12:17:26 +0200 | [diff] [blame] | 400 | cSize += dstBuffSize; |
Yann Collet | f15c1cb | 2016-06-21 13:11:48 +0200 | [diff] [blame] | 401 | } } |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 402 | crcOrig = XXH64_digest(&xxhState); |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 403 | |
| 404 | /* multi - fragments decompression test */ |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 405 | ZBUFF_decompressInitDictionary(zd, dict, dictSize); |
Yann Collet | 60ba31c | 2016-07-28 19:55:09 +0200 | [diff] [blame] | 406 | errorCode = 1; |
| 407 | for (totalCSize = 0, totalGenSize = 0 ; errorCode ; ) { |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 408 | size_t readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 409 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 410 | size_t dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
Yann Collet | 60ba31c | 2016-07-28 19:55:09 +0200 | [diff] [blame] | 411 | errorCode = ZBUFF_decompressContinue(zd, dstBuffer+totalGenSize, &dstBuffSize, cBuffer+totalCSize, &readCSrcSize); |
| 412 | CHECK (ZBUFF_isError(errorCode), "decompression error : %s", ZBUFF_getErrorName(errorCode)); |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 413 | totalGenSize += dstBuffSize; |
| 414 | totalCSize += readCSrcSize; |
Yann Collet | 5f2ec63 | 2015-11-26 10:32:17 +0100 | [diff] [blame] | 415 | } |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 416 | CHECK (errorCode != 0, "frame not fully decoded"); |
Yann Collet | 5f2ec63 | 2015-11-26 10:32:17 +0100 | [diff] [blame] | 417 | CHECK (totalGenSize != totalTestSize, "decompressed data : wrong size") |
| 418 | CHECK (totalCSize != cSize, "compressed data should be fully read") |
Yann Collet | efb1830 | 2016-04-01 18:54:13 +0200 | [diff] [blame] | 419 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 420 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 421 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); } |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 422 | |
Yann Collet | efb1830 | 2016-04-01 18:54:13 +0200 | [diff] [blame] | 423 | /*===== noisy/erroneous src decompression test =====*/ |
| 424 | |
Yann Collet | 26415d3 | 2015-11-26 12:43:28 +0100 | [diff] [blame] | 425 | /* add some noise */ |
Yann Collet | efb1830 | 2016-04-01 18:54:13 +0200 | [diff] [blame] | 426 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 427 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 428 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 429 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
Yann Collet | efb1830 | 2016-04-01 18:54:13 +0200 | [diff] [blame] | 430 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 431 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 432 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 433 | } } |
Yann Collet | 26415d3 | 2015-11-26 12:43:28 +0100 | [diff] [blame] | 434 | |
| 435 | /* try decompression on noisy data */ |
| 436 | ZBUFF_decompressInit(zd); |
| 437 | totalCSize = 0; |
| 438 | totalGenSize = 0; |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 439 | while ( (totalCSize < cSize) && (totalGenSize < dstBufferSize) ) { |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 440 | size_t readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 441 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 442 | size_t dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
Yann Collet | 3846108 | 2016-04-04 02:47:20 +0200 | [diff] [blame] | 443 | size_t const decompressError = ZBUFF_decompressContinue(zd, dstBuffer+totalGenSize, &dstBuffSize, cBuffer+totalCSize, &readCSrcSize); |
| 444 | if (ZBUFF_isError(decompressError)) break; /* error correctly detected */ |
| 445 | totalGenSize += dstBuffSize; |
| 446 | totalCSize += readCSrcSize; |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 447 | } } |
Yann Collet | 7447ee9 | 2015-11-26 10:52:30 +0100 | [diff] [blame] | 448 | DISPLAY("\r%u fuzzer tests completed \n", testNb); |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 449 | |
| 450 | _cleanup: |
| 451 | ZBUFF_freeCCtx(zc); |
| 452 | ZBUFF_freeDCtx(zd); |
| 453 | free(cNoiseBuffer[0]); |
| 454 | free(cNoiseBuffer[1]); |
| 455 | free(cNoiseBuffer[2]); |
| 456 | free(cNoiseBuffer[3]); |
| 457 | free(cNoiseBuffer[4]); |
| 458 | free(copyBuffer); |
| 459 | free(cBuffer); |
| 460 | free(dstBuffer); |
| 461 | return result; |
| 462 | |
| 463 | _output_error: |
| 464 | result = 1; |
| 465 | goto _cleanup; |
| 466 | } |
| 467 | |
| 468 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 469 | /*-******************************************************* |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 470 | * Command line |
| 471 | *********************************************************/ |
Yann Collet | fd265ca | 2016-04-04 02:48:55 +0200 | [diff] [blame] | 472 | int FUZ_usage(const char* programName) |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 473 | { |
| 474 | DISPLAY( "Usage :\n"); |
| 475 | DISPLAY( " %s [args]\n", programName); |
| 476 | DISPLAY( "\n"); |
| 477 | DISPLAY( "Arguments :\n"); |
| 478 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 479 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 480 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
| 481 | DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); |
| 482 | DISPLAY( " -v : verbose\n"); |
| 483 | DISPLAY( " -p : pause at the end\n"); |
| 484 | DISPLAY( " -h : display help and exit\n"); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | |
Yann Collet | fd265ca | 2016-04-04 02:48:55 +0200 | [diff] [blame] | 489 | int main(int argc, const char** argv) |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 490 | { |
| 491 | U32 seed=0; |
| 492 | int seedset=0; |
| 493 | int argNb; |
| 494 | int nbTests = nbTestsDefault; |
| 495 | int testNb = 0; |
| 496 | int proba = FUZ_COMPRESSIBILITY_DEFAULT; |
| 497 | int result=0; |
| 498 | U32 mainPause = 0; |
Yann Collet | fd265ca | 2016-04-04 02:48:55 +0200 | [diff] [blame] | 499 | const char* programName = argv[0]; |
inikep | 7cab86f | 2016-06-02 18:24:07 +0200 | [diff] [blame] | 500 | ZSTD_customMem customMem = { ZBUFF_allocFunction, ZBUFF_freeFunction, NULL }; |
inikep | 36fac00 | 2016-06-03 13:23:04 +0200 | [diff] [blame] | 501 | ZSTD_customMem customNULL = { NULL, NULL, NULL }; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 502 | |
| 503 | /* Check command line */ |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 504 | for(argNb=1; argNb<argc; argNb++) { |
Yann Collet | fd265ca | 2016-04-04 02:48:55 +0200 | [diff] [blame] | 505 | const char* argument = argv[argNb]; |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 506 | if(!argument) continue; /* Protection if argument empty */ |
| 507 | |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 508 | /* Parsing commands. Aggregated commands are allowed */ |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 509 | if (argument[0]=='-') { |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 510 | argument++; |
| 511 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 512 | while (*argument!=0) { |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 513 | switch(*argument) |
| 514 | { |
| 515 | case 'h': |
| 516 | return FUZ_usage(programName); |
| 517 | case 'v': |
| 518 | argument++; |
| 519 | g_displayLevel=4; |
| 520 | break; |
| 521 | case 'q': |
| 522 | argument++; |
| 523 | g_displayLevel--; |
| 524 | break; |
| 525 | case 'p': /* pause at the end */ |
| 526 | argument++; |
| 527 | mainPause = 1; |
| 528 | break; |
| 529 | |
| 530 | case 'i': |
| 531 | argument++; |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 532 | nbTests=0; g_clockTime=0; |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 533 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 534 | nbTests *= 10; |
| 535 | nbTests += *argument - '0'; |
| 536 | argument++; |
| 537 | } |
| 538 | break; |
| 539 | |
Yann Collet | 7447ee9 | 2015-11-26 10:52:30 +0100 | [diff] [blame] | 540 | case 'T': |
| 541 | argument++; |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 542 | nbTests=0; g_clockTime=0; |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 543 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 544 | g_clockTime *= 10; |
| 545 | g_clockTime += *argument - '0'; |
Yann Collet | 7447ee9 | 2015-11-26 10:52:30 +0100 | [diff] [blame] | 546 | argument++; |
| 547 | } |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 548 | if (*argument=='m') g_clockTime *=60, argument++; |
Yann Collet | 7447ee9 | 2015-11-26 10:52:30 +0100 | [diff] [blame] | 549 | if (*argument=='n') argument++; |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 550 | g_clockTime *= CLOCKS_PER_SEC; |
Yann Collet | 7447ee9 | 2015-11-26 10:52:30 +0100 | [diff] [blame] | 551 | break; |
| 552 | |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 553 | case 's': |
| 554 | argument++; |
| 555 | seed=0; |
| 556 | seedset=1; |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 557 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 558 | seed *= 10; |
| 559 | seed += *argument - '0'; |
| 560 | argument++; |
| 561 | } |
| 562 | break; |
| 563 | |
| 564 | case 't': |
| 565 | argument++; |
| 566 | testNb=0; |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 567 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 568 | testNb *= 10; |
| 569 | testNb += *argument - '0'; |
| 570 | argument++; |
| 571 | } |
| 572 | break; |
| 573 | |
| 574 | case 'P': /* compressibility % */ |
| 575 | argument++; |
| 576 | proba=0; |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 577 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 578 | proba *= 10; |
| 579 | proba += *argument - '0'; |
| 580 | argument++; |
| 581 | } |
| 582 | if (proba<0) proba=0; |
| 583 | if (proba>100) proba=100; |
| 584 | break; |
| 585 | |
| 586 | default: |
| 587 | return FUZ_usage(programName); |
| 588 | } |
Yann Collet | efb1830 | 2016-04-01 18:54:13 +0200 | [diff] [blame] | 589 | } } } /* for(argNb=1; argNb<argc; argNb++) */ |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 590 | |
| 591 | /* Get Seed */ |
Yann Collet | 45f84ab | 2016-05-20 12:34:40 +0200 | [diff] [blame] | 592 | DISPLAY("Starting zstd_buffered tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING); |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 593 | |
Yann Collet | 5153a08 | 2016-09-01 18:11:12 -0700 | [diff] [blame] | 594 | if (!seedset) { |
| 595 | time_t const t = time(NULL); |
| 596 | U32 const h = XXH32(&t, sizeof(t), 1); |
| 597 | seed = h % 10000; |
| 598 | } |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 599 | DISPLAY("Seed = %u\n", seed); |
| 600 | if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba); |
| 601 | |
| 602 | if (nbTests<=0) nbTests=1; |
| 603 | |
inikep | a1653fb | 2016-05-24 15:35:48 +0200 | [diff] [blame] | 604 | if (testNb==0) { |
inikep | 36fac00 | 2016-06-03 13:23:04 +0200 | [diff] [blame] | 605 | result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */ |
Yann Collet | 202844e | 2016-06-01 00:44:36 +0200 | [diff] [blame] | 606 | if (!result) { |
| 607 | DISPLAYLEVEL(4, "Unit tests using customMem :\n") |
inikep | a1653fb | 2016-05-24 15:35:48 +0200 | [diff] [blame] | 608 | result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */ |
Yann Collet | 202844e | 2016-06-01 00:44:36 +0200 | [diff] [blame] | 609 | } } |
inikep | a1653fb | 2016-05-24 15:35:48 +0200 | [diff] [blame] | 610 | |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 611 | if (!result) |
| 612 | result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100); |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 613 | |
| 614 | if (mainPause) { |
Yann Collet | 91a9796 | 2015-11-26 09:59:49 +0100 | [diff] [blame] | 615 | int unused; |
| 616 | DISPLAY("Press Enter \n"); |
| 617 | unused = getchar(); |
| 618 | (void)unused; |
| 619 | } |
| 620 | return result; |
| 621 | } |