#! /usr/bin/perl -w

# --------------------------------------------------------------------------- #
#                                  CAMPAIGN                                   #
# --------------------------------------------------------------------------- #
# This is part of the CAMPAIGN data clustering library originating from       #
# Simbios, the NIH National Center for Physics-Based Simulation of Biological #
# Structures at Stanford, funded under the NIH Roadmap for Medical Research,  #
# grant U54 GM072970 (See https://simtk.org), and the FEATURE Project at      #
# Stanford, funded under the NIH grant LM05652                                #
# (See http://feature.stanford.edu/index.php).                                #
#                                                                             #
# Portions copyright (c) 2010 Stanford University, Authors and Contributors.  #
# Authors: Marc Sosnick                                                       #
# Contributors: Kai J. Kolhoff, William Hsu                                   #
#                                                                             #
# This program is free software: you can redistribute it and/or modify it     #
# under the terms of the GNU Lesser General Public License as published by    #
# the Free Software Foundation, either version 3 of the License, or (at your  #
# option) any later version.                                                  #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public        #
# License for more details.                                                   #
#                                                                             #
# You should have received a copy of the GNU Lesser General Public License    #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
# --------------------------------------------------------------------------- #

# $Id$

###############################################################################
# File: smoketest.pl	
# Author: Marc Sosnick
# Date: 12/28/09
#
# Description:  
#   Performs smoke test of a particular clustering algorithm's program by
#   comparing the output of the current program with a baseline output 
#   capture given the same data.
# Usage:
#   smoketest.pl program_name data_file prev_output_file
#         program_name: the program to be executed
#            data_file: baseline data file
#     prev_output_file: baseline output based on data_file
###############################################################################

if($#ARGV < 2) {
  print "Error: Insufficient number of command line arguments.\n";
  print "Usage: smoketest.pl program_name data_file expected_output_file\n";
  exit 1;
} elsif ($#ARGV > 2) {
  print "Error: Too many command line arguments.\n";
  print "Usage: smoketest.pl program_name data_file expected_output_file\n";
  exit 1;
}


###############################################################################
# Change these to reflect system setup

# name of output file from CPU hierarchical clustering program
$EXE_OUTPUT_FILENAME = "smokeTestOut.t";

# name of output file from diffing known and gen'ed
$DIFF_OUTPUT_FILENAME = "smokeTestDiff.t";

# name of executable
$EXE_FILE = $ARGV[0];

# name of the test data file.
$TEST_DATA_FILE = $ARGV[1];

# name of output file with known results
$KNOWN_COMPARISON_FILE = $ARGV[2];

# check for valid files
if (! -x $EXE_FILE){
  print "Error: Program file \"$EXE_FILE\" cannot be executed.\n";
  exit 0;
}
if (! -e $TEST_DATA_FILE){
  print "Error: Data file \"$TEST_DATA_FILE\" does not exist.\n";  
  exit 0;
}

if (! -e $KNOWN_COMPARISON_FILE){
  print "Error: Comparison file \"$KNOWN_COMPARISON_FILE\" does not exist.\n";  
  exit 0;
}


##########################################################
# begin execution


# 345678901234567890123456789012345678901234567890123456789012345678901234567890
# run program using known dataset without reporting execution time.  
# Capture output to file.
  system("$EXE_FILE $TEST_DATA_FILE -t false 1> $EXE_OUTPUT_FILENAME 2> /dev/null");

# diff the known and newly generated files
system("diff ".$EXE_OUTPUT_FILENAME." ".$KNOWN_COMPARISON_FILE." > ".$DIFF_OUTPUT_FILENAME);
$wc_out = `wc -l $DIFF_OUTPUT_FILENAME 2>&1`;
@wcvalues = split(" ",$wc_out);

# output results of test formatted for Test::Harness
printf "1..1\n";

if($wcvalues[0] == 0){
	printf "ok\n";
} else {
	printf "not ok\n";
}
