#!/usr/bin/env perl

# Script to test-run T3 to generate test-suite on example test-classes.
# The script will generate 1x Junit test-class wrapper, with Nx test-methods.

# --------------------------------------------------------
# Few thing that may need to be re-configured
# --------------------------------------------------------

my $MYlaptop = 9 ;
my $UPV = 0 ;
my $deploy = $MYlaptop ;
#my $deploy = $UPV ;

# Need Java8 !

my $MYHOME = "/home/t2" ; 
my $JAVA = "/usr/lib/jvm/java-8-oracle/bin/java" ;
my $CPseparator = ":" ;

if ($deploy == $MYlaptop) { 
    $MYHOME = "." ; 
    $JAVA  = "/cygdrive/c/apps/Java/jdk1.8.0/bin/java" ;
    $CPseparator = ";" ;
}

# --------------------------------------------------------
# Should not change part
# --------------------------------------------------------

my $targetHome     = "./test" ;
my $targetClassDir = $targetHome . "/classes" ;

my $T3 = " Sequenic.T3.T3 "  ;
my $T3jar = $MYHOME . "/jars/T3v0.jar"  ;

my $NUMofTestMethods = 100 ;

my $T3params = "--multicore " 
                . "--nonviolating "
                . "--prefixlength=7 --suffixlength=0 "
                . "--msamples=100 "
                . "--trfile=T3suite "
                . "--splitsuite=" . $NUMofTestMethods . " "
                . "--savedir=" . $MYHOME . "/temp/testcases "
                . "--scandir=" . $targetClassDir ;
                
my $classpath = "\'" 
            . $T3jar . $CPseparator 
            . $targetClassDir
            . "\'" ;

# replaceIt(s,a,b) replaces occurences of string a in string s, with b
sub replaceIt {
   my $str     = $_[0] ;
   my $find    = $_[1];
   my $replace = $_[2] ;
   # escape regex metachars if present:
   $find    = quotemeta $find; 
   $replace = quotemeta $replace; 
   $str =~ s/$find/$replace/g ;
   return $str ;
}      

# as replaceIt, but without replcing metachars:
sub replaceIt2 {
   my $str     = $_[0] ;
   my $find    = $_[1];
   my $replace = $_[2] ;
   $str =~ s/$find/$replace/g ;
   return $str ;
}
            
# For creating a Junit test-class wrapper
sub mkJunit {
    my $CUT = $_[0] ;
    my $CUTdashed = replaceIt($CUT,".","_") ;
    my $template  = $MYHOME . "/T3TestClassWrapper_TM_Template.java" ;  
    my $testclassName = "TEST_" . $CUTdashed ;
    my $testclass     = $MYHOME . "/temp/testcases/" . $testclassName . ".java" ;
    open (TemplateFile,"<",$template);
    open (TestClassFile,">",$testclass) ;
    my $startOfTestFragment = -1 ;
    my $endOfTestFragment = -1 ;
    my $testFragment = "" ;
    # nasty code...., overlaying an FSM over a linear loop to read a file line by line :
    while (<TemplateFile>) {
       my $line = replaceIt2($_, "xxxClass", $testclassName) ;      
       
       if ($startOfTestFragment < 0) { 
          $startOfTestFragment = index($line,"STARTTEST") ; 
          # print("\n>>> startOfTestFragment = " . $startOfTestFragment) ; 
       }
       if ($startOfTestFragment >= 0 && $endOfTestFragment < 0) {
          # print("\n>>> appending to testfragment") ;
          $testFragment = $testFragment . $line ;
       }
       if ($endOfTestFragment < 0) { $endOfTestFragment = index($line,"ENDTEST") ; }
       if ($endOfTestFragment >= 0) {
          # now copy the frament N-times
          my $k = 0 ;
          while ($k < $NUMofTestMethods) {
             my $Test= replaceIt2($testFragment,"xxxreplay", "replay_" . $k) ;
             $Test= replaceIt2($Test,"xxxSuite", "T3suite_S" . $k) ;
             $Test = replaceIt2($Test,"xxxLog"  , $MYHOME . "/logs/REPLAY_" . $CUT . "_S" . $k) ;
             $Test = replaceIt2($Test,"xxxTargetDir"  , $MYHOME . "/temp/testcases/") ;      
             print TestClassFile $Test ;
             $k++;
          }
          # uh ok, so now the test-method has been duplicated k-times,
          # we will now need to disable copy-mode:
          $startOfTestFragment = -1 ;
          $endOfTestFragment = -1 ;
       }
       if ($startOfTestFragment < 0) { print TestClassFile $line  ; }
    }
    close (TemplateFile); 
    close (TestClassFile) ;
}
 
           
# to run T3:
sub runT3 {
    my $CUT = $_[0] ;
    my $LOG = $MYHOME  . "/logs/GEN_" . $CUT ;
    system(
     $JAVA . " -ea -cp " . $classpath
         . $T3 . " " 
         . $T3params 
         . " -lf " . $LOG 
         . ""
         . " " . $CUT
    ) ;
    # now generate Junit wrapper too, to prepare for replay:
    mkJunit($CUT) ;
}



sub mkTempDirs {
   my $DATADir = $MYHOME . "/temp/data" ;
   my $testcasesDir = $MYHOME . "/temp/testcases" ;
   if (!(-e $DATADir)) {
      system("mkdir -p " . $DATADir) ;
   }
   if (!(-e $testcasesDir)) {
      system("mkdir -p " . $testcasesDir) ;
   }
}

# now the runs:
mkTempDirs() ;
#runT3("SortedList.BuildSortedList") ; 
#runT3("Triangle.Triangle") ; 
runT3("Triangle.TriangleType") ; 

