| #include "../../unity/unity.h" |
| #include <stdlib.h> |
| #include <stdint.h> |
| #include <string.h> |
| #include <stdio.h> |
|
|
| void setUp(void) { |
| |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| |
| static void encode_byte(uint8_t byte, char out[9]) { |
| char in = (char)byte; |
| |
| base2lsbf_encode(&in, 1, out, 8); |
| out[8] = '\0'; |
| } |
|
|
| |
| 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) { |
| |
| char outbuf[16]; |
| memset(outbuf, 'X', sizeof(outbuf)); |
|
|
| |
| 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"); |
| expect_bits_for_byte(0xAAu, "01010101"); |
| } |
|
|
| void test_base2lsbf_encode_multi_byte_concatenation(void) { |
| const unsigned char in[2] = { 0xABu, 0xCDu }; |
| char out[16]; |
| |
| 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) { |
| |
| 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(); |
| } |