#!/bin/bash

# About:
#   1. In whole it works like this: for (tests) { gen_test, validate } for (tests) gen_ans
#   2. It always uses native EOL
#   3. Basic commands: nextTest(gen), repeatTest(gen, k, gen_params), echoTest(input)
#   4. To start next group use startGroup() [if you use groups, validator should output comment to stdout]
#   5. To start generation from test number x use "bash doall.sh x"
#   6. Result of doall.sh: ../tests, tests.commments, tests.grouping

source ./include.sh || exit

log_file=tests.comments
rm $log_file 
nt=1
base=239017
first_test=$1

function msg
{
  echo $*
  echo $* >> $log_file
}

function nt2ti
{
  ti=`printf "%03d" "$nt"`
  let nt=$nt+1
}

function nextTest
{
  nt2ti
  msg "$ti : $*"
  let base=$base+1

  if ! [ "$ti" '<' "$first_test" ] ; then
    rm -f $ti $ti.a 2>/dev/null
    bash -c "($*) | ./twf.exe" > $ti || finish "FAILED: nextTest"
    ./validate.exe < $ti || finish "FAILED: validate test"
  fi
}

function echoTest
{
  nextTest "echo $*"
}

function repeatTest
{                              
  gen_name=$1
  shift
  gen_k=$1
  shift
  for ((t = 1; t <= $gen_k; t++)) ; do
    nextTest $gen_name $base $*
  done
}

echo "-- Be carefull. This script DOES NOT clean workspace"

echo "-- Compile generators"
compile_cpp twf.c twf.exe || exit
compile validate || exit
for f in `ls gen*.cpp` ; do
  compile ${f/.cpp/} || exit
done

echo "-- Generate tests"

for f in `ls *.sample` ; do
  nextTest "cat $f"
done

m=10000
n=1000

repeatTest "./gen_random.exe" 5 "10 3 1"
repeatTest "./gen_random.exe" 5 "10 3 2"
repeatTest "./gen_random.exe" 2 "10 3 3"
repeatTest "./gen_random.exe" 5 "10 4 1"
repeatTest "./gen_random.exe" 2 "10 4 2"
repeatTest "./gen_colinear.exe" 1 "10 0 5 1 10 10 $m"
repeatTest "./gen_colinear.exe" 1 "15 0 5 2 10 10 $m"
repeatTest "./gen_colinear.exe" 2 "20 0 5 3 10 10 $m"
repeatTest "./gen_colinear.exe" 3 "25 0 10 3 10 10 $m"
repeatTest "./gen_colinear.exe" 3 "35 0 5 4 3 3 $m"
repeatTest "./gen_colinear.exe" 1 "35 0 10 4 10 10 $m"

function group
{
  repeatTest "./gen_colinear.exe" 1 "$1 0 3 3 $2 $2 $m"
  repeatTest "./gen_colinear.exe" 1 "$1 0 4 3 $2 $2 $m"
  repeatTest "./gen_colinear.exe" 1 "$1 0 3 4 $2 $2 $m"
  repeatTest "./gen_colinear.exe" 1 "$1 0 10 3 $2 $2 $m"
  repeatTest "./gen_colinear_2.exe" 1 "$1 3 3 $2 $2 $m"
  repeatTest "./gen_colinear_2.exe" 1 "$1 10 3 $2 $2 $m"
}

group 100 70
group 250 70
group 500 70
group $n 60

#repeatTest "./gen_colinear_2.exe" 3 "$n 10 3 50 50 $m"
#repeatTest "./gen_colinear_2.exe" 3 "$n 10 3 100 100 $m"

repeatTest "./gen_colinear.exe" 1 "$n 0 10 1 3 $m $m"
repeatTest "./gen_colinear.exe" 1 "$n 0 10 30 10 10 $m"
repeatTest "./gen_colinear.exe" 1 "$n $(($n/2)) 10 3 70 70 $m"
repeatTest "./gen_colinear_2.exe" 1 "$n 10 2 70 70 $m"
repeatTest "./gen_colinear_3.exe" 1 "$n 0 10 3 70 70 $m"
repeatTest "./gen_colinear_3.exe" 1 "$n $(($n/2)) 10 3 70 70 $m"
repeatTest "./gen_colinear_3.exe" 1 "$n $(($n/2)) 10 300 10 10 $m"
repeatTest "./gen_random.exe" 1 "$n 3 4"
repeatTest "./gen_random.exe" 1 "$n 10 2"
repeatTest "./gen_random.exe" 1 "$n 10 10000"

echo "-- Generate answers"
bash gen_ans_cpp.sh docheck || exit

testdir=../tests
echo "-- Move tests to $testdir"
rm -f -r $testdir
mkdir $testdir || exit
mv ?? $testdir/
mv ??.a $testdir/
mv ??? $testdir/
mv ???.a $testdir/
