diff --git a/src/spirit/include/adjust_duty_cycle.h b/src/spirit/include/adjust_duty_cycle.h index e4568c6..0055576 100644 --- a/src/spirit/include/adjust_duty_cycle.h +++ b/src/spirit/include/adjust_duty_cycle.h @@ -21,10 +21,31 @@ namespace spirit { * @note * Motor::State::Coast, Motor::State::Brake は必ずデューティー比を0として扱う */ -void adjust_duty_cycle(spirit::Motor::State target_state, float target_duty_cycle, float max_rise_delta, - float max_fall_delta, spirit::Motor::State current_state, float current_duty_cycle, - spirit::Motor::State& next_state, float& next_duty_cycle) +void adjust_duty_cycle(Motor::State target_state, float target_duty_cycle, float max_rise_delta, float max_fall_delta, + Motor::State current_state, float current_duty_cycle, Motor::State& next_state, + float& next_duty_cycle) { + /** + * @brief stateの範囲チェック + * @param type チェックしたい回転方向 + */ + auto within_state = [](Motor::State type) { + switch (type) { + case Motor::State::Coast: + case Motor::State::CW: + case Motor::State::CCW: + case Motor::State::Brake: + break; + default: + Error::get_instance().error(Error::Type::UnknownValue, 0, __FILE__, __func__, __LINE__, + "Unknown motor state (%d)", static_cast(type)); + return; + } + }; + + within_state(target_state); + within_state(current_state); + // 回転方向がCoast or Brakeの場合は現在のデューティー比を0として扱う if ((target_state == Motor::State::Coast) || (target_state == Motor::State::Brake)) { target_duty_cycle = 0.00F; diff --git a/tests/test_adjust_duty_cycle.cpp b/tests/test_adjust_duty_cycle.cpp index b61eaec..70487c9 100644 --- a/tests/test_adjust_duty_cycle.cpp +++ b/tests/test_adjust_duty_cycle.cpp @@ -1,5 +1,6 @@ #include +#include "Error.h" #include "Motor.h" #include "adjust_duty_cycle.h" @@ -374,5 +375,38 @@ TEST(AdjustDutyCycle, Normal) } } } +TEST(adjust_duty_cycle, AnomalyTest) +{ + Motor::State next_state; + float next_duty_cycle; + + // Error時に標準エラー出力に文字列が出力される + // 本当のエラー時にエラー出力させたいので、異常系のテスト中は標準エラー出力をキャプチャする + testing::internal::CaptureStderr(); + + Error& error = Error::get_instance(); + EXPECT_EQ(error.get_status(), Error::Status::Normal); + + /// @test target_stateが範囲外の時にエラーになることのテスト + error.reset(); + EXPECT_EQ(error.get_status(), Error::Status::Normal); + adjust_duty_cycle(static_cast(4), 0.00F, 0.50F, 0.50F, Motor::State::Coast, 0.00F, next_state, + next_duty_cycle); + EXPECT_EQ(error.get_status(), Error::Status::Error); + + /// @test current_stateが範囲外の時にエラーになることのテスト + error.reset(); + EXPECT_EQ(error.get_status(), Error::Status::Normal); + adjust_duty_cycle(Motor::State::Coast, 0.00F, 0.50F, 0.50F, static_cast(4), 0.00F, next_state, + next_duty_cycle); + EXPECT_EQ(error.get_status(), Error::Status::Error); + + /// @test target_stateとcurrent_stateが範囲外の時にエラーになることのテスト + error.reset(); + EXPECT_EQ(error.get_status(), Error::Status::Normal); + adjust_duty_cycle(static_cast(4), 0.00F, 0.50F, 0.50F, static_cast(4), 0.00F, + next_state, next_duty_cycle); + EXPECT_EQ(error.get_status(), Error::Status::Error); +} } // namespace