Skip to content

Instantly share code, notes, and snippets.

@natanbc
Last active April 8, 2019 23:01
Show Gist options
  • Save natanbc/dac9435b2df118c01413864de94acd04 to your computer and use it in GitHub Desktop.
Save natanbc/dac9435b2df118c01413864de94acd04 to your computer and use it in GitHub Desktop.
run.codes test runner

Test Runner

Installation

Copy the test-runner.sh file somewhere and set it as executable (chmod +x test-runner.sh)

Usage

Run ./test-runner.sh <folder>.

The folder to be executed must have the following structure (trabalho-1):

|-trabalho-1
|  |-testes
|  |  |-README.txt
|  |  |-7.out
|  |  |-7.in
|  |  |-6.out
|  |  |-6.in
|  |  |-5.out
|  |  |-5.in
|  |  |-4.out
|  |  |-4.in
|  |  |-3.out
|  |  |-3.in
|  |  |-2.out
|  |  |-2.in
|  |  |-1.out
|  |  |-1.in
|  |-main.c
|-test-runner.sh

The main file must be named main.c, and the test cases must be in a subfolder called testes in the form N.in for inputs, N.out for expected outputs.

GCC flags can be used by setting the CFLAGS environment variable:

CFLAGS="-O3" ./test-runner.sh FOLDER

CFLAGS defaults to -Wall -Wextra -pedantic -Werror

#!/bin/bash
echoerr() { echo "$@" 1>&2; }
cd $1
CFLAGS=${CFLAGS:--Wall -Wextra -pedantic -Werror}
echo "CFLAGS = $CFLAGS"
gcc $CFLAGS main.c -o main || { echoerr "Compilation failed"; exit 1; }
for filename in testes/*.in; do
[ -e "$filename" ] || continue
name=${filename##*/}
base=${name%.in}
echo "Testing input $name and output $base.out";
$(./main < "$filename" > "$base.out.real") || { echoerr "Execution failed"; exit 1; }
diff=$(diff -aZ "$base.out.real" "testes/$base.out" 2>&1)
if [[ $diff ]]; then
echoerr "Outputs differ";
echoerr "$diff"
exit 1;
fi
rm "$base.out.real"
done
rm main
echo "Success!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment