|
| 1 | +#! /usr/bin/env bash |
| 2 | + |
| 3 | +cd test_node_env |
| 4 | +npm update sicp |
| 5 | +cd .. |
| 6 | + |
| 7 | +OUT_WRITER="test_node_env/result_writer.js" |
| 8 | +EXP_WRITER="test_node_env/expected_writer.js" |
| 9 | + |
| 10 | +# must use BSD awk |
| 11 | +AWK="awk" |
| 12 | + |
| 13 | +SOURCEFILES=js_programs/*/*/*/*.js |
| 14 | + |
| 15 | +DEFAULT_CHAPTER=4 |
| 16 | +DEFAULT_VARIANT="default" |
| 17 | + |
| 18 | +red=`tput setaf 1` |
| 19 | +green=`tput setaf 2` |
| 20 | +normal=`tput setaf 7` |
| 21 | + |
| 22 | +passed=0 |
| 23 | +failed=0 |
| 24 | + |
| 25 | +# $1 is the source file to be tested |
| 26 | +# $2 is the chapter |
| 27 | +# $3 is the variant |
| 28 | + |
| 29 | +test_source() { |
| 30 | + if [ $3 ]; then |
| 31 | + variant=$3 |
| 32 | + else |
| 33 | + variant=$DEFAULT_VARIANT |
| 34 | + fi |
| 35 | + |
| 36 | + if [ "$(cat $1 | tail -1 | cut -c 1-13)" = "// expected: " ] && [ "$3" != "non-det" ] && [ "$3" != "concurrent" ] && [ "$3" != "lazy" ] |
| 37 | + then |
| 38 | + |
| 39 | + EXPECTED=`cat $1 | tail -1 | cut -c14-` |
| 40 | + |
| 41 | + if [[ "$EXPECTED" != "'all threads terminated'" ]] |
| 42 | + then |
| 43 | + echo "${normal}$1, expecting: $(cat $1 | tail -1 | cut -c14-)" |
| 44 | + |
| 45 | + # Run the writers |
| 46 | + node --stack_size=8000 $OUT_WRITER "$1" |
| 47 | + node $EXP_WRITER "$EXPECTED" |
| 48 | + |
| 49 | + # Compare outputs |
| 50 | + DIFF=$(diff test_node_env/result.txt test_node_env/expect.txt) |
| 51 | + |
| 52 | + if [ "$DIFF" = "" ] |
| 53 | + then passed=$(($passed+1)); echo "${green}PASS" |
| 54 | + else failed=$(($failed+1)); echo "${red}FAIL: |
| 55 | + $DIFF" |
| 56 | + fi |
| 57 | + fi |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +main() { |
| 62 | + for s in ${SOURCEFILES} |
| 63 | + do |
| 64 | + # DIR is full path including js_programs |
| 65 | + DIR=$(dirname ${s}) |
| 66 | + # CHAPTERDIR is path starting with chapterx |
| 67 | + CHAPTERDIR=${DIR#*/} |
| 68 | + # CHAPTER is just the chapter name, e.g. chapter2 |
| 69 | + CHAPTER=${CHAPTERDIR%%/*} |
| 70 | + # SECTIONDIR is path starting with sectionx |
| 71 | + SECTIONDIR=${CHAPTERDIR#*/} |
| 72 | + # SECTION is just the section name, e.g. section3 |
| 73 | + SECTION=${SECTIONDIR%%/*} |
| 74 | + |
| 75 | + if [[ ($1 == $CHAPTER || $1 == "") && ($2 == $SECTION || $2 == "") ]]; |
| 76 | + then |
| 77 | + # check if first line of test file contains 'chapter=' and retrieve |
| 78 | + # its value. Set to the default chapter if it does not |
| 79 | + chapter=$($AWK -F 'chapter=' 'FNR==1{ if ($0~"chapter=") { print $2 } else { print '$DEFAULT_CHAPTER' } }' $s | $AWK -F ' ' '{ print $1 }') |
| 80 | + |
| 81 | + # check if first line of test file contains 'variant=' and retrieve |
| 82 | + # its value. Set to the default variant if it does not |
| 83 | + variant=$($AWK -F 'variant=' 'FNR==1{ if ($0~"variant=") { print $2 } else { print '$DEFAULT_VARIANT' } }' $s | $AWK -F ' ' '{ print $1 }') |
| 84 | + |
| 85 | + test_source ${s} ${chapter} ${variant} |
| 86 | + fi |
| 87 | + done |
| 88 | +} |
| 89 | + |
| 90 | +# optional arguments: chapter... section..., limiting testing only to the |
| 91 | +# named chapter (or section): e.g. yarn test chapter2 section3 |
| 92 | + |
| 93 | +main $1 $2 |
| 94 | + |
| 95 | +echo "${normal}test cases completed; $passed passed, $failed failed" |
| 96 | +exit 0 |
0 commit comments