test-lexer.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/sh
  2. #
  3. # Run lexer tests
  4. #
  5. # Copyright © 2017 Eric Bavier <bavier@member.fsf.org>
  6. # License: GPLv3+
  7. #####
  8. output=tests/lexer/output
  9. log=test-lexer.log
  10. rm -f $log
  11. ret=0
  12. run_test ()
  13. {
  14. base=`basename "$1" | sed 's/\..*$//'`
  15. echo "$base: " >>$log
  16. tmp=test-lexer-$base.tmp
  17. timeout 5s ./lexer < "$1" >$tmp 2>/dev/null
  18. if diff -Nau $output/$base.out $tmp >>$log; then
  19. printf "$base: PASS\n\n" >>$log
  20. else
  21. printf "$base: FAIL\n\n" >>$log
  22. ret=1
  23. fi
  24. rm $tmp
  25. }
  26. # Check some simple inputs
  27. run_test tests/input/keywords.in
  28. run_test tests/input/assign.in
  29. run_test tests/input/flip-flop.in
  30. run_test tests/input/choice.in
  31. run_test tests/input/big-choice.in
  32. run_test tests/input/else.in
  33. run_test tests/input/print.in
  34. run_test tests/input/simple.c
  35. run_test tests/input/test-case-1.in
  36. run_test tests/input/test-case-2.in
  37. run_test tests/input/test-case-3.in
  38. run_test tests/input/test-case-4.in
  39. run_test tests/input/count.in
  40. run_test tests/input/100_doors.in
  41. run_test tests/input/negative.in
  42. run_test tests/input/deep.in
  43. run_test tests/input/gcd.in
  44. run_test tests/input/factorial.in
  45. run_test tests/input/fibonacci.in
  46. run_test tests/input/fizzbuzz.in
  47. run_test tests/input/99bottles.in
  48. run_test tests/input/primes.in
  49. run_test tests/input/mandelbrot.in
  50. # Check expected error/diagnostic outputs
  51. tmp=test-lexer-fail-empty-char.tmp
  52. echo "empty character:" >>$log
  53. timeout 1s ./lexer >$tmp 2>/dev/null <<EOF
  54. char foo = '';
  55. EOF
  56. if grep -q -i "unexpected" $tmp; then
  57. printf "empty character: PASS\n\n" >>$log
  58. else
  59. cat $tmp >>$log
  60. printf "empty character: FAIL\n\n" >>$log
  61. ret=1
  62. fi
  63. rm $tmp
  64. tmp=test-lexer-fail-escape-sequence.tmp
  65. echo "escape sequence:" >>$log
  66. timeout 1s ./lexer >$tmp 2>/dev/null <<EOF
  67. char foo = '\r';
  68. EOF
  69. if grep -q -i "unknown escape sequence" $tmp; then
  70. printf "escape sequence: PASS\n\n" >>$log
  71. else
  72. cat $tmp >>$log
  73. printf "escape sequence: FAIL\n\n" >>$log
  74. ret=1
  75. fi
  76. rm $tmp
  77. tmp=test-lexer-fail-multi-char.tmp
  78. echo "multi-character constant:" >>$log
  79. timeout 1s ./lexer >$tmp 2>/dev/null <<EOF
  80. char foo = 'ab';
  81. EOF
  82. if grep -q -i "unexpected" $tmp; then
  83. printf "multi-character constant: PASS\n\n" >>$log
  84. else
  85. cat $tmp >>$log
  86. printf "multi-character constant: FAIL\n\n" >>$log
  87. ret=1
  88. fi
  89. rm $tmp
  90. tmp=test-lexer-fail-eof-comment.tmp
  91. echo "eof in comment:" >>$log
  92. timeout 1s ./lexer >$tmp 2>/dev/null <<EOF
  93. /* This comment is not terminated
  94. EOF
  95. if grep -q -i "end-of-file in comment" $tmp; then
  96. printf "eof in comment: PASS\n\n" >>$log
  97. else
  98. cat $tmp >>$log
  99. printf "eof in comment: FAIL\n\n" >>$log
  100. ret=1
  101. fi
  102. rm $tmp
  103. tmp=test-lexer-fail-eof-string.tmp
  104. echo "eof in string:" >>$log
  105. timeout 1s ./lexer >$tmp 2>/dev/null <<EOF
  106. char * foo = "Hello, world\
  107. EOF
  108. if grep -q -i "end-of-file in string" $tmp; then
  109. printf "eof in string: PASS\n\n" >>$log
  110. else
  111. cat $tmp >>$log
  112. printf "eof in string: FAIL\n\n" >>$log
  113. ret=1
  114. fi
  115. rm $tmp
  116. tmp=test-lexer-fail-eol-string.tmp
  117. echo "eol in string:" >>$log
  118. timeout 1s ./lexer >$tmp 2>/dev/null <<EOF
  119. char * foo = "Hello,
  120. world!";
  121. EOF
  122. if grep -q -i "end-of-line in string" $tmp; then
  123. printf "eol in string: PASS\n\n" >>$log
  124. else
  125. cat $tmp >>$log
  126. printf "eol in string: FAIL\n\n" >>$log
  127. ret=1
  128. fi
  129. rm $tmp
  130. tmp=test-lexer-fail-char.tmp
  131. echo "unrecognized character:" >>$log
  132. timeout 1s ./lexer >$tmp 2>/dev/null <<EOF
  133. int foo @ 1;
  134. EOF
  135. if grep -q -i "character.*not recognized" $tmp; then
  136. printf "unrecognized character: PASS\n\n" >>$log
  137. else
  138. chat $tmp >>$log
  139. printf "unrecognized character: FAIL\n\n" >>$log
  140. ret=1
  141. fi
  142. rm $tmp
  143. tmp=test-lexer-fail-invalid-num.tmp
  144. echo "invalid number:" >>$log
  145. timeout 1s ./lexer >$tmp 2>/dev/null <<EOF
  146. int foo = 123abc;
  147. EOF
  148. if grep -q -i "invalid num" $tmp; then
  149. printf "invalid number: PASS\n\n" >>$log
  150. else
  151. cat $tmp >>$log
  152. printf "invalid number: FAIL\n\n" >>$log
  153. ret=1
  154. fi
  155. rm $tmp
  156. exit $ret