Advertisement
CSenshi

Information Theory - HW0 (checker V2)

Sep 28th, 2019
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.65 KB | None | 0 0
  1. # !/bin/bash
  2.  
  3. if [ "$#" -ne 6 ] && [ "$#" -ne 8 ]  ; then
  4.     echo "Invalid Arguments"
  5.     echo "Usage: script requires 3/4 Arguments: "
  6.     echo "       -s Path to folder which contains following scripts with exact names:"
  7.     echo "                   SimpleRead.py, SimpleWrite.py, CompleteWrite.py, CompleteRead.py"
  8.     echo "       -t Path to folder which contains public tests(where A,B,C,D folders are located)"
  9.     echo "       -r Path to folder where we should put output for each test"
  10.     echo "       -i Your Interpreter (If using executable "
  11.     echo "Example: ./check.sh -s src -t public_tests -r result -i python3"
  12.    
  13.     exit
  14. fi
  15.  
  16. # Read Command Line Arguments
  17. POSITIONAL=()
  18. while [[ $# -gt 0 ]]
  19. do
  20.     key="$1"
  21.     case $key in
  22.         -s)
  23.             SCRIPT_FOLDER_PATH="$2"
  24.             shift # past argument
  25.             shift # past value
  26.         ;;
  27.         -t)
  28.             TEST_FOLDER_PATH="$2"
  29.             shift # past argument
  30.             shift # past value
  31.         ;;
  32.         -r)
  33.             RESULT_DIR_NAME="$2"
  34.             shift # past argument
  35.             shift # past value
  36.         ;;
  37.         -i)
  38.             INTERPRETER="$2"
  39.             shift # past argument
  40.             shift # past value
  41.         ;;
  42.         *)    # unknown option
  43.             POSITIONAL+=("$1") # save it in an array for later
  44.             shift # past argument
  45.         ;;
  46.     esac
  47. done
  48. set -- "${POSITIONAL[@]}" # restore positional parameters
  49.  
  50.  
  51. [ -z "$SCRIPT_FOLDER_PATH" ] && printf "Script Folder path shouldn't be empty (use -s) \nexiting...\n" && exit
  52. echo "Script Folder path:    $(pwd)/${SCRIPT_FOLDER_PATH}"
  53.  
  54. [ -z "$TEST_FOLDER_PATH" ] && printf "Test Folders path shouldn't be empty (use -t) \nexiting...\n" && exit
  55. echo "Test Folders path:     $(pwd)/${TEST_FOLDER_PATH}"
  56.  
  57. [ -z "$RESULT_DIR_NAME" ] && printf "Result Directory Path shouldn't be empty (use -s) \nexiting...\n" && exit
  58. echo "Result Directory Path: $(pwd)/${RESULT_DIR_NAME}"
  59.  
  60. echo "Interpreter:           ${INTERPRETER}"
  61.  
  62. # delete Result Dir if exists
  63. if [ ! -d "${RESULT_DIR_NAME}" ]; then
  64.     mkdir ${RESULT_DIR_NAME}
  65. fi
  66.  
  67. echo
  68. echo "Starting Tests..."
  69.  
  70. compare_files(){
  71.     SCRIPT_PATH=$1
  72.     PTEST_DIR_NAME=$2
  73.     RES_DIR_NAME=$3
  74.     TNUM=$4
  75.    
  76.     FNAME=${SCRIPT_PATH%.*}
  77.     FNAME=${FNAME##*/}
  78.    
  79.     for i in $(seq 1 $TNUM);
  80.     do
  81.         while [ ${#i} -lt 3 ]; do
  82.             i=0$i
  83.         done
  84.        
  85.         CURRENT_TEST=${PTEST_DIR_NAME}${i}.dat
  86.         CURRENT_TEST_ANS=${PTEST_DIR_NAME}${i}.ans
  87.         DEST_FILE_NAME=${RES_DIR_NAME}/${FNAME}_${i}.txt
  88.        
  89.         # launch your python script
  90.         eval ${INTERPRETER} ${SCRIPT_PATH} \"$CURRENT_TEST\" "${DEST_FILE_NAME}"
  91.        
  92.         # check if correct
  93.         echo "Test ${i} : $(eval cmp --silent \"$CURRENT_TEST_ANS\" \"$DEST_FILE_NAME\" && echo "Success: files are same!" || echo "Failed: files are different")"
  94.     done
  95. }
  96.  
  97. run_test(){
  98.     TNAME="$1"
  99.     PROG_NAME="$2"
  100.     TEST_SUB_FOLD="$3"
  101.     TNUM="$4"
  102.    
  103.     echo
  104.     echo "### Checking ${TNAME}"
  105.    
  106.     SCRIPT=${SCRIPT_FOLDER_PATH}/${PROG_NAME}
  107.     TESTS=${TEST_FOLDER_PATH}/${TEST_SUB_FOLD}/
  108.     RES_DIR=${RESULT_DIR_NAME}/${TEST_SUB_FOLD}/
  109.    
  110.     if [ ! -f "${SCRIPT}" ]; then
  111.         echo "Script not found"
  112.         return
  113.     fi
  114.    
  115.     if [ -d "${RES_DIR}" ]; then
  116.         rm -rf ${RES_DIR}
  117.     fi
  118.     mkdir $RES_DIR
  119.     eval compare_files \"${SCRIPT}\" \"${TESTS}\" \"${RES_DIR}\" "${TNUM}"
  120. }
  121.  
  122. run_test 'Simple Read' 'SimpleRead.py' 'A' 5
  123. run_test 'Simple Write' 'SimpleWrite.py' 'B' 5
  124. run_test 'Complete Write' 'CompleteWrite.py' 'C' 10
  125. run_test 'Complete Read' 'CompleteRead.py' 'D' 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement