| #include "unity/unity.h" |
| #include "zlib.h" |
| #include <stdlib.h> |
| #include <string.h> |
| #include <stdio.h> |
|
|
| void setUp(void) { |
| |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| |
| static void* xmalloc(size_t n) { |
| void* p = malloc(n); |
| TEST_ASSERT_NOT_NULL(p); |
| return p; |
| } |
|
|
| |
| void test_compress2_roundtrip_small_text(void) { |
| const char *msg = "The quick brown fox jumps over the lazy dog"; |
| const uLong srcLen = (uLong)strlen(msg); |
| uLongf compCap = compressBound(srcLen); |
| Bytef *comp = (Bytef*)xmalloc(compCap); |
| uLongf compLen = compCap; |
|
|
| int err = compress2(comp, &compLen, (const Bytef*)msg, srcLen, 6); |
| TEST_ASSERT_EQUAL_INT(Z_OK, err); |
| TEST_ASSERT_TRUE(compLen > 0); |
| TEST_ASSERT_TRUE(compLen <= compCap); |
|
|
| Bytef *decomp = (Bytef*)xmalloc(srcLen ? srcLen : 1); |
| uLongf decompLen = srcLen; |
| err = uncompress(decomp, &decompLen, comp, compLen); |
| TEST_ASSERT_EQUAL_INT(Z_OK, err); |
| TEST_ASSERT_EQUAL_UINT32(srcLen, decompLen); |
| if (srcLen > 0) { |
| TEST_ASSERT_EQUAL_UINT8_ARRAY((const uint8_t*)msg, decomp, srcLen); |
| } |
|
|
| free(comp); |
| free(decomp); |
| } |
|
|
| |
| void test_compress2_zero_input(void) { |
| const Bytef *src = (const Bytef*)""; |
| const uLong srcLen = 0; |
| uLongf compCap = compressBound(srcLen); |
| if (compCap < 16) compCap = 16; |
| Bytef *comp = (Bytef*)xmalloc(compCap); |
| uLongf compLen = compCap; |
|
|
| int err = compress2(comp, &compLen, src, srcLen, Z_BEST_COMPRESSION); |
| TEST_ASSERT_EQUAL_INT(Z_OK, err); |
| TEST_ASSERT_TRUE(compLen > 0); |
| TEST_ASSERT_TRUE(compLen <= compCap); |
|
|
| Bytef *decomp = (Bytef*)xmalloc(1); |
| uLongf decompLen = 0; |
| err = uncompress(decomp, &decompLen, comp, compLen); |
| TEST_ASSERT_EQUAL_INT(Z_OK, err); |
| TEST_ASSERT_EQUAL_UINT32(0, decompLen); |
|
|
| free(comp); |
| free(decomp); |
| } |
|
|
| |
| void test_compress2_buffer_too_small(void) { |
| |
| const uLong srcLen = 1000; |
| Bytef *src = (Bytef*)xmalloc(srcLen); |
| memset(src, 'A', srcLen); |
|
|
| Bytef smallBuf[1]; |
| uLongf smallLen = sizeof(smallBuf); |
|
|
| int err = compress2(smallBuf, &smallLen, src, srcLen, Z_BEST_COMPRESSION); |
| TEST_ASSERT_EQUAL_INT(Z_BUF_ERROR, err); |
| |
| TEST_ASSERT_TRUE(smallLen <= sizeof(smallBuf)); |
|
|
| free(src); |
| } |
|
|
| |
| void test_compress2_zero_capacity(void) { |
| const uLong srcLen = 256; |
| Bytef *src = (Bytef*)xmalloc(srcLen); |
| for (uLong i = 0; i < srcLen; ++i) src[i] = (Bytef)(i & 0xFF); |
|
|
| Bytef *dest = NULL; |
| uLongf destLen = 0; |
|
|
| int err = compress2(dest, &destLen, src, srcLen, 6); |
| TEST_ASSERT_EQUAL_INT(Z_BUF_ERROR, err); |
| TEST_ASSERT_EQUAL_UINT32(0, destLen); |
|
|
| free(src); |
| } |
|
|
| |
| void test_compress2_invalid_level(void) { |
| const char *msg = "invalid level test data"; |
| const uLong srcLen = (uLong)strlen(msg); |
| Bytef destBuf[128]; |
| uLongf destLen = sizeof(destBuf); |
|
|
| int err = compress2(destBuf, &destLen, (const Bytef*)msg, srcLen, 99); |
| TEST_ASSERT_EQUAL_INT(Z_STREAM_ERROR, err); |
| TEST_ASSERT_EQUAL_UINT32(0, destLen); |
| } |
|
|
| |
| void test_compress2_large_input_levels(void) { |
| const uLong srcLen = 200000; |
| Bytef *src = (Bytef*)xmalloc(srcLen); |
| memset(src, 'A', srcLen); |
|
|
| |
| uLongf cap0 = compressBound(srcLen); |
| Bytef *comp0 = (Bytef*)xmalloc(cap0); |
| uLongf len0 = cap0; |
| int err = compress2(comp0, &len0, src, srcLen, 0); |
| TEST_ASSERT_EQUAL_INT(Z_OK, err); |
| TEST_ASSERT_TRUE(len0 > 0); |
| TEST_ASSERT_TRUE(len0 <= cap0); |
|
|
| |
| Bytef *decomp0 = (Bytef*)xmalloc(srcLen); |
| uLongf dlen0 = srcLen; |
| err = uncompress(decomp0, &dlen0, comp0, len0); |
| TEST_ASSERT_EQUAL_INT(Z_OK, err); |
| TEST_ASSERT_EQUAL_UINT32(srcLen, dlen0); |
| TEST_ASSERT_EQUAL_UINT8_ARRAY(src, decomp0, srcLen); |
|
|
| |
| uLongf cap9 = compressBound(srcLen); |
| Bytef *comp9 = (Bytef*)xmalloc(cap9); |
| uLongf len9 = cap9; |
| err = compress2(comp9, &len9, src, srcLen, 9); |
| TEST_ASSERT_EQUAL_INT(Z_OK, err); |
| TEST_ASSERT_TRUE(len9 > 0); |
| TEST_ASSERT_TRUE(len9 <= cap9); |
|
|
| |
| Bytef *decomp9 = (Bytef*)xmalloc(srcLen); |
| uLongf dlen9 = srcLen; |
| err = uncompress(decomp9, &dlen9, comp9, len9); |
| TEST_ASSERT_EQUAL_INT(Z_OK, err); |
| TEST_ASSERT_EQUAL_UINT32(srcLen, dlen9); |
| TEST_ASSERT_EQUAL_UINT8_ARRAY(src, decomp9, srcLen); |
|
|
| |
| TEST_ASSERT_TRUE(len9 <= len0); |
|
|
| free(src); |
| free(comp0); |
| free(decomp0); |
| free(comp9); |
| free(decomp9); |
| } |
|
|
| int main(void) { |
| UNITY_BEGIN(); |
| RUN_TEST(test_compress2_roundtrip_small_text); |
| RUN_TEST(test_compress2_zero_input); |
| RUN_TEST(test_compress2_buffer_too_small); |
| RUN_TEST(test_compress2_zero_capacity); |
| RUN_TEST(test_compress2_invalid_level); |
| RUN_TEST(test_compress2_large_input_levels); |
| return UNITY_END(); |
| } |