Skip to content

Commit

Permalink
✅ (tests): Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Feb 20, 2023
1 parent 3693d37 commit e94e355
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 67 deletions.
9 changes: 9 additions & 0 deletions libs/IMUKit/tests/IMUKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,17 @@ TEST_F(IMUKitTest, setOrigin)

TEST_F(IMUKitTest, onDataReady)
{
testing::MockFunction<void()> mock_callback {};

auto func = [&] { mock_callback.Call(); };
imukit.onEulerAnglesRdy(func);

const auto data_initial = interface::LSM6DSOX::SensorData {
.xl = {.x = 0.F, .y = 0.F, .z = 0.F}, .gy = {.x = 0.F, .y = 0.F, .z = 0.F }
};

EXPECT_CALL(mock_callback, Call);

mock_lsm6dox.call_drdy_callback(data_initial);

const auto angles_initial = imukit.getEulerAngles();
Expand All @@ -80,6 +87,8 @@ TEST_F(IMUKitTest, onDataReady)
.xl = {.x = 1.F, .y = 2.F, .z = 3.F}, .gy = {.x = 1.F, .y = 2.F, .z = 3.F }
};

EXPECT_CALL(mock_callback, Call);

mock_lsm6dox.call_drdy_callback(data_updated);

auto angles_updated = imukit.getEulerAngles();
Expand Down
119 changes: 65 additions & 54 deletions libs/MotionKit/tests/MotionKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "mocks/leka/CoreMotor.h"
#include "mocks/leka/LSM6DSOX.h"
#include "mocks/leka/Timeout.h"
#include "stubs/leka/EventLoopKit.h"

using namespace leka;

Expand All @@ -27,15 +26,9 @@ class MotionKitTest : public ::testing::Test
protected:
MotionKitTest() = default;

void SetUp() override
{
imukit.init();
motion.init();
}
void SetUp() override { imukit.init(); }
// void TearDown() override {}

stub::EventLoopKit stub_event_loop_motion {};

mock::CoreMotor mock_motor_left {};
mock::CoreMotor mock_motor_right {};

Expand All @@ -48,22 +41,16 @@ class MotionKitTest : public ::testing::Test

IMUKit imukit {lsm6dsox};

MotionKit motion {mock_motor_left, mock_motor_right, imukit, stub_event_loop_motion, mock_timeout};
MotionKit motion {mock_motor_left, mock_motor_right, imukit, mock_timeout};
};

TEST_F(MotionKitTest, initialization)
{
ASSERT_NE(&motion, nullptr);
}

TEST_F(MotionKitTest, registerMockCallbackAndRotate)
TEST_F(MotionKitTest, rotateClockwise)
{
auto mock_function_motion = MockFunction<void(void)> {};
auto loop_motion = [&] { mock_function_motion.Call(); };

EXPECT_CALL(lsm6dsox, setPowerMode(interface::LSM6DSOX::PowerMode::Normal)).Times(1);
EXPECT_CALL(mock_function_motion, Call()).Times(1);

EXPECT_CALL(mock_timeout, stop).Times(1);
EXPECT_CALL(mock_motor_left, stop).Times(1);
EXPECT_CALL(mock_motor_right, stop).Times(1);
Expand All @@ -74,61 +61,78 @@ TEST_F(MotionKitTest, registerMockCallbackAndRotate)
EXPECT_CALL(mock_motor_left, spin(Rotation::clockwise, 1)).Times(1);
EXPECT_CALL(mock_motor_right, spin(Rotation::clockwise, 1)).Times(1);

stub_event_loop_motion.registerCallback(loop_motion);
motion.rotate(1, Rotation::clockwise);

const auto imu_data = interface::LSM6DSOX::SensorData {
.xl = {.x = 0.F, .y = 0.F, .z = 0.F}, .gy = {.x = 0.F, .y = 0.F, .z = 0.F }
};
}

TEST_F(MotionKitTest, registerMockCallbackAndStartStabilisation)
TEST_F(MotionKitTest, rotateCounterClockwise)
{
auto mock_function_motion = MockFunction<void(void)> {};
auto loop_motion = [&] { mock_function_motion.Call(); };

EXPECT_CALL(lsm6dsox, setPowerMode(interface::LSM6DSOX::PowerMode::Normal)).Times(1);
EXPECT_CALL(mock_function_motion, Call()).Times(1);

EXPECT_CALL(mock_timeout, stop).Times(1);
EXPECT_CALL(mock_motor_left, stop).Times(1);
EXPECT_CALL(mock_motor_right, stop).Times(1);

stub_event_loop_motion.registerCallback(loop_motion);
motion.startStabilisation();
EXPECT_CALL(mock_timeout, onTimeout).Times(1);
EXPECT_CALL(mock_timeout, start).Times(1);

EXPECT_CALL(mock_motor_left, spin(Rotation::counterClockwise, 1)).Times(1);
EXPECT_CALL(mock_motor_right, spin(Rotation::counterClockwise, 1)).Times(1);

motion.rotate(1, Rotation::counterClockwise);

const auto imu_data = interface::LSM6DSOX::SensorData {
.xl = {.x = 0.F, .y = 0.F, .z = 0.F}, .gy = {.x = 0.F, .y = 0.F, .z = 0.F }
};
}

TEST_F(MotionKitTest, rotateAndStop)
TEST_F(MotionKitTest, startStabilisation)
{
auto mock_function_motion = MockFunction<void(void)> {};
auto loop_motion = [&] {
mock_function_motion.Call();
motion.stop();
EXPECT_CALL(mock_timeout, stop).Times(1);
EXPECT_CALL(mock_motor_left, stop).Times(2);
EXPECT_CALL(mock_motor_right, stop).Times(2);

motion.startStabilisation();

const auto imu_data = interface::LSM6DSOX::SensorData {
.xl = {.x = 0.F, .y = 0.F, .z = 0.F}, .gy = {.x = 0.F, .y = 0.F, .z = 0.F }
};

EXPECT_CALL(lsm6dsox, setPowerMode(interface::LSM6DSOX::PowerMode::Normal)).Times(1);
EXPECT_CALL(mock_function_motion, Call()).Times(1);
lsm6dsox.call_drdy_callback(imu_data);
}

EXPECT_CALL(mock_timeout, stop).Times(2);
EXPECT_CALL(mock_motor_left, stop).Times(2);
EXPECT_CALL(mock_motor_right, stop).Times(2);
TEST_F(MotionKitTest, rotateAndStop)
{
EXPECT_CALL(mock_timeout, stop).Times(1);
EXPECT_CALL(mock_motor_left, stop).Times(1);
EXPECT_CALL(mock_motor_right, stop).Times(1);

EXPECT_CALL(mock_motor_left, spin).Times(1);
EXPECT_CALL(mock_motor_right, spin).Times(1);

EXPECT_CALL(mock_timeout, onTimeout).Times(1);
EXPECT_CALL(mock_timeout, start).Times(1);

stub_event_loop_motion.registerCallback(loop_motion);
motion.rotate(1, Rotation::clockwise);

EXPECT_CALL(mock_timeout, stop).Times(1);
EXPECT_CALL(mock_motor_left, stop).Times(1);
EXPECT_CALL(mock_motor_right, stop).Times(1);

const auto imu_data = interface::LSM6DSOX::SensorData {
.xl = {.x = 0.F, .y = 0.F, .z = 0.F}, .gy = {.x = 0.F, .y = 0.F, .z = 0.F }
};

lsm6dsox.call_drdy_callback(imu_data);

motion.stop();
}

TEST_F(MotionKitTest, rotateAndTimeOutOver)
{
auto mock_function_motion = MockFunction<void(void)> {};
auto loop_motion = [&] { mock_function_motion.Call(); };

interface::Timeout::callback_t on_timeout_callback = {};

EXPECT_CALL(lsm6dsox, setPowerMode(interface::LSM6DSOX::PowerMode::Normal)).Times(1);
EXPECT_CALL(mock_function_motion, Call()).Times(1);

EXPECT_CALL(mock_timeout, stop).Times(1);
EXPECT_CALL(mock_motor_left, stop).Times(1);
EXPECT_CALL(mock_motor_right, stop).Times(1);
Expand All @@ -139,31 +143,38 @@ TEST_F(MotionKitTest, rotateAndTimeOutOver)
EXPECT_CALL(mock_timeout, onTimeout).WillOnce(GetCallback<interface::Timeout::callback_t>(&on_timeout_callback));
EXPECT_CALL(mock_timeout, start).Times(1);

stub_event_loop_motion.registerCallback(loop_motion);
motion.rotate(1, Rotation::clockwise);

EXPECT_CALL(mock_timeout, stop).Times(1);
EXPECT_CALL(mock_motor_left, stop).Times(1);
EXPECT_CALL(mock_motor_right, stop).Times(1);

const auto imu_data = interface::LSM6DSOX::SensorData {
.xl = {.x = 0.F, .y = 0.F, .z = 0.F}, .gy = {.x = 0.F, .y = 0.F, .z = 0.F }
};

lsm6dsox.call_drdy_callback(imu_data);

on_timeout_callback();
}

TEST_F(MotionKitTest, startStabilisationAndStop)
{
auto mock_function_motion = MockFunction<void(void)> {};
auto loop_motion = [&] {
mock_function_motion.Call();
motion.stop();
};
EXPECT_CALL(mock_timeout, stop).Times(1);
EXPECT_CALL(mock_motor_left, stop).Times(1);
EXPECT_CALL(mock_motor_right, stop).Times(1);

EXPECT_CALL(lsm6dsox, setPowerMode(interface::LSM6DSOX::PowerMode::Normal)).Times(1);
EXPECT_CALL(mock_function_motion, Call()).Times(1);
motion.startStabilisation();

EXPECT_CALL(mock_timeout, stop).Times(2);
EXPECT_CALL(mock_timeout, stop).Times(1);
EXPECT_CALL(mock_motor_left, stop).Times(2);
EXPECT_CALL(mock_motor_right, stop).Times(2);

stub_event_loop_motion.registerCallback(loop_motion);
motion.startStabilisation();
const auto imu_data = interface::LSM6DSOX::SensorData {
.xl = {.x = 0.F, .y = 0.F, .z = 0.F}, .gy = {.x = 0.F, .y = 0.F, .z = 0.F }
};

lsm6dsox.call_drdy_callback(imu_data);

motion.stop();
}
20 changes: 11 additions & 9 deletions libs/MotionKit/tests/PID_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class PIDTest : public ::testing::Test
protected:
PIDTest() = default;

// void SetUp() override { }
void SetUp() override { pid.setTargetYaw(kTargetYaw); }
// void TearDown() override {}

PID pid {};

float kTargetYaw = 0;

float max_speed_value = 1.8F; //? ((360-180)*Kp + (360-180)*Kd)/KDeltaT
};

Expand All @@ -30,7 +32,7 @@ TEST_F(PIDTest, processPIDDefaultPosition)
{
auto pitch = 0.F;
auto roll = 0.F;
auto yaw = 180.F;
auto yaw = 0.F;

auto [speed, direction] = pid.processPID(pitch, roll, yaw);

Expand All @@ -42,12 +44,12 @@ TEST_F(PIDTest, processPIDRolledOverAHalfRight)
{
auto pitch = 0.F;
auto roll = 0.F;
auto yaw = 0.F;
auto yaw = 180.F;

auto [speed, direction] = pid.processPID(pitch, roll, yaw);

EXPECT_EQ(speed, max_speed_value);
EXPECT_EQ(direction, Rotation::clockwise);
EXPECT_EQ(direction, Rotation::counterClockwise);
}

TEST_F(PIDTest, processPIDRolledOverAQuarterRight)
Expand All @@ -59,29 +61,29 @@ TEST_F(PIDTest, processPIDRolledOverAQuarterRight)
auto [speed, direction] = pid.processPID(pitch, roll, yaw);

EXPECT_EQ(speed, 0.9F);
EXPECT_EQ(direction, Rotation::clockwise);
EXPECT_EQ(direction, Rotation::counterClockwise);
}

TEST_F(PIDTest, processPIDRolledOverAQuarterLeft)
{
auto pitch = 0.F;
auto roll = 0.F;
auto yaw = 270.F;
auto yaw = -90.F;

auto [speed, direction] = pid.processPID(pitch, roll, yaw);

EXPECT_EQ(speed, 0.9F);
EXPECT_EQ(direction, Rotation::counterClockwise);
EXPECT_EQ(direction, Rotation::clockwise);
}

TEST_F(PIDTest, processPIDRolledOverAHalfLeft)
{
auto pitch = 0.F;
auto roll = 0.F;
auto yaw = 360.F;
auto yaw = -180.F;

auto [speed, direction] = pid.processPID(pitch, roll, yaw);

EXPECT_EQ(speed, max_speed_value);
EXPECT_EQ(direction, Rotation::counterClockwise);
EXPECT_EQ(direction, Rotation::clockwise);
}
5 changes: 1 addition & 4 deletions libs/ReinforcerKit/tests/ReinforcerKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class ReinforcerkitTest : public ::testing::Test

mock::LedKit mock_ledkit;

stub::EventLoopKit stub_event_loop_motion {};

mock::CoreMotor mock_motor_left {};
mock::CoreMotor mock_motor_right {};

Expand All @@ -50,7 +48,7 @@ class ReinforcerkitTest : public ::testing::Test

IMUKit imukit {lsm6dsox};

MotionKit motion {mock_motor_left, mock_motor_right, imukit, stub_event_loop_motion, mock_timeout};
MotionKit motion {mock_motor_left, mock_motor_right, imukit, mock_timeout};

ReinforcerKit reinforcerkit;

Expand All @@ -61,7 +59,6 @@ class ReinforcerkitTest : public ::testing::Test
EXPECT_CALL(mock_motor_right, stop).Times(1);
EXPECT_CALL(mock_motor_left, spin).Times(1);
EXPECT_CALL(mock_motor_right, spin).Times(1);
EXPECT_CALL(lsm6dsox, setPowerMode(interface::LSM6DSOX::PowerMode::Normal)).Times(1);
EXPECT_CALL(mock_timeout, stop).Times(AtMost(1));
EXPECT_CALL(mock_timeout, onTimeout).Times(AtMost(1));
EXPECT_CALL(mock_timeout, start).Times(AtMost(1));
Expand Down

0 comments on commit e94e355

Please sign in to comment.