test: Polish test script to work on single project or all at once

This commit is contained in:
Tomáš Kléger 2025-10-13 19:02:29 +02:00
parent 9adde41415
commit 9755abd043

View File

@ -1,12 +1,18 @@
#!/bin/bash
CC_COMMAND='g++ -Wall -Wextra -Werror -pedantic -D__PROGTEST__'
CC_COMMAND='g++ -Wall -Wextra -Werror -pedantic -D__PROGTEST__ -Wdouble-promotion -Wfloat-conversion'
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
RESET="\e[0m"
if [[ "$1" =~ --all|-a ]]; then
check_all=true
else
check_all=false
fi
EXIT_STATUS=0
_log() {
@ -36,23 +42,30 @@ _log() {
printf "[${log_color}${log_level_name}${RESET}] %s\n" "$log_message"
}
for dir in homework-* exercise-*; do
_test_dir() {
local dir="$1"
if ! cd "$dir" > /dev/null; then
_log 2 "Couldn't cd into $dir"
cd ..
continue
return 1
fi
local dir_name="${PWD##*/}"
if [[ ! -f ./main.c ]]; then
_log 1 "No main.c file in $dir"
cd ..
continue
_log 1 "No main.c file in $dir_name"
return 1
fi
if [[ ! -d ./test_data ]]; then
_log 1 "No test_data directory in $dir_name"
return 1
fi
if ! eval "$CC_COMMAND main.c"; then
_log 2 "Failed to compile code in $dir"
cd ..
continue
_log 2 "Failed to compile code in $dir_name"
EXIT_STATUS=1
return 1
fi
failed_inputs=0
@ -61,22 +74,25 @@ for dir in homework-* exercise-*; do
input_file=${input//*\/}
test_case_number=${input_file%%_in.txt}
command_output="$(diff <(./a.out < "test_data/${test_case_number}_in.txt") "test_data/${test_case_number}_out.txt")"
if [[ ! "$?" ]]; then
if ! diff --color <(./a.out < "test_data/${test_case_number}_in.txt") "test_data/${test_case_number}_out.txt"; then
EXIT_STATUS=1
((failed_inputs++))
_log 2 "$dir (#$test_case_number)"
echo "$command_output"
fi
done
if ((failed_inputs == 0)); then
_log 0 "$dir"
_log 0 "$dir_name"
fi
}
if ! $check_all; then
_test_dir .
else
for dir in homework-* exercise-*; do
_test_dir "$dir"
cd .. > /dev/null
done
done
fi
exit $EXIT_STATUS