I started AtCoder. Procon is fun because it's a brain teaser! I just thought I was doing it
That is. But I can't afford to write Unit tests ... Can you verify it more easily ...
So I tried it.
The environment should meet the following:
--Environment where Bash can be executed --This time run on macOS --Language that can be executed from the command line --This time Java 8 is used --It should be possible to support other languages (unverified)
This time, we will solve the following problem as an example. A --Welcome to AtCoder
Problem statement
Mr. Takahashi wants to process the data. Given the integers $ a, b, c $ and the string $ s $. Display the integer $ a + b + c $ and the string $ s $ side by side.
Input
The input is given in the following format.
input
a
b c
s
--The integer $ a $$ (1 ≤ a ≤ 1,000) $ is given to the $ 1 $ line. -The $ 2 $ line is given the integers $ b, c $$ (1 ≤ b, c ≤ 1,000) $. --The $ 3 $ line is given the string $ s $. The length of this string is greater than or equal to $ 1 $ and less than or equal to $ 100 $.
Output
Print $ a + b + c $ and $ s $ on a $ 1 $ line separated by whitespace. ** Also, insert a line break at the end of the output. ** **
Write the test case in a text file so that you can enter the test case in the standard input.
input.txt
1
2 3
test
Let's check using the example answer of AtCoder.
Main.java
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
//Integer input
int a = sc.nextInt();
//Space-separated integer input
int b = sc.nextInt();
int c = sc.nextInt();
//Character string input
String s = sc.next();
//output
System.out.println((a+b+c) + " " + s);
}
}
All you have to do is pipe this test case into the standard input.
terminal
$ javac Main.java #Compile because it is Java
$ cat input.txt | java Main #Run
#Output result
6 test
Also, if you want to compare with the output example, you can easily find the different part by using the diff
command.
First, create a text file of the output example.
expected.txt
6 text
You can verify by executing the following command.
terminal
$ javac Main.java #Compile because it is Java
$ cat input.txt | java Main > output.txt #Run&Write to file
$ diff -u expected.txt output.txt
#No output if exact match
All you have to do is write this in a shell script and put it together in a nice way!
test.sh
#!/bin/sh
javac Main.java #Compile because it is Java
cat input.txt | java Main > output.txt #Run&Write to file
diff -u expected.txt output.txt
Collect all directories in the same directory as shown below.
terminal
$ tree .
├── input.txt
├── expected.txt
└── test.sh
Run
$ sh test.sh
Below is the shell script I made. FYI.
GitHub https://github.com/terappy/auto-test-tool
test.sh
#!/bin/sh
CMDNAME=`basename $0`
# get option
while getopts e: OPT
do
case $OPT in
"e" ) FLG_E="TRUE" ; VALUE_E="$OPTARG" ;;
* ) echo "Usage: $CMDNAME [-e VALUE] PARAMETERS" 1>&2
exit 1 ;;
esac
done
# remove option value
shift `expr $OPTIND - 1`
# check the number of arguments
if [ $# -ne 2 ]; then
echo "Usage: $CMDNAME classname input-file-name" 1>&2
exit 1
fi
echo "---------------------"
echo "input is":
echo "---------------------"
cat $2
echo ""
echo "---------------------"
echo "output is":
echo "---------------------"
# decide output file name.
OUTFILE_NAME="out-$2"
# compile the target java file.
javac $1.java
# execute with input file and write result into $OUTFILE_NAME
cat $2 | java $1 > $OUTFILE_NAME
cat $OUTFILE_NAME
if [ "$FLG_E" = "TRUE" ]; then
echo ""
echo "---------------------"
echo "verification result":
echo "---------------------"
# verification
if diff -u $VALUE_E $OUTFILE_NAME ; then
echo "====================="
echo "All matched!!"
echo "====================="
else
echo "====================="
echo "Miss matched"
echo "====================="
fi
fi
Do the following: Enter the following arguments.
$ sh test.sh ${CLASS_NAME} ${INPUT_TEXTFILE}
or
When verifying with the output example
$ sh test.sh -e ${EXPECTED_RESULT_TEXTFILE} ${CLASS_NAME} ${INPUT_TEXTFILE}
$ sh test.sh -e expected.txt Main input.txt
I think many people use IntelliJ as an IDE for Java. IntelliJ has a convenient plug-in called CHelper that can be used with Procon, which makes it easy to execute the test case I did this time, and also allows you to specify the class and method used for input and output. If you want to do a professional computer in Java, you should use this ←.
Answering time is valuable in Procon, and verification is a task that is performed many times, so I think it is a must to be able to verify quickly with a script in this way. I'm glad if you can use it as a reference.
Recommended Posts