108 lines
3.8 KiB
Bash
108 lines
3.8 KiB
Bash
# usaco test <name> [nums...] | usaco test create <name>
|
|
norm() { # trim trailing whitespace and trailing blank lines so diffs aren't pedantic
|
|
sed 's/[[:space:]]*$//' "$1" | awk 'NF{for(i=0;i<b;i++)print ""; b=0; print; next}{b++}'
|
|
}
|
|
|
|
test_create() { # $1 = problem dir, $2 = io basename
|
|
local dir="$1" io="$2" n
|
|
mkdir -p "$dir/tests"
|
|
n=1; while [[ -e "$dir/tests/$n.in" ]]; do n=$((n+1)); done
|
|
if [[ -s "$dir/$io.in" ]]; then
|
|
cp "$dir/$io.in" "$dir/tests/$n.in"
|
|
echo "Case $n input: took current $io.in ($(wc -l < "$dir/tests/$n.in") lines)"
|
|
else
|
|
echo "Case $n input: paste it, then Ctrl-D on an empty line"
|
|
cat > "$dir/tests/$n.in"
|
|
fi
|
|
echo "Case $n expected output: paste it, then Ctrl-D (just Ctrl-D to skip checking)"
|
|
cat > "$dir/tests/$n.out"
|
|
if [[ ! -s "$dir/tests/$n.out" ]]; then
|
|
rm "$dir/tests/$n.out"
|
|
echo "Saved tests/$n.in (no expected output — 'usaco t' will just show what it prints)"
|
|
else
|
|
echo "Saved tests/$n.in + tests/$n.out"
|
|
fi
|
|
}
|
|
|
|
run_case() { # $1 = dir, $2 = io, $3 = tests/N.in path; returns 0 pass, 1 fail, 2 unchecked
|
|
local dir="$1" io="$2" tin="$3" n ms start exp
|
|
n="$(basename "$tin" .in)"
|
|
cp "$tin" "$dir/$io.in"
|
|
rm -f "$dir/$io.out"
|
|
start=$(date +%s%N)
|
|
if ! (cd "$dir" && ./main); then
|
|
echo "${RED}✗ case $n CRASHED (nonzero exit)${RST}"
|
|
return 1
|
|
fi
|
|
ms=$(( ($(date +%s%N) - start) / 1000000 ))
|
|
exp="$dir/tests/$n.out"
|
|
if [[ ! -e "$exp" ]]; then
|
|
echo "${YEL}• case $n${RST} ${DIM}(${ms}ms, no expected output)${RST} got:"
|
|
sed 's/^/ /' "$dir/$io.out" 2>/dev/null || echo " (no $io.out written)"
|
|
return 2
|
|
elif diff -q <(norm "$exp") <(norm "$dir/$io.out") >/dev/null 2>&1; then
|
|
echo "${GRN}✓ case $n${RST} ${DIM}(${ms}ms)${RST}"
|
|
return 0
|
|
else
|
|
echo "${RED}✗ case $n${RST} ${DIM}(${ms}ms)${RST}"
|
|
diff "$DIFF_COLOR" --label expected --label got -u \
|
|
<(norm "$exp") <(norm "$dir/$io.out") | sed 's/^/ /' | tail -n +3
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
[[ $# -ge 1 ]] || usage
|
|
sub=""
|
|
case "${1:-}" in create|add|new) sub=create; shift ;; esac
|
|
[[ $# -ge 1 ]] || usage
|
|
dir="$(find_problem "$1")"; shift || true
|
|
# io basename = the freopen filename ("<io>.in" in the problem dir)
|
|
io="$(detect_io "$dir" || true)"
|
|
[[ -n "$io" ]] || { echo "Error: no .in file in $dir — can't tell the io name" >&2; exit 1; }
|
|
|
|
if [[ "$sub" == create ]]; then
|
|
test_create "$dir" "$io"
|
|
exit 0
|
|
fi
|
|
|
|
# run mode: usaco t <name> [case-numbers...]
|
|
shopt -s nullglob
|
|
cases=("$dir"/tests/*.in)
|
|
shopt -u nullglob
|
|
if [[ ${#cases[@]} -eq 0 ]]; then
|
|
echo "No test cases yet. Create one with: usaco t create $(basename "$dir")" >&2
|
|
exit 1
|
|
fi
|
|
if [[ $# -gt 0 ]]; then # only the requested case numbers
|
|
picked=()
|
|
for want in "$@"; do
|
|
[[ -e "$dir/tests/$want.in" ]] || { echo "Error: tests/$want.in not found" >&2; exit 1; }
|
|
picked+=("$dir/tests/$want.in")
|
|
done
|
|
cases=("${picked[@]}")
|
|
fi
|
|
build_problem "$dir"
|
|
|
|
# keep whatever's in <io>.in/.out now, restore afterwards
|
|
tmp="$(mktemp -d)"
|
|
[[ -e "$dir/$io.in" ]] && cp "$dir/$io.in" "$tmp/in.orig"
|
|
[[ -e "$dir/$io.out" ]] && cp "$dir/$io.out" "$tmp/out.orig"
|
|
restore() {
|
|
[[ -e "$tmp/in.orig" ]] && cp "$tmp/in.orig" "$dir/$io.in"
|
|
[[ -e "$tmp/out.orig" ]] && cp "$tmp/out.orig" "$dir/$io.out"
|
|
rm -rf "$tmp"
|
|
}
|
|
trap restore EXIT
|
|
|
|
pass=0; fail=0
|
|
while IFS= read -r tin; do
|
|
rc=0; run_case "$dir" "$io" "$tin" || rc=$?
|
|
case $rc in 0) pass=$((pass+1)) ;; 1) fail=$((fail+1)) ;; esac
|
|
done < <(printf '%s\n' "${cases[@]}" | sort -V)
|
|
if [[ $fail -eq 0 ]]; then
|
|
echo "── ${GRN}$pass passed, $fail failed${RST} ──"
|
|
else
|
|
echo "── $pass passed, ${RED}$fail failed${RST} ──"
|
|
fi
|
|
[[ $fail -eq 0 ]]
|