Files
usaco/lib/cmd/test.sh
T
adamfandClaude Opus 5.5 1aee0050a0 Add Rust support: usaco lang, lint, and main.rs problems
Brings in the stashed Rust work, merged onto the current CLI. A
problem's language is whichever source it has (main.cpp or main.rs);
'usaco lang' sets the default for new problems (.lang, or $USACO_LANG),
and 'new --rust/--cpp' picks per problem. template.rs is a paste-ready
Rust template whose stdio() uses <io>.in/.out when present and
stdin/stdout otherwise.

run/test now feed <io>.in on stdin and capture stdout when the program
doesn't write <io>.out itself, so Codeforces-style solutions work too.
'reset <name> rust|cpp' switches a problem's language (template is
still an alias), and --debug builds Rust with overflow checks and debug
assertions. 'usaco lint' runs clippy through a generated, gitignored
Cargo.toml that also gives rust-analyzer a project; done/skip/undo
keep it in step. LeetCode imports stay C++.

Also: done stages solved/ and solving/ separately, so a problem that
was never committed from solving/ still gets its "Solve" commit, and
the site lists main.rs ahead of other files like it does main.cpp.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 21:17:50 +01:00

107 lines
3.7 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"
start=$(date +%s%N)
if ! run_solution "$dir" "$io"; 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 ]]