Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
test_BatteryController.cpp
Go to the documentation of this file.
1
15
16#include "BatteryController.hpp"
17#include "MockI2CController.hpp"
18#include <gmock/gmock.h>
19#include <gtest/gtest.h>
20
21using ::testing::_;
22using ::testing::NiceMock;
23using ::testing::Return;
24
32class BatteryControllerTest : public ::testing::Test
33{
34protected:
35 NiceMock<MockI2CController> mockI2C;
37
38 void SetUp() override { batteryController = new BatteryController(&mockI2C); }
39
40 void TearDown() override { delete batteryController; }
41};
42
49TEST_F(BatteryControllerTest, Initialization_CallsCalibration)
50{
51 EXPECT_CALL(mockI2C, writeRegister(0x05, 4096)).Times(1);
52 delete batteryController; // Force constructor to be re-run
53 batteryController = new BatteryController(&mockI2C);
54}
55
66TEST_F(BatteryControllerTest, GetBatteryPercentage_CorrectCalculation)
67{
68 EXPECT_CALL(mockI2C, readRegister(0x02)).WillOnce(Return(1000)); // Bus voltage raw value
69 EXPECT_CALL(mockI2C, readRegister(0x01)).WillOnce(Return(100)); // Shunt voltage raw value
70
71 float busVoltage = (1000 >> 3) * 0.004f;
72 float shuntVoltage = 100 * 0.01f;
73 float loadVoltage = busVoltage + shuntVoltage;
74 float expectedPercentage = (loadVoltage - 6.0f) / 2.4f * 100.0f;
75
76 if (expectedPercentage > 100.0f)
77 expectedPercentage = 100.0f;
78 if (expectedPercentage < 0.0f)
79 expectedPercentage = 0.0f;
80
81 float percentage = batteryController->getBatteryPercentage();
82 EXPECT_NEAR(percentage, expectedPercentage, 0.1f);
83}
Definition of the BatteryController class.
File containing Mock classes to test the controller of the I2C module.
Test fixture for testing the BatteryController class.
BatteryController * batteryController
NiceMock< MockI2CController > mockI2C
Class that manages the battery of the vehicle.
TEST_F(BatteryControllerTest, Initialization_CallsCalibration)
Ensures that the battery controller initializes correctly.