| #include "unity/unity.h" |
| #include "zlib.h" |
| #include <stdlib.h> |
| #include <string.h> |
|
|
| |
| |
| |
| extern int test_deflate_slow(struct internal_state *s, int flush); |
|
|
| |
| #define BS_NEED_MORE 0 |
| #define BS_BLOCK_DONE 1 |
| #define BS_FINISH_STARTED 2 |
| #define BS_FINISH_DONE 3 |
|
|
| void setUp(void) { |
| |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| static void init_stream(z_stream *strm, int level) { |
| memset(strm, 0, sizeof(*strm)); |
| |
| int ret = deflateInit2_(strm, |
| level, |
| Z_DEFLATED, |
| MAX_WBITS, |
| DEF_MEM_LEVEL, |
| Z_DEFAULT_STRATEGY, |
| ZLIB_VERSION, |
| (int)sizeof(z_stream)); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
| } |
|
|
| |
| void test_deflate_slow_no_input_need_more(void) { |
| z_stream strm; |
| init_stream(&strm, 6); |
|
|
| |
| strm.next_in = Z_NULL; |
| strm.avail_in = 0; |
|
|
| |
| unsigned char outbuf[64]; |
| strm.next_out = outbuf; |
| strm.avail_out = sizeof(outbuf); |
|
|
| |
| int bs = test_deflate_slow((struct internal_state *)strm.state, Z_NO_FLUSH); |
| TEST_ASSERT_EQUAL_INT(BS_NEED_MORE, bs); |
|
|
| deflateEnd(&strm); |
| } |
|
|
| |
| void test_deflate_slow_finish_no_input_finish_done(void) { |
| z_stream strm; |
| init_stream(&strm, 6); |
|
|
| strm.next_in = Z_NULL; |
| strm.avail_in = 0; |
|
|
| unsigned char outbuf[1024]; |
| strm.next_out = outbuf; |
| strm.avail_out = sizeof(outbuf); |
|
|
| int bs = test_deflate_slow((struct internal_state *)strm.state, Z_FINISH); |
| TEST_ASSERT_EQUAL_INT(BS_FINISH_DONE, bs); |
|
|
| |
| TEST_ASSERT_TRUE(strm.total_out > 0); |
|
|
| deflateEnd(&strm); |
| } |
|
|
| |
| |
| void test_deflate_slow_with_input_block_done(void) { |
| z_stream strm; |
| init_stream(&strm, 9); |
|
|
| |
| unsigned char inbuf[1024]; |
| for (size_t i = 0; i < sizeof(inbuf); ++i) inbuf[i] = (unsigned char)('A' + (i % 1)); |
|
|
| unsigned char outbuf[16384]; |
| memset(outbuf, 0, sizeof(outbuf)); |
|
|
| strm.next_in = inbuf; |
| strm.avail_in = (uInt)sizeof(inbuf); |
| strm.next_out = outbuf; |
| strm.avail_out = (uInt)sizeof(outbuf); |
|
|
| int bs = test_deflate_slow((struct internal_state *)strm.state, Z_NO_FLUSH); |
|
|
| TEST_ASSERT_EQUAL_INT(BS_BLOCK_DONE, bs); |
| |
| TEST_ASSERT_EQUAL_UINT(0u, strm.avail_in); |
| |
| TEST_ASSERT_TRUE(strm.total_out > 0); |
|
|
| deflateEnd(&strm); |
| } |
|
|
| |
| void test_deflate_slow_finish_with_no_output_space(void) { |
| z_stream strm; |
| init_stream(&strm, 6); |
|
|
| |
| strm.next_in = Z_NULL; |
| strm.avail_in = 0; |
|
|
| unsigned char outbuf[1]; |
| strm.next_out = outbuf; |
| strm.avail_out = 0; |
|
|
| int bs = test_deflate_slow((struct internal_state *)strm.state, Z_FINISH); |
| TEST_ASSERT_EQUAL_INT(BS_FINISH_STARTED, bs); |
|
|
| deflateEnd(&strm); |
| } |
|
|
| |
| |
| void test_deflate_slow_input_tiny_output_need_more(void) { |
| z_stream strm; |
| init_stream(&strm, 6); |
|
|
| unsigned char inbuf[256]; |
| memset(inbuf, 'B', sizeof(inbuf)); |
|
|
| unsigned char outbuf[1]; |
| strm.next_in = inbuf; |
| strm.avail_in = (uInt)sizeof(inbuf); |
| strm.next_out = outbuf; |
| strm.avail_out = (uInt)sizeof(outbuf); |
|
|
| int bs = test_deflate_slow((struct internal_state *)strm.state, Z_NO_FLUSH); |
|
|
| |
| TEST_ASSERT_EQUAL_INT(BS_NEED_MORE, bs); |
| |
| TEST_ASSERT_TRUE(strm.total_out <= 1); |
|
|
| deflateEnd(&strm); |
| } |
|
|
| int main(void) { |
| UNITY_BEGIN(); |
| RUN_TEST(test_deflate_slow_no_input_need_more); |
| RUN_TEST(test_deflate_slow_finish_no_input_finish_done); |
| RUN_TEST(test_deflate_slow_with_input_block_done); |
| RUN_TEST(test_deflate_slow_finish_with_no_output_space); |
| RUN_TEST(test_deflate_slow_input_tiny_output_need_more); |
| return UNITY_END(); |
| } |