TDD - You can write code without actual hardware (Part II)

TDD - You can write code without actual hardware (Part II)

This post is an extension of "TDD - You can write code without actual hardware (Part I)". Previously, eeprom_controller.c includes only the read-byte function. Let's start to add a write-byte test in test_eeprom_controller.c. After writing one byte in a given address of EEPROM, read back the same address to verify.

void test_eeprom_controller_write_byte(void)
{
    uint32_t test_address = 0x0;
    const uint8_t test_value = 0xAA;
    uint8_t read_data;
    bool op1 = eeprom_write_byte(test_address, &test_value);
    bool op2 = eeprom_read_byte(test_address, &read_data);
    TEST_ASSERT_EQUAL(true, op1);
    TEST_ASSERT_EQUAL(true, op2);
    TEST_ASSERT_EQUAL(test_value, read_data);
}

The test will fail because eeprom_write_byte is currently not implemented. Let's add the function prototype in eeprom_controller.h and then implement minimum code to pass the test in eeprom_controller.c.

bool eeprom_write_byte(uint32_t address, const uint8_t* w_data)
{
    bool op_status = true;
    flash_write(address, w_data, 1);

    return op_status;
}

Similarly from eeprom_read_byte, a parameter check was added to the function. Let's add parameter check test cases in test_eeprom_controller_write_byte.

void test_eeprom_controller_write_byte(void)
{
    uint32_t test_address = 0x0;
    const uint8_t test_value = 0xAA;
    uint8_t read_data;
    bool op1 = eeprom_write_byte(test_address, &test_value);
    bool op2 = eeprom_read_byte(test_address, &read_data);
    TEST_ASSERT_EQUAL(true, op1);
    TEST_ASSERT_EQUAL(true, op2);
    TEST_ASSERT_EQUAL(test_value, read_data);

    //parameter check tests
    bool op3 = eeprom_write_byte(EEPROM_SIZE, &test_value);
    bool op4 = eeprom_write_byte(test_address, 0);
    TEST_ASSERT_EQUAL(false, op3);
    TEST_ASSERT_EQUAL(false, op4);
}

This test will fail because the current implementation only returns true. Let's modify eeprom_write_byte function in eeprom_controller.c.

bool eeprom_write_byte(uint32_t address, const uint8_t* w_data)
{
    bool op_status = false;

    if(w_data != 0 && address < EEPROM_SIZE)
    {
        flash_write(address, w_data, 1);
        op_status = true;
    }
}

The next article will continuously add a test case for writing multiple bytes and verifying with the read multiple bytes function.

Did you find this article valuable?

Support Hyunwoo Choi by becoming a sponsor. Any amount is appreciated!