run_tests.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/bin/sh
  2. #
  3. # Step-2 test runner for M2c (scalars + MC64 backend).
  4. # Usage: ./run_tests.sh (from the m2c directory, after ./build.sh)
  5. #
  6. # Positive tests: compile with M2c, run the newest .MC4 under mcint,
  7. # compare the printed ExitCode value with the expectation.
  8. #
  9. MCINT="${MCINT:-../mc64/mcint}"
  10. pass=0
  11. fail=0
  12. expect_ok() {
  13. # $1 = test file (no dir) — parse only
  14. name="$1"
  15. out=$(./M2c "tests/$name" 2>&1)
  16. case "$out" in
  17. *"Parsed correctly"*)
  18. echo "ok $name accepted"
  19. pass=$((pass+1)) ;;
  20. *)
  21. echo "FAIL $name: expected accept, got: $out"
  22. fail=$((fail+1)) ;;
  23. esac
  24. }
  25. expect_fail() {
  26. # $1 = test file, $2 = message text expected in the .LST
  27. name="$1"
  28. want="$2"
  29. out=$(./M2c "tests/$name" 2>&1)
  30. case "$out" in
  31. *"Incorrect source"*) ;;
  32. *)
  33. echo "FAIL $name: expected rejection, got: $out"
  34. fail=$((fail+1)); return ;;
  35. esac
  36. lst="tests/$(basename "$name" .mod).LST"
  37. if grep -q "$want" "$lst" 2>/dev/null; then
  38. echo "ok $name rejected with [$want]"
  39. pass=$((pass+1))
  40. else
  41. echo "FAIL $name: [$want] not found in $lst"
  42. fail=$((fail+1))
  43. fi
  44. }
  45. expect_run() {
  46. # $1 = test file, $2 = expected printed ExitCode value
  47. name="$1"
  48. want="$2"
  49. mod=$(head -n 1 "tests/$name" | cut -d' ' -f2 | cut -d';' -f1)
  50. mcd="$mod.MC4"
  51. rm -f "$mcd"
  52. out=$(./M2c "tests/$name" 2>&1)
  53. case "$out" in
  54. *"Parsed correctly"*) ;;
  55. *)
  56. echo "FAIL $name: M2c rejected it: $out"
  57. fail=$((fail+1)); return ;;
  58. esac
  59. if [ ! -f "$mcd" ]; then
  60. echo "FAIL $name: no $mcd produced"
  61. fail=$((fail+1)); return
  62. fi
  63. got=$("$MCINT" "$mcd" 2>&1 | head -n 1 | tr -d '\r')
  64. if [ "$got" = "$want" ]; then
  65. echo "ok $name -> $got"
  66. pass=$((pass+1))
  67. else
  68. echo "FAIL $name: printed [$got], expected [$want]"
  69. fail=$((fail+1))
  70. fi
  71. }
  72. expect_run_none() {
  73. # $1 = test file — must compile and run with empty output
  74. name="$1"
  75. mod=$(head -n 1 "tests/$name" | cut -d' ' -f2 | cut -d';' -f1)
  76. mcd="$mod.MC4"
  77. rm -f "$mcd"
  78. out=$(./M2c "tests/$name" 2>&1)
  79. case "$out" in
  80. *"Parsed correctly"*) ;;
  81. *)
  82. echo "FAIL $name: M2c rejected it: $out"
  83. fail=$((fail+1)); return ;;
  84. esac
  85. if [ ! -f "$mcd" ]; then
  86. echo "FAIL $name: no $mcd produced"
  87. fail=$((fail+1)); return
  88. fi
  89. got=$("$MCINT" "$mcd" 2>&1 | head -n 1)
  90. if [ -z "$got" ]; then
  91. echo "ok $name runs silent"
  92. pass=$((pass+1))
  93. else
  94. echo "FAIL $name: unexpected output [$got]"
  95. fail=$((fail+1))
  96. fi
  97. }
  98. echo "=== Phase-0 tests (still green) ==="
  99. expect_ok ok_minimal.mod
  100. expect_ok ok_nobegin.mod
  101. expect_fail bad_mismatch.mod "name mismatch"
  102. expect_fail bad_syntax.mod "expected"
  103. echo "=== Step-2 run tests (M2c -> mcint) ==="
  104. expect_run_none t_min.mod
  105. expect_run t_arith.mod 7
  106. expect_run t_const.mod 15
  107. expect_run t_bool.mod 42
  108. expect_run t_if.mod 20
  109. expect_run t_case.mod 103
  110. expect_run t_while.mod 55
  111. expect_run t_repeat.mod 55
  112. expect_run t_loop.mod 7
  113. expect_run t_for.mod 85
  114. expect_run t_real.mod 31
  115. expect_run t_char.mod 97
  116. expect_run t_enum.mod 1
  117. expect_run t_full.mod 74
  118. expect_run t_set.mod 15
  119. echo "=== Step-2 rejection tests ==="
  120. expect_fail t_bad_asn.mod "incompatible assignment"
  121. expect_fail t_bad_array.mod "not supported in this phase"
  122. expect_fail t_bad_with.mod "not a RECORD type"
  123. expect_fail t_bad_exit.mod "not supported in this phase"
  124. expect_fail t_bad_const.mod "not supported in this phase"
  125. expect_fail t_bad_deref.mod "not a POINTER type"
  126. echo "=== Step-3 run tests (procedures) ==="
  127. expect_run p_proc.mod 7
  128. expect_run p_params.mod 21
  129. expect_run p_func.mod 42
  130. expect_run p_fact.mod 120
  131. expect_run p_fib.mod 55
  132. expect_run p_varpar.mod 3012
  133. expect_run p_nested.mod 42
  134. expect_run p_forward.mod 1
  135. expect_run p_return.mod 40
  136. expect_run p_local.mod 43
  137. expect_run p_mixed.mod 52
  138. expect_run p_chain.mod 9
  139. expect_run p_empty.mod 3
  140. expect_run p_realfn.mod 5
  141. echo "=== Step-3 rejection tests ==="
  142. expect_fail p_bad_call.mod "invalid procedure call"
  143. expect_fail p_bad_arity.mod "invalid procedure call"
  144. expect_fail p_bad_actual.mod "invalid procedure call"
  145. expect_fail p_bad_proper.mod "invalid procedure call"
  146. expect_fail p_bad_return1.mod "bad RETURN"
  147. expect_fail p_bad_return2.mod "bad RETURN"
  148. expect_fail p_bad_return3.mod "bad RETURN"
  149. expect_fail p_bad_fwd.mod "forward mismatch"
  150. expect_fail p_bad_nobody.mod "forward mismatch"
  151. expect_fail p_bad_undecl.mod "undeclared identifier"
  152. echo "=== Step-4 run tests (composites) ==="
  153. expect_run t_arr.mod 39
  154. expect_run a_mat.mod 134
  155. expect_run a_sieve.mod 129
  156. expect_run a_varpar.mod 150
  157. expect_run a_enum.mod 21
  158. expect_run t_rec.mod 30
  159. expect_run r_nest.mod 135
  160. expect_run t_with.mod 37
  161. expect_run r_varpar.mod 303
  162. expect_run t_ptr.mod 42
  163. expect_run t_ptr0.mod 5
  164. expect_run p_list.mod 21
  165. expect_run t_setop.mod 22
  166. expect_run s_range.mod 21
  167. expect_run t_str.mod 11
  168. echo "=== Step-4 rejection tests ==="
  169. expect_fail a_bad_idx.mod "array index must be integer"
  170. expect_fail a_bad_arr.mod "not an ARRAY type"
  171. expect_fail a_bad_asn.mod "incompatible assignment"
  172. expect_fail r_bad_field.mod "unknown field"
  173. expect_fail r_bad_rec.mod "not a RECORD type"
  174. expect_fail p_bad_new.mod "not a POINTER type"
  175. expect_fail s_bad_elem.mod "set operand mismatch"
  176. expect_fail a_bad_val.mod "not supported in this phase"
  177. expect_fail s_bad_long.mod "incompatible assignment"
  178. expect_fail r_bad_asn.mod "incompatible assignment"
  179. echo "=== Step-5 run tests (open arrays, copy, strings, I/O) ==="
  180. expect_run o_open.mod 150
  181. expect_run c_copy.mod 18
  182. expect_run s_streq.mod 21
  183. expect_run o_val.mod 14
  184. expect_run o_high.mod 14
  185. expect_run c_rec.mod 17
  186. expect_run w_io.mod hi42
  187. expect_run o_open2.mod 286
  188. expect_run c_copy2.mod 9
  189. expect_run s_cmp2.mod 10
  190. expect_run r_copy2.mod 15
  191. expect_run a_subopen.mod 66
  192. expect_run w_str2.mod ok8
  193. expect_run p_newarr.mod 66
  194. expect_run o_char.mod 4
  195. echo "=== Step-5 rejection tests ==="
  196. expect_fail o_bad_open.mod "invalid procedure call"
  197. expect_fail o_bad_high.mod "not an ARRAY type"
  198. expect_fail c_bad_len.mod "incompatible assignment"
  199. expect_fail s_bad_cmp.mod "incompatible comparison"
  200. expect_fail v_bad_var.mod "invalid procedure call"
  201. expect_fail w_bad_int.mod "incompatible assignment"
  202. expect_fail w_bad_str.mod "incompatible assignment"
  203. expect_fail r_bad_open.mod "not supported in this phase"
  204. expect_fail p_bad_dispose.mod "not a POINTER type"
  205. expect_fail s_bad_op.mod "arithmetic operand must be numeric"
  206. echo "=== $pass passed, $fail failed ==="
  207. test "$fail" = 0