| #include "unity/unity.h" |
| #include "zlib.h" |
| #include <stdlib.h> |
| #include <string.h> |
| #include <stdint.h> |
|
|
| |
| extern int test_deflate_fast(void *s, int flush); |
|
|
| |
| |
| enum { |
| BS_need_more = 0, |
| BS_block_done = 1, |
| BS_finish_started = 2, |
| BS_finish_done = 3 |
| }; |
|
|
| void setUp(void) { |
| |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| |
| static void init_def_stream(z_stream *strm, unsigned char *out_buf, size_t out_size) { |
| memset(strm, 0, sizeof(*strm)); |
| int ret = deflateInit2(strm, |
| 2, |
| Z_DEFLATED, |
| MAX_WBITS, |
| DEF_MEM_LEVEL, |
| Z_DEFAULT_STRATEGY); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
| strm->next_out = out_buf; |
| strm->avail_out = (uInt)out_size; |
| } |
|
|
| |
| void test_deflate_fast_need_more_when_no_input(void) { |
| z_stream strm; |
| unsigned char out_buf[1024]; |
| init_def_stream(&strm, out_buf, sizeof(out_buf)); |
|
|
| |
| strm.next_in = Z_NULL; |
| strm.avail_in = 0; |
|
|
| int bs = test_deflate_fast((void *)strm.state, Z_NO_FLUSH); |
| TEST_ASSERT_EQUAL_INT(BS_need_more, bs); |
|
|
| TEST_ASSERT_EQUAL_UINT(0, strm.total_in); |
| TEST_ASSERT_EQUAL_UINT(0, strm.avail_in); |
|
|
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
| } |
|
|
| |
| void test_deflate_fast_block_done_with_sufficient_input(void) { |
| z_stream strm; |
| |
| size_t out_size = 1 << 20; |
| unsigned char *out_buf = (unsigned char *)malloc(out_size); |
| TEST_ASSERT_NOT_NULL(out_buf); |
| init_def_stream(&strm, out_buf, out_size); |
|
|
| |
| size_t in_len = 1024; |
| unsigned char *in_buf = (unsigned char *)malloc(in_len); |
| TEST_ASSERT_NOT_NULL(in_buf); |
| for (size_t i = 0; i < in_len; i++) { |
| in_buf[i] = (unsigned char)(i & 0xFF); |
| } |
| strm.next_in = in_buf; |
| strm.avail_in = (uInt)in_len; |
|
|
| int bs = test_deflate_fast((void *)strm.state, Z_NO_FLUSH); |
| TEST_ASSERT_EQUAL_INT(BS_block_done, bs); |
|
|
| |
| TEST_ASSERT_EQUAL_UINT(0, strm.avail_in); |
| TEST_ASSERT_EQUAL_UINT(in_len, strm.total_in); |
|
|
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
| free(out_buf); |
| free(in_buf); |
| } |
|
|
| |
| void test_deflate_fast_finish_done_with_enough_output(void) { |
| z_stream strm; |
| size_t out_size = 1 << 16; |
| unsigned char *out_buf = (unsigned char *)malloc(out_size); |
| TEST_ASSERT_NOT_NULL(out_buf); |
| init_def_stream(&strm, out_buf, out_size); |
|
|
| |
| const char *msg = "Finish this block quickly!"; |
| size_t in_len = strlen(msg); |
| strm.next_in = (const Bytef *)msg; |
| strm.avail_in = (uInt)in_len; |
|
|
| int bs = test_deflate_fast((void *)strm.state, Z_FINISH); |
| TEST_ASSERT_EQUAL_INT(BS_finish_done, bs); |
|
|
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
| free(out_buf); |
| } |
|
|
| |
| void test_deflate_fast_finish_started_with_no_output_space(void) { |
| z_stream strm; |
| unsigned char dummy; |
| init_def_stream(&strm, &dummy, 0); |
|
|
| |
| strm.next_in = Z_NULL; |
| strm.avail_in = 0; |
|
|
| int bs = test_deflate_fast((void *)strm.state, Z_FINISH); |
| TEST_ASSERT_EQUAL_INT(BS_finish_started, bs); |
|
|
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
| } |
|
|
| int main(void) { |
| UNITY_BEGIN(); |
| RUN_TEST(test_deflate_fast_need_more_when_no_input); |
| RUN_TEST(test_deflate_fast_block_done_with_sufficient_input); |
| RUN_TEST(test_deflate_fast_finish_done_with_enough_output); |
| RUN_TEST(test_deflate_fast_finish_started_with_no_output_space); |
| return UNITY_END(); |
| } |