#! /usr/bin/python3
# Test program for module {trafo}
# Last edited on 2021-02-08 20:34:03 by jstolfi

import trafo
import sys
from math import sqrt, sin, cos, floor, ceil, inf, nan, pi

class MyTrafo(trafo.Trafo):

  pass
    
sys.stderr.write("--- testing {trafo.identity,trafo.is_identity} ----------------\n")

T0 = trafo.identity()
sys.stderr.write("T0 = %s\n" % trafo.to_string(T0))
assert trafo.is_identity(T0)
assert trafo.is_identity((+1,))
assert trafo.is_identity((-1,))

T1 = trafo.Trafo("T1")
sys.stderr.write("T1 = %s\n" % trafo.to_string(T1))
assert isinstance(T1, trafo.Trafo)
assert not trafo.is_identity(T1)
assert not trafo.is_identity((+1,T1))
assert not trafo.is_identity((-1,T1))

sys.stderr.write("--- testing {trafo.nfactor,trafo.factor} ----------------\n")

assert trafo.nfactors(T0) == 0

assert trafo.nfactors(T1) == 1
assert trafo.factor(T1,0) == T1

sys.stderr.write("--- testing {trafo.inv} ----------------\n")

T0C = trafo.inv(T0)
sys.stderr.write("T0C = %s\n" % trafo.to_string(T0C))
assert T0C == None
assert trafo.inv((+1,)) == None
assert trafo.inv((-1,)) == None

T1C = trafo.inv(T1)
sys.stderr.write("T1C = %s\n" % trafo.to_string(T1C))
assert not trafo.is_identity(T1C)
assert T1C == (-1,T1)
assert trafo.inv((+1,T1)) == T1C
assert trafo.inv((-1,T1)) == T1
assert trafo.inv(T1C) == T1

assert trafo.nfactors(T1C) == 1
assert trafo.factor(T1C,0) == trafo.inv(T1)

sys.stderr.write("--- testing {trafo.compose} ----------------\n")

T2 = trafo.Trafo("T2")
assert not trafo.is_identity(T2)

Tc = trafo.compose(())
assert trafo.is_identity(Tc)
Td = trafo.compose([])
assert trafo.is_identity(Td)

Tc1 = trafo.compose((T1,))
assert Tc1 == T1
Td1 = trafo.compose([T1])
assert Td1 == T1

Tc2 = trafo.compose((trafo.inv(T2),))
assert Tc2 == trafo.inv(T2)
Td2 = trafo.compose([trafo.inv(T2)])
assert Td2 == trafo.inv(T2)

Tc12 = trafo.compose((T1,T2))
sys.stderr.write("Tc12 = %s\n" % trafo.to_string(Tc12))
assert Tc12 == (+1,T1,T2)
Td12 = trafo.compose([T1,T2])
sys.stderr.write("Td12 = %s\n" % trafo.to_string(Td12))
assert Td12 == (+1,T1,T2)

assert trafo.nfactors(Tc12) == 2
assert trafo.factor(Tc12,0) == T1
assert trafo.factor(Tc12,1) == T2

Tc21 = trafo.inv(Tc12)
assert trafo.nfactors(Tc21) == 2
assert trafo.factor(Tc21,0) == trafo.inv(T2)
assert trafo.factor(Tc21,1) == trafo.inv(T1)

Tc1222 = trafo.compose((T1,T2,trafo.inv(T2),T2))
sys.stderr.write("Tc1222 = %s\n" % trafo.to_string(Tc1222))
assert trafo.nfactors(Tc1222) == 2
assert trafo.factor(Tc1222,0) == T1
assert trafo.factor(Tc1222,1) == T2

sys.stderr.write("--- testing {trafo.simplify} ----------------\n")

T3 = trafo.Trafo("T3")

Tbig = (-1,(+1,T1,T3),(-1,(-1,T2,T3),(-1,trafo.inv(T3),trafo.inv(T2),T3,T3,T1)))
sys.stderr.write("Tbig = %s\n" % trafo.to_string(Tbig))
Tsim = trafo.simplify(Tbig)
sys.stderr.write("Tsim = %s\n" % trafo.to_string(Tsim))
nsim = trafo.nfactors(Tsim)

assert trafo.nfactors(Tsim) == 7
assert trafo.factor(Tsim,0) == trafo.inv(T3)
assert trafo.factor(Tsim,1) == trafo.inv(T2)
assert trafo.factor(Tsim,2) == trafo.inv(T1)
assert trafo.factor(Tsim,3) == trafo.inv(T3)
assert trafo.factor(Tsim,4) == trafo.inv(T3)
assert trafo.factor(Tsim,5) == T2
assert trafo.factor(Tsim,6) == trafo.inv(T1)
