#!/bin/sh
# -*- coding: utf-8 -*-
# Copyright (C) 2012, 2013, 2014 Laboratoire de Recherche et Développement
# de l'Epita (LRDE).
#
# This file is part of Spot, a model checking library.
#
# Spot is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Spot is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

. ./defs

set -e

for f in 'p0 U p1 U p2' 'Gp00 | Gp13 | Gp42' '{(1;1)*}[]->p1'
do
  # Make sure Spot can read the LBTT it produces
  run 0 ../../bin/ltl2tgba --lbtt "$f" > out
  s=`wc -l < out`
  head -n 1 out | grep t
  run 0 ../../bin/autfilt --lbtt out > out2
  s2=`wc -l < out2`
  test "$s" -eq "$s2"

  # The LBTT output use 2 lines par state, one line per transition,
  # and one extra line for header.
  run 0 ../../bin/ltl2tgba "$f" --stats 'expr %s \* 2 + %e + 1' > size
  l=$(eval "$(cat size)")
  test "$s" -eq "$l"

  # Make sure we output the state-based format
  # for BA...
  run 0 ../../bin/ltl2tgba --ba --lbtt --low --any "$f" >out4
  head -n 1 out4 | grep t && exit 1
  s4=`wc -l < out4`
  test "$s" -eq "$s4"
  run 0 ../../bin/autfilt --lbtt out4 > out5
  run 0 ../../bin/autfilt out4 --are-isomorphic out5
  # ... unless --lbtt=t is used.
  run 0 ../../bin/ltl2tgba --ba --lbtt=t --low --any "$f" >out6
  head -n 1 out6 | grep t
  s6=`wc -l < out6`
  test "$s" -eq "$s6"
  run 0 ../../bin/autfilt --lbtt out6 > out7
  run 0 ../../bin/autfilt out6 --are-isomorphic out7
done


# multiple inputs (from different tools)

cat >input <<EOF
/* This is the output of 'lbt' on the formula 'U p0 p1'. */
4 1
0 1 -1
1 p0
2 p1
-1
1 0 -1
1 p0
2 p1
-1
2 0 0 -1
3 t
-1
3 0 0 -1
3 t
-1
/* This kind of output is returned by wring2lbtt, on the same formula.
   (Newer versions of LBTT reject this input with missing new lines.) */
4 1 0 1 -1 1 p0
2 p1
-1 1 0 -1 1 p0
2 p1
-1 2 0 0 -1 3 t
-1 3 0 0 -1 3 t
-1
/* This is an automaton without state and three acceptance sets.
   Spot is not able to deal with automata that do not have initial
   state, so it will add a dummy state. */
0 3
/* Another example from wring2lbtt (or modella), showing that the
   acceptance set of the states is not always numbered from 0. */
6 1 0 1 -1 1 | & ! p0 ! p1 & p0 ! p1
2 & ! p0 ! p1
3 | & p0 p1 & ! p0 p1
-1 1 0 -1 4 ! p1
-1 2 0 -1 2 & ! p0 ! p1
3 | & p0 p1 & ! p0 p1
-1 3 0 -1 5 t
-1 4 0 1 -1 4 ! p1
-1 5 0 1 -1 5 t
-1
EOF

run 0 ../../bin/autfilt --stats '%s %t %e %a' input > output
cat >expected<<EOF
4 16 6 1
4 16 6 1
1 0 0 3
6 20 9 1
EOF

diff output expected

cat > input <<EOF
1 2t
0 1
0 -1 & ! "a" ! "b" !
0 0 -1 & "a" ! "b" /* comments are OK */
0 1 -1 & ! "a" /* here too */ "b"
0 0 1 -1 & "a" "b"
-1
EOF
cat >expected <<EOF
input:3.5-20: failed to parse guard
input:3.20: syntax error, unexpected '!', expecting end of formula
input:3.20: ignoring trailing garbage
EOF
../../bin/autfilt -q input 2> stderr && exit 1
cat stderr
diff stderr expected
