#!/bin/bash

set -e

nt=0
function nextTest {
  nt=$(($nt+1))
  fn=`printf '%02d' $nt`
  echo "$fn: $*"
  $* > ../tests/$fn 2>> tests.lst
}

rm -rf ../tests
mkdir ../tests
pushd ../tests

echo "[ Generate tests ]"

for f in rand chain1 chain2 umb1 umb2 star1 star2 kary stall ; do
  g++ -O2 -Wall -Wl,--stack=65000000 -o gen_$f ../src/gen_$f.cpp || g++ -O2 -Wall -o gen_$f ../src/gen_$f.cpp
done

echo "Group with maxn=100"
nextTest "./gen_rand 1 30173312"
nextTest "./gen_rand 2 30273312"
nextTest "./gen_rand 3 30373312"
nextTest "./gen_rand 4 30473312"
nextTest "./gen_rand 17 30573312"
nextTest "./gen_rand 100 30373312"
nextTest "./gen_chain1 100 30473312"
nextTest "./gen_chain2 100 30573312"
nextTest "./gen_umb1 100 30573312"
nextTest "./gen_umb2 100 30573312"
nextTest "./gen_star1 100 30573312"
nextTest "./gen_star2 100 30573312"
nextTest "./gen_kary 100 2 3 30173312"
nextTest "./gen_kary 100 2 4 30173312"
nextTest "./gen_kary 100 1703 1333 30173312"
nextTest "./gen_kary 100 1000 1000 30173312"
nextTest "./gen_stall 100 2 3 30173312"

echo "Group with maxn=2000"
nextTest "./gen_rand 231 30673312"
nextTest "./gen_rand 477 30773312"
nextTest "./gen_rand 2000 30973312"
nextTest "./gen_chain1 2000 30073312"
nextTest "./gen_chain2 2000 30173312"
nextTest "./gen_umb1 2000 30173312"
nextTest "./gen_umb2 2000 30173312"
nextTest "./gen_star1 2000 30173312"
nextTest "./gen_star2 2000 30173312"
nextTest "./gen_kary 2000 2 3 30173312"
nextTest "./gen_kary 2000 17 13 30173312"
nextTest "./gen_kary 2000 500 500 30173312"
nextTest "./gen_stall 2000 2 3 30173312"
nextTest "./gen_stall 2000 2 4 30173312"
nextTest "./gen_stall 2000 17 13 30173312"
nextTest "./gen_stall 2000 170 130 30173312"
nextTest "./gen_stall 2000 500 500 30173312"

echo "Group with maxn=100000"
nextTest "./gen_rand 10000 30273312"
nextTest "./gen_rand 100000 30373312"
nextTest "./gen_chain1 100000 30473312"
nextTest "./gen_chain2 100000 30573312"
nextTest "./gen_umb1 100000 30573312"
nextTest "./gen_umb2 100000 30573312"
nextTest "./gen_star1 100000 30573312"
nextTest "./gen_star2 100000 30573312"
nextTest "./gen_kary 100000 2 3 30173312"
nextTest "./gen_kary 100000 2 4 30173312"
nextTest "./gen_kary 100000 1703 1333 30173312"
nextTest "./gen_kary 100000 1000 1000 30173312"
nextTest "./gen_stall 100000 2 3 30173312"
nextTest "./gen_stall 100000 2 4 30173312"
nextTest "./gen_stall 100000 1703 1333 30173312"
nextTest "./gen_stall 100000 1000 1000 30173312"

echo "[ Validate tests ]"
g++ -O2 -Wall -o validate ../src/validate.cpp
for f in {0..9}{0..9} ; do
  if [ -e $f ] ; then
    echo "Test $f"
    ./validate < $f
  fi
done

echo "[ Generate answers ]"
sol=../solutions/virus_yp.cpp
cp $sol sol.cpp
g++ -O2 -Wall -Wl,--stack=65000000 -o sol sol.cpp || g++ -O2 -Wall -o sol sol.cpp

for f in {0..9}{0..9} ; do
  if [ -e $f ] ; then
    echo "Test $f"
    cp $f virus.in
    ./sol
    cp virus.out $f.a
  fi
done

popd
