1 /* test_compress_bound.cc - Test compressBound() 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 class compress_bound_variant : public testing::TestWithParam<z_size_t> {
22 public:
estimate(z_size_t level)23     void estimate(z_size_t level) {
24         z_size_t estimate_len = 0;
25         uint8_t *uncompressed = NULL;
26         uint8_t dest[128];
27         int err;
28 
29         uncompressed = (uint8_t *)malloc(MAX_LENGTH);
30         ASSERT_TRUE(uncompressed != NULL);
31 
32         /* buffer with values for worst case compression */
33         for (int32_t j = 0; j < MAX_LENGTH; j++) {
34             uncompressed[j] = (uint8_t)j;
35         }
36 
37         for (z_size_t i = 0; i < MAX_LENGTH; i++) {
38             z_size_t dest_len = sizeof(dest);
39 
40             /* calculate actual output length */
41             estimate_len = PREFIX(compressBound)(i);
42 
43             err = PREFIX(compress2)(dest, &dest_len, uncompressed, i, level);
44             EXPECT_EQ(err, Z_OK);
45             EXPECT_GE(estimate_len, dest_len) <<
46                 "level: " << level << "\n" <<
47                 "length: " << i;
48         }
49 
50         free(uncompressed);
51     }
52 };
53 
TEST_P(compress_bound_variant,estimate)54 TEST_P(compress_bound_variant, estimate) {
55     estimate(GetParam());
56 }
57 
58 INSTANTIATE_TEST_SUITE_P(compress_bound, compress_bound_variant,
59     testing::Values(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
60