views:

87

answers:

3

I have a set of *.in files and a set of *.soln files with matching files names. I would like to run my program with the *.in file as input and compare the output to the ones found in the *.soln files. What would be the best way to go about this? I can think of 3 options.

  1. Write some driver in Java to list files in the folder, run the program, and compare. This would be hard and difficult.
  2. Write a bash script to do this. How?
  3. Write a python script to do this?
A: 

If the program exists, I suspect the bash script is the best bet. If your soln files are named right, some kind of loop like

for file in base*.soln do myprogram > new_$file diff $file new_$file done

Of course, you can check the exit code of diff and do various other things to create a test report . . .

That looks simplest to me . . .

Karl

Karl T.
A: 

This is primarily a problem that requires the use of the file-system with minimal logic. Bash isn't a bad choice for such problems. If it turns out you want to do something more complicated than just comparing for equality Python becomes a more attractive choice. Java doesn't seem like a good choice for a throwaway script such as this.

Basic bash implementation might look something like this:

cd dir_with_files

program=your_program
input_ext=".in"
compare_to_ext=".soIn"

for file in *$from_extension; do 
  diff <("$program" "$i") "${file:0:$((${#file}-3))}$compare_to_ext"
done

Untested.

Ollie Saunders
A: 

I would go for a the bash solution. Also given that what you are doing is a test, I would always save the output of the myprogram so that if there are failures, that you always have the output to compare it to.

#!/bin/bash

for infile in *.in; do
    basename=${infile%.*}
    myprogram $infile > $basename.output
    diff $basename.output $basename.soln
done

Adding the checking or exit statuses etc. as required by your report.

Beano