| 1 |
/* test_large_buffers.cc - Test deflate() and inflate() with large 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 |
#include <time.h> |
| 15 |
|
| 16 |
#include "test_shared.h" |
| 17 |
|
| 18 |
#include <gtest/gtest.h> |
| 19 |
|
| 20 |
#define COMPR_BUFFER_SIZE (48 * 1024) |
| 21 |
#define UNCOMPR_BUFFER_SIZE (32 * 1024) |
| 22 |
#define UNCOMPR_RAND_SIZE (8 * 1024) |
| 23 |
|
| 24 |
TEST(deflate, large_buffers) { |
| 25 |
PREFIX3(stream) c_stream, d_stream; |
| 26 |
uint8_t *compr, *uncompr; |
| 27 |
uint32_t compr_len, uncompr_len; |
| 28 |
int32_t i; |
| 29 |
time_t now; |
| 30 |
int err; |
| 31 |
|
| 32 |
memset(&c_stream, 0, sizeof(c_stream)); |
| 33 |
memset(&d_stream, 0, sizeof(d_stream)); |
| 34 |
|
| 35 |
compr = (uint8_t *)calloc(1, COMPR_BUFFER_SIZE); |
| 36 |
ASSERT_TRUE(compr != NULL); |
| 37 |
uncompr = (uint8_t *)calloc(1, UNCOMPR_BUFFER_SIZE); |
| 38 |
ASSERT_TRUE(uncompr != NULL); |
| 39 |
|
| 40 |
compr_len = COMPR_BUFFER_SIZE; |
| 41 |
uncompr_len = UNCOMPR_BUFFER_SIZE; |
| 42 |
|
| 43 |
srand((unsigned)time(&now)); |
| 44 |
for (i = 0; i < UNCOMPR_RAND_SIZE; i++) |
| 45 |
uncompr[i] = (uint8_t)(rand() % 256); |
| 46 |
|
| 47 |
err = PREFIX(deflateInit)(&c_stream, Z_DEFAULT_COMPRESSION); |
| 48 |
EXPECT_EQ(err, Z_OK); |
| 49 |
|
| 50 |
c_stream.next_out = compr; |
| 51 |
c_stream.avail_out = compr_len; |
| 52 |
c_stream.next_in = uncompr; |
| 53 |
c_stream.avail_in = uncompr_len; |
| 54 |
|
| 55 |
err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH); |
| 56 |
EXPECT_EQ(err, Z_OK); |
| 57 |
EXPECT_EQ(c_stream.avail_in, 0); |
| 58 |
|
| 59 |
err = PREFIX(deflate)(&c_stream, Z_FINISH); |
| 60 |
EXPECT_EQ(err, Z_STREAM_END); |
| 61 |
|
| 62 |
err = PREFIX(deflateEnd)(&c_stream); |
| 63 |
EXPECT_EQ(err, Z_OK); |
| 64 |
|
| 65 |
d_stream.next_in = compr; |
| 66 |
d_stream.avail_in = compr_len; |
| 67 |
d_stream.next_out = uncompr; |
| 68 |
|
| 69 |
err = PREFIX(inflateInit)(&d_stream); |
| 70 |
EXPECT_EQ(err, Z_OK); |
| 71 |
|
| 72 |
for (;;) { |
| 73 |
d_stream.next_out = uncompr; /* discard the output */ |
| 74 |
d_stream.avail_out = uncompr_len; |
| 75 |
err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); |
| 76 |
if (err == Z_STREAM_END) break; |
| 77 |
EXPECT_EQ(err, Z_OK); |
| 78 |
} |
| 79 |
|
| 80 |
err = PREFIX(inflateEnd)(&d_stream); |
| 81 |
EXPECT_EQ(err, Z_OK); |
| 82 |
|
| 83 |
EXPECT_EQ(d_stream.total_out, uncompr_len); |
| 84 |
|
| 85 |
free(compr); |
| 86 |
free(uncompr); |
| 87 |
} |