Skip to content

Commit

Permalink
#45: Cleanup of low level tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aul12 committed Dec 30, 2022
1 parent 972b032 commit 48d1b68
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 26 deletions.
123 changes: 115 additions & 8 deletions Tests/LowLevel/Application/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ extern "C" {
}

TEST(TEST_NAME, no_error) {
/*
* Setpoint equal current attitude
*/
imu_data_t imuData{};
imuData.roll_mul_16 = 37 * 16;
imuData.pitch_mul_16 = -49 * 16;
Expand All @@ -14,37 +17,141 @@ TEST(TEST_NAME, no_error) {
}

TEST(TEST_NAME, need_roll_right) {
/*
* Plane is banked left (89°), should be banked right (89°).
* Left elevon should be down, right up.
*/
imu_data_t imuData{};
imuData.roll_mul_16 = -90 * 16;
imuData.roll_mul_16 = -89 * 16;
imuData.pitch_mul_16 = -49 * 16;
auto controllerCmd = controller_update(&imuData, 90, -49);
auto controllerCmd = controller_update(&imuData, 89, -49);
EXPECT_EQ(controllerCmd.elevon_right, 500);
EXPECT_EQ(controllerCmd.elevon_left, -500);
}

TEST(TEST_NAME, need_roll_left) {
/*
* Plane is banked right (89°), should be banked left (89°).
* Left elevon should be up, right down.
*/
imu_data_t imuData{};
imuData.roll_mul_16 = 90 * 16;
imuData.roll_mul_16 = 89 * 16;
imuData.pitch_mul_16 = -49 * 16;
auto controllerCmd = controller_update(&imuData, -90, -49);
auto controllerCmd = controller_update(&imuData, -89, -49);
EXPECT_EQ(controllerCmd.elevon_right, -500);
EXPECT_EQ(controllerCmd.elevon_left, 500);
}

TEST(TEST_NAME, need_pitch_up) {
/*
* Plane is pitched down (89°), should be pitched up (89°).
* Left and right elevon should be up.
*/
imu_data_t imuData{};
imuData.roll_mul_16 = 37 * 16;
imuData.pitch_mul_16 = 90 * 16;
auto controllerCmd = controller_update(&imuData, 37, -90);
imuData.pitch_mul_16 = 89 * 16;
auto controllerCmd = controller_update(&imuData, 37, -89);
EXPECT_EQ(controllerCmd.elevon_right, 500);
EXPECT_EQ(controllerCmd.elevon_left, 500);
}

TEST(TEST_NAME, need_pitch_down) {
/*
* Plane is pitched up (89°), should be pitched down (89°).
* Left and right elevon should be down.
*/
imu_data_t imuData{};
imuData.roll_mul_16 = 37 * 16;
imuData.pitch_mul_16 = -90 * 16;
auto controllerCmd = controller_update(&imuData, 37, 90);
imuData.pitch_mul_16 = -89 * 16;
auto controllerCmd = controller_update(&imuData, 37, 89);
EXPECT_EQ(controllerCmd.elevon_right, -500);
EXPECT_EQ(controllerCmd.elevon_left, -500);
}

TEST(TEST_NAME, need_roll_left_inverted) {
/*
* Plane is banked left (91°), should be banked right (91°), the shortest path is via inverted roll.
* Left elevon should be up, right down.
*/
imu_data_t imuData{};
imuData.roll_mul_16 = -91 * 16;
imuData.pitch_mul_16 = -49 * 16;
auto controllerCmd = controller_update(&imuData, 91, -49);
EXPECT_EQ(controllerCmd.elevon_right, -500);
EXPECT_EQ(controllerCmd.elevon_left, 500);
}

TEST(TEST_NAME, need_roll_right_inverted) {
/*
* Plane is banked right (91°), should be banked left (91°), the shortest path is via inverted roll.
* Left elevon should be down, right up.
*/
imu_data_t imuData{};
imuData.roll_mul_16 = 91 * 16;
imuData.pitch_mul_16 = -49 * 16;
auto controllerCmd = controller_update(&imuData, -91, -49);
EXPECT_EQ(controllerCmd.elevon_right, 500);
EXPECT_EQ(controllerCmd.elevon_left, -500);
}

TEST(TEST_NAME, need_pitch_down_inverted) {
/*
* Plane is pitched down (91°), should be pitched up (91°), the shortest path is via half-looping.
* Left and right elevon should be down.
*/
imu_data_t imuData{};
imuData.roll_mul_16 = 37 * 16;
imuData.pitch_mul_16 = 91 * 16;
auto controllerCmd = controller_update(&imuData, 37, -91);
EXPECT_EQ(controllerCmd.elevon_right, -500);
EXPECT_EQ(controllerCmd.elevon_left, -500);
}

TEST(TEST_NAME, need_pitch_up_inverted) {
/*
* Plane is pitched up (91°), should be pitched down (91°), the shortest path is via half-looping.
* Left and right elevon should be up.
*/
imu_data_t imuData{};
imuData.roll_mul_16 = 37 * 16;
imuData.pitch_mul_16 = -91 * 16;
auto controllerCmd = controller_update(&imuData, 37, 91);
EXPECT_EQ(controllerCmd.elevon_right, 500);
EXPECT_EQ(controllerCmd.elevon_left, 500);
}

TEST(TEST_NAME, no_error_roll_wrap_pos) {
imu_data_t imuData{};
imuData.roll_mul_16 = (37 + 4 * 360) * 16;
imuData.pitch_mul_16 = -49 * 16;
auto controllerCmd = controller_update(&imuData, 37, -49);
EXPECT_EQ(controllerCmd.elevon_right, 0);
EXPECT_EQ(controllerCmd.elevon_left, 0);
}

TEST(TEST_NAME, no_error_pitch_wrap_pos) {
imu_data_t imuData{};
imuData.roll_mul_16 = 37 * 16;
imuData.pitch_mul_16 = (-49 + 4 * 360) * 16;
auto controllerCmd = controller_update(&imuData, 37, -49);
EXPECT_EQ(controllerCmd.elevon_right, 0);
EXPECT_EQ(controllerCmd.elevon_left, 0);
}

TEST(TEST_NAME, no_error_roll_wrap_neg) {
imu_data_t imuData{};
imuData.roll_mul_16 = (37 - 4 * 360) * 16;
imuData.pitch_mul_16 = -49 * 16;
auto controllerCmd = controller_update(&imuData, 37, -49);
EXPECT_EQ(controllerCmd.elevon_right, 0);
EXPECT_EQ(controllerCmd.elevon_left, 0);
}

TEST(TEST_NAME, no_error_pitch_wrap_neg) {
imu_data_t imuData{};
imuData.roll_mul_16 = 37 * 16;
imuData.pitch_mul_16 = (-49 - 4 * 360) * 16;
auto controllerCmd = controller_update(&imuData, 37, -49);
EXPECT_EQ(controllerCmd.elevon_right, 0);
EXPECT_EQ(controllerCmd.elevon_left, 0);
}
14 changes: 14 additions & 0 deletions Tests/LowLevel/Application/error_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Mock/avr/io.hpp>
#include <Mock/avr/wdt.hpp>
#include <gtest/gtest.h>

extern "C" {
Expand All @@ -15,3 +16,16 @@ TEST(TEST_NAME, handle_warning) {
error_handler_handle_warning(static_cast<error_group_t>(6), 11);
EXPECT_EQ(PORTL, 16 * 11 + 6);
}

TEST(TEST_NAME, handle_error) {
auto wdtHandle = mock::wdt.getHandle();
std::size_t count = 0;
wdtHandle.overrideFunc<wdt_reset>([&count]() {
EXPECT_EQ(PORTL, 16 * 11 + 6);
count += 1;
if (count >= 10) { // This solves the halting problem...
throw std::runtime_error{"EXCEPTION TO BREAK LOOP FOR TESTS"};
}
});
EXPECT_THROW(error_handler_handle_error(static_cast<error_group_t>(6), 11), std::runtime_error);
}
18 changes: 0 additions & 18 deletions Tests/LowLevel/Drivers/sbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ TEST(TEST_NAME, init) {
auto uartHandle = mock::uart.getHandle();
auto ringBufferHandle = mock::ring_buffer.getHandle();
ringBufferHandle.overrideFunc<ring_buffer_init>([]() { return ring_buffer_data_t{}; });
ringBufferHandle.overrideFunc<ring_buffer_put>(
[](ring_buffer_data_t * /*ring_buffer_data*/, uint8_t /*data*/) { return true; });
sbus_init();
/*
* https://github.com/bolderflight/sbus/blob/main/README.md:
Expand Down Expand Up @@ -46,22 +44,6 @@ TEST(TEST_NAME, rx_fill_buffer) {
EXPECT_TRUE(ringBufferHandle.functionGotCalled<ring_buffer_put>(std::ignore, 54));
}

TEST(TEST_NAME, available_read_buffer) {
auto uartHandle = mock::uart.getHandle();
auto ringBufferHandle = mock::ring_buffer.getHandle();
ringBufferHandle.overrideFunc<ring_buffer_init>([]() { return ring_buffer_data_t{}; });
sbus_init();
EXPECT_TRUE(uartHandle.functionGotCalled<uart_init>());

std::size_t count = 0;
ringBufferHandle.overrideFunc<ring_buffer_get>([&count](ring_buffer_data_t *ringBufferData, uint8_t *out) {
EXPECT_NE(ringBufferData, nullptr);
count += 1;
*out = count;
return count <= 4;
});
}

TEST(TEST_NAME, decode) {
auto uartHandle = mock::uart.getHandle();
auto ringBufferHandle = mock::ring_buffer.getHandle();
Expand Down

0 comments on commit 48d1b68

Please sign in to comment.