Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | frameTest - test tool for lz4frame |
| 3 | Copyright (C) Yann Collet 2014 |
| 4 | GPL v2 License |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License along |
| 17 | with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | |
| 20 | You can contact the author at : |
| 21 | - LZ4 source repository : http://code.google.com/p/lz4/ |
| 22 | - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c |
| 23 | */ |
| 24 | |
| 25 | /************************************** |
| 26 | Compiler specific |
| 27 | **************************************/ |
| 28 | #define _CRT_SECURE_NO_WARNINGS // fgets |
| 29 | #ifdef _MSC_VER /* Visual Studio */ |
| 30 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 31 | # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ |
| 32 | #endif |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 33 | #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) |
| 34 | #ifdef __GNUC__ |
| 35 | # pragma GCC diagnostic ignored "-Wmissing-braces" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ |
| 36 | # pragma GCC diagnostic ignored "-Wmissing-field-initializers" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ |
| 37 | #endif |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 38 | |
| 39 | |
| 40 | /************************************** |
Yann Collet | 96e328c | 2014-12-13 15:05:46 +0100 | [diff] [blame] | 41 | Includes |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 42 | **************************************/ |
Yann Collet | 96e328c | 2014-12-13 15:05:46 +0100 | [diff] [blame] | 43 | #include <stdlib.h> // free |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 44 | #include <stdio.h> // fgets, sscanf |
| 45 | #include <sys/timeb.h> // timeb |
| 46 | #include <string.h> // strcmp |
Yann Collet | 96e328c | 2014-12-13 15:05:46 +0100 | [diff] [blame] | 47 | #include "lz4frame_static.h" |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 48 | #include "xxhash.h" // XXH64 |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 49 | |
| 50 | |
| 51 | /************************************** |
| 52 | Basic Types |
| 53 | **************************************/ |
| 54 | #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ |
| 55 | # include <stdint.h> |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 56 | typedef uint8_t BYTE; |
| 57 | typedef uint16_t U16; |
| 58 | typedef uint32_t U32; |
| 59 | typedef int32_t S32; |
| 60 | typedef uint64_t U64; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 61 | #else |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 62 | typedef unsigned char BYTE; |
| 63 | typedef unsigned short U16; |
| 64 | typedef unsigned int U32; |
| 65 | typedef signed int S32; |
| 66 | typedef unsigned long long U64; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 67 | #endif |
| 68 | |
| 69 | |
| 70 | /************************************** |
| 71 | Constants |
| 72 | **************************************/ |
| 73 | #ifndef LZ4_VERSION |
| 74 | # define LZ4_VERSION "" |
| 75 | #endif |
| 76 | |
Yann Collet | fc8e3f5 | 2014-09-01 22:44:02 +0100 | [diff] [blame] | 77 | #define KB *(1U<<10) |
| 78 | #define MB *(1U<<20) |
| 79 | #define GB *(1U<<30) |
| 80 | |
Yann Collet | f1b00b1 | 2014-10-22 00:36:32 +0100 | [diff] [blame] | 81 | static const U32 nbTestsDefault = 256 KB; |
Yann Collet | fc8e3f5 | 2014-09-01 22:44:02 +0100 | [diff] [blame] | 82 | #define COMPRESSIBLE_NOISE_LENGTH (2 MB) |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 83 | #define FUZ_COMPRESSIBILITY_DEFAULT 50 |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 84 | static const U32 prime1 = 2654435761U; |
| 85 | static const U32 prime2 = 2246822519U; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 86 | |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 87 | |
| 88 | |
| 89 | /************************************** |
| 90 | Macros |
| 91 | **************************************/ |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 92 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 93 | #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
Yann Collet | da1b0c1 | 2014-09-06 13:06:28 +0100 | [diff] [blame] | 94 | #define DISPLAYUPDATE(l, ...) if (displayLevel>=l) { \ |
| 95 | if ((FUZ_GetMilliSpan(g_time) > refreshRate) || (displayLevel>=4)) \ |
| 96 | { g_time = FUZ_GetMilliStart(); DISPLAY(__VA_ARGS__); \ |
| 97 | if (displayLevel>=4) fflush(stdout); } } |
| 98 | static const U32 refreshRate = 150; |
| 99 | static U32 g_time = 0; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 100 | |
| 101 | |
| 102 | /***************************************** |
| 103 | Local Parameters |
| 104 | *****************************************/ |
Yann Collet | da1b0c1 | 2014-09-06 13:06:28 +0100 | [diff] [blame] | 105 | static U32 no_prompt = 0; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 106 | static char* programName; |
Yann Collet | da1b0c1 | 2014-09-06 13:06:28 +0100 | [diff] [blame] | 107 | static U32 displayLevel = 2; |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 108 | static U32 pause = 0; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 109 | |
| 110 | |
| 111 | /********************************************************* |
| 112 | Fuzzer functions |
| 113 | *********************************************************/ |
Yann Collet | da1b0c1 | 2014-09-06 13:06:28 +0100 | [diff] [blame] | 114 | static U32 FUZ_GetMilliStart(void) |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 115 | { |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 116 | struct timeb tb; |
| 117 | U32 nCount; |
| 118 | ftime( &tb ); |
| 119 | nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm); |
| 120 | return nCount; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Yann Collet | da1b0c1 | 2014-09-06 13:06:28 +0100 | [diff] [blame] | 123 | |
| 124 | static U32 FUZ_GetMilliSpan(U32 nTimeStart) |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 125 | { |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 126 | U32 nCurrent = FUZ_GetMilliStart(); |
| 127 | U32 nSpan = nCurrent - nTimeStart; |
| 128 | if (nTimeStart > nCurrent) |
| 129 | nSpan += 0x100000 * 1000; |
| 130 | return nSpan; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 131 | } |
Yann Collet | da1b0c1 | 2014-09-06 13:06:28 +0100 | [diff] [blame] | 132 | |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 133 | |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 134 | # define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 135 | unsigned int FUZ_rand(unsigned int* src) |
| 136 | { |
| 137 | U32 rand32 = *src; |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 138 | rand32 *= prime1; |
| 139 | rand32 += prime2; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 140 | rand32 = FUZ_rotl32(rand32, 13); |
| 141 | *src = rand32; |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 142 | return rand32 >> 5; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 146 | #define FUZ_RAND15BITS (FUZ_rand(seed) & 0x7FFF) |
| 147 | #define FUZ_RANDLENGTH ( (FUZ_rand(seed) & 3) ? (FUZ_rand(seed) % 15) : (FUZ_rand(seed) % 510) + 15) |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 148 | static void FUZ_fillCompressibleNoiseBuffer(void* buffer, unsigned bufferSize, double proba, U32* seed) |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 149 | { |
| 150 | BYTE* BBuffer = (BYTE*)buffer; |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 151 | unsigned pos = 0; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 152 | U32 P32 = (U32)(32768 * proba); |
| 153 | |
| 154 | // First Byte |
| 155 | BBuffer[pos++] = (BYTE)(FUZ_rand(seed)); |
| 156 | |
| 157 | while (pos < bufferSize) |
| 158 | { |
| 159 | // Select : Literal (noise) or copy (within 64K) |
| 160 | if (FUZ_RAND15BITS < P32) |
| 161 | { |
| 162 | // Copy (within 64K) |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 163 | unsigned match, end; |
| 164 | unsigned length = FUZ_RANDLENGTH + 4; |
| 165 | unsigned offset = FUZ_RAND15BITS + 1; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 166 | if (offset > pos) offset = pos; |
| 167 | if (pos + length > bufferSize) length = bufferSize - pos; |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 168 | match = pos - offset; |
| 169 | end = pos + length; |
| 170 | while (pos < end) BBuffer[pos++] = BBuffer[match++]; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 171 | } |
| 172 | else |
| 173 | { |
| 174 | // Literal (noise) |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 175 | unsigned end; |
| 176 | unsigned length = FUZ_RANDLENGTH; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 177 | if (pos + length > bufferSize) length = bufferSize - pos; |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 178 | end = pos + length; |
| 179 | while (pos < end) BBuffer[pos++] = (BYTE)(FUZ_rand(seed) >> 5); |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 185 | static unsigned FUZ_highbit(U32 v32) |
| 186 | { |
| 187 | unsigned nbBits = 0; |
| 188 | if (v32==0) return 0; |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 189 | while (v32) |
| 190 | { |
| 191 | v32 >>= 1; |
| 192 | nbBits ++; |
| 193 | } |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 194 | return nbBits; |
| 195 | } |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 196 | |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 197 | |
Yann Collet | f881ccb | 2014-09-11 22:27:14 +0100 | [diff] [blame] | 198 | int basicTests(U32 seed, double compressibility) |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 199 | { |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 200 | int testResult = 0; |
| 201 | void* CNBuffer; |
| 202 | void* compressedBuffer; |
| 203 | void* decodedBuffer; |
| 204 | U32 randState = seed; |
| 205 | size_t cSize, testSize; |
| 206 | LZ4F_preferences_t prefs = { 0 }; |
| 207 | LZ4F_decompressionContext_t dCtx; |
| 208 | U64 crcOrig; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 209 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 210 | // Create compressible test buffer |
| 211 | CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); |
| 212 | compressedBuffer = malloc(LZ4F_compressFrameBound(COMPRESSIBLE_NOISE_LENGTH, NULL)); |
| 213 | decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); |
| 214 | FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); |
| 215 | crcOrig = XXH64(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 216 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 217 | // Trivial tests : one-step frame |
| 218 | testSize = COMPRESSIBLE_NOISE_LENGTH; |
| 219 | DISPLAYLEVEL(3, "Using NULL preferences : \n"); |
| 220 | cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, NULL), CNBuffer, testSize, NULL); |
| 221 | if (LZ4F_isError(cSize)) goto _output_error; |
| 222 | DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 223 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 224 | DISPLAYLEVEL(3, "Decompression test : \n"); |
| 225 | { |
Yann Collet | 0ac00ee | 2014-09-03 19:49:59 +0100 | [diff] [blame] | 226 | size_t decodedBufferSize = COMPRESSIBLE_NOISE_LENGTH; |
| 227 | size_t compressedBufferSize = cSize; |
Yann Collet | 8fbacf4 | 2014-09-04 22:56:51 +0100 | [diff] [blame] | 228 | BYTE* op = (BYTE*)decodedBuffer; |
| 229 | BYTE* const oend = (BYTE*)decodedBuffer + COMPRESSIBLE_NOISE_LENGTH; |
| 230 | BYTE* ip = (BYTE*)compressedBuffer; |
| 231 | BYTE* const iend = (BYTE*)compressedBuffer + cSize; |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 232 | U64 crcDest; |
| 233 | |
| 234 | LZ4F_errorCode_t errorCode = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); |
Yann Collet | 0ac00ee | 2014-09-03 19:49:59 +0100 | [diff] [blame] | 235 | if (LZ4F_isError(errorCode)) goto _output_error; |
Yann Collet | 8fbacf4 | 2014-09-04 22:56:51 +0100 | [diff] [blame] | 236 | |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 237 | DISPLAYLEVEL(3, "Single Block : \n"); |
Yann Collet | 0ac00ee | 2014-09-03 19:49:59 +0100 | [diff] [blame] | 238 | errorCode = LZ4F_decompress(dCtx, decodedBuffer, &decodedBufferSize, compressedBuffer, &compressedBufferSize, NULL); |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 239 | crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); |
| 240 | if (crcDest != crcOrig) goto _output_error; |
| 241 | DISPLAYLEVEL(3, "Regenerated %i bytes \n", (int)decodedBufferSize); |
Yann Collet | 8fbacf4 | 2014-09-04 22:56:51 +0100 | [diff] [blame] | 242 | |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 243 | DISPLAYLEVEL(3, "Byte after byte : \n"); |
Yann Collet | 8fbacf4 | 2014-09-04 22:56:51 +0100 | [diff] [blame] | 244 | while (ip < iend) |
| 245 | { |
| 246 | size_t oSize = oend-op; |
| 247 | size_t iSize = 1; |
| 248 | //DISPLAY("%7i \n", (int)(ip-(BYTE*)compressedBuffer)); |
| 249 | errorCode = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); |
| 250 | if (LZ4F_isError(errorCode)) goto _output_error; |
| 251 | op += oSize; |
| 252 | ip += iSize; |
| 253 | } |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 254 | crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); |
| 255 | if (crcDest != crcOrig) goto _output_error; |
| 256 | DISPLAYLEVEL(3, "Regenerated %i bytes \n", (int)decodedBufferSize); |
Yann Collet | 8fbacf4 | 2014-09-04 22:56:51 +0100 | [diff] [blame] | 257 | |
Yann Collet | 0ac00ee | 2014-09-03 19:49:59 +0100 | [diff] [blame] | 258 | errorCode = LZ4F_freeDecompressionContext(dCtx); |
| 259 | if (LZ4F_isError(errorCode)) goto _output_error; |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 260 | } |
Yann Collet | 0ac00ee | 2014-09-03 19:49:59 +0100 | [diff] [blame] | 261 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 262 | DISPLAYLEVEL(3, "Using 64 KB block : \n"); |
| 263 | prefs.frameInfo.blockSizeID = max64KB; |
| 264 | prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; |
| 265 | cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); |
| 266 | if (LZ4F_isError(cSize)) goto _output_error; |
| 267 | DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 268 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 269 | DISPLAYLEVEL(3, "without checksum : \n"); |
| 270 | prefs.frameInfo.contentChecksumFlag = noContentChecksum; |
| 271 | cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); |
| 272 | if (LZ4F_isError(cSize)) goto _output_error; |
| 273 | DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 274 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 275 | DISPLAYLEVEL(3, "Using 256 KB block : \n"); |
| 276 | prefs.frameInfo.blockSizeID = max256KB; |
| 277 | prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; |
| 278 | cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); |
| 279 | if (LZ4F_isError(cSize)) goto _output_error; |
| 280 | DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 281 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 282 | DISPLAYLEVEL(3, "Decompression test : \n"); |
| 283 | { |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 284 | size_t decodedBufferSize = COMPRESSIBLE_NOISE_LENGTH; |
Yann Collet | e39b22c | 2014-09-22 17:38:17 +0100 | [diff] [blame] | 285 | unsigned maxBits = FUZ_highbit((U32)decodedBufferSize); |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 286 | BYTE* op = (BYTE*)decodedBuffer; |
| 287 | BYTE* const oend = (BYTE*)decodedBuffer + COMPRESSIBLE_NOISE_LENGTH; |
| 288 | BYTE* ip = (BYTE*)compressedBuffer; |
| 289 | BYTE* const iend = (BYTE*)compressedBuffer + cSize; |
| 290 | U64 crcDest; |
| 291 | |
| 292 | LZ4F_errorCode_t errorCode = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); |
| 293 | if (LZ4F_isError(errorCode)) goto _output_error; |
| 294 | |
| 295 | DISPLAYLEVEL(3, "random segment sizes : \n"); |
| 296 | while (ip < iend) |
| 297 | { |
| 298 | unsigned nbBits = FUZ_rand(&randState) % maxBits; |
| 299 | size_t iSize = (FUZ_rand(&randState) & ((1<<nbBits)-1)) + 1; |
| 300 | size_t oSize = oend-op; |
| 301 | if (iSize > (size_t)(iend-ip)) iSize = iend-ip; |
| 302 | //DISPLAY("%7i : + %6i\n", (int)(ip-(BYTE*)compressedBuffer), (int)iSize); |
| 303 | errorCode = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); |
| 304 | if (LZ4F_isError(errorCode)) goto _output_error; |
| 305 | op += oSize; |
| 306 | ip += iSize; |
| 307 | } |
| 308 | crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); |
| 309 | if (crcDest != crcOrig) goto _output_error; |
| 310 | DISPLAYLEVEL(3, "Regenerated %i bytes \n", (int)decodedBufferSize); |
| 311 | |
| 312 | errorCode = LZ4F_freeDecompressionContext(dCtx); |
| 313 | if (LZ4F_isError(errorCode)) goto _output_error; |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 314 | } |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 315 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 316 | DISPLAYLEVEL(3, "without checksum : \n"); |
| 317 | prefs.frameInfo.contentChecksumFlag = noContentChecksum; |
| 318 | cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); |
| 319 | if (LZ4F_isError(cSize)) goto _output_error; |
| 320 | DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 321 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 322 | DISPLAYLEVEL(3, "Using 1 MB block : \n"); |
| 323 | prefs.frameInfo.blockSizeID = max1MB; |
| 324 | prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; |
| 325 | cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); |
| 326 | if (LZ4F_isError(cSize)) goto _output_error; |
| 327 | DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 328 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 329 | DISPLAYLEVEL(3, "without checksum : \n"); |
| 330 | prefs.frameInfo.contentChecksumFlag = noContentChecksum; |
| 331 | cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); |
| 332 | if (LZ4F_isError(cSize)) goto _output_error; |
| 333 | DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 334 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 335 | DISPLAYLEVEL(3, "Using 4 MB block : \n"); |
| 336 | prefs.frameInfo.blockSizeID = max4MB; |
| 337 | prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; |
| 338 | cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); |
| 339 | if (LZ4F_isError(cSize)) goto _output_error; |
| 340 | DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); |
Yann Collet | 23effdb | 2014-08-31 23:14:20 +0100 | [diff] [blame] | 341 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 342 | DISPLAYLEVEL(3, "without checksum : \n"); |
| 343 | prefs.frameInfo.contentChecksumFlag = noContentChecksum; |
| 344 | cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); |
| 345 | if (LZ4F_isError(cSize)) goto _output_error; |
| 346 | DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 347 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 348 | DISPLAY("Basic tests completed \n"); |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 349 | _end: |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 350 | free(CNBuffer); |
| 351 | free(compressedBuffer); |
| 352 | free(decodedBuffer); |
| 353 | return testResult; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 354 | |
| 355 | _output_error: |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 356 | testResult = 1; |
| 357 | DISPLAY("Error detected ! \n"); |
| 358 | goto _end; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 362 | static void locateBuffDiff(const void* buff1, const void* buff2, size_t size, unsigned nonContiguous) |
Yann Collet | f881ccb | 2014-09-11 22:27:14 +0100 | [diff] [blame] | 363 | { |
| 364 | int p=0; |
| 365 | BYTE* b1=(BYTE*)buff1; |
| 366 | BYTE* b2=(BYTE*)buff2; |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 367 | if (nonContiguous) |
| 368 | { |
| 369 | DISPLAY("Non-contiguous output test (%i bytes)\n", (int)size); |
| 370 | return; |
| 371 | } |
Yann Collet | f881ccb | 2014-09-11 22:27:14 +0100 | [diff] [blame] | 372 | while (b1[p]==b2[p]) p++; |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 373 | DISPLAY("Error at pos %i/%i : %02X != %02X \n", p, (int)size, b1[p], b2[p]); |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 374 | } |
Yann Collet | f881ccb | 2014-09-11 22:27:14 +0100 | [diff] [blame] | 375 | |
| 376 | |
Yann Collet | b96f8c1 | 2014-09-10 13:53:42 +0100 | [diff] [blame] | 377 | static const U32 srcDataLength = 9 MB; /* needs to be > 2x4MB to test large blocks */ |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 378 | |
Yann Collet | 3d9d178 | 2014-09-07 12:12:46 +0100 | [diff] [blame] | 379 | int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressibility) |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 380 | { |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 381 | unsigned testResult = 0; |
| 382 | unsigned testNb = 0; |
| 383 | void* srcBuffer = NULL; |
| 384 | void* compressedBuffer = NULL; |
| 385 | void* decodedBuffer = NULL; |
| 386 | U32 coreRand = seed; |
| 387 | LZ4F_decompressionContext_t dCtx = NULL; |
| 388 | LZ4F_compressionContext_t cCtx = NULL; |
Yann Collet | 5929de4 | 2014-09-07 12:57:09 +0100 | [diff] [blame] | 389 | size_t result; |
Yann Collet | efd9360 | 2014-10-06 00:48:45 +0100 | [diff] [blame] | 390 | XXH64_state_t xxh64; |
Yann Collet | da1b0c1 | 2014-09-06 13:06:28 +0100 | [diff] [blame] | 391 | # define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 392 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 393 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 394 | // Create buffers |
| 395 | result = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); |
Yann Collet | 5929de4 | 2014-09-07 12:57:09 +0100 | [diff] [blame] | 396 | CHECK(LZ4F_isError(result), "Allocation failed (error %i)", (int)result); |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 397 | result = LZ4F_createCompressionContext(&cCtx, LZ4F_VERSION); |
Yann Collet | 5929de4 | 2014-09-07 12:57:09 +0100 | [diff] [blame] | 398 | CHECK(LZ4F_isError(result), "Allocation failed (error %i)", (int)result); |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 399 | srcBuffer = malloc(srcDataLength); |
Yann Collet | 5929de4 | 2014-09-07 12:57:09 +0100 | [diff] [blame] | 400 | CHECK(srcBuffer==NULL, "srcBuffer Allocation failed"); |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 401 | compressedBuffer = malloc(LZ4F_compressFrameBound(srcDataLength, NULL)); |
Yann Collet | 5929de4 | 2014-09-07 12:57:09 +0100 | [diff] [blame] | 402 | CHECK(compressedBuffer==NULL, "compressedBuffer Allocation failed"); |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 403 | decodedBuffer = malloc(srcDataLength); |
Yann Collet | 5929de4 | 2014-09-07 12:57:09 +0100 | [diff] [blame] | 404 | CHECK(decodedBuffer==NULL, "decodedBuffer Allocation failed"); |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 405 | FUZ_fillCompressibleNoiseBuffer(srcBuffer, srcDataLength, compressibility, &coreRand); |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 406 | |
Yann Collet | 3d9d178 | 2014-09-07 12:12:46 +0100 | [diff] [blame] | 407 | // jump to requested testNb |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 408 | for (testNb =0; testNb < startTest; testNb++) (void)FUZ_rand(&coreRand); // sync randomizer |
Yann Collet | 3d9d178 | 2014-09-07 12:12:46 +0100 | [diff] [blame] | 409 | |
| 410 | // main fuzzer loop |
| 411 | for ( ; testNb < nbTests; testNb++) |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 412 | { |
Yann Collet | 3d9d178 | 2014-09-07 12:12:46 +0100 | [diff] [blame] | 413 | U32 randState = coreRand ^ prime1; |
Yann Collet | 20ae8b9 | 2014-09-06 09:47:28 +0100 | [diff] [blame] | 414 | unsigned BSId = 4 + (FUZ_rand(&randState) & 3); |
Yann Collet | 2b4cdd0 | 2014-09-09 23:54:22 +0100 | [diff] [blame] | 415 | unsigned BMId = FUZ_rand(&randState) & 1; |
Yann Collet | 414cfb9 | 2014-09-10 13:00:39 +0100 | [diff] [blame] | 416 | unsigned CCflag = FUZ_rand(&randState) & 1; |
Yann Collet | 9464bfd | 2014-09-13 23:44:07 +0100 | [diff] [blame] | 417 | unsigned autoflush = (FUZ_rand(&randState) & 7) == 2; |
Yann Collet | 18cdb73 | 2014-09-13 10:08:55 +0100 | [diff] [blame] | 418 | LZ4F_preferences_t prefs = { 0 }; |
Yann Collet | f587a86 | 2014-09-15 00:59:30 +0100 | [diff] [blame] | 419 | LZ4F_compressOptions_t cOptions = { 0 }; |
| 420 | LZ4F_decompressOptions_t dOptions = { 0 }; |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 421 | unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(srcDataLength-1) - 1)) + 1; |
| 422 | size_t srcSize = (FUZ_rand(&randState) & ((1<<nbBits)-1)) + 1; |
| 423 | size_t srcStart = FUZ_rand(&randState) % (srcDataLength - srcSize); |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 424 | size_t cSize; |
Yann Collet | b73b2af | 2014-09-06 12:19:26 +0100 | [diff] [blame] | 425 | U64 crcOrig, crcDecoded; |
Yann Collet | efd9360 | 2014-10-06 00:48:45 +0100 | [diff] [blame] | 426 | LZ4F_preferences_t* prefsPtr = &prefs; |
Yann Collet | b73b2af | 2014-09-06 12:19:26 +0100 | [diff] [blame] | 427 | |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 428 | (void)FUZ_rand(&coreRand); // update rand seed |
Yann Collet | 1499523 | 2014-10-28 00:39:06 +0100 | [diff] [blame] | 429 | prefs.frameInfo.blockMode = (blockMode_t)BMId; |
| 430 | prefs.frameInfo.blockSizeID = (blockSizeID_t)BSId; |
| 431 | prefs.frameInfo.contentChecksumFlag = (contentChecksum_t)CCflag; |
Yann Collet | 18cdb73 | 2014-09-13 10:08:55 +0100 | [diff] [blame] | 432 | prefs.autoFlush = autoflush; |
Yann Collet | 1499523 | 2014-10-28 00:39:06 +0100 | [diff] [blame] | 433 | prefs.compressionLevel = FUZ_rand(&randState) % 5; |
Yann Collet | efd9360 | 2014-10-06 00:48:45 +0100 | [diff] [blame] | 434 | if ((FUZ_rand(&randState)&0xF) == 1) prefsPtr = NULL; |
Yann Collet | 18cdb73 | 2014-09-13 10:08:55 +0100 | [diff] [blame] | 435 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 436 | DISPLAYUPDATE(2, "\r%5u ", testNb); |
Yann Collet | e39b22c | 2014-09-22 17:38:17 +0100 | [diff] [blame] | 437 | crcOrig = XXH64((BYTE*)srcBuffer+srcStart, (U32)srcSize, 1); |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 438 | |
Yann Collet | 73025f2 | 2014-09-22 02:59:42 +0100 | [diff] [blame] | 439 | if ((FUZ_rand(&randState)&0xF) == 2) |
| 440 | { |
Yann Collet | efd9360 | 2014-10-06 00:48:45 +0100 | [diff] [blame] | 441 | cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(srcSize, prefsPtr), (char*)srcBuffer + srcStart, srcSize, prefsPtr); |
Yann Collet | 73025f2 | 2014-09-22 02:59:42 +0100 | [diff] [blame] | 442 | CHECK(LZ4F_isError(cSize), "LZ4F_compressFrame failed : error %i (%s)", (int)cSize, LZ4F_getErrorName(cSize)); |
| 443 | } |
| 444 | else |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 445 | { |
Yann Collet | f881ccb | 2014-09-11 22:27:14 +0100 | [diff] [blame] | 446 | const BYTE* ip = (const BYTE*)srcBuffer + srcStart; |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 447 | const BYTE* const iend = ip + srcSize; |
Yann Collet | 1499523 | 2014-10-28 00:39:06 +0100 | [diff] [blame] | 448 | BYTE* op = (BYTE*)compressedBuffer; |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 449 | BYTE* const oend = op + LZ4F_compressFrameBound(srcDataLength, NULL); |
Yann Collet | e39b22c | 2014-09-22 17:38:17 +0100 | [diff] [blame] | 450 | unsigned maxBits = FUZ_highbit((U32)srcSize); |
Yann Collet | efd9360 | 2014-10-06 00:48:45 +0100 | [diff] [blame] | 451 | result = LZ4F_compressBegin(cCtx, op, oend-op, prefsPtr); |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 452 | CHECK(LZ4F_isError(result), "Compression header failed (error %i)", (int)result); |
| 453 | op += result; |
| 454 | while (ip < iend) |
| 455 | { |
Yann Collet | 3d9d178 | 2014-09-07 12:12:46 +0100 | [diff] [blame] | 456 | unsigned nbBitsSeg = FUZ_rand(&randState) % maxBits; |
| 457 | size_t iSize = (FUZ_rand(&randState) & ((1<<nbBitsSeg)-1)) + 1; |
Yann Collet | efd9360 | 2014-10-06 00:48:45 +0100 | [diff] [blame] | 458 | size_t oSize = LZ4F_compressBound(iSize, prefsPtr); |
Yann Collet | b96f8c1 | 2014-09-10 13:53:42 +0100 | [diff] [blame] | 459 | unsigned forceFlush = ((FUZ_rand(&randState) & 3) == 1); |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 460 | if (iSize > (size_t)(iend-ip)) iSize = iend-ip; |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 461 | cOptions.stableSrc = ((FUZ_rand(&randState) & 3) == 1); |
Yann Collet | 18cdb73 | 2014-09-13 10:08:55 +0100 | [diff] [blame] | 462 | |
Yann Collet | f587a86 | 2014-09-15 00:59:30 +0100 | [diff] [blame] | 463 | result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, &cOptions); |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 464 | CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); |
| 465 | op += result; |
| 466 | ip += iSize; |
Yann Collet | 18cdb73 | 2014-09-13 10:08:55 +0100 | [diff] [blame] | 467 | |
Yann Collet | b96f8c1 | 2014-09-10 13:53:42 +0100 | [diff] [blame] | 468 | if (forceFlush) |
| 469 | { |
Yann Collet | f587a86 | 2014-09-15 00:59:30 +0100 | [diff] [blame] | 470 | result = LZ4F_flush(cCtx, op, oend-op, &cOptions); |
Yann Collet | b96f8c1 | 2014-09-10 13:53:42 +0100 | [diff] [blame] | 471 | CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); |
| 472 | op += result; |
| 473 | } |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 474 | } |
Yann Collet | f587a86 | 2014-09-15 00:59:30 +0100 | [diff] [blame] | 475 | result = LZ4F_compressEnd(cCtx, op, oend-op, &cOptions); |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 476 | CHECK(LZ4F_isError(result), "Compression completion failed (error %i)", (int)result); |
| 477 | op += result; |
Yann Collet | a6af48e | 2014-09-07 11:04:29 +0100 | [diff] [blame] | 478 | cSize = op-(BYTE*)compressedBuffer; |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 479 | } |
Yann Collet | b18d409 | 2014-09-06 22:48:03 +0100 | [diff] [blame] | 480 | |
| 481 | { |
Yann Collet | 1499523 | 2014-10-28 00:39:06 +0100 | [diff] [blame] | 482 | const BYTE* ip = (const BYTE*)compressedBuffer; |
Yann Collet | b18d409 | 2014-09-06 22:48:03 +0100 | [diff] [blame] | 483 | const BYTE* const iend = ip + cSize; |
Yann Collet | 1499523 | 2014-10-28 00:39:06 +0100 | [diff] [blame] | 484 | BYTE* op = (BYTE*)decodedBuffer; |
Yann Collet | b18d409 | 2014-09-06 22:48:03 +0100 | [diff] [blame] | 485 | BYTE* const oend = op + srcDataLength; |
Yann Collet | e39b22c | 2014-09-22 17:38:17 +0100 | [diff] [blame] | 486 | unsigned maxBits = FUZ_highbit((U32)cSize); |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 487 | unsigned nonContiguousDst = (FUZ_rand(&randState) & 3) == 1; |
| 488 | nonContiguousDst += FUZ_rand(&randState) & nonContiguousDst; /* 0=>0; 1=>1,2 */ |
Yann Collet | efd9360 | 2014-10-06 00:48:45 +0100 | [diff] [blame] | 489 | XXH64_reset(&xxh64, 1); |
Yann Collet | b18d409 | 2014-09-06 22:48:03 +0100 | [diff] [blame] | 490 | while (ip < iend) |
| 491 | { |
Yann Collet | 3d9d178 | 2014-09-07 12:12:46 +0100 | [diff] [blame] | 492 | unsigned nbBitsI = (FUZ_rand(&randState) % (maxBits-1)) + 1; |
Yann Collet | b563857 | 2014-09-13 21:21:41 +0100 | [diff] [blame] | 493 | unsigned nbBitsO = (FUZ_rand(&randState) % (maxBits)) + 1; |
Yann Collet | 3d9d178 | 2014-09-07 12:12:46 +0100 | [diff] [blame] | 494 | size_t iSize = (FUZ_rand(&randState) & ((1<<nbBitsI)-1)) + 1; |
Yann Collet | b563857 | 2014-09-13 21:21:41 +0100 | [diff] [blame] | 495 | size_t oSize = (FUZ_rand(&randState) & ((1<<nbBitsO)-1)) + 2; |
Yann Collet | b18d409 | 2014-09-06 22:48:03 +0100 | [diff] [blame] | 496 | if (iSize > (size_t)(iend-ip)) iSize = iend-ip; |
Yann Collet | a6af48e | 2014-09-07 11:04:29 +0100 | [diff] [blame] | 497 | if (oSize > (size_t)(oend-op)) oSize = oend-op; |
Yann Collet | f587a86 | 2014-09-15 00:59:30 +0100 | [diff] [blame] | 498 | dOptions.stableDst = FUZ_rand(&randState) & 1; |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 499 | if (nonContiguousDst==2) dOptions.stableDst = 0; |
| 500 | //if (ip == compressedBuffer+62073) DISPLAY("oSize : %i : pos %i \n", (int)oSize, (int)(op-(BYTE*)decodedBuffer)); |
Yann Collet | f587a86 | 2014-09-15 00:59:30 +0100 | [diff] [blame] | 501 | result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, &dOptions); |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 502 | //if (op+oSize >= (BYTE*)decodedBuffer+94727) DISPLAY("iSize : %i : pos %i \n", (int)iSize, (int)(ip-(BYTE*)compressedBuffer)); |
| 503 | //if ((int)result<0) DISPLAY("iSize : %i : pos %i \n", (int)iSize, (int)(ip-(BYTE*)compressedBuffer)); |
| 504 | if (result == (size_t)-ERROR_checksum_invalid) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize, nonContiguousDst); |
| 505 | CHECK(LZ4F_isError(result), "Decompression failed (error %i:%s)", (int)result, LZ4F_getErrorName((LZ4F_errorCode_t)result)); |
| 506 | XXH64_update(&xxh64, op, (U32)oSize); |
Yann Collet | b18d409 | 2014-09-06 22:48:03 +0100 | [diff] [blame] | 507 | op += oSize; |
| 508 | ip += iSize; |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 509 | op += nonContiguousDst; |
| 510 | if (nonContiguousDst==2) op = decodedBuffer; // overwritten destination |
Yann Collet | b18d409 | 2014-09-06 22:48:03 +0100 | [diff] [blame] | 511 | } |
Yann Collet | 9258ee5 | 2014-09-13 12:15:54 +0100 | [diff] [blame] | 512 | CHECK(result != 0, "Frame decompression failed (error %i)", (int)result); |
Yann Collet | efd9360 | 2014-10-06 00:48:45 +0100 | [diff] [blame] | 513 | crcDecoded = XXH64_digest(&xxh64); |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 514 | if (crcDecoded != crcOrig) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize, nonContiguousDst); |
Yann Collet | b18d409 | 2014-09-06 22:48:03 +0100 | [diff] [blame] | 515 | CHECK(crcDecoded != crcOrig, "Decompression corruption"); |
| 516 | } |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 519 | DISPLAYLEVEL(2, "\rAll tests completed \n"); |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 520 | |
| 521 | _end: |
Yann Collet | da1b0c1 | 2014-09-06 13:06:28 +0100 | [diff] [blame] | 522 | LZ4F_freeDecompressionContext(dCtx); |
Yann Collet | 5929de4 | 2014-09-07 12:57:09 +0100 | [diff] [blame] | 523 | LZ4F_freeCompressionContext(cCtx); |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 524 | free(srcBuffer); |
| 525 | free(compressedBuffer); |
| 526 | free(decodedBuffer); |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 527 | |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 528 | if (pause) |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 529 | { |
| 530 | DISPLAY("press enter to finish \n"); |
| 531 | getchar(); |
| 532 | } |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 533 | return testResult; |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 534 | |
| 535 | _output_error: |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 536 | testResult = 1; |
| 537 | goto _end; |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 541 | int FUZ_usage(void) |
| 542 | { |
| 543 | DISPLAY( "Usage :\n"); |
| 544 | DISPLAY( " %s [args]\n", programName); |
| 545 | DISPLAY( "\n"); |
| 546 | DISPLAY( "Arguments :\n"); |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 547 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 548 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 549 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
| 550 | DISPLAY( " -p# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); |
| 551 | DISPLAY( " -v : verbose\n"); |
| 552 | DISPLAY( " -h : display help and exit\n"); |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | |
Yann Collet | fc8e3f5 | 2014-09-01 22:44:02 +0100 | [diff] [blame] | 557 | int main(int argc, char** argv) |
| 558 | { |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 559 | U32 seed=0; |
| 560 | int seedset=0; |
| 561 | int argNb; |
Yann Collet | 837551f | 2014-09-07 09:13:02 +0100 | [diff] [blame] | 562 | int nbTests = nbTestsDefault; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 563 | int testNb = 0; |
| 564 | int proba = FUZ_COMPRESSIBILITY_DEFAULT; |
Yann Collet | f881ccb | 2014-09-11 22:27:14 +0100 | [diff] [blame] | 565 | int result=0; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 566 | |
| 567 | // Check command line |
| 568 | programName = argv[0]; |
| 569 | for(argNb=1; argNb<argc; argNb++) |
| 570 | { |
| 571 | char* argument = argv[argNb]; |
| 572 | |
| 573 | if(!argument) continue; // Protection if argument empty |
| 574 | |
| 575 | // Decode command (note : aggregated commands are allowed) |
| 576 | if (argument[0]=='-') |
| 577 | { |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 578 | if (!strcmp(argument, "--no-prompt")) |
| 579 | { |
| 580 | no_prompt=1; |
| 581 | seedset=1; |
| 582 | displayLevel=1; |
| 583 | continue; |
| 584 | } |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 585 | argument++; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 586 | |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 587 | while (*argument!=0) |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 588 | { |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 589 | switch(*argument) |
| 590 | { |
| 591 | case 'h': |
| 592 | return FUZ_usage(); |
| 593 | case 'v': |
| 594 | argument++; |
| 595 | displayLevel=4; |
| 596 | break; |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 597 | case 'q': |
| 598 | argument++; |
| 599 | displayLevel--; |
| 600 | break; |
Yann Collet | f1b00b1 | 2014-10-22 00:36:32 +0100 | [diff] [blame] | 601 | case 'p': /* pause at the end */ |
| 602 | argument++; |
| 603 | pause = 1; |
| 604 | break; |
| 605 | |
Yann Collet | 1499523 | 2014-10-28 00:39:06 +0100 | [diff] [blame] | 606 | case 'i': |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 607 | argument++; |
| 608 | nbTests=0; |
| 609 | while ((*argument>='0') && (*argument<='9')) |
| 610 | { |
| 611 | nbTests *= 10; |
| 612 | nbTests += *argument - '0'; |
| 613 | argument++; |
| 614 | } |
| 615 | break; |
| 616 | case 's': |
| 617 | argument++; |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 618 | seed=0; |
| 619 | seedset=1; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 620 | while ((*argument>='0') && (*argument<='9')) |
| 621 | { |
| 622 | seed *= 10; |
| 623 | seed += *argument - '0'; |
| 624 | argument++; |
| 625 | } |
| 626 | break; |
| 627 | case 't': |
| 628 | argument++; |
| 629 | testNb=0; |
| 630 | while ((*argument>='0') && (*argument<='9')) |
| 631 | { |
| 632 | testNb *= 10; |
| 633 | testNb += *argument - '0'; |
| 634 | argument++; |
| 635 | } |
| 636 | break; |
Yann Collet | f1b00b1 | 2014-10-22 00:36:32 +0100 | [diff] [blame] | 637 | case 'P': /* compressibility % */ |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 638 | argument++; |
| 639 | proba=0; |
| 640 | while ((*argument>='0') && (*argument<='9')) |
| 641 | { |
| 642 | proba *= 10; |
| 643 | proba += *argument - '0'; |
| 644 | argument++; |
| 645 | } |
| 646 | if (proba<0) proba=0; |
| 647 | if (proba>100) proba=100; |
| 648 | break; |
Yann Collet | dc5fb73 | 2014-09-22 18:42:00 +0100 | [diff] [blame] | 649 | default: |
| 650 | ; |
Yann Collet | 6330f77 | 2014-09-21 09:56:21 +0100 | [diff] [blame] | 651 | return FUZ_usage(); |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | // Get Seed |
| 658 | printf("Starting lz4frame tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION); |
| 659 | |
Yann Collet | fc8e3f5 | 2014-09-01 22:44:02 +0100 | [diff] [blame] | 660 | if (!seedset) seed = FUZ_GetMilliStart() % 10000; |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 661 | printf("Seed = %u\n", seed); |
| 662 | if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) printf("Compressibility : %i%%\n", proba); |
| 663 | |
| 664 | if (nbTests<=0) nbTests=1; |
| 665 | |
Yann Collet | aa1246b | 2014-11-29 20:19:39 +0100 | [diff] [blame] | 666 | if (testNb==0) result = basicTests(seed, ((double)proba) / 100); |
Yann Collet | 2b4cdd0 | 2014-09-09 23:54:22 +0100 | [diff] [blame] | 667 | if (result) return 1; |
Yann Collet | 1ffc02e | 2014-09-05 16:32:04 +0100 | [diff] [blame] | 668 | return fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100); |
Yann Collet | db4c012 | 2014-08-30 18:14:44 +0100 | [diff] [blame] | 669 | } |