#!/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

repeatTest "./gen_random.exe" 2 "3 100"
repeatTest "./gen_random.exe" 2 "5 100"
repeatTest "./gen_random.exe" 2 "10 400"
repeatTest "./gen_random.exe" 2 "100 400"
repeatTest "./gen_random.exe" 2 "200 600"
repeatTest "./gen_random.exe" 2 "400 800"
repeatTest "./gen_deg_2.exe" 1 "400"
repeatTest "./gen_full.exe" 1 "400 800"
repeatTest "./gen_full_random.exe" 1 "400 800"

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/
