From: Nathan Moinvaziri Date: Sun, 10 Apr 2022 05:19:56 +0000 (-0700) Subject: Added compressBound tests for small buffers. X-Git-Tag: 2.1.0-beta1~257 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e123ecdcd2e543b37324e564139e25952a5d5446;p=thirdparty%2Fzlib-ng.git Added compressBound tests for small buffers. Co-authored-by: Mika T. Lindqvist --- diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6ebaf7c96..8b250ed78 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -54,6 +54,7 @@ set(TEST_SRCS test_aligned_alloc.cc test_compare256.cc test_compress.cc + test_compress_bound.cc test_crc32.cc test_cve-2003-0107.cc test_deflate_bound.cc diff --git a/test/test_compress_bound.cc b/test/test_compress_bound.cc new file mode 100644 index 000000000..2757fb2cf --- /dev/null +++ b/test/test_compress_bound.cc @@ -0,0 +1,59 @@ +/* test_compress_bound.cc - Test compressBound() with small buffers */ + +#include "zbuild.h" +#ifdef ZLIB_COMPAT +# include "zlib.h" +#else +# include "zlib-ng.h" +#endif + +#include +#include +#include +#include + +#include "test_shared.h" + +#include + +#define MAX_LENGTH (32) + +class compress_bound_variant : public testing::TestWithParam { +public: + void estimate(z_size_t level) { + z_size_t estimate_len = 0; + uint8_t *uncompressed = NULL; + uint8_t dest[128]; + int err; + + uncompressed = (uint8_t *)malloc(MAX_LENGTH); + ASSERT_TRUE(uncompressed != NULL); + + /* buffer with values for worst case compression */ + for (int32_t j = 0; j < MAX_LENGTH; j++) { + uncompressed[j] = (uint8_t)j; + } + + for (z_size_t i = 0; i < MAX_LENGTH; i++) { + z_size_t dest_len = sizeof(dest); + + /* calculate actual output length */ + estimate_len = PREFIX(compressBound)(i); + + err = PREFIX(compress2)(dest, &dest_len, uncompressed, i, level); + EXPECT_EQ(err, Z_OK); + EXPECT_GE(estimate_len, dest_len) << + "level: " << level << "\n" << + "length: " << i; + } + + free(uncompressed); + } +}; + +TEST_P(compress_bound_variant, estimate) { + estimate(GetParam()); +} + +INSTANTIATE_TEST_SUITE_P(compress_bound, compress_bound_variant, + testing::Values(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));