summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorNathan Moinvaziri <nathan@nathanm.com>2022-04-09 22:20:39 -0700
committerHans Kristian Rosbach <hk-github@circlestorm.org>2022-05-03 10:33:05 +0200
commitafbc9406a64624982c21d11efd8fb84b9bc00cd3 (patch)
tree7f009486d6de680ab0eef9e5645dc852e54e5c21 /test
parente123ecdcd2e543b37324e564139e25952a5d5446 (diff)
downloadProject-Tick-afbc9406a64624982c21d11efd8fb84b9bc00cd3.tar.gz
Project-Tick-afbc9406a64624982c21d11efd8fb84b9bc00cd3.zip
Improve deflateBound unit test to test a range of small buffer lengths with various deflateBound initialization values.
Co-authored-by: Mika T. Lindqvist <postmaster@raasu.org>
Diffstat (limited to 'test')
-rw-r--r--test/test_deflate_bound.cc101
1 files changed, 69 insertions, 32 deletions
diff --git a/test/test_deflate_bound.cc b/test/test_deflate_bound.cc
index d6f576a824..27020c6f92 100644
--- a/test/test_deflate_bound.cc
+++ b/test/test_deflate_bound.cc
@@ -16,38 +16,75 @@
#include <gtest/gtest.h>
-TEST(deflate, bound) {
- PREFIX3(stream) c_stream;
- int estimate_len = 0;
- uint8_t *out_buf = NULL;
- int err;
-
- memset(&c_stream, 0, sizeof(c_stream));
-
- c_stream.avail_in = hello_len;
- c_stream.next_in = (z_const unsigned char *)hello;
- c_stream.avail_out = 0;
- c_stream.next_out = out_buf;
-
- err = PREFIX(deflateInit)(&c_stream, Z_DEFAULT_COMPRESSION);
- EXPECT_EQ(err, Z_OK);
-
- /* calculate actual output length and update structure */
- estimate_len = PREFIX(deflateBound)(&c_stream, hello_len);
- out_buf = (uint8_t *)malloc(estimate_len);
-
- if (out_buf != NULL) {
- /* update zlib configuration */
- c_stream.avail_out = estimate_len;
- c_stream.next_out = out_buf;
-
- /* do the compression */
- err = PREFIX(deflate)(&c_stream, Z_FINISH);
- EXPECT_EQ(err, Z_STREAM_END);
- }
+#define MAX_LENGTH (32)
+
+typedef struct {
+ int32_t level;
+ int32_t window_size;
+ int32_t mem_level;
+} deflate_bound_test;
+
+static const deflate_bound_test tests[] = {
+ {0, MAX_WBITS + 16, 1},
+ {Z_BEST_SPEED, MAX_WBITS, MAX_MEM_LEVEL},
+ {Z_BEST_COMPRESSION, MAX_WBITS, MAX_MEM_LEVEL}
+};
+
+class deflate_bound_variant : public testing::TestWithParam<deflate_bound_test> {
+public:
+ void estimate(deflate_bound_test param) {
+ PREFIX3(stream) c_stream;
+ int estimate_len = 0;
+ uint8_t *uncompressed = NULL;
+ uint8_t *out_buf = NULL;
+ int err;
+
+ uncompressed = (uint8_t *)malloc(MAX_LENGTH);
+ ASSERT_TRUE(uncompressed != NULL);
+ memset(uncompressed, 'a', MAX_LENGTH);
+
+ for (int32_t i = 0; i < MAX_LENGTH; i++) {
+ memset(&c_stream, 0, sizeof(c_stream));
+
+ c_stream.avail_in = i;
+ c_stream.next_in = (z_const unsigned char *)uncompressed;
+ c_stream.avail_out = 0;
+ c_stream.next_out = out_buf;
+
+ err = PREFIX(deflateInit2)(&c_stream, param.level, Z_DEFLATED,
+ param.window_size, param.mem_level, Z_DEFAULT_STRATEGY);
+ EXPECT_EQ(err, Z_OK);
- err = PREFIX(deflateEnd)(&c_stream);
- EXPECT_EQ(err, Z_OK);
+ /* calculate actual output length and update structure */
+ estimate_len = PREFIX(deflateBound)(&c_stream, i);
+ out_buf = (uint8_t *)malloc(estimate_len);
- free(out_buf);
+ if (out_buf != NULL) {
+ /* update zlib configuration */
+ c_stream.avail_out = estimate_len;
+ c_stream.next_out = out_buf;
+
+ /* do the compression */
+ err = PREFIX(deflate)(&c_stream, Z_FINISH);
+ EXPECT_EQ(err, Z_STREAM_END) <<
+ "level: " << param.level << "\n" <<
+ "window_size: " << param.window_size << "\n" <<
+ "mem_level: " << param.mem_level << "\n" <<
+ "length: " << i;
+
+ free(out_buf);
+ }
+
+ err = PREFIX(deflateEnd)(&c_stream);
+ EXPECT_EQ(err, Z_OK);
+ }
+
+ free(uncompressed);
+ }
+};
+
+TEST_P(deflate_bound_variant, estimate) {
+ estimate(GetParam());
}
+
+INSTANTIATE_TEST_SUITE_P(deflate_bound, deflate_bound_variant, testing::ValuesIn(tests));