#include "../../unity/unity.h" #include #include #include #include void setUp(void) { /* Setup code here, or leave empty */ } void tearDown(void) { /* Cleanup code here, or leave empty */ } /* Helper: encode single byte and return a null-terminated string in out[9]. */ static void encode_byte(uint8_t byte, char out[9]) { char in = (char)byte; /* base2lsbf_encode writes exactly 8 chars for one input byte when outlen>=8 */ base2lsbf_encode(&in, 1, out, 8); out[8] = '\0'; } /* Helper: reconstruct byte value from LSB-first bit string of length 8. */ static uint8_t bits_to_byte(const char bits[8]) { uint8_t v = 0; for (int i = 0; i < 8; i++) { if (bits[i] == '1') v |= (uint8_t)(1u << i); } return v; } static void expect_bits_for_byte(uint8_t b, const char *expected) { char out[9]; encode_byte(b, out); TEST_ASSERT_EQUAL_STRING(expected, out); } void test_base2lsbf_encode_zero_length_input(void) { /* Ensure no writes occur for inlen==0 */ char outbuf[16]; memset(outbuf, 'X', sizeof(outbuf)); /* Call with inlen=0; outlen can be any value, function should not write */ base2lsbf_encode((const char *)"", 0, outbuf, 16); for (size_t i = 0; i < sizeof(outbuf); i++) { TEST_ASSERT_EQUAL_CHAR('X', outbuf[i]); } } void test_base2lsbf_encode_00_all_zeros(void) { expect_bits_for_byte(0x00u, "00000000"); } void test_base2lsbf_encode_ff_all_ones(void) { expect_bits_for_byte(0xFFu, "11111111"); } void test_base2lsbf_encode_01_and_80_edges(void) { expect_bits_for_byte(0x01u, "10000000"); expect_bits_for_byte(0x80u, "00000001"); } void test_base2lsbf_encode_f0_and_0f_nibbles(void) { expect_bits_for_byte(0xF0u, "00001111"); expect_bits_for_byte(0x0Fu, "11110000"); } void test_base2lsbf_encode_55_and_aa_patterns(void) { expect_bits_for_byte(0x55u, "10101010"); /* 01010101 msb->lsb */ expect_bits_for_byte(0xAAu, "01010101"); /* 10101010 msb->lsb */ } void test_base2lsbf_encode_multi_byte_concatenation(void) { const unsigned char in[2] = { 0xABu, 0xCDu }; /* 0xAB=10101011, 0xCD=11001101 */ char out[16]; /* Expect: 0xAB -> 11010101, 0xCD -> 10110011 */ const char expected[16] = { '1','1','0','1','0','1','0','1', '1','0','1','1','0','0','1','1' }; base2lsbf_encode((const char *)in, 2, out, 16); TEST_ASSERT_EQUAL_MEMORY(expected, out, 16); } void test_base2lsbf_encode_all_byte_values_roundtrip_property(void) { char out[9]; for (int b = 0; b <= 255; b++) { encode_byte((uint8_t)b, out); uint8_t reconstructed = bits_to_byte(out); if (reconstructed != (uint8_t)b) { /* Provide diagnostic */ char msg[128]; snprintf(msg, sizeof msg, "Mismatch for 0x%02X: got '%s' -> 0x%02X", b, out, reconstructed); TEST_FAIL_MESSAGE(msg); } } } int main(void) { UNITY_BEGIN(); RUN_TEST(test_base2lsbf_encode_zero_length_input); RUN_TEST(test_base2lsbf_encode_00_all_zeros); RUN_TEST(test_base2lsbf_encode_ff_all_ones); RUN_TEST(test_base2lsbf_encode_01_and_80_edges); RUN_TEST(test_base2lsbf_encode_f0_and_0f_nibbles); RUN_TEST(test_base2lsbf_encode_55_and_aa_patterns); RUN_TEST(test_base2lsbf_encode_multi_byte_concatenation); RUN_TEST(test_base2lsbf_encode_all_byte_values_roundtrip_property); return UNITY_END(); }