0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

競プロ用のテストシェルスクリプトを作った

Last updated at Posted at 2020-02-06

はじめに

データ構造の勉強のために競プロをやろうと思っており、そのためにテストを作りました。
よかったら使用してみてください。
デバッグ時にも標準入出力のまま実行できるため、提出時にコードを書き換える必要がなく、まあまあ便利かなと思います。

シェルスクリプト

コンパイルコマンドは$ makeとしているので、同一ディレクトリにmakefileを用意する必要がある。
makefileを使わない場合は、make部分をg++ main.cpp -o main.outと変更すると良い。
input0 input1 ...は、プログラムへの標準入力をあらかじめ用意する。output0 output1 ...に関しては、用意しなくとも動作には影響を及ぼさないが実行時の便利のために用意した方が良い。

test.sh
# !/bin/bash
# Compile
make

PRE_IFS=$IFS
IFS=$'\n'

# Put data {input output}
input0="1"
output0="1"
input1="2"
output1="2"

inputs=(  ${input0}  ${input1}  ${input2}  ${input3})
outputs=(${output0} ${output1} ${output2} ${output3})

# Test
for ((idx=0; idx<${#inputs[@]}; idx++))
do
	echo "== Testing with input" ${idx} " ====="
	echo ${inputs[${idx}]}
	echo "------- Result ------------"
	echo ${inputs[${idx}]} | ./main.out
	echo "------- Answer ------------"
	echo ${outputs[${idx}]}
done
echo "==========================="

解説

以下、各処理の簡単な説明を列挙する。

test.sh
# !/bin/bash

# Compile
make

これはシバンとコンパイル。

test.sh
# Put data {input output}
input0="1"
output0="1"
input1="2"
output1="2"

inputs=(  ${input0}  ${input1}  ${input2}  ${input3})
outputs=(${output0} ${output1} ${output2} ${output3})

ここでは、入力データおよび出力データを準備する。input0="1"で、文字列として値を保存しており、inputs=( ${input0} ${input1} ... )では、文字列の配列として各入力を保存する。この時、準備していない変数(input2, input3)を配列の要素としたが、bashでは値の存在しないものを配列に入れても要素にされないようなのでこのように記述した。出力に関しても同様である。

# Test
for ((idx=0; idx<${#inputs[@]}; idx++))
do
	echo "== Testing with input" ${idx} " ====="
	echo ${inputs[${idx}]}
	echo "------- Result ------------"
	echo ${inputs[${idx}]} | ./main.out
	echo "------- Answer ------------"
	echo ${outputs[${idx}]}
done
echo "==========================="

IFS=$PRE_IFS

${#inputs[@]}は入力配列の文字数を表し、for (( ... ))によって準備された入力の個数だけidx=0, 1, ...とループ処理を行う。for idx in ${#inputs[@]}とするとidx=1, 2, ...となってしまう点に注意する必要がある。echo ${input} | ./main.outとすることで、echo ... の結果をmain.outの標準入力にパイプすることができる。

ループ内では、

  • 何番目のテストなのか
  • 入力内容
  • 出力結果
  • 正解

の情報を人の目で見てわかりやすいように適宜情報をechoした。

おわりに

bashはほとんど書いたことがないので、不具合・改善策等あれば言ってもらえると幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?