|
| 1 | +open OUnit2 |
| 2 | +open Modelcheck_mutex |
| 3 | + |
| 4 | + |
| 5 | +module NaiveMutexChecker = Modelcheck.Checker.MakeNaive (NaiveMutex) |
| 6 | + |
| 7 | +let test_naive_correct _ = |
| 8 | + assert_equal ~printer:string_of_bool false (NaiveMutexChecker.is_correct ()) |
| 9 | + |
| 10 | +let test_naive_all_states _ = |
| 11 | + let all_states = NaiveMutexChecker.all_states () in |
| 12 | + let assert_exists expected f = |
| 13 | + assert_equal ~printer:string_of_bool expected (NaiveMutexChecker.StateSet.exists f all_states) |
| 14 | + in |
| 15 | + assert_exists true (fun s -> s.p1 = P1 && s.p2 = P1); |
| 16 | + assert_exists true (fun s -> s.p1 = P5 && s.p2 = P5); |
| 17 | + assert_exists true (fun s -> s.p1 = P3 && s.p2 = P3); |
| 18 | + assert_exists true (fun s -> s.p1 = P1 && s.p2 = P3); |
| 19 | + assert_exists true (fun s -> s.p1 = P3 && s.p2 = P1); |
| 20 | + assert_exists true (fun s -> s.p1 = P5 && s.p2 = P3); |
| 21 | + assert_exists true (fun s -> s.p1 = P3 && s.p2 = P5); |
| 22 | + assert_equal ~printer:string_of_int 29 (NaiveMutexChecker.StateSet.cardinal all_states) |
| 23 | + |
| 24 | +let test_naive_error_states _ = |
| 25 | + let error_states = NaiveMutexChecker.error_states () in |
| 26 | + let assert_mem expected state = |
| 27 | + assert_equal ~printer:string_of_bool ~msg:(NaiveMutex.show state) expected (NaiveMutexChecker.StateSet.mem state error_states) |
| 28 | + in |
| 29 | + (* Kõik veaolekud. *) |
| 30 | + assert_mem true {p1 = P3; p2 = P3; flag = true}; |
| 31 | + assert_equal ~printer:string_of_int 1 (NaiveMutexChecker.StateSet.cardinal error_states) |
| 32 | + |
| 33 | + |
| 34 | +module PetersonAlgorithmChecker = Modelcheck.Checker.MakeNaive (PetersonAlgorithm) |
| 35 | + |
| 36 | +let test_peterson_correct _ = |
| 37 | + assert_equal ~printer:string_of_bool true (PetersonAlgorithmChecker.is_correct ()) |
| 38 | + |
| 39 | +let test_peterson_all_states _ = |
| 40 | + let all_states = PetersonAlgorithmChecker.all_states () in |
| 41 | + let assert_exists expected f = |
| 42 | + assert_equal ~printer:string_of_bool expected (PetersonAlgorithmChecker.StateSet.exists f all_states) |
| 43 | + in |
| 44 | + assert_exists true (fun s -> s.p1 = P1 && s.p2 = P1); |
| 45 | + assert_exists true (fun s -> s.p1 = P6 && s.p2 = P6); |
| 46 | + assert_exists false (fun s -> s.p1 = P4 && s.p2 = P4); |
| 47 | + assert_exists true (fun s -> s.p1 = P1 && s.p2 = P4); |
| 48 | + assert_exists true (fun s -> s.p1 = P4 && s.p2 = P1); |
| 49 | + assert_exists true (fun s -> s.p1 = P6 && s.p2 = P4); |
| 50 | + assert_exists true (fun s -> s.p1 = P4 && s.p2 = P6); |
| 51 | + assert_equal ~printer:string_of_int 34 (PetersonAlgorithmChecker.StateSet.cardinal all_states) |
| 52 | + |
| 53 | +let test_peterson_error_states _ = |
| 54 | + let error_states = PetersonAlgorithmChecker.error_states () in |
| 55 | + assert_equal ~printer:string_of_int 0 (PetersonAlgorithmChecker.StateSet.cardinal error_states); |
| 56 | + (* Kontrollime veaoleku funktsiooni hüpoteetilistel olekutel. *) |
| 57 | + assert_equal ~printer:string_of_bool true (PetersonAlgorithm.is_error {p1 = P4; p2 = P4; flag1 = true; flag2 = true; turn = T1}); |
| 58 | + assert_equal ~printer:string_of_bool true (PetersonAlgorithm.is_error {p1 = P4; p2 = P4; flag1 = true; flag2 = true; turn = T2}) |
| 59 | + |
| 60 | + |
| 61 | +module DekkerAlgorithmChecker = Modelcheck.Checker.MakeNaive (DekkerAlgorithm) |
| 62 | + |
| 63 | +let test_dekker_correct _ = |
| 64 | + assert_equal ~printer:string_of_bool true (DekkerAlgorithmChecker.is_correct ()) |
| 65 | + |
| 66 | +let test_dekker_all_states _ = |
| 67 | + let all_states = DekkerAlgorithmChecker.all_states () in |
| 68 | + let assert_exists expected f = |
| 69 | + assert_equal ~printer:string_of_bool expected (DekkerAlgorithmChecker.StateSet.exists f all_states) |
| 70 | + in |
| 71 | + assert_exists true (fun s -> s.p1 = P1 && s.p2 = P1); |
| 72 | + assert_exists true (fun s -> s.p1 = P12 && s.p2 = P12); |
| 73 | + assert_exists false (fun s -> s.p1 = P9 && s.p2 = P9); |
| 74 | + assert_exists true (fun s -> s.p1 = P1 && s.p2 = P9); |
| 75 | + assert_exists true (fun s -> s.p1 = P9 && s.p2 = P1); |
| 76 | + assert_exists true (fun s -> s.p1 = P12 && s.p2 = P9); |
| 77 | + assert_exists true (fun s -> s.p1 = P9 && s.p2 = P12); |
| 78 | + assert_equal ~printer:string_of_int 53 (DekkerAlgorithmChecker.StateSet.cardinal all_states) |
| 79 | + |
| 80 | +let test_dekker_error_states _ = |
| 81 | + let error_states = DekkerAlgorithmChecker.error_states () in |
| 82 | + assert_equal ~printer:string_of_int 0 (DekkerAlgorithmChecker.StateSet.cardinal error_states); |
| 83 | + (* Kontrollime veaoleku funktsiooni hüpoteetilistel olekutel. *) |
| 84 | + assert_equal ~printer:string_of_bool true (DekkerAlgorithm.is_error {p1 = P9; p2 = P9; flag1 = true; flag2 = true; turn = T1}); |
| 85 | + assert_equal ~printer:string_of_bool true (DekkerAlgorithm.is_error {p1 = P9; p2 = P9; flag1 = true; flag2 = true; turn = T2}) |
| 86 | + |
| 87 | + |
| 88 | +let tests = |
| 89 | + "modelcheck" >::: [ |
| 90 | + "mutex" >::: [ |
| 91 | + "naive" >::: [ |
| 92 | + "correct" >:: test_naive_correct; |
| 93 | + "all_states" >:: test_naive_all_states; |
| 94 | + "error_states" >:: test_naive_error_states; |
| 95 | + ]; |
| 96 | + "peterson" >::: [ |
| 97 | + "correct" >:: test_peterson_correct; |
| 98 | + "all_states" >:: test_peterson_all_states; |
| 99 | + "error_states" >:: test_peterson_error_states; |
| 100 | + ]; |
| 101 | + "dekker" >::: [ |
| 102 | + "correct" >:: test_dekker_correct; |
| 103 | + "all_states" >:: test_dekker_all_states; |
| 104 | + "error_states" >:: test_dekker_error_states; |
| 105 | + ]; |
| 106 | + ]; |
| 107 | + ] |
| 108 | + |
| 109 | +let () = run_test_tt_main (OUnitTodo.wrap tests) |
0 commit comments