#include "unity.h"
#include "unity_test_module.h"
#include "decode.h"

// 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) {}
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
}
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_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
}
//
// mutiple bit flip test
void test_decode_multi_bit_flip(void){
    uint8_t wrong_data = 0b01111101; // 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_NOT_EQUAL(0xD, right_data); // so the result should be 0b01101100 or 0xD
}
//
void run_decode_tests(){
    UnityRegisterSetupTearDown(decode_setUp, decode_tearDown);

    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);
    MY_RUN_TEST(test_decode_common_databit_flip);
    MY_RUN_TEST(test_decode_parity_flip);
    MY_RUN_TEST(test_decode_multi_bit_flip);

    UnityUnregisterSetupTearDown();
}
