test_gap3_emergency_state_ordering.c
1 /******************************************************************************* 2 * test_gap3_emergency_state_ordering.c 3 * 4 * Gap-3 Fix 5 (FIXED): system_emergency_state set BEFORE Emergency_Stop(). 5 * 6 * Before fix: handleSystemError() called Emergency_Stop() first (line 854), 7 * then set system_emergency_state = true (line 855). 8 * Since Emergency_Stop() never returns (infinite loop), the flag 9 * was never set — dead code. 10 * 11 * After fix: system_emergency_state = true is set BEFORE Emergency_Stop(). 12 * This ensures any interrupt or parallel check can see the 13 * emergency state flag is set even though Emergency_Stop blocks. 14 * 15 * Test strategy: 16 * Simulate the handleSystemError critical-error path and verify that 17 * system_emergency_state is set to true BEFORE the Emergency_Stop would 18 * be called (we use a flag to track ordering). 19 ******************************************************************************/ 20 #include <assert.h> 21 #include <stdio.h> 22 #include <stdbool.h> 23 24 /* Simulated global state */ 25 static bool system_emergency_state = false; 26 static bool emergency_stop_called = false; 27 static bool state_was_true_when_estop_called = false; 28 29 /* Simulated Emergency_Stop (doesn't loop — just records) */ 30 static void Mock_Emergency_Stop(void) 31 { 32 emergency_stop_called = true; 33 /* Check: was system_emergency_state already true? */ 34 state_was_true_when_estop_called = system_emergency_state; 35 } 36 37 /* Error codes (subset matching main.cpp) */ 38 typedef enum { 39 ERROR_NONE = 0, 40 ERROR_RF_PA_OVERCURRENT = 9, 41 ERROR_RF_PA_BIAS = 10, 42 ERROR_STEPPER_FAULT = 11, 43 ERROR_FPGA_COMM = 12, 44 ERROR_POWER_SUPPLY = 13, 45 ERROR_TEMPERATURE_HIGH = 14, 46 } SystemError_t; 47 48 /* Extracted critical-error handling logic (post-fix ordering) */ 49 static void simulate_handleSystemError_critical(SystemError_t error) 50 { 51 /* Only critical errors (PA overcurrent through power supply) trigger e-stop */ 52 if (error >= ERROR_RF_PA_OVERCURRENT && error <= ERROR_POWER_SUPPLY) { 53 /* FIX 5: set flag BEFORE calling Emergency_Stop */ 54 system_emergency_state = true; 55 Mock_Emergency_Stop(); 56 /* NOTREACHED in real code */ 57 } 58 } 59 60 int main(void) 61 { 62 printf("=== Gap-3 Fix 5: system_emergency_state ordering ===\n"); 63 64 /* Test 1: PA overcurrent → flag set BEFORE Emergency_Stop */ 65 printf(" Test 1: PA overcurrent path... "); 66 system_emergency_state = false; 67 emergency_stop_called = false; 68 state_was_true_when_estop_called = false; 69 simulate_handleSystemError_critical(ERROR_RF_PA_OVERCURRENT); 70 assert(emergency_stop_called == true); 71 assert(system_emergency_state == true); 72 assert(state_was_true_when_estop_called == true); 73 printf("PASS\n"); 74 75 /* Test 2: Power supply fault → same ordering */ 76 printf(" Test 2: Power supply fault path... "); 77 system_emergency_state = false; 78 emergency_stop_called = false; 79 state_was_true_when_estop_called = false; 80 simulate_handleSystemError_critical(ERROR_POWER_SUPPLY); 81 assert(emergency_stop_called == true); 82 assert(system_emergency_state == true); 83 assert(state_was_true_when_estop_called == true); 84 printf("PASS\n"); 85 86 /* Test 3: PA bias fault → same ordering */ 87 printf(" Test 3: PA bias fault path... "); 88 system_emergency_state = false; 89 emergency_stop_called = false; 90 state_was_true_when_estop_called = false; 91 simulate_handleSystemError_critical(ERROR_RF_PA_BIAS); 92 assert(emergency_stop_called == true); 93 assert(state_was_true_when_estop_called == true); 94 printf("PASS\n"); 95 96 /* Test 4: Non-critical error → no e-stop, flag stays false */ 97 printf(" Test 4: Non-critical error (no e-stop)... "); 98 system_emergency_state = false; 99 emergency_stop_called = false; 100 simulate_handleSystemError_critical(ERROR_TEMPERATURE_HIGH); 101 assert(emergency_stop_called == false); 102 assert(system_emergency_state == false); 103 printf("PASS\n"); 104 105 /* Test 5: ERROR_NONE → no e-stop */ 106 printf(" Test 5: ERROR_NONE (no action)... "); 107 system_emergency_state = false; 108 emergency_stop_called = false; 109 simulate_handleSystemError_critical(ERROR_NONE); 110 assert(emergency_stop_called == false); 111 assert(system_emergency_state == false); 112 printf("PASS\n"); 113 114 printf("\n=== Gap-3 Fix 5: ALL TESTS PASSED ===\n\n"); 115 return 0; 116 }