Skip to content

Commit c43464b

Browse files
authored
Merge pull request #14 from dfeen87/flight-ready-rollback-17323931531489441353
Make rollback execution logic flight-ready
2 parents a8e9279 + d81548c commit c43464b

3 files changed

Lines changed: 160 additions & 0 deletions

File tree

src/raps/rollback_execution.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
11
#pragma once
22

33
#include <string>
4+
#include <cmath>
5+
#include <limits>
6+
7+
#include "raps/core/raps_core_types.hpp"
8+
#include "platform/platform_hal.hpp"
49

510
// Executes a rollback plan via the actuator interface.
611
// Returns true if execution succeeded.
712
inline bool execute_rollback_plan(
813
const RollbackPlan& rollback,
914
std::string& out_tx_id) {
1015

16+
// 1. Validate the plan itself
17+
if (!rollback.valid) {
18+
return false;
19+
}
20+
21+
// 2. Validate control inputs (Sanity Checks)
22+
// Thrust cannot be negative
23+
if (rollback.thrust_magnitude_kN < 0.0f) {
24+
return false;
25+
}
26+
27+
// Gimbal angles must be finite numbers
28+
if (!std::isfinite(rollback.gimbal_theta_rad)) {
29+
return false;
30+
}
31+
32+
if (!std::isfinite(rollback.gimbal_phi_rad)) {
33+
return false;
34+
}
35+
1136
out_tx_id = PlatformHAL::generate_tx_id();
37+
if (out_tx_id.empty()) {
38+
return false;
39+
}
1240

1341
return PlatformHAL::actuator_execute(
1442
out_tx_id.c_str(),

tests/sil/CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,39 @@ add_test(
9393
NAME raps_sil_fault_injection
9494
COMMAND raps_sil_fault_tests
9595
)
96+
97+
# ------------------------------------------------------------
98+
# Rollback Execution Tests
99+
# ------------------------------------------------------------
100+
101+
add_executable(raps_sil_rollback_tests
102+
test_rollback_execution.cpp
103+
../../src/platform/platform_hal.cpp
104+
)
105+
106+
target_include_directories(raps_sil_rollback_tests PRIVATE
107+
${PROJECT_SOURCE_DIR}/../../include
108+
${PROJECT_SOURCE_DIR}/../../src
109+
)
110+
111+
if (RAPS_ENABLE_SIL_FAULTS)
112+
target_compile_definitions(raps_sil_rollback_tests PRIVATE
113+
RAPS_ENABLE_SIL_FAULTS=1
114+
)
115+
endif()
116+
117+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
118+
target_compile_options(raps_sil_rollback_tests PRIVATE
119+
-Wall
120+
-Wextra
121+
-Wpedantic
122+
-Wshadow
123+
-Wconversion
124+
-Wno-unused-parameter
125+
)
126+
endif()
127+
128+
add_test(
129+
NAME raps_sil_rollback_test
130+
COMMAND raps_sil_rollback_tests
131+
)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// ============================================================
2+
// SIL Test: Rollback Execution Logic
3+
// - Verifies rollback plan validation
4+
// - Verifies correct actuator dispatch
5+
// ============================================================
6+
7+
#include <iostream>
8+
#include <cmath>
9+
#include <limits>
10+
#include <string>
11+
12+
#include "raps/core/raps_core_types.hpp"
13+
#include "platform/platform_hal.hpp"
14+
15+
// We include the implementation-under-test directly because it is a header-only library
16+
#include "raps/rollback_execution.hpp"
17+
18+
static int g_failures = 0;
19+
20+
static void expect_true(bool cond, const char* msg) {
21+
if (!cond) {
22+
++g_failures;
23+
std::cerr << "" << msg << "\n";
24+
} else {
25+
std::cout << "" << msg << "\n";
26+
}
27+
}
28+
29+
static void expect_false(bool cond, const char* msg) {
30+
if (cond) {
31+
++g_failures;
32+
std::cerr << "" << msg << "\n";
33+
} else {
34+
std::cout << "" << msg << "\n";
35+
}
36+
}
37+
38+
void test_rollback_validation() {
39+
RollbackPlan plan{};
40+
std::string tx_id;
41+
42+
std::cout << "--- Testing Rollback Validation ---\n";
43+
44+
// 1. Invalid plan
45+
// By default, the struct is zero-initialized, so valid might be false depending on compiler/initialization,
46+
// but we explicitly set it to false to be sure.
47+
plan.valid = false;
48+
// We expect this to fail (return false) once validation is added.
49+
// Currently, without validation, it might succeed if PlatformHAL succeeds with 0 values.
50+
bool res1 = execute_rollback_plan(plan, tx_id);
51+
// Note: In current implementation, this will likely return TRUE because there is no check.
52+
// So this expectation will fail until I fix the code.
53+
// But for TDD, I write the test expecting the CORRECT behavior.
54+
expect_false(res1, "execute_rollback_plan fails for invalid plan (valid=false)");
55+
56+
57+
// 2. Negative thrust
58+
plan.valid = true;
59+
plan.thrust_magnitude_kN = -1.0f;
60+
plan.gimbal_theta_rad = 0.0f;
61+
bool res2 = execute_rollback_plan(plan, tx_id);
62+
expect_false(res2, "execute_rollback_plan fails for negative thrust");
63+
64+
// 3. Infinite gimbal
65+
plan.thrust_magnitude_kN = 100.0f;
66+
plan.gimbal_theta_rad = std::numeric_limits<float>::infinity();
67+
bool res3 = execute_rollback_plan(plan, tx_id);
68+
expect_false(res3, "execute_rollback_plan fails for infinite gimbal");
69+
70+
// 4. Valid plan
71+
plan.valid = true;
72+
plan.thrust_magnitude_kN = 50.0f;
73+
plan.gimbal_theta_rad = 0.1f;
74+
bool res4 = execute_rollback_plan(plan, tx_id);
75+
expect_true(res4, "execute_rollback_plan succeeds for valid inputs");
76+
expect_true(tx_id.length() > 0, "tx_id is generated");
77+
}
78+
79+
int main() {
80+
std::cout << "========================================================\n";
81+
std::cout << " SIL TEST: Rollback Execution Logic\n";
82+
std::cout << "========================================================\n";
83+
84+
// Seed RNG for deterministic behavior of PlatformHAL
85+
PlatformHAL::seed_rng_for_stubs(12345);
86+
87+
test_rollback_validation();
88+
89+
std::cout << "--------------------------------------------------------\n";
90+
if (g_failures == 0) {
91+
std::cout << "✅ ALL ROLLBACK TESTS PASSED\n";
92+
return 0;
93+
}
94+
std::cout << "❌ FAILURES: " << g_failures << "\n";
95+
return 1;
96+
}

0 commit comments

Comments
 (0)