]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
add simple test for maxBlockSize expected functionality
authorDanielle Rozenblit <drozenblit@fb.com>
Thu, 12 Jan 2023 16:55:39 +0000 (08:55 -0800)
committerDanielle Rozenblit <drozenblit@fb.com>
Thu, 12 Jan 2023 16:55:39 +0000 (08:55 -0800)
lib/compress/zstd_compress.c
lib/zstd.h
tests/zstreamtest.c

index e27cbd6133dd2a4e847ddaa789b6491b07d2774c..6872d3364b197930fbccd43aff2f3483bd958000 100644 (file)
@@ -288,7 +288,7 @@ static int ZSTD_resolveExternalSequenceValidation(int mode) {
 #endif
 }
 
-/* Sets the default maximum block size. */
+/* Resolves maxBlockSize to the default if no value is present. */
 static size_t ZSTD_resolveMaxBlockSize(size_t maxBlockSize) {
     if (maxBlockSize == 0) {
         return ZSTD_BLOCKSIZE_MAX;
index 2287886858cb4e4d1147ec8bd76f31b027a0f906..0c0fc1afc919d5e4ef3df3260c43d61d7e92c205 100644 (file)
@@ -141,7 +141,7 @@ ZSTDLIB_API const char* ZSTD_versionString(void);
 
 #define ZSTD_BLOCKSIZELOG_MAX  17
 #define ZSTD_BLOCKSIZE_MAX     (1<<ZSTD_BLOCKSIZELOG_MAX)
-#define ZSTD_BLOCKSIZE_MAX_MIN 1 << 10 /* The minimum valid max blocksize. Maximum blocksizes smaller than this make compressBound() inaccurate. */
+#define ZSTD_BLOCKSIZE_MAX_MIN (1 << 10) /* The minimum valid max blocksize. Maximum blocksizes smaller than this make compressBound() inaccurate. */
 
 
 /***************************************
index 664ff632ac1d80b83a4fefef5ab854a48079ad1b..bd829fbb1ca54b75f96a61865d72d7bc7f9e4362 100644 (file)
@@ -1925,6 +1925,49 @@ static int basicUnitTests(U32 seed, double compressibility, int bigTests)
     }
     DISPLAYLEVEL(3, "OK \n");
 
+
+    /* Test maxBlockSize cctx param functionality */
+    DISPLAYLEVEL(3, "test%3i : Testing maxBlockSize PR#3418: ", testNb++);
+    {
+        ZSTD_CCtx* cctx = ZSTD_createCCtx();
+        size_t const srcSize = 2 << 10;
+        void* const src = CNBuffer;
+        size_t const dstSize = ZSTD_compressBound(srcSize);
+        void* const dst1 = compressedBuffer;
+        void* const dst2 = (BYTE*)compressedBuffer + dstSize;
+        size_t size1, size2;
+        void* const checkBuf = malloc(srcSize);
+
+        memset(src, 'x', srcSize);
+
+        /* Quick test to make sure maxBlockSize bounds are enforced */
+        assert(ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_maxBlockSize, ZSTD_BLOCKSIZE_MAX_MIN - 1)));
+        assert(ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_maxBlockSize, ZSTD_BLOCKSIZE_MAX + 1)));
+
+        /* maxBlockSize = 1KB */
+        CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_maxBlockSize, 1 << 10));
+        size1 = ZSTD_compress2(cctx, dst1, dstSize, src, srcSize);
+
+        if (ZSTD_isError(size1)) goto _output_error;
+        CHECK_Z(ZSTD_decompress(checkBuf, srcSize, dst1, size1));
+        CHECK(memcmp(src, checkBuf, srcSize) != 0, "Corruption!");
+
+        /* maxBlockSize = 3KB */
+        CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_maxBlockSize, 3 << 10));
+        size2 = ZSTD_compress2(cctx, dst2, dstSize, src, srcSize);
+
+        if (ZSTD_isError(size2)) goto _output_error;
+        CHECK_Z(ZSTD_decompress(checkBuf, srcSize, dst2, size2));
+        CHECK(memcmp(src, checkBuf, srcSize) != 0, "Corruption!");
+
+        /* Compressed output should not be equal */
+        assert(memcmp(dst1, dst2, dstSize) != 0);
+        assert(size1 - size2 == 4); /* We add another RLE block with header + character */
+        ZSTD_freeCCtx(cctx);
+        free(checkBuf);
+    }
+    DISPLAYLEVEL(3, "OK \n");
+
 _end:
     FUZ_freeDictionary(dictionary);
     ZSTD_freeCStream(zc);