This is a TestFlows example that tests a Ceiling Speed Monitoring Controller (CSMC), the part of a train's control system that warns the driver and applies the emergency brake when the train runs too fast. The test program is built from the controller's formal description and checks all 250 combinations of its input equivalence classes against a Python implementation generated by ChatGPT.
The implementation, train_control_system.py, is kept exactly as ChatGPT generated it, with its two bugs, so that the test program finds them.
- Formal description of the controller: Exhaustive Model-Based Equivalence Class Testing by W. Huang and J. Peleska, 2013
- Reference implementation: generated by ChatGPT 4o from the formal description
-
Decoding Formal Description of a Simple Train Control System. Explains the controller's formal description: its states, guard conditions and transition relation.
-
Testing Simple Train Control System Using Formal Description. Builds this test program from the formal description and runs it against the generated implementation.
pip3 install -r requirements.txtThe test program starts the controller with python train_control_system.py, so python must be on your PATH.
python3 test_train_control_system.py -o slickThe first run fails at combination #3, where the speed limit is 100 km/h and the train reaches 113 km/h:
AssertionError: expected state IS but got WS
The controller checks the speed against the limit before it checks the emergency brake guards, so it warns when it must brake. In the NS state, move the gebi1 or gebi2 check before self.V_est > self.V_MRSP, and run the tests again. They fail at the same combination with the second bug:
AssertionError: expected W=1 but got 0
The controller applies the emergency brake without setting the warning signal. Set self.W to 1 together with self.EB when it moves to the IS state:
if self.state == "NS":
if self.V_est <= self.V_MRSP:
self.state = "NS" # phi_0
elif gebi1 or gebi2:
self.state, self.W, self.EB = "IS", 1, 1 # phi_2
elif self.V_est > self.V_MRSP:
self.state, self.W = "WS", 1 # phi_1With both fixes, all 250 combinations pass.