個人で利用している物です。
Atcoder-CLIのファイル構成をもとに作成しています。
ojのテスト実行の代替になるものです。
このshファイルを実行すると、Pythonとg++の切り替えをせずともテストケースを実行、結果を表示できます。
VSCodeのショートカットを設定すると使いやすいかと思います。
ショートカットの設定方法は以下の記事をご参照ください。
https://zenn.dev/tooyya/articles/b22a84994e131e
実行方法は、test.sh ${実行したいファイルのパス} です。
test.sh
#!/bin/bash
src="$1"
if [ -z "$src" ]; then
echo "Usage: run_tests.sh path/to/source.cpp_or_source.py"
exit 1
fi
ext="${src##*.}"
base="$(basename "$src" ."$ext")"
dir="$(dirname "$src")"
test_dir="$dir/tests"
case "$ext" in
cpp)
tmp_exec=$(mktemp /tmp/cpp_exec.XXXXXX)
#ACLのファイルパスです。パスを置き換えてください。
acl_path="${ACL_PATH:-$HOME/projects/kyopro/ac-library}"
if ! g++ -std=gnu++17 -O2 -Wall -I "$acl_path" "$src" -o "$tmp_exec"; then
echo "❌ CE: コンパイルに失敗しました"
exit 1
fi
run_command="$tmp_exec"
;;
py)
run_command="python3 \"$src\""
;;
*)
echo "Unsupported file extension: .$ext"
exit 1
;;
esac
if [ ! -d "$test_dir" ]; then
echo "❌ テストディレクトリが見つかりません: $test_dir"
exit 1
fi
ac_count=0
wa_count=0
re_count=0
total_count=0
for input_file in "$test_dir"/*.in; do
[ -e "$input_file" ] || continue
testname="$(basename "$input_file" .in)"
expected_file="$test_dir/$testname.out"
actual_output=$(mktemp)
error_output=$(mktemp)
if [ ! -f "$expected_file" ]; then
echo "⚠️ $expected_file が存在しません。スキップします。"
continue
fi
if ! eval "$run_command" < "$input_file" > "$actual_output" 2> "$error_output"; then
echo "❌ [$testname] RE"
echo "--- エラーメッセージ ---"
cat "$error_output"
echo "------------------------"
re_count=$((re_count + 1))
total_count=$((total_count + 1))
rm "$actual_output" "$error_output"
continue
fi
if diff -q "$expected_file" "$actual_output" > /dev/null; then
echo "✅ [$testname] AC"
ac_count=$((ac_count + 1))
else
echo "❌ [$testname] WA"
echo "--- Expected Answer ---"
cat "$expected_file"
echo "--- Actual Answer ---"
cat "$actual_output"
echo "-----------------------"
wa_count=$((wa_count + 1))
fi
total_count=$((total_count + 1))
rm "$actual_output" "$error_output"
done
[ "$ext" = "cpp" ] && rm "$tmp_exec"
echo
echo "=== テスト結果 ==="
echo "合計: $total_count, AC: $ac_count, WA: $wa_count, RE: $re_count"