mock_setup.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <assert.h> //header file to verify the assumptions made by the program and print a diagnostic message if the assumption is false
  2. #include "src/config.h"
  3. #include "src/controller/controller.h"
  4. #include "src/i2c_comm/i2c_hal.h"
  5. #include <stdio.h>
  6. #include "mock_setup.h"
  7. //Declare the extern variables for mock testing
  8. txPacket tx_packet;
  9. rxPacket rx_packet;
  10. //mock I2C HAL functions which will be used to test the controller functions
  11. bool mock_i2c_write(uint8_t const TARGET_ADDRESS)
  12. {
  13. printf("[I2C Mock]Write to target 0x%02X\n", TARGET_ADDRESS);
  14. // Mock implementation of I2C write
  15. return true; // Simulate successful write
  16. }
  17. bool mock_i2c_read(uint8_t const TARGET_ADDRESS)
  18. {
  19. printf("[I2C Mock]Read from target 0x%02X\n", TARGET_ADDRESS);
  20. // Mock implementation of I2C read
  21. return true; // Simulate successful read
  22. }
  23. //Setting Current Mock:
  24. void test_Controller_SetCurrent()
  25. {
  26. // Set the current to a specific value
  27. int current = -1000; // Example value
  28. controller_SetCurrent(0x48, 0, current);
  29. //Debug
  30. printf("Mock command: 0x%02X\n", tx_packet.txBuffer[0]);
  31. printf("Mock slot_id: 0x%02X\n", tx_packet.txBuffer[1]);
  32. printf("Mock current: 0x%02X\n", tx_packet.txBuffer[2]);
  33. printf("Mock current: 0x%02X\n", tx_packet.txBuffer[3]);
  34. //assert() checks if the variable is true or false, if False the program gets aborted and a diagnostic message is printed
  35. assert(tx_packet.txBuffer[0] == CMD_SET_CURRENT);
  36. assert(tx_packet.txBuffer[1] == 0);
  37. assert(tx_packet.txBuffer[2] == 0x18);
  38. assert(tx_packet.txBuffer[3] == 0xFC);
  39. assert(tx_packet.txLen == 4); // 1 byte for command + 1 byte for slot_id + 1 byte for current
  40. printf("[TEST] Set Current: Passed\n");
  41. }
  42. void test_Controller_GetBatteryMeasurement()
  43. {
  44. // Mock data for battery measurement
  45. BatteryMeasurement measurement;
  46. measurement.voltage = 350; // Example voltage
  47. measurement.current = 10; // Example current
  48. measurement.temperature = 25; // Example temperature
  49. // Check if the function returns success
  50. rx_packet.rxBuffer[0] = measurement.voltage & 0xFF; // Voltage LSB
  51. rx_packet.rxBuffer[1] = (measurement.voltage >> 8) & 0xFF; // Voltage MSB
  52. rx_packet.rxBuffer[2] = measurement.current & 0xFF; // Current LSB
  53. rx_packet.rxBuffer[3] = (measurement.current >> 8) & 0xFF; // Current MSB
  54. rx_packet.rxBuffer[4] = measurement.temperature & 0xFF; // Temperature LSB
  55. rx_packet.rxBuffer[5] = (measurement.temperature >> 8) & 0xFF; // Temperature MSB
  56. rx_packet.rxLen = 6;
  57. // Call the function to get battery measurement
  58. bool result= controller_GetBatteryMeasurement(0x48, 0, &measurement);
  59. assert(result == true);
  60. assert(measurement.voltage == 350);
  61. assert(measurement.current == 10);
  62. assert(measurement.temperature == 25);
  63. assert(rx_packet.rxLen==6);
  64. printf("[TEST] Get Battery Measurement: Passed\n");
  65. }
  66. I2C_Mock_Interface i2c_mock_interface = {
  67. .write = mock_i2c_write,
  68. .read = mock_i2c_read,
  69. };