#include "unity/unity.h" #include "zlib.h" #include "deflate.h" #include #include #include /* Wrapper provided by the module (available when built with ZLIB_DEBUG) */ extern void test_check_match(deflate_state *s, IPos start, IPos match, int length); void setUp(void) { /* Setup code here, or leave empty */ } void tearDown(void) { /* Cleanup code here, or leave empty */ } /* Helper to initialize a deflate stream and return its internal state */ static deflate_state* init_state(z_stream *strm) { memset(strm, 0, sizeof(*strm)); int ret = deflateInit(strm, Z_DEFAULT_COMPRESSION); TEST_ASSERT_EQUAL_INT(Z_OK, ret); TEST_ASSERT_NOT_NULL(strm->state); return (deflate_state*)strm->state; } /* Test 1: Basic equal regions within the window should pass without error */ void test_check_match_basic_equal(void) { z_stream strm; deflate_state *s = init_state(&strm); /* Choose indices well within the allocated window */ const IPos match = 50; const IPos start = 100; const int length = 9; const unsigned char seq[] = { 'H','e','l','l','o','Z','l','i','b' }; /* Ensure window is allocated and large enough, then place equal data */ TEST_ASSERT_NOT_NULL(s->window); memcpy(s->window + match, seq, length); memcpy(s->window + start, seq, length); /* Should not abort (valid match) */ test_check_match(s, start, match, length); TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); } /* Test 2: Special case when match == (IPos)-1; compare length-1 bytes after increment */ void test_check_match_match_minus_one_case(void) { z_stream strm; deflate_state *s = init_state(&strm); /* We will call with match == -1, so check_match will compare: back = window[0 .. len-2] here = window[start+1 .. start+len-1] */ const IPos match = (IPos)-1; const IPos start = 10; const int length = 6; /* check_match will compare length-1 bytes == 5 */ const unsigned char pattern[] = { 1, 2, 3, 4, 5 }; TEST_ASSERT_NOT_NULL(s->window); /* Set the two compared regions equal according to the function's logic */ memcpy(s->window + 0, pattern, sizeof(pattern)); /* back (after ++), indexes 0..4 */ memcpy(s->window + (start + 1), pattern, sizeof(pattern)); /* here (after ++), indexes 11..15 */ /* Should not abort (valid match given the semantics) */ test_check_match(s, start, match, length); TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); } /* Test 3: Boundary case with length == 1 */ void test_check_match_length_one(void) { z_stream strm; deflate_state *s = init_state(&strm); const IPos match = 42; const IPos start = 1234; const int length = 1; TEST_ASSERT_NOT_NULL(s->window); /* Ensure single byte equality */ s->window[match] = (Byte)'Q'; s->window[start] = (Byte)'Q'; /* Should not abort */ test_check_match(s, start, match, length); TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); } int main(void) { UNITY_BEGIN(); RUN_TEST(test_check_match_basic_equal); RUN_TEST(test_check_match_match_minus_one_case); RUN_TEST(test_check_match_length_one); return UNITY_END(); }