run_tests.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 .MCD 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.MCD"
  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.MCD"
  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 "module 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 supported in this phase"
  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 supported in this phase"
  126. echo "=== $pass passed, $fail failed ==="
  127. test "$fail" = 0