add a test to check long sequences (#631)

the test fails, as intended,
since #631 is not merged yet in this branch.
This commit is contained in:
Yann Collet 2019-01-09 12:09:52 -08:00
parent d6eac9c5cf
commit 7741c60f98

View File

@ -48,6 +48,7 @@
#include <string.h> /* strcmp */ #include <string.h> /* strcmp */
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */ #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
#include <assert.h> #include <assert.h>
#include <limits.h> /* INT_MAX */
#if defined(__unix__) && defined(_AIX) #if defined(__unix__) && defined(_AIX)
# include <sys/mman.h> /* mmap */ # include <sys/mman.h> /* mmap */
#endif #endif
@ -1064,45 +1065,79 @@ static void FUZ_unitTests(int compressionLevel)
} }
/* LZ4 HC streaming tests */ /* LZ4 HC streaming tests */
{ LZ4_streamHC_t* sp; { LZ4_streamHC_t sHC; /* statically allocated */
LZ4_streamHC_t sHC;
U64 crcOrig; U64 crcOrig;
int result; int result;
/* Allocation test */ /* Allocation test */
sp = LZ4_createStreamHC(); DISPLAYLEVEL(3, " Basic HC allocation : ");
FUZ_CHECKTEST(sp==NULL, "LZ4_createStreamHC() allocation failed"); { LZ4_streamHC_t* const sp = LZ4_createStreamHC();
LZ4_freeStreamHC(sp); FUZ_CHECKTEST(sp==NULL, "LZ4_createStreamHC() allocation failed");
LZ4_freeStreamHC(sp);
}
DISPLAYLEVEL(3, " OK \n");
/* simple HC compression test */ /* simple HC compression test */
crcOrig = XXH64(testInput, testCompressedSize, 0); DISPLAYLEVEL(3, " Simple HC round-trip : ");
LZ4_resetStreamHC(&sHC, compressionLevel); { U64 const crc64 = XXH64(testInput, testCompressedSize, 0);
result = LZ4_compress_HC_continue(&sHC, testInput, testCompressed, testCompressedSize, testCompressedSize-1); LZ4_resetStreamHC(&sHC, compressionLevel);
FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed"); result = LZ4_compress_HC_continue(&sHC, testInput, testCompressed, testCompressedSize, testCompressedSize-1);
FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed");
FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize); result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize);
FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed");
{ U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); } FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe() decompression corruption");
} }
DISPLAYLEVEL(3, " OK \n");
/* long sequence test */
DISPLAYLEVEL(3, " Long sequence HC test : ");
{ size_t const blockSize = 1 MB;
size_t const targetSize = 4116; /* size carefully selected to trigger an overflow */
void* const block = malloc(blockSize);
void* const dstBlock = malloc(targetSize+1);
BYTE const sentinel = 101;
int srcSize;
assert(block != NULL); assert(dstBlock != NULL);
memset(block, 0, blockSize);
((char*)dstBlock)[targetSize] = sentinel;
LZ4_resetStreamHC(&sHC, 3);
assert(blockSize < INT_MAX);
srcSize = (int)blockSize;
result = LZ4_compress_HC_destSize(&sHC, block, dstBlock, &srcSize, targetSize, 3);
FUZ_CHECKTEST(result!=0, "LZ4_compress_HC_destSize() : compression must fail !");
FUZ_CHECKTEST(((char*)dstBlock)[targetSize] != sentinel, "LZ4_compress_HC_destSize()")
LZ4_resetStreamHC(&sHC, 3); /* make sure the context is clean after the test */
free(block);
free(dstBlock);
}
DISPLAYLEVEL(3, " OK \n");
/* simple dictionary HC compression test */ /* simple dictionary HC compression test */
crcOrig = XXH64(testInput + 64 KB, testCompressedSize, 0); DISPLAYLEVEL(3, " HC dictionary compression test : ");
LZ4_resetStreamHC_fast(&sHC, compressionLevel); { U64 const crc64 = XXH64(testInput + 64 KB, testCompressedSize, 0);
LZ4_loadDictHC(&sHC, testInput, 64 KB); LZ4_resetStreamHC_fast(&sHC, compressionLevel);
result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1); LZ4_loadDictHC(&sHC, testInput, 64 KB);
FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result); result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1);
FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result);
FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 64 KB); result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 64 KB);
FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() simple dictionary decompression test failed"); FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() simple dictionary decompression test failed");
{ U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() simple dictionary decompression test : corruption"); } FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe() simple dictionary decompression test : corruption");
} }
DISPLAYLEVEL(3, " OK \n");
/* multiple HC compression test with dictionary */ /* multiple HC compression test with dictionary */
{ int result1, result2; { int result1, result2;
int segSize = testCompressedSize / 2; int segSize = testCompressedSize / 2;
crcOrig = XXH64(testInput + segSize, testCompressedSize, 0); U64 const crc64 = XXH64(testInput + segSize, testCompressedSize, 0);
LZ4_resetStreamHC_fast(&sHC, compressionLevel); LZ4_resetStreamHC_fast(&sHC, compressionLevel);
LZ4_loadDictHC(&sHC, testInput, segSize); LZ4_loadDictHC(&sHC, testInput, segSize);
result1 = LZ4_compress_HC_continue(&sHC, testInput + segSize, testCompressed, segSize, segSize -1); result1 = LZ4_compress_HC_continue(&sHC, testInput + segSize, testCompressed, segSize, segSize -1);
@ -1116,22 +1151,23 @@ static void FUZ_unitTests(int compressionLevel)
FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 1 failed"); FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 1 failed");
result = LZ4_decompress_safe_usingDict(testCompressed+result1, testVerify+segSize, result2, segSize, testInput, 2*segSize); result = LZ4_decompress_safe_usingDict(testCompressed+result1, testVerify+segSize, result2, segSize, testInput, 2*segSize);
FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 2 failed"); FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 2 failed");
{ U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() dictionary decompression corruption"); } FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe() dictionary decompression corruption");
} } }
/* remote dictionary HC compression test */ /* remote dictionary HC compression test */
crcOrig = XXH64(testInput + 64 KB, testCompressedSize, 0); { U64 const crc64 = XXH64(testInput + 64 KB, testCompressedSize, 0);
LZ4_resetStreamHC_fast(&sHC, compressionLevel); LZ4_resetStreamHC_fast(&sHC, compressionLevel);
LZ4_loadDictHC(&sHC, testInput, 32 KB); LZ4_loadDictHC(&sHC, testInput, 32 KB);
result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1); result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1);
FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() remote dictionary failed : result = %i", result); FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() remote dictionary failed : result = %i", result);
FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 32 KB); result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 32 KB);
FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe_usingDict() decompression failed following remote dictionary HC compression test"); FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe_usingDict() decompression failed following remote dictionary HC compression test");
{ U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_usingDict() decompression corruption"); } FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe_usingDict() decompression corruption");
} }
/* multiple HC compression with ext. dictionary */ /* multiple HC compression with ext. dictionary */
{ XXH64_state_t crcOrigState; { XXH64_state_t crcOrigState;