-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfinite_state_machine_test.ml
56 lines (44 loc) · 1.84 KB
/
finite_state_machine_test.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module TrafficLight : Finite_state_machine.S = struct
type state = Green | Yellow | Red
type symbol = Wait
let transition state symbol = match (state, symbol) with
| (Green, Wait) -> Yellow
| (Yellow, Wait) -> Red
| (Red, Wait) -> Green
let () =
assert (Green = List.fold_left transition Green [Wait; Wait; Wait]);
assert (Red = List.fold_left transition Green [Wait; Wait])
end
module Turnstile : Finite_state_machine.S = struct
type state = Locked | Unlocked
type symbol = InsertCoin | Push
let transition state symbol = match (state, symbol) with
| (Locked, InsertCoin) -> Unlocked
| (Locked, Push) -> Locked
| (Unlocked, Push) -> Locked
| (Unlocked, InsertCoin) -> Unlocked
let () =
assert (Unlocked = List.fold_left transition Locked [InsertCoin]);
assert (Locked = List.fold_left transition Locked [InsertCoin; Push; Push]);
end
module HAPlusRegex : Finite_state_machine.S = struct
type state = Start | H | Success | Failure
type symbol = char
let transition state symbol = match (state, symbol) with
| (Start, 'H') -> H
| (Start, _) -> Failure
| (H, 'A') -> Success
| (H, _) -> Failure
| (Success, 'H') -> H
| (Success, _) -> Failure
| (Failure, _) -> Failure
let () =
assert (Success = List.fold_left transition Start ['H'; 'A']);
assert (Success = List.fold_left transition Start ['H'; 'A'; 'H'; 'A']);
assert (Success = List.fold_left transition Start ['H'; 'A'; 'H'; 'A'; 'H'; 'A']);
assert (Success != List.fold_left transition Start ['K']);
assert (Success != List.fold_left transition Start ['H'; 'A'; 'Z']);
assert (Success != List.fold_left transition Start ['H'; 'A'; 'H']);
assert (Success != List.fold_left transition Start ['T'; 'H'; 'A']);
assert (Success != List.fold_left transition Start ['H'; 'A'; 'H'; 'A'; 'T']);
end