File size: 5,778 Bytes
e996a55 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include "unity/unity.h"
#include "zlib.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void setUp(void) {
/* Setup code here, or leave empty */
}
void tearDown(void) {
/* Cleanup code here, or leave empty */
}
/* Helper to allocate and check non-null */
static void* xmalloc(size_t n) {
void* p = malloc(n);
TEST_ASSERT_NOT_NULL(p);
return p;
}
/* Test 1: Round-trip small text with typical compression level */
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);
}
/* Test 2: Zero-length input should succeed and decompress to zero bytes */
void test_compress2_zero_input(void) {
const Bytef *src = (const Bytef*)""; /* non-null pointer */
const uLong srcLen = 0;
uLongf compCap = compressBound(srcLen);
if (compCap < 16) compCap = 16; /* ensure some room */
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); /* capacity 1, expect output size 0 */
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);
}
/* Test 3: Output buffer too small should return Z_BUF_ERROR and update destLen */
void test_compress2_buffer_too_small(void) {
/* Prepare some input */
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);
/* destLen should be the number of bytes produced before running out of space */
TEST_ASSERT_TRUE(smallLen <= sizeof(smallBuf));
free(src);
}
/* Test 4: Zero capacity buffer should return Z_BUF_ERROR and leave destLen 0 */
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; /* no space */
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);
}
/* Test 5: Invalid compression level should return Z_STREAM_ERROR and set destLen to 0 */
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); /* non-zero to verify it becomes 0 on error */
int err = compress2(destBuf, &destLen, (const Bytef*)msg, srcLen, 99); /* invalid level */
TEST_ASSERT_EQUAL_INT(Z_STREAM_ERROR, err);
TEST_ASSERT_EQUAL_UINT32(0, destLen);
}
/* Test 6: Large input round-trip at levels 0 and 9; level 9 should compress better */
void test_compress2_large_input_levels(void) {
const uLong srcLen = 200000; /* 200 KB */
Bytef *src = (Bytef*)xmalloc(srcLen);
memset(src, 'A', srcLen); /* highly compressible */
/* Level 0 (store) */
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);
/* Decompress and verify */
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);
/* Level 9 (best) */
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);
/* Decompress and verify */
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);
/* Expect better compression at level 9 than at level 0 for repetitive data */
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();
} |