LCOV - code coverage report
Current view: top level - unit_tests/tests/util - test_closed_loop_controller.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 58 58 100.0 %
Date: 2024-05-26 17:59:41 Functions: 30 30 100.0 %

          Line data    Source code
       1             : #include "closed_loop_controller.h"
       2             : 
       3             : #include "gmock/gmock.h"
       4             : #include "gtest/gtest.h"
       5             : 
       6             : using ::testing::StrictMock;
       7             : using ::testing::Return;
       8             : using ::testing::Eq;
       9             : 
      10             : class TestController : public ClosedLoopController<float, float> {
      11             : public:
      12          10 :         MOCK_METHOD(expected<float>, getSetpoint, (), (override));
      13           8 :         MOCK_METHOD(expected<float>, observePlant, (), (override));
      14           6 :         MOCK_METHOD(expected<float>, getOpenLoop, (float setpoint), (override));
      15           4 :         MOCK_METHOD(expected<float>, getClosedLoop, (float setpoint, float observation), (override));
      16          10 :         MOCK_METHOD(void, setOutput, (expected<float> outputValue), (override));
      17             : };
      18             : 
      19           4 : TEST(ClosedLoopController, TestSetpoint) {
      20           1 :         StrictMock<TestController> controller;
      21             : 
      22             :         // If getSetpoint returns unexpected, no other functions should be called
      23           3 :         EXPECT_CALL(controller, getSetpoint())
      24           3 :                 .WillOnce(Return(unexpected));
      25             : 
      26             :         // And output should be called with unexpected
      27           1 :         EXPECT_CALL(controller, setOutput(Eq(unexpected)));
      28             : 
      29           1 :         controller.update();
      30           2 : }
      31             : 
      32           4 : TEST(ClosedLoopController, TestSetpointSuccessPlantFail) {
      33           1 :         StrictMock<TestController> controller;
      34             : 
      35             :         // If getSetpoint returns unexpected, no other functions should be called
      36           3 :         EXPECT_CALL(controller, getSetpoint())
      37           3 :                 .WillOnce(Return(25.0f));
      38             : 
      39             :         // Fail to observe plant - exec should stop there
      40           3 :         EXPECT_CALL(controller, observePlant())
      41           3 :                 .WillOnce(Return(unexpected));
      42             : 
      43             :         // And output should be called with unexpected
      44           1 :         EXPECT_CALL(controller, setOutput(Eq(unexpected)));
      45             : 
      46           1 :         controller.update();
      47           2 : }
      48             : 
      49           4 : TEST(ClosedLoopController, TestPlantSuccessOpenLoopFail) {
      50           1 :         StrictMock<TestController> controller;
      51             : 
      52             :         // Return valid setpoint
      53           3 :         EXPECT_CALL(controller, getSetpoint())
      54           3 :                 .WillOnce(Return(25.0f));
      55             : 
      56             :         // Return valid observation
      57           3 :         EXPECT_CALL(controller, observePlant())
      58           3 :                 .WillOnce(Return(75.0f));
      59             : 
      60             :         // Setpoint should get passed to open loop
      61             :         // It will fail, nothing else should be called
      62           3 :         EXPECT_CALL(controller, getOpenLoop(25.0f))
      63           3 :                 .WillOnce(Return(unexpected));
      64             : 
      65             :         // And output should be called with unexpected
      66           1 :         EXPECT_CALL(controller, setOutput(Eq(unexpected)));
      67             : 
      68           1 :         controller.update();
      69           2 : }
      70             : 
      71             : 
      72           4 : TEST(ClosedLoopController, TestPlantOpenLoopSuccessClosedLoopFail) {
      73           1 :         StrictMock<TestController> controller;
      74             : 
      75             :         // Return valid setpoint
      76           3 :         EXPECT_CALL(controller, getSetpoint())
      77           3 :                 .WillOnce(Return(25.0f));
      78             : 
      79             :         // Return valid observation
      80           3 :         EXPECT_CALL(controller, observePlant())
      81           3 :                 .WillOnce(Return(75.0f));
      82             : 
      83             :         // Setpoint should get passed to open loop
      84             :         // Return something valid
      85           3 :         EXPECT_CALL(controller, getOpenLoop(25.0f))
      86           3 :                 .WillOnce(Return(37.0f));
      87             : 
      88             :         // Setpoint & observation should get passed
      89             :         // But return invalid
      90           3 :         EXPECT_CALL(controller, getClosedLoop(25.0f, 75.0f))
      91           3 :                 .WillOnce(Return(unexpected));
      92             : 
      93             :         // And output should be called with unexpected
      94           1 :         EXPECT_CALL(controller, setOutput(Eq(unexpected)));
      95             : 
      96           1 :         controller.update();
      97           2 : }
      98             : 
      99           4 : TEST(ClosedLoopController, TestAllSuccess) {
     100           1 :         StrictMock<TestController> controller;
     101             : 
     102             :         // Return valid setpoint
     103           3 :         EXPECT_CALL(controller, getSetpoint())
     104           3 :                 .WillOnce(Return(25.0f));
     105             : 
     106             :         // Return valid observation
     107           3 :         EXPECT_CALL(controller, observePlant())
     108           3 :                 .WillOnce(Return(75.0f));
     109             : 
     110             :         // Setpoint should get passed to open loop
     111             :         // Return something valid
     112           3 :         EXPECT_CALL(controller, getOpenLoop(25.0f))
     113           3 :                 .WillOnce(Return(37.0f));
     114             : 
     115             :         // Setpoint & observation should get passed
     116             :         // Return something valid
     117           3 :         EXPECT_CALL(controller, getClosedLoop(25.0f, 75.0f))
     118           3 :                 .WillOnce(Return(22.0f));
     119             : 
     120             :         // Output should get called with sum of open & closed loop
     121           1 :         EXPECT_CALL(controller, setOutput(expected<float>(59.0f)));
     122             : 
     123           1 :         controller.update();
     124           2 : }

Generated by: LCOV version 1.14