|
|
@@ -0,0 +1,83 @@
|
|
|
+#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"
|
|
|
+
|
|
|
+//Declare the extern variables for mock testing
|
|
|
+txPacket tx_packet;
|
|
|
+rxPacket rx_packet;
|
|
|
+
|
|
|
+//mock I2C HAL functions which will be used to test the controller functions
|
|
|
+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: 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);
|
|
|
+ assert(tx_packet.txBuffer[2] == 0x18);
|
|
|
+ assert(tx_packet.txBuffer[3] == 0xFC);
|
|
|
+ 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
|
|
|
+
|
|
|
+ // 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.rxLen = 6;
|
|
|
+
|
|
|
+ // Call the function to get battery measurement
|
|
|
+ bool result= controller_GetBatteryMeasurement(0x48, 0, &measurement);
|
|
|
+
|
|
|
+
|
|
|
+ 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,
|
|
|
+};
|
|
|
+
|
|
|
+
|