#!/bin/sh
# -*- sh -*-
set -e

complain() {
    echo "### $@" > complaint
}

die() {
    test -f progress && cat progress
    rm progress
    complain "$@"
    exit 1
}

report() {
    test -f report || return 0
    v=`grep "^$1:" report | cut -f2- -d: | cut -c2-`
    test -n "$3" && op="$3" || op="=";

    if test -n "$2" && ! test "$2" "$op" "$v"; then
        complain "Expected $1 $op '$2' but got '$v' instead"
        return 1
    fi
    echo "$v" > value;
    return 0
}

report_print() {
    report "$@"
    cat value
}

ltl_algorithm() {
    report Algorithm OWCTY && return 0
    report Algorithm "Nested DFS" && return 0
    report Algorithm MAP && return 0
    return 1
}

ltl_valid() {
    report Finished Yes
    report Property-Type neverclaim || return 0
    ltl_algorithm || return 0
    report Property-Holds Yes
}

ltl_invalid() {
    report Finished Yes
    report Property-Type neverclaim || return 0
    ltl_algorithm || return 0
    report Property-Holds No
}

reachability_valid() {
    report Finished Yes
    report Algorithm Reachability || return 0
    report Property-Holds Yes
}

reachability_deadlock() {
    report Finished Yes
    report Property-Type deadlock || return 0
    report Algorithm Reachability || return 0
    report Property-Holds No
    report CE-Type deadlock
}

reachability_goal() {
    report Finished Yes
    report Property-Type goal || return 0
    report Algorithm Reachability || return 0
    report Property-Holds No
    report CE-Type goal
}

statespace() {
    test -f report || return 0

    report Finished Yes
    op=-eq
    report Full-State-Space No && op=-le
    grep "^Transformations:.*POR" report && op=-le

    states=`report_print States-Visited`
    trans=`report_print Transition-Count`
    accept=`report_print States-Accepting`
    dead=`report_print Deadlock-Count`

    test $states $op $1 || die "States-Visited: $1 expected, $states got"
    test "$trans" = "-" || test $trans $op $2 || die "Transition-Count: $2 expected, $trans got"
    test $accept $op $3 || die "States-Accepting: $3 expected, $accept got"
    test "$dead" = "-" || test $dead $op $4 || die "Deadlock-Count: $4 expected, $dead got"
}

clear() {
    rm -f progress report testcase.{c,dve}
    echo "#### PASSED"
    echo
}

owcty_sizes() {
    test -f report || return 0
    test -f progress || return 0

    if ! grep -q "^Algorithm: OWCTY" report; then return 0; fi
    if grep -q "^Full-State-Space: No" report; then return 0; fi
    if grep -q "^Transformations:.*POR" report; then return 0; fi

    grep '|S| = ' progress | sed -r -e 's,[^0-9]*([0-9]+).*,\1,' > numbers
    rm -f numbers-right
    for n in $@; do echo $n >> numbers-right; done
    diff -u numbers-right numbers
}

debris() {
    echo
    echo "#### TEST END"
    echo
    test -f progress && (cat progress; echo) || true
    test -f testcase.c && (nl -ba testcase.c; echo) || true
    test -f testcase.dve && (nl -ba testcase.dve; echo) || true
    test -f complaint && cat complaint || true
    rm -f testcase.{c,dve} complaint progress
    echo
    echo "."
}

"$@"
