Commit b1d461d3 authored by Akim Demaille's avatar Akim Demaille
Browse files

style: tests: use metext, meaut, etc.

* tests/python/automaton.py, tests/python/derivation.py,
* tests/python/factory.py, tests/python/standard.py,
* tests/python/automaton.py, tests/python/compose.py,
* tests/python/efsm.py, tests/python/eliminate-state.py,
* tests/python/infiltrate.py, tests/python/ldivide.py,
* tests/python/minimize.py, tests/python/partial-identity.py,
* tests/python/power.py, tests/python/prefix.py,
* tests/python/rdivide.py, tests/python/synchronize.py,
* tests/python/to-expression.py, tests/python/union.py:
here.
parent d41e1daf
......@@ -21,6 +21,11 @@ srcdir = os.environ['abs_srcdir'] if 'abs_srcdir' in os.environ \
medir = sys.argv[0].replace(".py", ".dir")
def set_medir(dir):
global medir
medir = dir
def mefile(fn, ext=None):
'''The path to the test's file `fn.ext`,
where `ext` is possibly empty.'''
......
......@@ -221,7 +221,7 @@ $ -> 3
1 -> $
2 -> 1 a
3 -> 2 b''',
vcsn.automaton(filename=medir + '/html.gv').format('daut'))
meaut('html.gv').format('daut'))
## --------------- ##
......@@ -418,7 +418,7 @@ CHECK_EQ('''digraph
ctx = vcsn.context('lal<string>, b')
e = ctx.expression("'🍺':'🍾':'☕️':'🍷' & [^]*'🍺'[^]*'☕️'[^]* & ([^]*'🍷''🍾'[^]*){c}")
CHECK_EQ(open(medir + '/drinks-simple.gv').read().strip(),
CHECK_EQ(metext('drinks-simple.gv'),
e.automaton().minimize().dot('simple'))
......@@ -443,7 +443,7 @@ for fn in glob.glob(os.path.join(medir, '*.in.gv')):
# Check state names in TikZ.
a = vcsn.context('lal_char, b').expression(r'\e+a').derived_term()
exp = open(os.path.join(medir, 'derived-term.tex')).read().strip()
exp = metext('derived-term.tex')
CHECK_EQ(exp, a.format('tikz'))
......@@ -456,10 +456,10 @@ ctx = vcsn.context('lal_char, q')
a = ctx.expression('a*+(<2>a)*').derived_term(deterministic=True, lazy=True)
a('aaa')
exp = open(os.path.join(medir, 'lazy.out.gv')).read().strip()
exp = metext('lazy.out.gv')
CHECK_EQ(exp, a.format('dot'))
exp = open(os.path.join(medir, 'lazy.tex')).read().strip()
exp = metext('lazy.tex')
CHECK_EQ(exp, a.format('tikz'))
......
......@@ -226,13 +226,13 @@ check(c_r.expression("<3.1>(a|x)").standard(),
## Fibonacci normalizer ##
##########################
check(vcsn.automaton(filename=medir + "/left.gv"),
vcsn.automaton(filename=medir + "/right.gv"),
open(medir + "/result.gv").read().strip())
check(meaut('left.gv'),
meaut('right.gv'),
metext('result.gv'))
######################
## Lazy composition ##
######################
CHECK_EQ(open(medir + "/result.gv").read().strip(),
vcsn.automaton(filename=medir + "/left.gv").compose(vcsn.automaton(filename=medir + "/right.gv"), lazy=True).accessible())
CHECK_EQ(metext('result.gv'),
meaut('left.gv').compose(meaut('right.gv'), lazy=True).accessible())
......@@ -28,7 +28,7 @@ def check_derived_term(r, exp, algo):
eff = lazy.strip()
else:
eff = r.derived_term(algo)
CHECK_EQ(open(medir + '/' + exp + '.gv').read().strip(),
CHECK_EQ(metext(exp, 'gv'),
eff)
def check_dt(r, exp):
......
......@@ -55,7 +55,7 @@ check(a, 'slowgrow.efsm')
for f in ["lal-char-zmin", "lat-zmin", "ascii-to-one"]:
print("f:", f)
a = vcsn.automaton(filename=medir + "/" + f + '.gv')
a = meaut(f, 'gv')
check(a, f + '.efsm')
# Check the case of an automaton without any transition.
......
......@@ -4,10 +4,10 @@ import vcsn
from test import *
# Eliminate state 0, or 1, or 2.
a = vcsn.automaton(filename = medir + '/lao.gv')
a = meaut('lao.gv')
for i in [0, 1, 2]:
print("Eliminating state", i)
exp = open(medir + '/lao-elim-{}.gv'.format(i)).read().strip()
exp = metext('lao-elim-{}.gv'.format(i))
CHECK_EQ(exp, a.eliminate_state(i))
XFAIL(lambda: a.eliminate_state(3))
......@@ -15,7 +15,7 @@ XFAIL(lambda: a.eliminate_state(3))
a = vcsn.B.expression('ab*c').standard().lift()
for i in range(4):
print("elimination:", i)
exp = open(medir + '/abc-elim-{}.gv'.format(i)).read().strip()
exp = metext('abc-elim-{}.gv'.format(i))
a = a.eliminate_state(-1)
CHECK_EQ(exp, a)
......
......@@ -16,17 +16,17 @@ vcsn.automaton.labels = get_labels
a = vcsn.context('lal_char(abc), b').cerny(6)
CHECK_EQ(a.info()['number of states'], 6)
CHECK_EQ(vcsn.automaton(filename=medir + '/cerny-6.gv'), a)
CHECK_EQ(meaut('cerny-6.gv'), a)
## ----------- ##
## de_bruijn. ##
## ----------- ##
CHECK_EQ(vcsn.automaton(filename=medir + '/de-bruijn-2.gv'),
CHECK_EQ(meaut('de-bruijn-2.gv'),
vcsn.context('lal_char(ab), b').de_bruijn(2))
CHECK_EQ(vcsn.automaton(filename=medir + '/de-bruijn-3.gv'),
CHECK_EQ(meaut('de-bruijn-3.gv'),
vcsn.context('lal_char(xyz), b').de_bruijn(3))
## ---------------- ##
......@@ -87,10 +87,10 @@ digraph
'''))
CHECK_EQ(ctx.double_ring(1, [0]),
vcsn.automaton(filename=medir + '/double-ring-1-0.gv'))
meaut('double-ring-1-0.gv'))
CHECK_EQ(ctx.double_ring(4, [2, 3]),
vcsn.automaton(filename=medir + '/double-ring-4-2-3.gv'))
meaut('double-ring-4-2-3.gv'))
## ---------- ##
......@@ -100,12 +100,12 @@ CHECK_EQ(ctx.double_ring(4, [2, 3]),
b = vcsn.context('lal_char(abc), b')
z = vcsn.context('lal_char(abc), z')
exp = vcsn.automaton(filename=medir + '/ladybird-2.gv')
exp = meaut('ladybird-2.gv')
CHECK_EQ(exp, b.ladybird(2))
CHECK_EQ(vcsn.automaton(str(exp).replace(', b', ', z')), z.ladybird(2))
exp = vcsn.automaton(filename=medir + '/ladybird-2-zmin.gv')
exp = meaut('ladybird-2-zmin.gv')
CHECK_EQ(exp,
vcsn.context('lal_char(abc), zmin').ladybird(2))
......@@ -115,7 +115,7 @@ CHECK_EQ(exp,
nmin = vcsn.context('lat<lan_char(abc), lan_char(bcd)>, nmin')
exp = vcsn.automaton(filename=medir + '/levenshtein.gv')
exp = meaut('levenshtein.gv')
CHECK_EQ(exp, nmin.levenshtein())
......@@ -125,7 +125,7 @@ CHECK_EQ(exp, nmin.levenshtein())
# Expect a clique.
c1 = vcsn.context('lal_char(a), b').random_automaton(4, 1, 4, 4)
c2 = vcsn.automaton(filename=medir + '/clique-a-4.gv')
c2 = meaut('clique-a-4.gv')
CHECK_EQ(c1, c2)
......@@ -235,5 +235,5 @@ CHECK(a.is_complete())
## u. ##
## --- ##
CHECK_EQ(vcsn.automaton(filename=medir + '/u-5.gv'),
CHECK_EQ(meaut('u-5.gv'),
vcsn.context('lal_char(abc), b').u(5))
......@@ -5,7 +5,7 @@ from test import *
z = vcsn.context('lal_char(abcd), z')
medir = srcdir + '/tests/python/conjunction.dir'
set_medir(srcdir + '/tests/python/conjunction.dir')
## ---------------------- ##
## Existing transitions. ##
......@@ -21,7 +21,7 @@ CHECK_EQ('(<3>a)*(a+<4>(aa*a))', str(a1.infiltrate(a1).expression()))
## -------------------- ##
# a infiltrate a
a = vcsn.automaton(filename = medir + "/a.gv")
a = meaut("a.gv")
CHECK_EQ('''digraph
{
vcsn_context = "letterset<char_letters(a)>, z"
......@@ -49,7 +49,7 @@ CHECK_EQ('''digraph
}''', a.infiltrate(a))
# abc infiltrate abc
abc = vcsn.automaton(filename = medir + "/abc.gv")
abc = meaut("abc.gv")
CHECK_EQ('''digraph
{
vcsn_context = "letterset<char_letters(abc)>, z"
......@@ -111,7 +111,7 @@ CHECK_EQ('''digraph
}''', abc.infiltrate(abc))
# abc infiltrate xy
xy = vcsn.automaton(filename = medir + "/xy.gv")
xy = meaut('xy.gv')
CHECK_EQ('''digraph
{
vcsn_context = "letterset<char_letters(abcxy)>, z"
......
......@@ -8,10 +8,7 @@ ctx = vcsn.context('lan_char, q')
def aut(e):
return ctx.expression(e).automaton()
def load(fname):
return open(medir + '/' + fname).read().strip()
CHECK_EQ(load('aut1.gv'), aut('a+b').ldivide(aut('a*b*')))
CHECK_EQ(metext('aut1.gv'), aut('a+b').ldivide(aut('a*b*')))
# Addition
CHECK_EQUIV(aut(r'<3>\e'), aut('<2>a').ldivide(aut('<6>(a+b)')))
......
......@@ -6,18 +6,12 @@ from test import *
algos = ['hopcroft', 'moore', 'signature', 'weighted']
def aut(file):
return vcsn.automaton(filename = medir + "/" + file)
def file_to_string(file):
return open(medir + "/" + file, "r").read().strip()
def check(algo, aut, exp):
if isinstance(algo, list):
for a in algo:
check(a, aut, exp)
else:
print("checking minimize with algorithm ", algo)
print('checking minimize with algorithm ', algo)
CHECK_EQ(exp, aut.minimize(algo))
# Check that repeated minimization still gives the same type of
# automaton. We don't want to get partition_automaton of
......@@ -46,13 +40,13 @@ def xfail(algo, aut):
## Simple minimization test. The example comes from the "Théorie des
## langages" lecture notes by François Yvon & Akim Demaille.
## Automaton 4.23 at page 59, as of revision a0761d6.
a = aut("redundant.gv")
exp = file_to_string('redundant.exp.gv')
a = meaut('redundant.gv')
exp = metext('redundant.exp.gv')
check('brzozowski', a, vcsn.automaton(exp))
check(algos, a, exp)
## An automaton equal to redundant.exp, with one transition removed.
a = aut('incomplete-non-trim.gv')
a = meaut('incomplete-non-trim.gv')
#xfail('brzozowski', a)
xfail('moore', a)
xfail('signature', a)
......@@ -60,8 +54,8 @@ xfail('weighted', a)
## An automaton equal to redundant.exp, with no initial states. It
## must be minimized into an empty automaton.
a = aut('no-initial-states.gv')
z = file_to_string('no-initial-states.exp.gv')
a = meaut('no-initial-states.gv')
z = metext('no-initial-states.exp.gv')
check('brzozowski', a, z)
xfail('moore', a)
xfail('signature', a)
......@@ -69,7 +63,7 @@ xfail('weighted', a)
## An automaton equal to redundant.exp, with no final states. It must
## be minimized into an empty automaton.
a = aut("no-final-states.gv")
a = meaut('no-final-states.gv')
check('brzozowski', a, z)
xfail('moore', a)
xfail('signature', a)
......@@ -84,7 +78,7 @@ check('moore', all_states_final, all_states_final.minimize('signature'))
a = vcsn.context('lal_char(a-k), b') \
.expression('[a-k]{10}') \
.standard()
exp = file_to_string("intricate.exp.gv")
exp = metext('intricate.exp.gv')
check('brzozowski', a, vcsn.automaton(exp))
check(algos, a, exp)
......@@ -93,25 +87,25 @@ check(algos, a, exp)
a = vcsn.context('lal_char(a), b') \
.expression('a{2}*+a{2}*', 'trivial') \
.standard()
exp = file_to_string("small-nfa.exp.gv")
exp = metext('small-nfa.exp.gv')
check('brzozowski', a, vcsn.automaton(exp))
xfail('moore', a)
check('signature', a, exp)
check('weighted', a, exp)
## A small weighted automaton.
a = aut("small-z.gv")
exp = file_to_string("small-z.exp.gv")
a = meaut('small-z.gv')
exp = metext('small-z.exp.gv')
xfail('brzozowski', a)
xfail('moore', a)
xfail('signature', a)
check('weighted', a, exp)
## Non-lal automata.
a = vcsn.context('law_char(a-c), b').expression("abc(bc)*+acb(bc)*").standard()
exp = file_to_string("nonlal.exp.gv")
check("signature", a, exp)
check("weighted", a, exp)
a = vcsn.context('law_char(a-c), b').expression('abc(bc)*+acb(bc)*').standard()
exp = metext('nonlal.exp.gv')
check('signature', a, exp)
check('weighted', a, exp)
## An already-minimal automaton. This used to fail with Moore,
## because of a subtly wrong optimization attempt in
......@@ -129,12 +123,12 @@ check("weighted", a, exp)
## It remained associated to its old class identifier in
## state_to_class, which in the mean time would come to identify some
## subset of its old value.
a = vcsn.context('lal_char(ab), b').expression("a+ba").automaton()
a = vcsn.context('lal_char(ab), b').expression('a+ba').automaton()
check('brzozowski', a, a)
CHECK_ISOMORPHIC(a.minimize('moore'), a)
CHECK_ISOMORPHIC(a.minimize('signature'), a)
## Check minimization idempotency in the non-lal case as well.
a = vcsn.context('law_char(ab), b').expression("ab").standard()
a = vcsn.context('law_char(ab), b').expression('ab').standard()
CHECK_ISOMORPHIC(a.minimize('signature'), a)
CHECK_ISOMORPHIC(a.minimize('weighted'), a)
......@@ -3,9 +3,9 @@
import vcsn
from test import *
a = vcsn.Q.expression("<3>abc*(<2>d)*+ce<5>").derived_term().strip()
a = vcsn.Q.expression('<3>abc*(<2>d)*+ce<5>').derived_term().strip()
ref = vcsn.automaton("""
ref = vcsn.automaton('''
digraph
{
vcsn_context = "lat<letterset<char_letters(abcde)>, letterset<char_letters(abcde)>>, q"
......@@ -39,12 +39,6 @@ digraph
5 -> F5
5 -> 5 [label = "<2>d|d"]
}
""")
def load(file):
return open(medir + "/" + file + ".gv").read().strip()
''')
CHECK_EQ(a.partial_identity(), ref)
......@@ -10,11 +10,11 @@ z = vcsn.context('lal_char(01), z')
binary = load('lal_char_z/binary.gv')
# power 0.
CHECK_EQ(vcsn.automaton(filename = medir + '/binary^0.gv'),
CHECK_EQ(meaut('binary^0.gv'),
binary & 0)
# power 1.
CHECK_EQ(vcsn.automaton(filename = medir + '/binary^1.gv'),
CHECK_EQ(meaut('binary^1.gv'),
binary & 1)
# power 4.
......
......@@ -3,28 +3,23 @@
import vcsn
from test import *
def load_automaton_from(file):
path = medir + "/" + file + ".gv"
res = open(path).read().strip()
return res
def suffix_check(i, o):
o = load_automaton_from(o)
o = meaut(o, 'gv')
CHECK_EQ(o, i.suffix())
CHECK_EQ(o, i.suffix().suffix())
def prefix_check(i, o):
o = load_automaton_from(o)
o = meaut(o, 'gv')
CHECK_EQ(o, i.prefix())
CHECK_EQ(o, i.prefix().prefix())
def factor_check(i, o):
o = load_automaton_from(o)
o = meaut(o, 'gv')
CHECK_EQ(o, i.factor())
CHECK_EQ(o, i.factor().factor())
def subword_check(i, o):
o = load_automaton_from(o)
o = meaut(o, 'gv')
CHECK_EQ(o, i.subword())
# subword is not idempotent.
......@@ -34,7 +29,7 @@ def subword_check(i, o):
for i in range(1, 5):
base = "aut{}".format(i)
print("Working on {}".format(base))
aut = vcsn.automaton(load_automaton_from(base))
aut = meaut(base, 'gv')
suffix_check(aut, base + "-suffix")
prefix_check(aut, base + "-prefix")
factor_check(aut, base + "-factor")
......
......@@ -8,10 +8,7 @@ ctx = vcsn.context('lan_char, q')
def aut(e):
return ctx.expression(e).automaton()
def load(fname):
return open(medir + '/' + fname).read().strip()
CHECK_EQ(load('aut1.gv'), aut('a*b*').rdivide(aut('a+b')))
CHECK_EQ(metext('aut1.gv'), aut('a*b*').rdivide(aut('a+b')))
# Addition
CHECK_EQUIV(aut(r'<3>\e'), aut('<6>(a+b)').rdivide(aut('<2>a')))
......
......@@ -37,7 +37,7 @@ def check(r, exp=None, file=None):
r = ctx.expression(r)
print("check: {:u}".format(r))
if file:
exp = open(medir + '/' + file + '.gv').read().strip()
exp = metext(file, 'gv')
# Check inductive, standard flavor.
a = r.inductive('standard')
......
......@@ -3,15 +3,12 @@
import vcsn
from test import *
def load(file):
return open(medir + "/" + file + ".gv").read().strip()
## ------------- ##
## synchronize. ##
## ------------- ##
def check(input, file):
aut = load(file)
aut = metext(file, 'gv')
syn = input.synchronize()
CHECK_EQ(aut, syn)
#CHECK_EQUIV(input, syn) #free labelset for determinize?
......@@ -33,7 +30,7 @@ check(ctx.expression(r"""(a|e)((f|\e)+((g|h)(i|j)*(o|k)))(l|n)""").standard().mi
## ----------------- ##
def check(input, file):
aut = load(file)
aut = metext(file, 'gv')
d = input.delay_automaton()
CHECK_EQ(aut, d)
......
......@@ -14,7 +14,7 @@ def check_aut(aut, exp, algo = "auto"):
CHECK_EQ(exp, aut.expression(algo = algo))
# Check the associativity of the product.
check_aut(vcsn.automaton(filename = medir + "/a.gv"), '<x>a(<y>b)*<z>c')
check_aut(meaut('a.gv'), '<x>a(<y>b)*<z>c')
check_aut(load('lal_char_b/a1.gv'), '(a+b)*ab(a+b)*')
check_aut(load('lal_char_b/b1.gv'), '(a+b)*a(a+b)*')
......
......@@ -11,7 +11,7 @@ def union(*auts):
ab = vcsn.context('lal_char(ab), b').expression('(a+b)*').standard()
bc = vcsn.context('lal_char(bc), b').expression('(b+c)*').standard()
CHECK_EQ(vcsn.automaton(filename = medir + '/abc.gv'), union(ab, bc))
CHECK_EQ(meaut('abc.gv'), union(ab, bc))
## ------------ ##
## lal_char_z. ##
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment