1 /* test_deflate_params.cc - Test deflate() with dynamic change of compression level */
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 #include <inttypes.h>
15 #include <time.h>
16
17 #include "deflate.h"
18
19 #include "test_shared.h"
20
21 #include <gtest/gtest.h>
22
23 #define COMPR_BUFFER_SIZE (48 * 1024)
24 #define UNCOMPR_BUFFER_SIZE (64 * 1024)
25 #define UNCOMPR_RAND_SIZE (8 * 1024)
26
TEST(deflate,params)27 TEST(deflate, params) {
28 PREFIX3(stream) c_stream, d_stream;
29 uint8_t *compr, *uncompr;
30 uint32_t compr_len, uncompr_len;
31 uint32_t diff;
32 int32_t i;
33 time_t now;
34 int err;
35 #ifndef ZLIB_COMPAT
36 int level = -1;
37 int strategy = -1;
38 zng_deflate_param_value params[2];
39
40 params[0].param = Z_DEFLATE_LEVEL;
41 params[0].buf = &level;
42 params[0].size = sizeof(level);
43
44 params[1].param = Z_DEFLATE_STRATEGY;
45 params[1].buf = &strategy;
46 params[1].size = sizeof(strategy);
47 #endif
48
49 memset(&c_stream, 0, sizeof(c_stream));
50 memset(&d_stream, 0, sizeof(d_stream));
51
52 compr = (uint8_t *)calloc(1, COMPR_BUFFER_SIZE);
53 ASSERT_TRUE(compr != NULL);
54 uncompr = (uint8_t *)calloc(1, UNCOMPR_BUFFER_SIZE);
55 ASSERT_TRUE(uncompr != NULL);
56
57 compr_len = COMPR_BUFFER_SIZE;
58 uncompr_len = UNCOMPR_BUFFER_SIZE;
59
60 srand((unsigned)time(&now));
61 for (i = 0; i < UNCOMPR_RAND_SIZE; i++)
62 uncompr[i] = (uint8_t)(rand() % 256);
63
64 err = PREFIX(deflateInit)(&c_stream, Z_BEST_SPEED);
65 EXPECT_EQ(err, Z_OK);
66
67 c_stream.next_out = compr;
68 c_stream.avail_out = compr_len;
69 c_stream.next_in = uncompr;
70 c_stream.avail_in = uncompr_len;
71
72 err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH);
73 EXPECT_EQ(err, Z_OK);
74 EXPECT_EQ(c_stream.avail_in, 0);
75
76 /* Feed in already compressed data and switch to no compression: */
77 #ifndef ZLIB_COMPAT
78 zng_deflateGetParams(&c_stream, params, sizeof(params) / sizeof(params[0]));
79 EXPECT_EQ(level, Z_BEST_SPEED);
80 EXPECT_EQ(strategy, Z_DEFAULT_STRATEGY);
81
82 level = Z_NO_COMPRESSION;
83 strategy = Z_DEFAULT_STRATEGY;
84 zng_deflateSetParams(&c_stream, params, sizeof(params) / sizeof(params[0]));
85 #else
86 PREFIX(deflateParams)(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
87 #endif
88
89 c_stream.next_in = compr;
90 diff = (unsigned int)(c_stream.next_out - compr);
91 c_stream.avail_in = diff;
92 err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH);
93 EXPECT_EQ(err, Z_OK);
94
95 /* Switch back to compressing mode: */
96 #ifndef ZLIB_COMPAT
97 level = -1;
98 strategy = -1;
99 zng_deflateGetParams(&c_stream, params, sizeof(params) / sizeof(params[0]));
100 EXPECT_EQ(level, Z_NO_COMPRESSION);
101 EXPECT_EQ(strategy, Z_DEFAULT_STRATEGY);
102
103 level = Z_BEST_COMPRESSION;
104 strategy = Z_FILTERED;
105 zng_deflateSetParams(&c_stream, params, sizeof(params) / sizeof(params[0]));
106 #else
107 PREFIX(deflateParams)(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
108 #endif
109
110 c_stream.next_in = uncompr;
111 c_stream.avail_in = (unsigned int)uncompr_len;
112 err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH);
113 EXPECT_EQ(err, Z_OK);
114
115 err = PREFIX(deflate)(&c_stream, Z_FINISH);
116 EXPECT_EQ(err, Z_STREAM_END);
117
118 err = PREFIX(deflateEnd)(&c_stream);
119 EXPECT_EQ(err, Z_OK);
120
121 d_stream.next_in = compr;
122 d_stream.avail_in = (unsigned int)compr_len;
123
124 err = PREFIX(inflateInit)(&d_stream);
125 EXPECT_EQ(err, Z_OK);
126
127 do {
128 d_stream.next_out = uncompr; /* discard the output */
129 d_stream.avail_out = uncompr_len;
130 err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH);
131 if (err == Z_STREAM_END)
132 break;
133 EXPECT_EQ(err, Z_OK);
134 } while (err == Z_OK);
135
136 err = PREFIX(inflateEnd)(&d_stream);
137 EXPECT_EQ(err, Z_OK);
138
139 EXPECT_EQ(d_stream.total_out, (2 * uncompr_len) + diff);
140
141 free(compr);
142 free(uncompr);
143 }
144