mirror of
https://github.com/Hizenberg469/Algorithms-snippets.git
synced 2026-04-19 22:52:23 +03:00
Bash script for playground updated
This commit is contained in:
@@ -1,5 +1,18 @@
|
||||
#!/bin/bash
|
||||
|
||||
debug=false
|
||||
if [ $# -eq 1 ]; then
|
||||
|
||||
if [[ $1 != "-d" ]]; then
|
||||
echo "Invalid argument"
|
||||
echo "$0 -d for debug mode"
|
||||
exit 4
|
||||
fi
|
||||
|
||||
debug=true
|
||||
fi
|
||||
|
||||
|
||||
#Creating .txt files
|
||||
TESTCASE_FILE=IOFiles/testcase.txt
|
||||
OUTPUT_FILE=IOFiles/output.txt
|
||||
@@ -33,8 +46,31 @@ pass_count=0
|
||||
fail_count=0
|
||||
|
||||
# Separate test cases and expected outputs by empty lines
|
||||
mapfile -t test_cases < <(awk -v RS= '1' "$TESTCASE_FILE")
|
||||
mapfile -t expected_outputs < <(awk -v RS= '1' "$CORRECT_ANSWER_FILE")
|
||||
|
||||
test_cases=()
|
||||
exepected_outputs=()
|
||||
|
||||
test_case_block=""
|
||||
while IFS= read -r line || [ -n "$line" ]; do
|
||||
if [ -z "$line" ];then
|
||||
test_cases+=("$test_case_block")
|
||||
test_case_block=""
|
||||
else
|
||||
test_case_block="${test_case_block}${line}"$'\n'
|
||||
fi
|
||||
done < "$TESTCASE_FILE"
|
||||
test_cases+=("$test_case_block")
|
||||
|
||||
output_block=""
|
||||
while IFS= read -r line || [ -n "$line" ]; do
|
||||
if [ -z "$line" ]; then
|
||||
expected_outputs+=("$output_block")
|
||||
output_block=""
|
||||
else
|
||||
output_block="${output_block}${line}"$'\n'
|
||||
fi
|
||||
done < "$CORRECT_ANSWER_FILE"
|
||||
expected_outputs+=("$output_block")
|
||||
|
||||
# Ensure number of testcase is same as number of output.
|
||||
if [ ${#test_cases[@]} -ne ${#expected_outputs[@]} ]; then
|
||||
@@ -42,12 +78,13 @@ if [ ${#test_cases[@]} -ne ${#expected_outputs[@]} ]; then
|
||||
exit 2
|
||||
fi
|
||||
|
||||
|
||||
for (( test_num = 0 ; test_num < ${#test_cases[@]} ; test_num++))
|
||||
do
|
||||
test_case=${test_cases[test_num]}
|
||||
expected_output=${expected_outputs[test_num]}
|
||||
test_case="${test_cases[test_num]}"
|
||||
expected_output="${expected_outputs[test_num]}"
|
||||
|
||||
if ! echo "$test_case" | ./$SOLUTION > "$OUTPUT_FILE"; then
|
||||
if ! echo -e "$test_case" | ./$SOLUTION > "$OUTPUT_FILE"; then
|
||||
echo -e "\033[0;31mTest #$(($test_num+1)): Execution failed\033[0m"
|
||||
echo "solution executable didn't executed successfully"
|
||||
let fail_count++
|
||||
@@ -56,17 +93,27 @@ do
|
||||
|
||||
test_output=$(<"$OUTPUT_FILE")
|
||||
|
||||
if diff -q -Z <(echo "$test_output") <(echo "$expected_output") > /dev/null; then
|
||||
if diff -q -Z <(echo $test_output) <(echo $expected_output) > /dev/null; then
|
||||
echo -e "\033[0;32mTest #$(($test_num+1)): PASS\033[0m"
|
||||
|
||||
#Debug mode - in case to see the output.
|
||||
if $debug; then
|
||||
echo "Test case:"
|
||||
echo -n "$test_case"
|
||||
echo "Your output:"
|
||||
echo -n "$test_output"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
let pass_count++
|
||||
else
|
||||
echo -e "\033[0;31mTest #$(($test_num+1)): FAIL\033[0m"
|
||||
echo "Test case:"
|
||||
echo $test_case
|
||||
echo -n "$test_case"
|
||||
echo "Your output:"
|
||||
echo $test_output
|
||||
echo "$test_output"
|
||||
echo "Correct output:"
|
||||
echo $expected_output
|
||||
echo -n "$expected_output"
|
||||
let fail_count++
|
||||
fi
|
||||
done
|
||||
|
||||
@@ -1,11 +1,32 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ $# -ne 1 ]; then
|
||||
echo "Provide the limit of stress testing"
|
||||
echo "$0 'test case limit'"
|
||||
echo "$0 -1 (for unlimited)"
|
||||
exit 1
|
||||
fi
|
||||
# if [ $# -ne 1 ]; then
|
||||
# echo "Provide the limit of stress testing"
|
||||
# echo "$0 'test case limit'"
|
||||
# echo "$0 -1 (for unlimited)"
|
||||
# echo "OPTIONAL: $0 -n (for running with bruteforce)"
|
||||
# exit 1
|
||||
# fi
|
||||
|
||||
limit=0
|
||||
flag=true
|
||||
while getopts l:nh param; do
|
||||
case $param in
|
||||
l)
|
||||
limit=$OPTARG
|
||||
;;
|
||||
n)
|
||||
flag=false
|
||||
;;
|
||||
h)
|
||||
echo "Provide the limit of stress testing"
|
||||
echo "$0 'test case limit'"
|
||||
echo "$0 -1 (for unlimited)"
|
||||
echo "OPTIONAL: $0 -n (for running with bruteforce)"
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
#Required tools
|
||||
SOLUTION=build/solution
|
||||
@@ -24,24 +45,34 @@ pass_count=0
|
||||
|
||||
while (( 1 ))
|
||||
do
|
||||
./generator > $TESTCASE_FILE
|
||||
./SOLUTION < $TESTCASE_FILE > $OUTPUT_FILE
|
||||
./BRUTEFORCE < $TESTCASE_FILE > $CORRECT_ANSWER_FILE
|
||||
./$GENERATOR $(($pass_count+1)) > $TESTCASE_FILE
|
||||
./$SOLUTION < $TESTCASE_FILE > $OUTPUT_FILE
|
||||
|
||||
if diff -q -Z $OUTPUT_FILE $CORRECT_ANSWER_FILE >> /dev/null; then
|
||||
echo -e "\033[0;31mTest #$(($pass_count+1)): FAIL\033[0m"
|
||||
if $flag ; then
|
||||
./$BRUTEFORCE < $TESTCASE_FILE > $CORRECT_ANSWER_FILE
|
||||
|
||||
if diff -q -Z $OUTPUT_FILE $CORRECT_ANSWER_FILE >> /dev/null; then
|
||||
echo -e "\033[0;31mTest #$(($pass_count+1)): FAIL\033[0m"
|
||||
echo "Test case:"
|
||||
cat $TESTCASE_FILE
|
||||
echo "Your output:"
|
||||
cat $OUTPUT_FILE
|
||||
echo "Correct output:"
|
||||
cat $CORRECT_ANSWER_FILE
|
||||
break
|
||||
fi
|
||||
|
||||
echo -e "\033[0;32mPassed test: #$(($pass_count+1))\033[0m"
|
||||
else
|
||||
echo -e "Test #$(($pass_count+1)):"
|
||||
echo "Test case:"
|
||||
cat $TESTCASE_FILE
|
||||
echo "Your output:"
|
||||
cat $OUTPUT_FILE
|
||||
echo "Correct output:"
|
||||
cat $CORRECT_ANSWER_FILE
|
||||
break
|
||||
fi
|
||||
|
||||
echo -e "\033[0;32mPassed test: #$(($pass_count+1))\033[0m"
|
||||
|
||||
if [ $1 -ne -1 ] || [ $pass_count -lt $1 ]; then
|
||||
let pass_count++
|
||||
if [ $limit -ne -1 ] && [ $pass_count -ge $limit ]; then
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user