#!/usr/bin/env perl
##
## Copyright (c) 2012, 2015 The University of Utah
## All rights reserved.
##
## This file is distributed under the University of Illinois Open Source
## License.  See the file COPYING for details.

###############################################################################

use strict;
use warnings;

use File::Temp qw/ tempfile tempdir /;
use Cwd;

my @tests = (
    { "name" => "test0",
      "unreduced" => "file1.c",
      "test_script" => "test0.sh",
    },
    { "name" => "test1",
      "unreduced" => "file2.c",
      "test_script" => "test1.sh",
    },
    { "name" => "test2",
      "unreduced" => "file1.c",
      "test_script" => "test2.sh",
    },
    { "name" => "test3",
      "unreduced" => "file2.c",
      "test_script" => "test3.sh",
    },
    { "name" => "test4",
      "unreduced" => "file2.c",
      "test_script" => "test4.sh",
    },
    { "name" => "test5",
      "unreduced" => "file2.c",
      "test_script" => "test5.sh",
    },
    { "name" => "test6",
      "unreduced" => "file3.c",
      "test_script" => "test6.sh",
    },
    { "name" => "test7",
      "unreduced" => "file3.c",
      "test_script" => "test7.sh",
    },
    );

sub run_test ($) {
    (my $num) = @_;

    my $tref = $tests[$num];
    my %test = %{$tref};
    
    my $name = $test{"name"};
    my $unreduced = $test{"unreduced"};
    my $test_script = $test{"test_script"};
    die unless (defined ($name) &&
		defined ($unreduced) &&
		defined ($test_script));

    my $test_dir = getcwd();
    my $temp_dir = tempdir( "tmp_${name}_XXXXX", 
		       DIR => $test_dir,
		       CLEANUP => 0 );

    print "\n\n===== running test $num in $temp_dir =====\n\n\n";

    chdir $temp_dir or die;

    system "cp ../$unreduced .";
    system "../../creduce/creduce --die-on-pass-bug ../${test_script} $unreduced";
    
    chdir $test_dir or die;
}

if (scalar(@ARGV)>0) {
    foreach my $i (@ARGV) {
	run_test ($i);
    }
} else {
    for (my $i=0; $i<scalar(@tests); $i++) {
	run_test ($i);
    }
}
