How to make a simple judge in bash to evaluate your test cases

Revision en5, by gabrc, 2020-05-20 03:43:20

I've created a Bash script to evaluate your own test cases:

#!/bin/bash
N=`ls *.in | wc -w`
for ((i = 1; i <= N; i++)); do
    echo ----- case $i -----
    diff -wb <($1 <$i.in) $i.out || break
done

To use this script, create test cases in the format 1.in, 1.out, 2.in, 2.out, 3.in, 3.out, ...

You need to pass your executable as first argument

By default, it will try doing tests sequentially until it finds a test case where it fails, then it will print the difference between your output and the correct output. If you want it to test every case, remove the part that says || break

I've also created a script that can create these files for you, which may be more convenient

#!/bin/bash
cases=`ls *.out 2>/dev/null` && N=`echo $cases | wc -w` || N=0
((N++))
echo Enter the contents of $N.in and press Ctrl+D when done
cat > $N.in
echo Enter the contents of $N.out and press Ctrl+D when done
cat > $N.out

You may replace cat with nano, vim, geany, etc if you prefer.

You may add both of these to your PATH, for instance you can create the folder ~/bin, add both files there, and then run:

echo PATH=$PATH:$HOME/bin >> ~/.bashrc

They need to be set as exectuable, i.e:

chmod +x ~/bin/judge
chmod +x ~/bin/mkcase

Then, if the judge is in ~/bin/judge and the case creator in ~/bin/mkcase, you can simply run judge ./program to test your program and run mkcase to make a new case

It can even be useful to learn and write these scripts during an on-site contest.

Both scripts use the Bash double parentheses construct that lets you write more C-like expressions, which isn't that well known.

Tags judge, local, script, #customtest

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en6 English gabrc 2020-05-20 03:44:56 5 similar change to judge
en5 English gabrc 2020-05-20 03:43:20 5 Change ls *.in -> ls *.out in order to make mkcase cancellable
en4 English gabrc 2020-05-20 01:18:32 89 (published)
en3 English gabrc 2020-05-20 01:16:44 106
en2 English gabrc 2020-05-20 01:14:10 510
en1 English gabrc 2020-05-20 01:08:16 1139 Initial revision (saved to drafts)