|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +declare -r color_start="\033[" |
| 4 | +declare -r color_red="${color_start}0;31m" |
| 5 | +declare -r color_green="${color_start}0;32m" |
| 6 | +declare -r color_blue="${color_start}0;34m" |
| 7 | +declare -r color_norm="${color_start}0m" |
| 8 | + |
| 9 | +GRADER_ROOT=$(dirname ${BASH_SOURCE}) |
| 10 | + |
| 11 | +PROJECT_PATH=${GRADER_ROOT}/.. |
| 12 | + |
| 13 | +function print_dir_contents { |
| 14 | + local proj_path=$1 |
| 15 | + echo "Project contents:" |
| 16 | + echo -e "${color_blue}$(ls ${proj_path})${color_norm}" |
| 17 | +} |
| 18 | + |
| 19 | +function find_file_or_dir_in_project { |
| 20 | + local proj_path=$1 |
| 21 | + local file_or_dir_name=$2 |
| 22 | + if [[ ! -e "${proj_path}/${file_or_dir_name}" ]]; then |
| 23 | + echo -e "[${color_red}FAIL${color_norm}]: no ${file_or_dir_name} found" |
| 24 | + print_dir_contents ${proj_path} |
| 25 | + echo -e "${color_red}${file_or_dir_name} [MISSING]${color_norm}" |
| 26 | + exit 1 |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +# check project directory structure |
| 31 | +function check_project_struct { |
| 32 | + find_file_or_dir_in_project ${PROJECT_PATH} run.sh |
| 33 | + find_file_or_dir_in_project ${PROJECT_PATH} src |
| 34 | + find_file_or_dir_in_project ${PROJECT_PATH} paymo_input |
| 35 | + find_file_or_dir_in_project ${PROJECT_PATH} paymo_output |
| 36 | +} |
| 37 | + |
| 38 | +# setup testing output folder |
| 39 | +function setup_testing_input_output { |
| 40 | + TEST_OUTPUT_PATH=${GRADER_ROOT}/temp |
| 41 | + if [ -d ${TEST_OUTPUT_PATH} ]; then |
| 42 | + rm -rf ${TEST_OUTPUT_PATH} |
| 43 | + fi |
| 44 | + |
| 45 | + mkdir -p ${TEST_OUTPUT_PATH} |
| 46 | + |
| 47 | + cp -r ${PROJECT_PATH}/src ${TEST_OUTPUT_PATH} |
| 48 | + cp -r ${PROJECT_PATH}/run.sh ${TEST_OUTPUT_PATH} |
| 49 | + cp -r ${PROJECT_PATH}/paymo_input ${TEST_OUTPUT_PATH} |
| 50 | + cp -r ${PROJECT_PATH}/paymo_output ${TEST_OUTPUT_PATH} |
| 51 | + |
| 52 | + rm -r ${TEST_OUTPUT_PATH}/paymo_input/* |
| 53 | + rm -r ${TEST_OUTPUT_PATH}/paymo_output/* |
| 54 | + cp -r ${GRADER_ROOT}/tests/${test_folder}/paymo_input/batch_payment.txt ${TEST_OUTPUT_PATH}/paymo_input/batch_payment.txt |
| 55 | + cp -r ${GRADER_ROOT}/tests/${test_folder}/paymo_input/stream_payment.txt ${TEST_OUTPUT_PATH}/paymo_input/stream_payment.txt |
| 56 | +} |
| 57 | + |
| 58 | +function compare_outputs { |
| 59 | + PROJECT_ANSWER_PATH1=${GRADER_ROOT}/temp/paymo_output/output1.txt |
| 60 | + PROJECT_ANSWER_PATH2=${GRADER_ROOT}/temp/paymo_output/output2.txt |
| 61 | + PROJECT_ANSWER_PATH3=${GRADER_ROOT}/temp/paymo_output/output3.txt |
| 62 | + TEST_ANSWER_PATH1=${GRADER_ROOT}/tests/${test_folder}/paymo_output/output1.txt |
| 63 | + TEST_ANSWER_PATH2=${GRADER_ROOT}/tests/${test_folder}/paymo_output/output2.txt |
| 64 | + TEST_ANSWER_PATH3=${GRADER_ROOT}/tests/${test_folder}/paymo_output/output3.txt |
| 65 | + |
| 66 | + DIFF_RESULT1=$(diff -bB ${PROJECT_ANSWER_PATH1} ${TEST_ANSWER_PATH1} | wc -l) |
| 67 | + if [ "${DIFF_RESULT1}" -eq "0" ] && [ -f ${PROJECT_ANSWER_PATH1} ]; then |
| 68 | + echo -e "[${color_green}PASS${color_norm}]: ${test_folder} (output1.txt)" |
| 69 | + PASS_CNT=$(($PASS_CNT+1)) |
| 70 | + else |
| 71 | + echo -e "[${color_red}FAIL${color_norm}]: ${test_folder} (output1.txt)" |
| 72 | + diff ${PROJECT_ANSWER_PATH1} ${TEST_ANSWER_PATH1} |
| 73 | + fi |
| 74 | + |
| 75 | + DIFF_RESULT2=$(diff -bB ${PROJECT_ANSWER_PATH2} ${TEST_ANSWER_PATH2} | wc -l) |
| 76 | + if [ "${DIFF_RESULT2}" -eq "0" ] && [ -f ${PROJECT_ANSWER_PATH2} ]; then |
| 77 | + echo -e "[${color_green}PASS${color_norm}]: ${test_folder} (output2.txt)" |
| 78 | + PASS_CNT=$(($PASS_CNT+1)) |
| 79 | + else |
| 80 | + echo -e "[${color_red}FAIL${color_norm}]: ${test_folder} (output2.txt)" |
| 81 | + diff ${PROJECT_ANSWER_PATH2} ${TEST_ANSWER_PATH2} |
| 82 | + fi |
| 83 | + |
| 84 | + DIFF_RESULT3=$(diff -bB ${PROJECT_ANSWER_PATH3} ${TEST_ANSWER_PATH3} | wc -l) |
| 85 | + if [ "${DIFF_RESULT3}" -eq "0" ] && [ -f ${PROJECT_ANSWER_PATH3} ]; then |
| 86 | + echo -e "[${color_green}PASS${color_norm}]: ${test_folder} (output3.txt)" |
| 87 | + PASS_CNT=$(($PASS_CNT+1)) |
| 88 | + else |
| 89 | + echo -e "[${color_red}FAIL${color_norm}]: ${test_folder} (output3.txt)" |
| 90 | + diff ${PROJECT_ANSWER_PATH3} ${TEST_ANSWER_PATH3} |
| 91 | + fi |
| 92 | +} |
| 93 | + |
| 94 | +function run_all_tests { |
| 95 | + TEST_FOLDERS=$(ls ${GRADER_ROOT}/tests) |
| 96 | + NUM_TESTS=$(($(echo $(echo ${TEST_FOLDERS} | wc -w)) * 3)) |
| 97 | + PASS_CNT=0 |
| 98 | + |
| 99 | + # Loop through all tests |
| 100 | + for test_folder in ${TEST_FOLDERS}; do |
| 101 | + |
| 102 | + setup_testing_input_output |
| 103 | + |
| 104 | + cd ${GRADER_ROOT}/temp |
| 105 | + bash run.sh 2>&1 |
| 106 | + cd ../ |
| 107 | + |
| 108 | + compare_outputs |
| 109 | + done |
| 110 | + |
| 111 | + echo "[$(date)] ${PASS_CNT} of ${NUM_TESTS} tests passed" >> ${GRADER_ROOT}/results.txt |
| 112 | +} |
| 113 | + |
| 114 | +check_project_struct |
| 115 | +run_all_tests |
0 commit comments