diff --git a/.vscode/settings.json b/.vscode/settings.json index 26366da..9f2aa53 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "files.associations": { - "decode.h": "c" + "decode.h": "c", + "unity_test_module.h": "c" }, "cmake.sourceDirectory": "/home/rens/files/T2/HC/holyc-lang/src" } \ No newline at end of file diff --git a/C/C5 and C6 Adidas/build/main_test b/C/C5 and C6 Adidas/build/main_test old mode 100644 new mode 100755 index 6dc41e0..6115af8 Binary files a/C/C5 and C6 Adidas/build/main_test and b/C/C5 and C6 Adidas/build/main_test differ diff --git a/C/C5 and C6 Adidas/shared/channel.c b/C/C5 and C6 Adidas/shared/channel.c index bc14fb8..a0cc6e6 100644 --- a/C/C5 and C6 Adidas/shared/channel.c +++ b/C/C5 and C6 Adidas/shared/channel.c @@ -1,10 +1,16 @@ #include "channel.h" #include -// add here your implementation -void channel_init(){ +#include +void channel_init(){ + srand((unsigned int)time(NULL)); } uint8_t channel_change_one_random_bit(uint8_t value){ - + // Generate random bit position (0-7) + uint8_t bit_position = rand() % 8; + uint8_t bit_mask = 1 << bit_position; + // XOR with the mask to flip the bit + // 01101010 with mask 00000100 becomes 01101110 + return value ^ bit_mask; } \ No newline at end of file diff --git a/C/C5 and C6 Adidas/shared/decode.c b/C/C5 and C6 Adidas/shared/decode.c index 725ab8b..dff285a 100644 --- a/C/C5 and C6 Adidas/shared/decode.c +++ b/C/C5 and C6 Adidas/shared/decode.c @@ -5,9 +5,9 @@ uint8_t decode_combine_nibbles(uint8_t high, uint8_t low){ uint8_t byte; uint8_t high_nibble; uint8_t low_nibble; - decode_byte(high,high_nibble); - decode_byte(low,low_nibble); - combine_nibles_to_byte(high_nibble, low_nibble, byte); + decode_byte(high,&high_nibble); + decode_byte(low,&low_nibble); + combine_nibles_to_byte(high_nibble, low_nibble, &byte); return byte; } diff --git a/C/C5 and C6 Adidas/test/channel_test.c b/C/C5 and C6 Adidas/test/channel_test.c index 86529e8..23b35a4 100644 --- a/C/C5 and C6 Adidas/test/channel_test.c +++ b/C/C5 and C6 Adidas/test/channel_test.c @@ -6,26 +6,28 @@ // I rather dislike keeping line numbers updated, so I made my own macro to ditch the line number #define MY_RUN_TEST(func) RUN_TEST(func, 0) -extern void channel_setUp(void) -{ - // This is run before EACH test +extern void channel_setUp(void){ + channel_init(); +} +extern void channel_tearDown(void) {} + +void test_channel_all_zero_test(){ + uint8_t value = 0x00; + uint8_t result = channel_change_one_random_bit(value); + TEST_ASSERT_NOT_EQUAL(value, result); } -extern void channel_tearDown(void) -{ - // This is run after EACH test +void test_channel_all_one_test(){ + uint8_t value = 0xFF; + uint8_t result = channel_change_one_random_bit(value); + TEST_ASSERT_NOT_EQUAL(value, result); } -static void test_channel(void) -{ - TEST_ASSERT_EQUAL(1, 0); -} - -void run_channel_tests() -{ +void run_channel_tests(){ UnityRegisterSetupTearDown( channel_setUp, channel_tearDown); - MY_RUN_TEST(test_channel); + MY_RUN_TEST(test_channel_all_zero_test); + MY_RUN_TEST(test_channel_all_one_test); UnityUnregisterSetupTearDown(); } diff --git a/C/C5 and C6 Adidas/test/decode_test.c b/C/C5 and C6 Adidas/test/decode_test.c index 0e08dc0..6e608a0 100644 --- a/C/C5 and C6 Adidas/test/decode_test.c +++ b/C/C5 and C6 Adidas/test/decode_test.c @@ -5,26 +5,61 @@ // I rather dislike keeping line numbers updated, so I made my own macro to ditch the line number #define MY_RUN_TEST(func) RUN_TEST(func, 0) -extern void decode_setUp(void) -{ - // This is run before EACH test +extern void decode_setUp(void) {} +extern void decode_tearDown(void) {} +// Format: [0][d3][d2][d1][d0][p2][p1][p0] (MSB first) + +//general decode combine nibbles test +void test_decode_get_nibbles_normal(void){ + uint8_t high = 0b01101100, low = 0b01001001; + // high data: 1101 or 0xD and low data: 1001 or 0x9 + TEST_ASSERT_EQUAL(0xD9, decode_combine_nibbles(high, low)); // so the result should be 0b11011001 or 0xD9 } - -extern void decode_tearDown(void) -{ - // This is run after EACH test +void test_decode_get_nibbles_zero(void){ + uint8_t high = 0b00000000, low = 0b00000000; + // high data: 0000 or 0x0 and low data: 0000 or 0x0 + TEST_ASSERT_EQUAL(0x00, decode_combine_nibbles(high, low)); // so the result should be 0b00000000 or 0x00 } - -void test_decode(void) -{ - TEST_ASSERT_EQUAL(1, 0); +void test_decode_get_nibbles_all_ones(void){ + uint8_t high = 0b11111111, low = 0b11111111; + // high data: 1111 or 0xF and low data: 1111 or 0xF + TEST_ASSERT_EQUAL(0xFF, decode_combine_nibbles(high, low)); // so the result should be 0b11111111 or 0xFF } +void test_decode_MSB_flip (void){ + uint8_t high = 0b11101100, low = 0b11001001; + // high data: 1101 or 0xD and low data: 1001 or 0x9 + // but the MSB is flipped + TEST_ASSERT_EQUAL(0xD9, decode_combine_nibbles(high, low)); // so the result should be 0b11011001 or 0xD9 +} +// +// parity bit testing +void test_decode_databit_flip (void){ + uint8_t wrong_data = 0b01001100; // this should be 0b01101100 or unparityd 0b1101, 0xD + uint8_t right_data; // this is the correct data + decode_byte(wrong_data, &right_data); + TEST_ASSERT_EQUAL(0xD, right_data); // so the result should be 0b01101100 or 0xD +} +void test_decode_common_databit_flip (void){ // the common databit is d0 + uint8_t wrong_data = 0b01100100; // this should be 0b01101100 or unparityd 0b1101, 0xD + uint8_t right_data; // this is the correct data + decode_byte(wrong_data, &right_data); + TEST_ASSERT_EQUAL(0xD, right_data); // so the result should be 0b01101100 or 0xD +} +void test_decode_parity_flip(void){ + uint8_t wrong_data = 0b01101101; // this should be 0b01101100 or unparityd 0b1101, 0xD + uint8_t right_data; // this is the correct data + decode_byte(wrong_data, &right_data); + TEST_ASSERT_EQUAL(0xD, right_data); // so the result should be 0b01101100 or 0xD +} +// +void run_decode_tests(){ + UnityRegisterSetupTearDown(decode_setUp, decode_tearDown); -void run_decode_tests() -{ - UnityRegisterSetupTearDown( decode_setUp, decode_tearDown); - - MY_RUN_TEST(test_decode); + MY_RUN_TEST(test_decode_get_nibbles_normal); + MY_RUN_TEST(test_decode_get_nibbles_zero); + MY_RUN_TEST(test_decode_get_nibbles_all_ones); + MY_RUN_TEST(test_decode_MSB_flip); + MY_RUN_TEST(test_decode_databit_flip); UnityUnregisterSetupTearDown(); }