1 /* test_deflate_copy.cc - Test deflateCopy() 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 "deflate.h"
16
17 #include "test_shared.h"
18
19 #include <gtest/gtest.h>
20
TEST(deflate,copy)21 TEST(deflate, copy) {
22 PREFIX3(stream) c_stream, c_stream_copy;
23 uint8_t compr[128];
24 z_size_t compr_len = sizeof(compr);
25 int err;
26
27 memset(&c_stream, 0, sizeof(c_stream));
28 memset(&c_stream_copy, 0, sizeof(c_stream_copy));
29
30 err = PREFIX(deflateInit)(&c_stream, Z_DEFAULT_COMPRESSION);
31 EXPECT_EQ(err, Z_OK);
32
33 c_stream.next_in = (z_const unsigned char *)hello;
34 c_stream.next_out = compr;
35
36 while (c_stream.total_in != hello_len && c_stream.total_out < compr_len) {
37 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
38 err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH);
39 EXPECT_EQ(err, Z_OK);
40 }
41
42 /* Finish the stream, still forcing small buffers: */
43 for (;;) {
44 c_stream.avail_out = 1;
45 err = PREFIX(deflate)(&c_stream, Z_FINISH);
46 if (err == Z_STREAM_END) break;
47 EXPECT_EQ(err, Z_OK);
48 }
49
50 err = PREFIX(deflateCopy)(&c_stream_copy, &c_stream);
51 EXPECT_EQ(err, Z_OK);
52
53 EXPECT_EQ(c_stream.state->status, c_stream_copy.state->status);
54
55 err = PREFIX(deflateEnd)(&c_stream);
56 EXPECT_EQ(err, Z_OK);
57
58 err = PREFIX(deflateEnd)(&c_stream_copy);
59 EXPECT_EQ(err, Z_OK);
60 }
61