| 1 | /* test_deflate_bound.cc - Test deflateBound() with small buffers */ |
| 2 | |
| 3 | #include "zbuild.h" |
| 4 | #ifdef ZLIB_COMPAT |
| 5 | # include "zlib.h" |
| 6 | #else |
| 7 | # include "zlib-ng.h" |
| 8 | #endif |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdint.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include "test_shared.h" |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #define MAX_LENGTH (32) |
| 20 | |
| 21 | typedef struct { |
| 22 | int32_t level; |
| 23 | int32_t window_size; |
| 24 | int32_t mem_level; |
| 25 | } deflate_bound_test; |
| 26 | |
| 27 | static const deflate_bound_test tests[] = { |
| 28 | {0, MAX_WBITS + 16, 1}, |
| 29 | {Z_BEST_SPEED, MAX_WBITS, MAX_MEM_LEVEL}, |
| 30 | {Z_BEST_COMPRESSION, MAX_WBITS, MAX_MEM_LEVEL} |
| 31 | }; |
| 32 | |
| 33 | class deflate_bound_variant : public testing::TestWithParam<deflate_bound_test> { |
| 34 | public: |
| 35 | void estimate(deflate_bound_test param) { |
| 36 | PREFIX3(stream) c_stream; |
| 37 | int estimate_len = 0; |
| 38 | uint8_t *uncompressed = NULL; |
| 39 | uint8_t *out_buf = NULL; |
| 40 | int err; |
| 41 | |
| 42 | uncompressed = (uint8_t *)malloc(MAX_LENGTH); |
| 43 | ASSERT_TRUE(uncompressed != NULL); |
| 44 | memset(uncompressed, 'a', MAX_LENGTH); |
| 45 | |
| 46 | for (int32_t i = 0; i < MAX_LENGTH; i++) { |
| 47 | memset(&c_stream, 0, sizeof(c_stream)); |
| 48 | |
| 49 | c_stream.avail_in = i; |
| 50 | c_stream.next_in = (z_const unsigned char *)uncompressed; |
| 51 | c_stream.avail_out = 0; |
| 52 | c_stream.next_out = out_buf; |
| 53 | |
| 54 | err = PREFIX(deflateInit2)(&c_stream, param.level, Z_DEFLATED, |
| 55 | param.window_size, param.mem_level, Z_DEFAULT_STRATEGY); |
| 56 | EXPECT_EQ(err, Z_OK); |
| 57 | |
| 58 | /* calculate actual output length and update structure */ |
| 59 | estimate_len = PREFIX(deflateBound)(&c_stream, i); |
| 60 | out_buf = (uint8_t *)malloc(estimate_len); |
| 61 | |
| 62 | if (out_buf != NULL) { |
| 63 | /* update zlib configuration */ |
| 64 | c_stream.avail_out = estimate_len; |
| 65 | c_stream.next_out = out_buf; |
| 66 | |
| 67 | /* do the compression */ |
| 68 | err = PREFIX(deflate)(&c_stream, Z_FINISH); |
| 69 | EXPECT_EQ(err, Z_STREAM_END) << |
| 70 | "level: " << param.level << "\n" << |
| 71 | "window_size: " << param.window_size << "\n" << |
| 72 | "mem_level: " << param.mem_level << "\n" << |
| 73 | "length: " << i; |
| 74 | |
| 75 | free(out_buf); |
| 76 | } |
| 77 | |
| 78 | err = PREFIX(deflateEnd)(&c_stream); |
| 79 | EXPECT_EQ(err, Z_OK); |
| 80 | } |
| 81 | |
| 82 | free(uncompressed); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | TEST_P(deflate_bound_variant, estimate) { |
| 87 | estimate(GetParam()); |
| 88 | } |
| 89 | |
| 90 | INSTANTIATE_TEST_SUITE_P(deflate_bound, deflate_bound_variant, testing::ValuesIn(tests)); |