A solution that makes it easy to input Procon and Web tests to verify results

Introduction

I started AtCoder. Procon is fun because it's a brain teaser! I just thought I was doing it

It's annoying to manually enter the test case and check the output!

That is. But I can't afford to write Unit tests ... Can you verify it more easily ...

That's right, why not write a script! !! !! !!

So I tried it.

environment

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)

Automatically validate test cases

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

Completion script

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

How to use

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}

Example of use

$ sh test.sh -e expected.txt Main input.txt

bonus

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 ←.

CHelper reference site

Finally

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

A solution that makes it easy to input Procon and Web tests to verify results
Created a library that makes it easy to handle Android Shared Prefences
I made a GitHub Action that makes it easy to understand the execution result of RSpec
Create a Spring Boot web app that uses IBM Cloudant and deploy it to Cloud Foundry
A shell script that builds a Docker image and pushes it to ECR
Find a value that is convenient to have a method and make it a ValueObject
A solution to an error that makes you angry that you are not following the MySQL default setting ONLY_FULL_GROUP_BY in a production environment and it is not unique.
Create assert_equal to make it easy to write tests
I want to use PowerMock in a class that combines parameterized tests and ordinary tests