Last active
October 12, 2019 15:56
-
-
Save CSenshi/f26c51ef907ff28e0c4562fc197c256e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # !/bin/bash | |
| if [ "$#" -ne 10 ] && [ "$#" -ne 8 ] && [ "$#" -ne 12 ] ; then | |
| echo "Invalid Arguments" | |
| echo "Usage: script requires 3-5 Arguments: " | |
| echo " -s Path to folder which contains following scripts with exact names:" | |
| echo " SimpleRead.py, SimpleWrite.py, CompleteWrite.py, CompleteRead.py" | |
| echo " -t Path to folder which contains public tests(where A,B,C,D folders are located)" | |
| echo " -T Path to folder which contains test scriptis (i.e. test_C.py, test_d.py" | |
| echo " -r Path to folder where we should put output for each test" | |
| echo " -e Extension of your file (pass with dot exaple: .py)" | |
| echo " -i Your Interpreter (If using executable " | |
| echo "Example: /check.sh -s src/ -t public_tests/ -r res/ -e .py -i python3 -T script/" | |
| exit | |
| fi | |
| # Read Command Line Arguments | |
| POSITIONAL=() | |
| while [[ $# -gt 0 ]] | |
| do | |
| key="$1" | |
| case $key in | |
| -s) | |
| SCRIPT_FOLDER_PATH="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| -t) | |
| TEST_FOLDER_PATH="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| -T) | |
| TESTER_SCRIPT_FOLDER="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| -r) | |
| RESULT_DIR_NAME="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| -e) | |
| EXTENSION="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| -i) | |
| INTERPRETER="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| *) # unknown option | |
| POSITIONAL+=("$1") # save it in an array for later | |
| shift # past argument | |
| ;; | |
| esac | |
| done | |
| set -- "${POSITIONAL[@]}" # restore positional parameters | |
| [ -z "$SCRIPT_FOLDER_PATH" ] && printf "Script Folder path shouldn't be empty (use -s) \nexiting...\n" && exit | |
| echo "Script Folder path: $(pwd)/${SCRIPT_FOLDER_PATH}" | |
| [ -z "$TEST_FOLDER_PATH" ] && printf "Test Folders path shouldn't be empty (use -t) \nexiting...\n" && exit | |
| echo "Test Folders path: $(pwd)/${TEST_FOLDER_PATH}" | |
| [ -z "$RESULT_DIR_NAME" ] && printf "Result Directory Path shouldn't be empty (use -s) \nexiting...\n" && exit | |
| echo "Result Directory Path: $(pwd)/${RESULT_DIR_NAME}" | |
| echo "Extension: ${EXTENSION}" | |
| echo "Interpreter: ${INTERPRETER}" | |
| # delete Result Dir if exists | |
| if [ ! -d "${RESULT_DIR_NAME}" ]; then | |
| mkdir ${RESULT_DIR_NAME} | |
| fi | |
| echo | |
| echo "Starting Tests..." | |
| compare_files(){ | |
| SCRIPT_PATH=$1 | |
| PTEST_DIR_NAME=$2 | |
| RES_DIR_NAME=$3 | |
| TNUM=$4 | |
| FNAME=${SCRIPT_PATH%.*} | |
| FNAME=${FNAME##*/} | |
| for i in $(seq 1 $TNUM); | |
| do | |
| while [ ${#i} -lt 3 ]; do | |
| i=0$i | |
| done | |
| CURRENT_TEST=${PTEST_DIR_NAME}${i}.dat | |
| CURRENT_TEST_ANS=${PTEST_DIR_NAME}${i}.ans | |
| DEST_FILE_NAME=${RES_DIR_NAME}/${FNAME}_${i}.txt | |
| # launch your python script | |
| eval ${INTERPRETER} ${SCRIPT_PATH} \"$CURRENT_TEST\" "${DEST_FILE_NAME}" | |
| # check if correct | |
| echo "Test ${i} : $(eval diff -w -q \"$CURRENT_TEST_ANS\" \"$DEST_FILE_NAME\" && echo "Success: files are same!" || echo "Failed: files are different")" | |
| done | |
| } | |
| run_test(){ | |
| TNAME="$1" | |
| PROG_NAME="$2" | |
| TEST_SUB_FOLD="$3" | |
| TNUM="$4" | |
| echo | |
| echo "### Checking ${TNAME}" | |
| SCRIPT=${SCRIPT_FOLDER_PATH}/${PROG_NAME} | |
| TESTS=${TEST_FOLDER_PATH}/${TEST_SUB_FOLD}/ | |
| RES_DIR=${RESULT_DIR_NAME}/${TEST_SUB_FOLD}/ | |
| if [ ! -f "${SCRIPT}" ]; then | |
| echo "Script not found (Try using -e flag)" | |
| return | |
| fi | |
| if [ -d "${RES_DIR}" ]; then | |
| rm -rf ${RES_DIR} | |
| fi | |
| mkdir $RES_DIR | |
| eval compare_files \"${SCRIPT}\" \"${TESTS}\" \"${RES_DIR}\" "${TNUM}" | |
| } | |
| compare_files1(){ | |
| SCRIPT_PATH=$1 | |
| PTEST_DIR_NAME=$2 | |
| RES_DIR_NAME=$3 | |
| TNUM=$4 | |
| TEST_SCRIPT=$5 | |
| FNAME=${SCRIPT_PATH%.*} | |
| FNAME=${FNAME##*/} | |
| for i in $(seq 1 $TNUM); | |
| do | |
| while [ ${#i} -lt 3 ]; do | |
| i=0$i | |
| done | |
| CURRENT_TEST=${PTEST_DIR_NAME}${i}.dat | |
| CURRENT_TEST_ANS=${PTEST_DIR_NAME}${i}.ans | |
| DEST_FILE_NAME=${RES_DIR_NAME}/${FNAME}_${i}.txt | |
| # launch your python script | |
| eval ${INTERPRETER} ${SCRIPT_PATH} \"$CURRENT_TEST\" "${DEST_FILE_NAME}" | |
| # check if correct | |
| echo "Test ${i}: " | |
| python3 "${TESTER_SCRIPT_FOLDER}/${TEST_SCRIPT}" "$CURRENT_TEST_ANS" "${DEST_FILE_NAME}" | |
| echo | |
| done | |
| } | |
| run_test1(){ | |
| TNAME="$1" | |
| PROG_NAME="$2" | |
| TEST_SUB_FOLD="$3" | |
| TNUM="$4" | |
| TEST_SCRIPT="$5" | |
| echo | |
| echo "### Checking ${TNAME}" | |
| SCRIPT=${SCRIPT_FOLDER_PATH}/${PROG_NAME} | |
| TESTS=${TEST_FOLDER_PATH}/${TEST_SUB_FOLD}/ | |
| RES_DIR=${RESULT_DIR_NAME}/${TEST_SUB_FOLD}/ | |
| if [ ! -f "${SCRIPT}" ]; then | |
| echo "Script not found (Try using -e flag)" | |
| return | |
| fi | |
| if [ -d "${RES_DIR}" ]; then | |
| rm -rf ${RES_DIR} | |
| fi | |
| mkdir $RES_DIR | |
| eval compare_files1 \"${SCRIPT}\" \"${TESTS}\" \"${RES_DIR}\" "${TNUM}" ${TEST_SCRIPT} | |
| } | |
| compare_files2(){ | |
| SCRIPT_PATH=$1 | |
| PTEST_DIR_NAME=$2 | |
| RES_DIR_NAME=$3 | |
| TNUM=$4 | |
| FNAME=${SCRIPT_PATH%.*} | |
| FNAME=${FNAME##*/} | |
| for i in $(seq 1 $TNUM); | |
| do | |
| while [ ${#i} -lt 3 ]; do | |
| i=0$i | |
| done | |
| CURRENT_TEST=${PTEST_DIR_NAME}${i}.dat | |
| CURRENT_TEST_CODE=${PTEST_DIR_NAME}${i}.code | |
| CURRENT_TEST_ANS=${PTEST_DIR_NAME}${i}.ans | |
| DEST_FILE_NAME=${RES_DIR_NAME}/${FNAME}_${i}.txt | |
| # launch your python script | |
| eval ${INTERPRETER} ${SCRIPT_PATH} \"$CURRENT_TEST_CODE\" \"$CURRENT_TEST\" "${DEST_FILE_NAME}" | |
| # check if correct | |
| echo "Test ${i} : $(eval diff -w -q \"$CURRENT_TEST_ANS\" \"$DEST_FILE_NAME\" && echo "Success: files are same!" || echo "Failed: files are different")" | |
| done | |
| } | |
| run_test2(){ | |
| TNAME="$1" | |
| PROG_NAME="$2" | |
| TEST_SUB_FOLD="$3" | |
| TNUM="$4" | |
| echo | |
| echo "### Checking ${TNAME}" | |
| SCRIPT=${SCRIPT_FOLDER_PATH}/${PROG_NAME} | |
| TESTS=${TEST_FOLDER_PATH}/${TEST_SUB_FOLD}/ | |
| RES_DIR=${RESULT_DIR_NAME}/${TEST_SUB_FOLD}/ | |
| if [ ! -f "${SCRIPT}" ]; then | |
| echo "Script not found (Try using -e flag)" | |
| return | |
| fi | |
| if [ -d "${RES_DIR}" ]; then | |
| rm -rf ${RES_DIR} | |
| fi | |
| mkdir $RES_DIR | |
| eval compare_files2 \"${SCRIPT}\" \"${TESTS}\" \"${RES_DIR}\" "${TNUM}" | |
| } | |
| run_test "Distrib" "Distrib${EXTENSION}" "A" 2 | |
| run_test "Entropy" "Entropy${EXTENSION}" "B" 2 | |
| run_test1 "PrefCode" "PrefCode${EXTENSION}" "C" 8 "test_C.py" | |
| run_test1 "Huffman" "Huffman${EXTENSION}" "D" 5 "test_D.py" | |
| run_test2 "Compress" "Compress${EXTENSION}" "E" 5 | |
| run_test2 "Decompress" "Decompress${EXTENSION}" "F" 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import os | |
| ans_file = sys.argv[1] | |
| my_file = sys.argv[2] | |
| with open(ans_file, 'r') as af, open(my_file, 'r') as mf: | |
| cv = float(af.readline()) | |
| if cv > 1: | |
| if (os.stat(my_file).st_size == 0): | |
| print("True") | |
| else: | |
| print("False: Should be empty, Craft Value is {}".format(cv)) | |
| exit() | |
| arr = list(map(int, af.readline().split())) | |
| code_arr = [x for x in mf.read().splitlines()] | |
| res_arr = [len(x) for x in code_arr] | |
| if arr != res_arr: | |
| print('False') | |
| print('Answer: {} Length:{}'.format(arr, len(arr))) | |
| print('Your Result: {} Length:{}'.format(res_arr, len(res_arr))) | |
| exit() | |
| # check for prefix | |
| for c1 in code_arr: | |
| for c2 in code_arr: | |
| if c1 != c2 and c1.startswith(c2): | |
| print("False : {} is prefix of {}".format(c2, c1)) | |
| exit() | |
| print('True') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| ans_file = sys.argv[1] | |
| my_file = sys.argv[2] | |
| with open(ans_file, 'r') as af, open(my_file, 'r') as mf: | |
| ans_mean = float(af.readline()) | |
| af.readline() | |
| ans_freq_arr = list(map(float, af.readline().split())) | |
| arr = [x for x in mf.read().splitlines()] | |
| sum = 0 | |
| for i in range(len(arr)): | |
| sum += len(arr[i])*ans_freq_arr[i] | |
| sum = round(sum, 6) | |
| if sum != ans_mean: | |
| print("False") | |
| print("Answer Mean : {}".format(ans_mean)) | |
| print("Your Mean : {}".format(sum)) | |
| exit() | |
| # check for prefix | |
| for c1 in arr: | |
| for c2 in arr: | |
| if c1 != c2 and c1.startswith(c2): | |
| print("False : {} is prefix of {}".format(c2, c1)) | |
| print("True") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment