Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
test_MileageCalculator.cpp
Go to the documentation of this file.
1
15
16#include <QElapsedTimer>
17#include <QTest>
18#include "MileageCalculator.hpp"
19#include <gtest/gtest.h>
20
28class MileageCalculatorTest : public ::testing::Test
29{
30protected:
32};
33
43TEST_F(MileageCalculatorTest, AddSpeed_DoesNotCrash)
44{
45 calculator.addSpeed(50.0);
46 calculator.addSpeed(80.0);
47 EXPECT_NO_THROW(calculator.calculateDistance());
48}
49
59TEST_F(MileageCalculatorTest, CalculateDistance_NoSpeeds_ReturnsZero)
60{
61 EXPECT_DOUBLE_EQ(calculator.calculateDistance(), 0.0);
62}
63
73TEST_F(MileageCalculatorTest, CalculateDistance_ZeroSpeed_ReturnsZero)
74{
75 calculator.addSpeed(0.0);
76 QTest::qWait(100); // Wait before next speed is logged
77 calculator.addSpeed(0.0);
78 EXPECT_DOUBLE_EQ(calculator.calculateDistance(), 0.0);
79}
80
90TEST_F(MileageCalculatorTest, CalculateDistance_BasicCalculation)
91{
92 QElapsedTimer timer;
93
94 timer.start();
95 QTest::qWait(100); // Reduced wait time
96 calculator.addSpeed(60.0); // 60 km/h (16.67 m/s)
97 qint64 elapsed1 = timer.restart(); // Measure interval before next speed
98
99 QTest::qWait(100); // Reduced wait time
100 calculator.addSpeed(90.0); // 90 km/h (25.0 m/s)
101 qint64 elapsed2 = timer.restart(); // Measure interval before next speed
102
103 double distance = calculator.calculateDistance();
104
105 // Compute expected distance using elapsed time BEFORE each speed entry
106 double expected = ((60.0 / 3.6) * (elapsed1 / 1000.0)) + ((90.0 / 3.6) * (elapsed2 / 1000.0));
107
108 EXPECT_NEAR(distance, expected, 0.1);
109}
110
120TEST_F(MileageCalculatorTest, CalculateDistance_MultipleSpeeds)
121{
122 QElapsedTimer timer;
123
124 timer.start();
125 QTest::qWait(50);
126 calculator.addSpeed(30.0); // 30 km/h (8.33 m/s)
127 qint64 elapsed1 = timer.restart();
128
129 QTest::qWait(75);
130 calculator.addSpeed(50.0); // 50 km/h (13.89 m/s)
131 qint64 elapsed2 = timer.restart();
132
133 QTest::qWait(50);
134 calculator.addSpeed(80.0); // 80 km/h (22.22 m/s)
135 qint64 elapsed3 = timer.restart();
136
137 QTest::qWait(75);
138 calculator.addSpeed(100.0); // 100 km/h (27.78 m/s)
139 qint64 elapsed4 = timer.restart();
140
141 double distance = calculator.calculateDistance();
142
143 // Compute expected distance using measured intervals
144 double expected = ((30.0 / 3.6) * (elapsed1 / 1000.0)) + ((50.0 / 3.6) * (elapsed2 / 1000.0))
145 + ((80.0 / 3.6) * (elapsed3 / 1000.0))
146 + ((100.0 / 3.6) * (elapsed4 / 1000.0));
147
148 EXPECT_NEAR(distance, expected, 0.1);
149}
Definition of the MileageCalculator class.
Test fixture for testing the MileageCalculator class.
Class that calculates the total distance traveled based on speed measurements.
TEST_F(MileageCalculatorTest, AddSpeed_DoesNotCrash)
Ensures that the mileage calculator does not crash when adding speeds.