#include "../../unity/unity.h" #include #include #include #include /* base2msbf_encode is defined in the program source and included before this file. */ void setUp(void) { /* Setup code here, or leave empty */ } void tearDown(void) { /* Cleanup code here, or leave empty */ } /* Helper: manually encode a single byte to MSB-first bit string */ static void byte_to_bits_msb(unsigned char c, char out[9]) { for (int i = 0; i < 8; i++) { out[i] = (c & 0x80) ? '1' : '0'; c <<= 1; } out[8] = '\0'; } /* Helper: encode a buffer using the reference helper above */ static char *bytes_to_bits_msb(const unsigned char *in, size_t inlen) { size_t outlen = inlen * 8; char *res = (char *)malloc(outlen + 1); TEST_ASSERT_NOT_NULL(res); char *p = res; for (size_t i = 0; i < inlen; i++) { char tmp[9]; byte_to_bits_msb(in[i], tmp); memcpy(p, tmp, 8); p += 8; } res[outlen] = '\0'; return res; } void test_base2msbf_encode_empty_input_no_output(void) { char outbuf[16]; /* prefill with sentinel */ for (size_t i = 0; i < sizeof(outbuf); i++) outbuf[i] = 'X'; const char inbuf[] = ""; /* inlen = 0 */ base2msbf_encode(inbuf, (idx_t)0, outbuf, (idx_t)8); /* No output should be written; buffer remains unchanged */ for (size_t i = 0; i < sizeof(outbuf); i++) { TEST_ASSERT_EQUAL_CHAR('X', outbuf[i]); } } void test_base2msbf_encode_single_byte_zero(void) { unsigned char inbuf[1] = {0x00}; char outbuf[8 + 1]; memset(outbuf, 0, sizeof(outbuf)); base2msbf_encode((const char *)inbuf, (idx_t)1, outbuf, (idx_t)8); outbuf[8] = '\0'; TEST_ASSERT_EQUAL_STRING("00000000", outbuf); } void test_base2msbf_encode_single_byte_ff(void) { unsigned char inbuf[1] = {0xFF}; char outbuf[8 + 1]; memset(outbuf, 0, sizeof(outbuf)); base2msbf_encode((const char *)inbuf, (idx_t)1, outbuf, (idx_t)8); outbuf[8] = '\0'; TEST_ASSERT_EQUAL_STRING("11111111", outbuf); } void test_base2msbf_encode_single_byte_a5(void) { unsigned char inbuf[1] = {0xA5}; /* 10100101 */ char outbuf[8 + 1]; memset(outbuf, 0, sizeof(outbuf)); base2msbf_encode((const char *)inbuf, (idx_t)1, outbuf, (idx_t)8); outbuf[8] = '\0'; TEST_ASSERT_EQUAL_STRING("10100101", outbuf); } void test_base2msbf_encode_single_byte_80(void) { unsigned char inbuf[1] = {0x80}; /* 10000000 */ char outbuf[8 + 1]; memset(outbuf, 0, sizeof(outbuf)); base2msbf_encode((const char *)inbuf, (idx_t)1, outbuf, (idx_t)8); outbuf[8] = '\0'; TEST_ASSERT_EQUAL_STRING("10000000", outbuf); } void test_base2msbf_encode_two_bytes_Hi(void) { const unsigned char inbuf[2] = {'H', 'i'}; /* 0x48, 0x69 */ /* Expected: 0x48 -> 01001000, 0x69 -> 01101001 */ const char *expected = "0100100001101001"; char outbuf[16 + 1]; memset(outbuf, 0, sizeof(outbuf)); base2msbf_encode((const char *)inbuf, (idx_t)2, outbuf, (idx_t)16); outbuf[16] = '\0'; TEST_ASSERT_EQUAL_STRING(expected, outbuf); } void test_base2msbf_encode_truncate_outlen_multiple_of_8(void) { const unsigned char inbuf[3] = {'A', 'B', 'C'}; /* 0x41, 0x42, 0x43 */ /* Expect only first two bytes encoded because outlen==16 */ const char *expected_first2 = "0100000101000010"; /* 'A' then 'B' */ char outbuf[16 + 2]; memset(outbuf, 0, sizeof(outbuf)); outbuf[16] = 'Z'; /* sentinel */ outbuf[17] = '\0'; base2msbf_encode((const char *)inbuf, (idx_t)3, outbuf, (idx_t)16); outbuf[16] = '\0'; TEST_ASSERT_EQUAL_STRING(expected_first2, outbuf); /* Ensure sentinel not overwritten beyond outlen */ TEST_ASSERT_EQUAL_CHAR('Z', outbuf[16]); } void test_base2msbf_encode_outlen_zero_no_write(void) { const unsigned char inbuf[2] = {0x01, 0xFF}; char outbuf[4]; for (size_t i = 0; i < sizeof(outbuf); i++) outbuf[i] = 'Q'; base2msbf_encode((const char *)inbuf, (idx_t)2, outbuf, (idx_t)0); for (size_t i = 0; i < sizeof(outbuf); i++) { TEST_ASSERT_EQUAL_CHAR('Q', outbuf[i]); } } void test_base2msbf_encode_reference_compare_randomish_pattern(void) { const unsigned char inbuf[] = {0x00, 0xFF, 0x12, 0xAB, 0x7E, 0x80, 0x01, 0xC3}; size_t inlen = sizeof(inbuf); size_t outlen = inlen * 8; char *expected = bytes_to_bits_msb(inbuf, inlen); char *outbuf = (char *)malloc(outlen + 1); TEST_ASSERT_NOT_NULL(outbuf); memset(outbuf, 0, outlen + 1); base2msbf_encode((const char *)inbuf, (idx_t)inlen, outbuf, (idx_t)outlen); outbuf[outlen] = '\0'; TEST_ASSERT_EQUAL_STRING(expected, outbuf); free(expected); free(outbuf); } int main(void) { UNITY_BEGIN(); RUN_TEST(test_base2msbf_encode_empty_input_no_output); RUN_TEST(test_base2msbf_encode_single_byte_zero); RUN_TEST(test_base2msbf_encode_single_byte_ff); RUN_TEST(test_base2msbf_encode_single_byte_a5); RUN_TEST(test_base2msbf_encode_single_byte_80); RUN_TEST(test_base2msbf_encode_two_bytes_Hi); RUN_TEST(test_base2msbf_encode_truncate_outlen_multiple_of_8); RUN_TEST(test_base2msbf_encode_outlen_zero_no_write); RUN_TEST(test_base2msbf_encode_reference_compare_randomish_pattern); return UNITY_END(); }