Skip to content

Instantly share code, notes, and snippets.

@kirumbik
Last active February 12, 2024 07:27
Show Gist options
  • Save kirumbik/48a2b404cfbcd35b3aba9acbc56ba226 to your computer and use it in GitHub Desktop.
Save kirumbik/48a2b404cfbcd35b3aba9acbc56ba226 to your computer and use it in GitHub Desktop.
Check tests for Route 365 contest
#!/bin/bash
COLOR_BLACK=0
COLOR_RED=1
COLOR_GREEN=2
COLOR_YELLOW=3
COLOR_GREY=8
reset_color="$(tput sgr0)"
source_file=${1:-main.go}
print_error() {
local error_text="$1"
local color_red="$(tput setaf $COLOR_RED)"
echo "${color_red}ERROR: ${error_text}${reset_color}"
}
highlight () {
local text="$1"
local color="$2"
local color_text="$(tput setab $color; tput setaf $COLOR_BLACK)"
echo "${color_text} $text ${reset_color}"
}
print_time () {
local runtime=$1
if (( $(echo "$runtime > 1000" | bc -l) )); then
local color=$COLOR_RED
elif (( $(echo "$runtime > 500" | bc -l) )); then
local color=$COLOR_YELLOW
else
local color=$COLOR_GREY
fi
local color_time="$(tput setaf $color)"
printf "${color_time}%.0fms${reset_color}" "$runtime"
}
clear_line () {
tput cuu1
tput el
}
if [ ! -f "$source_file" ]; then
print_error "File '$source_file' does not exist"
tput bel
exit 1
fi
tests_dir="$(dirname $(readlink -f $source_file))/tests"
if [ ! -d "$tests_dir" ]; then
print_error "Tests directory does not exist"
tput bel
exit 1
fi
if ! which dos2unix &> /dev/null; then
print_error "dos2unix not installed. Please install before running."
tput bel
exit 1
fi
for test_input in "$tests_dir"/*
do
if [[ "$test_input" =~ ^"$tests_dir"/([0-9]+)$ ]]; then
test_num="${BASH_REMATCH[1]}"
test_output="${tests_dir}/${test_num}.a"
test_name="Test $test_num"
echo "$(highlight "RUNS" $COLOR_YELLOW) $test_name"
start_time=$(date +%s.%N)
output=$(go run $source_file < "$test_input" | dos2unix)
end_time=$(date +%s.%N)
runtime=$(echo "($end_time - $start_time) * 1000" | bc)
clear_line
expected_output=$(cat "$test_output" | dos2unix)
if diff -Z -q <(echo "$output") <(echo "$expected_output") > /dev/null ; then
echo "$(highlight "PASS" $COLOR_GREEN) $test_name $(print_time $runtime)"
else
echo "$(highlight "FAIL" $COLOR_RED) $test_name"
diff -Z -y --color=auto <(echo "$output") <(echo "$expected_output")
exit 1
fi
fi
done
@kirumbik
Copy link
Author

Usage
Let's pretend you have directory contest where you store contest code. Place run_tests.sh script in root of contest directory.
Place tests near you go code:

./b/main.go
./b/tests/
 |----01
 |----01.a
 |----...
 |----08
 |----08.a
./c/main.go
./c/tests/
 |----01
 |----01.a
 |----...

After that run:
./run_tests.sh ./b/main.go

You will see each test running:

Screenshot_37-2

@Ismail14098
Copy link

MacOS:

  • install homebrew
  • brew install dos2unix
  • brew install diffutils
  • brew install coreutils
  • add gnubin into your PATH variable
    PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment