瀏覽代碼

Files added to mock the tests for Controller side GET and SET commands

namrota ghosh 7 月之前
父節點
當前提交
b0690a01b5
共有 2 個文件被更改,包括 96 次插入0 次删除
  1. 83 0
      mock_setup.c
  2. 13 0
      mock_setup.h

+ 83 - 0
mock_setup.c

@@ -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,
+};
+
+

+ 13 - 0
mock_setup.h

@@ -0,0 +1,13 @@
+#ifndef MOCK_SETUP_H
+#define MOCK_SETUP_H
+
+typedef struct{
+    bool (*write)(uint8_t const TARGET_ADDRESS);
+    bool (*read) (uint8_t const TARGET_ADDRESS);
+} I2C_Mock_Interface;
+
+void test_Controller_SetCurrent();
+void test_Controller_GetBatteryMeasurement();
+extern I2C_Mock_Interface i2c_mock_interface;
+
+#endif