| #include "unity/unity.h" |
| #include "zlib.h" |
|
|
| #include <string.h> |
| #include <stdlib.h> |
| #include <stdint.h> |
|
|
| |
| static uLong calc_fixed(uLong n) { |
| |
| return n + (n >> 3) + (n >> 8) + (n >> 9) + 4u; |
| } |
| static uLong calc_store(uLong n) { |
| |
| return n + (n >> 5) + (n >> 7) + (n >> 11) + 7u; |
| } |
| static uLong calc_default(uLong n, uLong wraplen) { |
| |
| |
| return n + (n >> 12) + (n >> 14) + (n >> 25) + 13u - 6u + wraplen; |
| } |
|
|
| void setUp(void) { |
| |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| |
| void test_deflateBound_invalid_stream_null_ptr(void) { |
| uLong src = 123456u; |
| uLong expected = ((calc_fixed(src) > calc_store(src)) ? calc_fixed(src) : calc_store(src)) + 18u; |
| uLong got = deflateBound(NULL, src); |
| TEST_ASSERT_EQUAL_UINT64(expected, got); |
| } |
|
|
| |
| void test_deflateBound_invalid_stream_uninitialized(void) { |
| z_stream strm; |
| memset(&strm, 0, sizeof(strm)); |
| uLong src = 98765u; |
| uLong expected = ((calc_fixed(src) > calc_store(src)) ? calc_fixed(src) : calc_store(src)) + 18u; |
| uLong got = deflateBound(&strm, src); |
| TEST_ASSERT_EQUAL_UINT64(expected, got); |
| } |
|
|
| |
| void test_deflateBound_default_stream_no_dict(void) { |
| z_stream strm; |
| memset(&strm, 0, sizeof(strm)); |
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateInit(&strm, Z_DEFAULT_COMPRESSION)); |
| |
| uLong src = 100000u; |
| uLong wraplen = 6u; |
| uLong expected = calc_default(src, wraplen); |
| uLong got = deflateBound(&strm, src); |
| TEST_ASSERT_EQUAL_UINT64(expected, got); |
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
| } |
|
|
| |
| void test_deflateBound_default_stream_with_preset_dict(void) { |
| z_stream strm; |
| memset(&strm, 0, sizeof(strm)); |
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateInit(&strm, Z_DEFAULT_COMPRESSION)); |
|
|
| |
| const unsigned char dict[] = "preset-dictionary-bytes"; |
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateSetDictionary(&strm, dict, (uInt)sizeof(dict))); |
|
|
| |
| uLong src = 54321u; |
| uLong wraplen = 10u; |
| uLong expected = calc_default(src, wraplen); |
| uLong got = deflateBound(&strm, src); |
| TEST_ASSERT_EQUAL_UINT64(expected, got); |
|
|
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
| } |
|
|
| |
| void test_deflateBound_nondefault_memLevel_storelen_path(void) { |
| z_stream strm; |
| memset(&strm, 0, sizeof(strm)); |
| |
| |
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15, 1, Z_DEFAULT_STRATEGY)); |
|
|
| uLong src = 200000u; |
| uLong wraplen = 6u; |
| uLong expected = calc_store(src) + wraplen; |
| uLong got = deflateBound(&strm, src); |
| TEST_ASSERT_EQUAL_UINT64(expected, got); |
|
|
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
| } |
|
|
| |
| void test_deflateBound_small_window_fixedlen_path(void) { |
| z_stream strm; |
| memset(&strm, 0, sizeof(strm)); |
| |
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateInit2(&strm, 6, Z_DEFLATED, 9, 8, Z_DEFAULT_STRATEGY)); |
|
|
| uLong src = 65536u; |
| uLong wraplen = 6u; |
| uLong expected = calc_fixed(src) + wraplen; |
| uLong got = deflateBound(&strm, src); |
| TEST_ASSERT_EQUAL_UINT64(expected, got); |
|
|
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
| } |
|
|
| |
| void test_deflateBound_raw_deflate_no_wrapper(void) { |
| z_stream strm; |
| memset(&strm, 0, sizeof(strm)); |
| |
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY)); |
|
|
| uLong src = 314159u; |
| uLong wraplen = 0u; |
| uLong expected = calc_default(src, wraplen); |
| uLong got = deflateBound(&strm, src); |
| TEST_ASSERT_EQUAL_UINT64(expected, got); |
|
|
| TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
| } |
|
|
| int main(void) { |
| UNITY_BEGIN(); |
| RUN_TEST(test_deflateBound_invalid_stream_null_ptr); |
| RUN_TEST(test_deflateBound_invalid_stream_uninitialized); |
| RUN_TEST(test_deflateBound_default_stream_no_dict); |
| RUN_TEST(test_deflateBound_default_stream_with_preset_dict); |
| RUN_TEST(test_deflateBound_nondefault_memLevel_storelen_path); |
| RUN_TEST(test_deflateBound_small_window_fixedlen_path); |
| RUN_TEST(test_deflateBound_raw_deflate_no_wrapper); |
| return UNITY_END(); |
| } |