|
| 1 | +open OUnit2 |
| 2 | +open Modelcheck |
| 3 | + |
| 4 | +module BadModel = |
| 5 | +struct |
| 6 | + type t = int [@@deriving ord, show] |
| 7 | + |
| 8 | + let initial = 42 |
| 9 | + |
| 10 | + let step i = |
| 11 | + if i >= 0 then |
| 12 | + [i - 1; i - 2; i - 3] |
| 13 | + else |
| 14 | + [] |
| 15 | + |
| 16 | + let is_error i = i < 0 |
| 17 | +end |
| 18 | + |
| 19 | +module BadModelChecker = Checker.MakeNaive (BadModel) |
| 20 | + |
| 21 | +let test_bad_correct _ = |
| 22 | + assert_equal ~printer:string_of_bool true (BadModelChecker.has_error ()); |
| 23 | + assert_equal ~printer:string_of_bool false (BadModelChecker.is_correct ()) |
| 24 | + |
| 25 | +let test_bad_all_states _ = |
| 26 | + let all_states = BadModelChecker.all_states () in |
| 27 | + let assert_mem expected state = |
| 28 | + assert_equal ~printer:string_of_bool ~msg:(BadModel.show state) expected (BadModelChecker.StateSet.mem state all_states) |
| 29 | + in |
| 30 | + assert_mem true 42; |
| 31 | + assert_mem false 43; |
| 32 | + assert_mem true 0; |
| 33 | + assert_equal ~printer:string_of_int 46 (BadModelChecker.StateSet.cardinal all_states) |
| 34 | + |
| 35 | +let test_bad_error_states _ = |
| 36 | + let error_states = BadModelChecker.error_states () in |
| 37 | + let assert_mem expected state = |
| 38 | + assert_equal ~printer:string_of_bool ~msg:(BadModel.show state) expected (BadModelChecker.StateSet.mem state error_states) |
| 39 | + in |
| 40 | + (* Kõik veaolekud. *) |
| 41 | + assert_mem true (-1); |
| 42 | + assert_mem true (-2); |
| 43 | + assert_mem true (-3); |
| 44 | + assert_equal ~printer:string_of_int 3 (BadModelChecker.StateSet.cardinal error_states) |
| 45 | + |
| 46 | + |
| 47 | +module GoodModel = |
| 48 | +struct |
| 49 | + type t = int [@@deriving ord, show] |
| 50 | + |
| 51 | + let initial = 42 |
| 52 | + |
| 53 | + let step i = |
| 54 | + if i >= 3 then |
| 55 | + [i - 1; i - 2; i - 3] |
| 56 | + else |
| 57 | + [] |
| 58 | + |
| 59 | + let is_error i = i < 0 |
| 60 | +end |
| 61 | + |
| 62 | +module GoodModelChecker = Checker.MakeNaive (GoodModel) |
| 63 | + |
| 64 | +let test_good_correct _ = |
| 65 | + assert_equal ~printer:string_of_bool false (GoodModelChecker.has_error ()); |
| 66 | + assert_equal ~printer:string_of_bool true (GoodModelChecker.is_correct ()) |
| 67 | + |
| 68 | +let test_good_all_states _ = |
| 69 | + let all_states = GoodModelChecker.all_states () in |
| 70 | + let assert_mem expected state = |
| 71 | + assert_equal ~printer:string_of_bool ~msg:(GoodModel.show state) expected (GoodModelChecker.StateSet.mem state all_states) |
| 72 | + in |
| 73 | + assert_mem true 42; |
| 74 | + assert_mem false 43; |
| 75 | + assert_mem true 0; |
| 76 | + assert_equal ~printer:string_of_int 43 (GoodModelChecker.StateSet.cardinal all_states) |
| 77 | + |
| 78 | +let test_good_error_states _ = |
| 79 | + let error_states = GoodModelChecker.error_states () in |
| 80 | + assert_equal ~printer:string_of_int 0 (GoodModelChecker.StateSet.cardinal error_states) |
| 81 | + |
| 82 | + |
| 83 | +let tests = |
| 84 | + "modelcheck" >::: [ |
| 85 | + "bad" >::: [ |
| 86 | + "correct" >:: test_bad_correct; |
| 87 | + "all_states" >:: test_bad_all_states; |
| 88 | + "error_states" >:: test_bad_error_states; |
| 89 | + ]; |
| 90 | + "good" >::: [ |
| 91 | + "correct" >:: test_good_correct; |
| 92 | + "all_states" >:: test_good_all_states; |
| 93 | + "error_states" >:: test_good_error_states; |
| 94 | + ]; |
| 95 | + ] |
| 96 | + |
| 97 | +let () = run_test_tt_main (OUnitTodo.wrap tests) |
0 commit comments