# -*- mode: python; coding: utf-8 -*-
# Copyright (C) by the Spot authors, see the AUTHORS file for details.
#
# 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/>.

import spot
import sys
from unittest import TestCase
tc = TestCase()

# CPython use reference counting, so that automata are destructed
# when we expect them to be.   However other implementations like
# PyPy may call destructors latter, causing different output.
from platform import python_implementation
if python_implementation() == 'CPython':
    def gcollect():
        pass
else:
    import gc
    def gcollect():
        # From some reason PyPy 7.3.20 (only version tested)
        # requires double collection() for this test to pass.
        # That's odd, because collect() is supposed to perform
        # a full collection pass according to the doc.
        gc.collect()
        gc.collect()

# Some of the tests here assume timely destructor calls, as they occur
# in the reference-counted CPython implementation.  Other
# implementation such as PyPy, should skip those tests.
from platform import python_implementation
is_cpython = python_implementation() == 'CPython'

# ----------------------------------------------------------------------
a = spot.formula.ap('a')
b = spot.formula.ap('b')
c = spot.formula.ap('c')
c2 = spot.formula.ap('c')

tc.assertEqual(c, c2)

op = spot.formula.And(a, b)
op2 = spot.formula.And(op, c)
op3 = spot.formula.And([a, c, b])

tc.assertEqual(op2, op3)

# The symbol for a subformula which hasn't been cloned is better
# suppressed, so we don't attempt to reuse it elsewhere.
del op, c

sys.stdout.write('op2 = %s\n' % str(op2))

del a, b, c2

sys.stdout.write('op3 = %s\n' % str(op3))
tc.assertEqual(op2, op3)

op4 = spot.formula.Or(op2, op3)

sys.stdout.write('op4 = %s\n' % str(op4))
tc.assertEqual(op4, op2)

op5 = spot.formula.Or([op2, op3, op3])

sys.stdout.write('op5 = %s\n' % str(op5))
tc.assertEqual(op5, op2)

tc.assertEqual(spot.formula.apid_count(), 3)
tc.assertEqual(str(spot.formula.apid_map()), '["a", "b", "c"]')

del op2, op3, op4, op5

gcollect()

tc.assertEqual(spot.formula.apid_count(), 0)
tc.assertEqual(str(spot.formula.apid_map()), '[]')

# ----------------------------------------------------------------------
a = spot.formula.ap('a')
b = spot.formula.ap('b')
c = spot.formula.ap('c')
T = spot.formula.tt()
F = spot.formula.ff()

f1 = spot.formula.Equiv(c, a)
f2 = spot.formula.Implies(a, b)
f3 = spot.formula.Xor(b, c)
f4 = spot.formula.Not(f3)
del f3
f5 = spot.formula.Xor(F, c)

del a, b, c, T, F, f1, f2, f4, f5

gcollect()
tc.assertTrue(spot.fnode_instances_check())

# ----------------------------------------------------------------------
tc.assertEqual(str([str(x) for x in spot.formula('a &b & c')]),
               "['a', 'b', 'c']")


def switch_g_f(x):
    if x._is(spot.op_G):
        return spot.formula.F(switch_g_f(x[0]))
    if x._is(spot.op_F):
        return spot.formula.G(switch_g_f(x[0]))
    return x.map(switch_g_f)


f = spot.formula('GFa & XFGb & Fc & G(a | b | Fd)')
tc.assertEqual(str(switch_g_f(f)), 'FGa & XGFb & Gc & F(a | b | Gd)')

x = 0


def count_g(f):
    global x
    if f._is(spot.op_G):
        x += 1


f.traverse(count_g)
tc.assertEqual(x, 3)

# ----------------------------------------------------------------------

# The example from tut01.org

formula = spot.formula('a U b U "$strange[0]=name"')
res = """\
Default output:    {f}
Spin syntax:       {f:s}
(Spin syntax):     {f:sp}
Default for shell: echo {f:q} | ...
LBT for shell:     echo {f:lq} | ...
Default for CSV:   ...,{f:c},...
Wring, centered:   {f:w:~^50}""".format(f=formula)

tc.assertEqual(res, """\
Default output:    a U (b U "$strange[0]=name")
Spin syntax:       a U (b U ($strange[0]=name))
(Spin syntax):     (a) U ((b) U ($strange[0]=name))
Default for shell: echo 'a U (b U "$strange[0]=name")' | ...
LBT for shell:     echo 'U "a" U "b" "$strange[0]=name"' | ...
Default for CSV:   ...,"a U (b U ""$strange[0]=name"")",...
Wring, centered:   ~~~~~(a=1) U ((b=1) U ("$strange[0]=name"=1))~~~~~""")


opt = spot.tl_simplifier_options(False, True, True,
                                 True, True, True,
                                 False, False, False)

for (input, output) in [('(a&b)<->b', 'b->(a&b)'),
                        ('b<->(a&b)', 'b->(a&b)'),
                        ('(a&b)->b', '1'),
                        ('b->(a&b)', 'b->(a&b)'),
                        ('(!(a&b)) xor b', 'b->(a&b)'),
                        ('(a&b) xor !b', 'b->(a&b)'),
                        ('b xor (!(a&b))', 'b->(a&b)'),
                        ('!b xor (a&b)', 'b->(a&b)')]:
    f = spot.tl_simplifier(opt).simplify(input)
    tc.assertEqual(f, output)
    tc.assertTrue(spot.are_equivalent(input, output))


def myparse(input):
    env = spot.default_environment.instance()
    pf = spot.parse_infix_psl(input, env)
    return pf.f


# This used to fail, because myparse would return a pointer
# to pf.f inside the destroyed pf.
tc.assertEqual(myparse('a U b'), spot.formula('a U b'))


tc.assertTrue(spot.is_liveness('a <-> GFb'))
tc.assertFalse(spot.is_liveness('a & GFb'))

gcollect()

tc.assertEqual(spot.formula('a').apid(), 0)
tc.assertEqual(spot.formula('b').apid(), 1)
tc.assertEqual(spot.formula.apid_count(), 5)
tc.assertEqual(str(spot.formula.apid_map()),
               '["a", "b", None, None, "\\"$strange[0]=name\\""]')

del formula
gcollect()

tc.assertEqual(str(spot.formula.apid_map()), '["a", "b", None, None]')
tc.assertEqual(repr(spot.formula.apid_map()),
               '[spot.formula("a"), spot.formula("b"), None, None]')

tc.assertEqual(str(spot.formula.ap_from_apid(0)), 'a')

tc.assertTrue(spot.ltl_satisfiable('(!a U a) & FGa'))
tc.assertFalse(spot.ltl_satisfiable('(!a U Ga) & GF!a'))
