// CSC 152 Class Project:  Scheme Testing Program
// Version 1
// Class to run individual test cases in Scheme
// Author: Henry M. Walker
// Created:  March 4, 2001
// File:  /home/walker/152/project.s01/RunSchemeTest1.java

/*
   Compile the C program and this Java program with commands

   /net/jdk1.2.2/bin/javac RunSchemeTest1.java
   /net/jdk1.2.2/bin/javah RunSchemeTest1
   gcc -c -fPIC -I/net/jdk1.2.2/include -I/net/jdk1.2.2/include/linux runTestCase.c
   gcc -shared -Wl,-soname,libRunTestCase.so -o libRunTestCase.so runTestCase.o 
   /net/jdk1.2.2/bin/javac Coordinator1.java

   Set Java library path and run program with commands
   export LD_LIBRARY_PATH=$LD_LIBRARY_PATH.:
   /net/jdk1.2.2/bin/java Coordinator1
*/

public class RunSchemeTest1 {

    String progName;
    static long msToWait;
    static long uSecToWait;
    static long maxChar;
    char programType;
    ComparisonChecker1 CompCheck;
    static boolean DEBUG = false;

    public static native String runTestCase(String prog, String testCmd,
                                            long uSecToWait, long maxCharOut);
        // interface to Linux and Scheme environments

    static {
        System.loadLibrary("RunTestCase");
    }

    public RunSchemeTest1 (String nameSchemeProg, 
                           int maxSec, 
                           int maxCharOut,
                           char progType,
                           ComparisonChecker1 out) {
        // constructor
        // nameSchemeProg specifies the full path name of the Scheme program
        //     to be tested
        // maxSec gives the number of seconds a test can run before it
        //     is terminated and assumed to be in an infinite loop
        // progType:  
        //     'A' represents authority program
        //     'U' represents user's program
        // maxCharOut gives an upper bound on the number of characters
        //     allowed in the output for a test case

        progName = nameSchemeProg;
        msToWait = 1000 * (long)(maxSec);
        uSecToWait = 1000 * (long)(msToWait);
        maxChar = maxCharOut;
        programType = progType;
        CompCheck = out;
    }

    public void runTest (int testNumber, String testCase) {
        // run test case for given Scheme program
        // Pre-condition: testCase gives the full Scheme command for testing
        // Post-conditions:  
        //   if test completes within the time limit
        //      then method sends results of the test to out
        //      else method sends "test timed out" to out

        if (programType=='A')
        CompCheck.setAuthorityResults
            (runTestCase (progName, testCase, uSecToWait, maxChar),
             testNumber // test case sequence number
             );
        else
        CompCheck.setUserResults
            (runTestCase (progName, testCase, uSecToWait, maxChar),
             testNumber // test case sequence number
             );

    }

}
