| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- #include <assert.h> //header file to verify the assumptions made by the program and print a diagnostic message if the assumption is false
- #include "src/config.h"
- #include "src/controller/controller.h"
- #include "src/i2c_comm/i2c_hal.h"
- #include <stdio.h>
- #include "mock_setup.h"
- //Testing:: Declare the extern variables for mock testing, comment it out in production
- txPacket tx_packet;
- rxPacket rx_packet;
- //mock I2C HAL functions which will be used to test the controller functions, since the target board is not available
- bool mock_i2c_write(uint8_t const TARGET_ADDRESS)
- {
- printf("[I2C Mock]Write to target 0x%02X\n", TARGET_ADDRESS);
- // Mock implementation of I2C write
- return true; // Simulate successful write
- }
- bool mock_i2c_read(uint8_t const TARGET_ADDRESS)
- {
- printf("[I2C Mock]Read from target 0x%02X\n", TARGET_ADDRESS);
- // Mock implementation of I2C read
- return true; // Simulate successful read
- }
- //Setting Current Mock:
- void test_Controller_SetCurrent()
- {
- // Set the current to a specific value
- int current = -1000; // Example value
-
- controller_SetCurrent(0x48, 0, current);
- //Debug
- printf("Mock command SET: 0x%02X\n", tx_packet.txBuffer[0]);
- printf("Mock slot_id: 0x%02X\n", tx_packet.txBuffer[1]);
- printf("Mock current: 0x%02X\n", tx_packet.txBuffer[2]);
- printf("Mock current: 0x%02X\n", tx_packet.txBuffer[3]);
- //assert() checks if the variable is true or false, if False the program gets aborted and a diagnostic message is printed
- assert(tx_packet.txBuffer[0] == CMD_SET_CURRENT);
- assert(tx_packet.txBuffer[1] == 0);
- //Current is sent in two bytes, as Little Endian format
- assert(tx_packet.txBuffer[2] == (current & 0xFF)); // LSB
- assert(tx_packet.txBuffer[3] == ((current >> 8) & 0xFF)); // MSB
- assert(tx_packet.txLen == 4); // 1 byte for command + 1 byte for slot_id + 1 byte for current
- printf("[TEST] Set Current: Passed\n");
- }
- void test_Controller_GetBatteryMeasurement()
- {
- // Mock data for battery measurement
- BatteryMeasurement measurement;
- measurement.voltage = 350; // Example voltage
- measurement.current = 10; // Example current
- measurement.temperature = 25; // Example temperature
- measurement.slot_state = SLOT_STATE_OK; // Example slot state
- // Check if the function returns success
- rx_packet.rxBuffer[0] = measurement.voltage & 0xFF; // Voltage LSB
- rx_packet.rxBuffer[1] = (measurement.voltage >> 8) & 0xFF; // Voltage MSB
- rx_packet.rxBuffer[2] = measurement.current & 0xFF; // Current LSB
- rx_packet.rxBuffer[3] = (measurement.current >> 8) & 0xFF; // Current MSB
- rx_packet.rxBuffer[4] = measurement.temperature & 0xFF; // Temperature LSB
- rx_packet.rxBuffer[5] = (measurement.temperature >> 8) & 0xFF; // Temperature MSB
- rx_packet.rxBuffer[6] = measurement.slot_state & 0xFF; // Slot state LSB
- rx_packet.rxBuffer[7] = (measurement.slot_state >> 8) & 0xFF; // Slot state MSB
- rx_packet.rxLen = 6;
- // Call the function to get battery measurement
- bool result= controller_GetBatteryMeasurement(0x48, 0, &measurement);
- printf("Mock command GET: 0x%02X\n", tx_packet.txBuffer[0]);
- assert(result == true);
- assert(measurement.voltage == 350);
- assert(measurement.current == 10);
- assert(measurement.temperature == 25);
- assert(rx_packet.rxLen==6);
- printf("[TEST] Get Battery Measurement: Passed\n");
- }
- I2C_Mock_Interface i2c_mock_interface = {
- .write = mock_i2c_write,
- .read = mock_i2c_read,
- };
|