run_tests.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/sh
  2. # Regression for M2comp step 2 (Coco/R + SymTab semantic analysis).
  3. # Usage: ./run_tests.sh (from the m2comp directory)
  4. pass=0; fail=0
  5. expect_ok() {
  6. if ./M2comp "$1" 2>&1 | grep -q "Parsed correctly"; then
  7. pass=$((pass+1)); echo "PASS(ok): $1"
  8. else
  9. fail=$((fail+1)); echo "FAIL(ok, rejected): $1"
  10. fi
  11. collect_mcd
  12. }
  13. expect_fail() {
  14. if ./M2comp "$1" 2>&1 | grep -q "Incorrect source"; then
  15. pass=$((pass+1)); echo "PASS(fail): $1"
  16. else
  17. fail=$((fail+1)); echo "FAIL(fail, accepted): $1"
  18. fi
  19. collect_mcd
  20. }
  21. expect_dump() {
  22. if ./M2comp "$1" 2>&1 | grep -q "$2"; then
  23. pass=$((pass+1)); echo "PASS(ast): $1 [$2]"
  24. else
  25. fail=$((fail+1)); echo "FAIL(ast): $1 [$2]"
  26. fi
  27. collect_mcd
  28. }
  29. MCINT="${MCINT:-../m-code-64/mcint}"
  30. mkdir -p MC4
  31. MCTIMEOUT="${MCTIMEOUT:-10}"
  32. # MGen emits <Module>.MC4 in the current directory; the run helpers
  33. # below collect it into MC4/ and run it from there.
  34. collect_mcd() {
  35. for f in *.MC4; do
  36. [ -f "$f" ] && mv -f "$f" MC4/
  37. done
  38. }
  39. expect_run() {
  40. # $1 = test file, $2 = expected printed ExitCode value
  41. name="$1"
  42. want="$2"
  43. mod=$(head -n 1 "tests/$name" | cut -d' ' -f2 | cut -d';' -f1)
  44. mcd="MC4/$mod.MC4"
  45. rm -f "$mcd" "$mod.MC4"
  46. if ./M2comp "tests/$name" 2>&1 | grep -q "Incorrect source"; then
  47. fail=$((fail+1)); echo "FAIL(run): $name rejected"; return
  48. fi
  49. [ -f "$mod.MC4" ] && mv -f "$mod.MC4" "$mcd"
  50. if [ ! -f "$mcd" ]; then
  51. fail=$((fail+1)); echo "FAIL(run): $name no $mcd"; return
  52. fi
  53. timeout "$MCTIMEOUT" "$MCINT" "$mcd" > /tmp/m2comp_out.txt 2>&1; rc=$?
  54. if [ "$rc" -ne 0 ]; then
  55. fail=$((fail+1)); echo "FAIL(run): $name vm rc=$rc (diverges?)"; return
  56. fi
  57. got=$(head -n 1 /tmp/m2comp_out.txt | tr -d '\r')
  58. if [ "$got" = "$want" ]; then
  59. pass=$((pass+1)); echo "PASS(run): $name -> $got"
  60. else
  61. fail=$((fail+1)); echo "FAIL(run): $name printed [$got], want [$want]"
  62. fi
  63. }
  64. expect_run_none() {
  65. # $1 = test file — must compile and run silent (terminating)
  66. name="$1"
  67. mod=$(head -n 1 "tests/$name" | cut -d' ' -f2 | cut -d';' -f1)
  68. mcd="MC4/$mod.MC4"
  69. rm -f "$mcd" "$mod.MC4"
  70. if ./M2comp "tests/$name" 2>&1 | grep -q "Incorrect source"; then
  71. fail=$((fail+1)); echo "FAIL(silent): $name rejected"; return
  72. fi
  73. [ -f "$mod.MC4" ] && mv -f "$mod.MC4" "$mcd"
  74. if [ ! -f "$mcd" ]; then
  75. fail=$((fail+1)); echo "FAIL(silent): $name no $mcd"; return
  76. fi
  77. timeout "$MCTIMEOUT" "$MCINT" "$mcd" > /tmp/m2comp_out.txt 2>&1; rc=$?
  78. if [ "$rc" -ne 0 ]; then
  79. fail=$((fail+1)); echo "FAIL(silent): $name vm rc=$rc (diverges?)"; return
  80. fi
  81. got=$(head -n 1 /tmp/m2comp_out.txt)
  82. if [ -z "$got" ]; then
  83. pass=$((pass+1)); echo "PASS(silent): $name runs silent"
  84. else
  85. fail=$((fail+1)); echo "FAIL(silent): $name output [$got]"
  86. fi
  87. }
  88. expect_run_files() {
  89. # $1 = program module (MC4 base), $2 = expected value, rest = sources
  90. # (definitions, then implementations, then the program).
  91. prog="$1"
  92. want="$2"
  93. shift 2
  94. mcd="MC4/$prog.MC4"
  95. rm -f "$mcd" "$prog.MC4"
  96. if ./M2comp "$@" 2>&1 | grep -q "Incorrect source"; then
  97. fail=$((fail+1)); echo "FAIL(run): $prog rejected"; return
  98. fi
  99. [ -f "$prog.MC4" ] && mv -f "$prog.MC4" "$mcd"
  100. collect_mcd
  101. if [ ! -f "$mcd" ]; then
  102. fail=$((fail+1)); echo "FAIL(run): $prog no $mcd"; return
  103. fi
  104. timeout "$MCTIMEOUT" "$MCINT" "$mcd" > /tmp/m2comp_out.txt 2>&1; rc=$?
  105. if [ "$rc" -ne 0 ]; then
  106. fail=$((fail+1)); echo "FAIL(run): $prog vm rc=$rc (diverges?)"; return
  107. fi
  108. got=$(head -n 1 /tmp/m2comp_out.txt | tr -d '\r')
  109. if [ "$got" = "$want" ]; then
  110. pass=$((pass+1)); echo "PASS(run): $prog -> $got"
  111. else
  112. fail=$((fail+1)); echo "FAIL(run): $prog printed [$got], want [$want]"
  113. fi
  114. }
  115. expect_fail_files() {
  116. # $1 = message in the failing file's .LST, rest = sources.
  117. want="$1"
  118. shift
  119. if ./M2comp "$@" 2>&1 | grep -q "Incorrect source"; then
  120. :
  121. else
  122. fail=$((fail+1)); echo "FAIL(fail, accepted): $*"; return
  123. fi
  124. collect_mcd
  125. last=""
  126. for f in "$@"; do last="$f"; done
  127. lst=$(echo "$last" | sed 's/\.[^.]*$/.LST/')
  128. if grep -q "$want" "$lst" 2>/dev/null; then
  129. pass=$((pass+1)); echo "PASS(fail): $last [$want]"
  130. else
  131. fail=$((fail+1)); echo "FAIL(fail): [$want] not found in $lst"
  132. fi
  133. }
  134. expect_fail_multi() {
  135. # stdout-only rejection (driver stage errors leave no .LST mark).
  136. if ./M2comp "$@" 2>&1 | grep -q "Incorrect source"; then
  137. pass=$((pass+1)); echo "PASS(fail): $*"
  138. else
  139. fail=$((fail+1)); echo "FAIL(fail, accepted): $*"
  140. fi
  141. collect_mcd
  142. }
  143. expect_ok tests/ok_minimal.mod
  144. expect_ok tests/ok_proc.mod
  145. expect_ok tests/showcase.mod
  146. expect_fail tests/bad_mismatch.mod
  147. expect_fail tests/t_dup.mod
  148. expect_fail tests/t_undecl.mod
  149. expect_fail tests/t_assign.mod
  150. expect_fail tests/t_arith.mod
  151. expect_fail tests/t_bool.mod
  152. expect_fail tests/t_rel.mod
  153. expect_fail tests/t_cond.mod
  154. expect_fail tests/t_field.mod
  155. expect_fail tests/t_index.mod
  156. expect_fail tests/t_ptr.mod
  157. expect_fail tests/t_for.mod
  158. expect_fail tests/t_call.mod
  159. expect_fail tests/t_call2.mod
  160. expect_fail tests/t_return.mod
  161. expect_fail tests/t_exit.mod
  162. expect_dump tests/showcase.mod "Module Showcase"
  163. expect_dump tests/showcase.mod "Proc Work"
  164. expect_dump tests/showcase.mod "CallExpr"
  165. expect_dump tests/showcase.mod "Field q"
  166. expect_dump tests/showcase.mod "Int 255"
  167. expect_dump tests/ok_proc.mod "Proc P"
  168. expect_run_none ok_minimal.mod
  169. expect_run_none ok_proc.mod
  170. expect_run r_arith.mod 8
  171. expect_run r_flow.mod 69
  172. expect_run r_bool.mod 11
  173. expect_run r_real.mod 1111
  174. expect_run r_proc.mod 162
  175. expect_run r_varpar.mod 48
  176. expect_run r_nested.mod 213
  177. expect_run r_array.mod 39
  178. expect_run r_record.mod 15
  179. expect_run r_set.mod 12
  180. expect_run r_string.mod 111
  181. expect_run r_ptr.mod 111
  182. expect_run r_module.mod 83
  183. expect_run r_const.mod 1102
  184. echo "=== Step-5 run tests (DEFINITION/IMPLEMENTATION/IMPORT) ==="
  185. expect_run_files DBasic 60 tests/d_lib.def tests/d_libimpl.mod tests/d_basic.mod
  186. expect_run_files DFrom 35 tests/d_lib.def tests/d_libimpl.mod tests/d_lib2.def tests/d_lib2impl.mod tests/d_from.mod
  187. expect_run_files DMulti 68 tests/d_lib.def tests/d_libimpl.mod tests/d_lib2.def tests/d_lib2impl.mod tests/d_multi.mod
  188. expect_run_files DInit 7 tests/d_lib.def tests/d_libimpl.mod tests/d_init.mod
  189. expect_run_files DFunc 81 tests/d_lib.def tests/d_libimpl.mod tests/d_func.mod
  190. expect_run_files DType 40 tests/d_lib.def tests/d_libimpl.mod tests/d_type.mod
  191. echo "=== Step-5 rejection tests ==="
  192. expect_fail_files "undeclared identifier" tests/d_bad_nodef.mod
  193. expect_fail_files "procedure forward mismatch" tests/d_lib.def tests/d_bad_sig.mod
  194. expect_fail_files "procedure forward mismatch" tests/d_lib.def tests/d_bad_nobody.mod
  195. expect_fail_files "undeclared identifier" tests/d_lib.def tests/d_bad_noimpl.mod
  196. expect_fail_files "duplicate identifier" tests/d_lib.def tests/d_bad_dup.def
  197. expect_fail_files "undeclared identifier" tests/d_lib.def tests/d_libimpl.mod tests/d_bad_priv.mod
  198. expect_fail_multi tests/d_lib.def tests/d_libimpl.mod tests/d_basic.mod tests/d_basic.mod
  199. echo "--- $pass passed, $fail failed ---"
  200. [ "$fail" -eq 0 ]