#!/bin/csh -f

if ($#argv != 1) then
    echo "Usage: ./test_generic.csh <test_nr>"
    exit 1
endif

set test_nr=$1
set test_input_dir="/u/c/s/cs537-1/ta/p1/test_data/"
set test_out_dir="/u/c/s/cs537-1/ta/p1/output/"

switch($test_nr)
case 1:
    if (-f mysort.c) then
        if (-f README) then
           echo "Test ${test_nr}: Pass"
           exit 0
        else
           echo "ERROR: README file not present"
           echo "Test ${test_nr}: Fail"
           exit 1
        endif
    else
        echo "ERROR: mysort.c not present"
        echo "Test ${test_nr}: Fail"
        exit 1
    endif
breaksw
case 2:
    if (! -f mysort.c) then
        echo "ERROR: mysort.c not available to be compiled"
        echo "Test ${test_nr}: Fail"
        exit 1
    else 
        rm -f mysort
        gcc -Wall -o mysort mysort.c -O
        if ($status != 0) then
           echo "ERROR: mysort.c not properly compilable"
           echo "Test ${test_nr}: Fail"
           exit 1
        endif
    endif
       echo "Test ${test_nr}: Pass"
breaksw
case 3:
    if(! -f mysort) then
       echo "Please compile mysort.c before running this Test"
       exit 2
    endif
    (./mysort > /dev/null) >& test${test_nr}.err
    set exit_code=${status}
    diff test${test_nr}.err ${test_out_dir}test${test_nr}.err >& /dev/null
    if ($status != 0) then
        echo "ERROR: Does not print error message appropriately"
        echo "Test ${test_nr}: Fail"
        rm test${test_nr}.err
        exit 1
    else if($exit_code == 0) then 
        echo "ERROR: Does not issue error code on exit"
        echo "Test ${test_nr}: Fail"
        rm test${test_nr}.err
        exit 1
    else
        rm test${test_nr}.err
        (./mysort 123 xyz > /dev/null) >& test${test_nr}.err
        set exit_code=${status}
        diff test${test_nr}.err ${test_out_dir}test${test_nr}.err >& /dev/null
        if ($status != 0) then
            echo "ERROR: Does not print error message appropriately"
            echo "Test ${test_nr}: Fail"
            rm test${test_nr}.err
            exit 1
        else if($exit_code == 0) then
            echo "ERROR: Does not issue error code on exit"
            echo "Test ${test_nr}: Fail"
            rm test${test_nr}.err
            exit 1
        endif
    endif
       rm test${test_nr}.err
       echo "Test ${test_nr}: Pass"
breaksw
case 4:
    if(! -f mysort) then
       echo "Please compile mysort.c before running this Test"
       exit 2
    endif
    (./mysort invalid_input_file_xyz > /dev/null) >& test${test_nr}.err
    set exit_code=${status}
    diff test${test_nr}.err ${test_out_dir}test${test_nr}.err >& /dev/null
    if ($status != 0) then
        echo "ERROR: does not check whether input file is valid"
        echo "Test ${test_nr}: Fail"
        rm test${test_nr}.err
        exit 1
    else if ($exit_code == 0) then
        echo "ERROR: Does not issue error code on exit"
        echo "Test ${test_nr}: Fail"
        rm test${test_nr}.err
        exit 1
    endif
        rm test${test_nr}.err
        echo "Test ${test_nr}: Pass"
breaksw
case 9:
    if(! -f mysort) then
       echo "Please compile mysort.c before running this Test"
       exit 2
    endif
    (./mysort ${test_input_dir}test${test_nr}.txt > /dev/null) >& test${test_nr}.err
    set exit_code=${status}
    diff test${test_nr}.err ${test_out_dir}test${test_nr}.err >& /dev/null
    if ($status != 0) then
       echo "Test ${test_nr}: Fail"
       rm test${test_nr}.err
       exit 1
    else if ($exit_code == 0) then
        echo "ERROR: Does not issue error code on exit"
        echo "Test ${test_nr}: Fail"
        rm test${test_nr}.err
        exit 1
    endif 
       rm test${test_nr}.err
       echo "Test ${test_nr}: Pass"
breaksw
case 5:
case 6:
case 7:
case 8:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
    if(! -f mysort) then
       echo "Please compile mysort.c before running this Test"
       exit 2
    endif
   ./mysort ${test_input_dir}test${test_nr}.txt > test${test_nr}.out
   diff test${test_nr}.out ${test_out_dir}test${test_nr}.out >& /dev/null
   if ($status != 0) then
      echo "Test ${test_nr}: Fail"
      rm -f test${test_nr}.out
      exit 1
   endif
      echo "Test ${test_nr}: Pass"
      rm -rf test${test_nr}.out
breaksw
default:
   echo "Invalid <test_nr> option used"
   exit 1
breaksw
endsw

exit 0
