#!/usr/bin/env bash

# -------------------- Constants --------------------

ADETERMINIZE_EXECUTABLE="adeterminize"
NFSM_TESTS_COUNT="4"
NIDPDA_TESTS_COUNT="3"



# -------------------- Functions --------------------

function checkAdeterminizeExecutable
{
    if [ ! -f $ADETERMINIZE_EXECUTABLE ]; then
        echo "Make sure $ADETERMINIZE_EXECUTABLE, which is necessary for running this test, exists in bin folder."
        exit 1
    fi
}


function compareTmpAutomatons
{
    TMP=$(./adiff.automaton tmp1.xml tmp2.xml)
    if [ $? == 0 ]; then
        ((SUCCESS_COUNT++))
        echo "    Test $1: successful"
    else
        ((FAILED_COUNT++))
        echo "    Test $1: FAILED"
    fi
    rm tmp1.xml tmp2.xml
}


function testFsm
{
    echo "FSM:"
    for i in $(seq 1 $NFSM_TESTS_COUNT); do
        NFSM="../examples/automaton/NFSM$i.xml"
        DFSM="../examples/automaton/NFSM$i.DET.xml"
        ./adeterminize --type=fsm < $NFSM > tmp1.xml
        cp $DFSM tmp2.xml
        compareTmpAutomatons "NFSM$i"
    done
}


function testIdpda
{
    echo "IDPDA:"
    for i in $(seq 1 $NIDPDA_TESTS_COUNT); do
        NIDPDA="../examples/automaton/NIDPDA$i.xml"
        DIDPDA="../examples/automaton/NIDPDA$i.DET.xml"
        ./adeterminize --type=idpda < $NIDPDA > tmp1.xml
        cp $DIDPDA tmp2.xml
        compareTmpAutomatons "IDPDA$i"
    done
}


function printIntroduction
{
    echo "adeterminize tests:"
    echo "-------------------------"
}


function printResults
{
    echo "-------------------------"
    echo "Results: $SUCCESS_COUNT successful, $FAILED_COUNT failed"
}



# -------------------- Run test --------------------

SUCCESS_COUNT=0
FAILED_COUNT=0

cd bin
printIntroduction
checkAdeterminizeExecutable
testFsm
testIdpda
printResults
