nx_state_machine_test.nx source
↩ module page · 62 lines · 2415 B
1// nx_state_machine_test.nx -- smoke for nx_state_machine.
2
3import "nx_syscalls.nx"
4import "nx_state_machine.nx"
5
6func main() -> i64 {
7 // Simple 4-state machine: 0=IDLE, 1=ACTIVE, 2=PAUSED, 3=DONE
8 // Allowed: 0->1, 1->2, 2->1, 1->3, 2->3
9 let sm: *NxStateMachine = nx_state_machine_new(4, 0)
10 if (sm as i64) == 0 { return 1 }
11 if sm.states_count != 4 { return 2 }
12 if nx_state_machine_current(sm) != 0 { return 3 }
13
14 // Bad construction
15 let bad1: *NxStateMachine = nx_state_machine_new(0, 0)
16 if (bad1 as i64) != 0 { return 4 }
17 let bad2: *NxStateMachine = nx_state_machine_new(4, 99)
18 if (bad2 as i64) != 0 { return 5 }
19 let bad3: *NxStateMachine = nx_state_machine_new(100, 0)
20 if (bad3 as i64) != 0 { return 6 } // too many states
21
22 // Default: only self-transitions allowed
23 if nx_state_machine_is_allowed(sm, 0, 0) != 1 { return 7 }
24 if nx_state_machine_is_allowed(sm, 0, 1) != 0 { return 8 } // not yet allowed
25 if nx_state_machine_transition(sm, 1) != NX_SM_ERR_BAD_TRANSITION { return 9 }
26
27 // Add transitions
28 nx_state_machine_allow(sm, 0, 1)
29 nx_state_machine_allow(sm, 1, 2)
30 nx_state_machine_allow(sm, 2, 1)
31 nx_state_machine_allow(sm, 1, 3)
32 nx_state_machine_allow(sm, 2, 3)
33
34 // 0 -> 1
35 if nx_state_machine_transition(sm, 1) != NX_SM_OK { return 10 }
36 if nx_state_machine_current(sm) != 1 { return 11 }
37 if nx_state_machine_transitions(sm) != 1 { return 12 }
38
39 // 1 -> 2
40 if nx_state_machine_transition(sm, 2) != NX_SM_OK { return 13 }
41 // 2 -> 1 (return)
42 if nx_state_machine_transition(sm, 1) != NX_SM_OK { return 14 }
43 // 1 -> 3 (terminal)
44 if nx_state_machine_transition(sm, 3) != NX_SM_OK { return 15 }
45 if nx_state_machine_current(sm) != 3 { return 16 }
46 if nx_state_machine_transitions(sm) != 4 { return 17 }
47
48 // No transitions out of 3 allowed (we didn't add any)
49 if nx_state_machine_transition(sm, 0) != NX_SM_ERR_BAD_TRANSITION { return 18 }
50
51 // Bad allow params
52 if nx_state_machine_allow(sm, 99, 0) != NX_SM_ERR_BAD_FROM { return 19 }
53 if nx_state_machine_allow(sm, 0, 99) != NX_SM_ERR_BAD_TO { return 20 }
54
55 // Reset returns to initial state
56 nx_state_machine_reset(sm)
57 if nx_state_machine_current(sm) != 0 { return 21 }
58 // transition_count doesn't reset (monotonic)
59 if nx_state_machine_transitions(sm) != 4 { return 22 }
60
61 return 0
62}