|
| 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