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

g++ -O2 -Wall -o gen ../src/gen.cpp

echo "Group with maxn=200"
nextTest "./gen 2 1 1"
nextTest "./gen 2 1 2"
nextTest "./gen 2 2 1"
nextTest "./gen 2 10 1"
nextTest "./gen 3 7 5"
nextTest "./gen 4 2 1"
nextTest "./gen 4 5 9"
nextTest "./gen 17 50 1"
nextTest "./gen 18 5 6"
nextTest "./gen 19 19 11"
nextTest "./gen 50 5 6"
nextTest "./gen 97 56 6"
nextTest "./gen 132 5 6"
nextTest "./gen 150 5 1"
nextTest "./gen 197 200 200"
nextTest "./gen 198 5 5"
nextTest "./gen 199 5 6"
nextTest "./gen 199 200 68"
nextTest "./gen 200 1 200"
nextTest "./gen 200 28 17"
nextTest "./gen 200 200 200"

echo "Group with maxn=2000"
nextTest "./gen 507 50 1"
nextTest "./gen 897 5 6"
nextTest "./gen 1147 19 11"
nextTest "./gen 1502 5 6"
nextTest "./gen 1703 560 6"
nextTest "./gen 1812 15 6"
nextTest "./gen 1945 5 133"
nextTest "./gen 1955 2000 2000"
nextTest "./gen 1984 23 377"
nextTest "./gen 1999 313 3"
nextTest "./gen 1999 200 68"
nextTest "./gen 2000 1 2000"
nextTest "./gen 2000 17 107"
nextTest "./gen 2000 2000 2000"

echo "Group with maxn=1000000"
nextTest "./gen 52307 5 17"
nextTest "./gen 83197 22 6"
nextTest "./gen 2502 6 3"
nextTest "./gen 31703 560 6"
nextTest "./gen 112047 1001 121"
nextTest "./gen 139445 15 133"
nextTest "./gen 142812 152 6"
nextTest "./gen 912355 1000000 1000000"
nextTest "./gen 984291 23 377"
nextTest "./gen 999999 313 3"
nextTest "./gen 999999 201230 68"
nextTest "./gen 1000000 9 1000000"
nextTest "./gen 1000000 1000000 7"
nextTest "./gen 1000000 1237 10237"
nextTest "./gen 1000000 1000000 1000000"

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/apricot_yp_n.cpp
cp $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 apricot.in
    ./sol
    cp apricot.out $f.a
  fi
done

popd
