Commit 3151708d authored by Akim Demaille's avatar Akim Demaille
Browse files

multiply: determistic: fix the code, fix the tests

Issue #36.

* vcsn/algos/multiply.hh: When determinizing, use the result...
* vcsn/algos/sum.hh: Provide a functional version.
* tests/python/multiply.py (check_mult_deter): Remove.
Instead of duplicating code, make it more generic.
Check that the result is deterministic when it should be.
While at it, check that the result is standard when the input
is.
parent 32b74667
......@@ -109,7 +109,7 @@ def PASS(*msg, **kwargs):
def SKIP(*msg):
PASS('# SKIP', *msg)
def XFAIL(fun, exp = None):
def XFAIL(fun, exp=None):
'''Run `fun`: it should fail. If `exp` is given, check that the
exception includes it.'''
try:
......@@ -124,12 +124,12 @@ def XFAIL(fun, exp = None):
else:
FAIL('did not raise an exception', str(fun))
def CHECK(effective, loc = None):
def CHECK(effective, msg='', loc=None):
"Check that `effective` is `True`."
if effective:
PASS(loc=loc)
else:
FAIL("assertion failed", loc=loc)
FAIL("assertion failed", msg, loc=loc)
def CHECK_EQ(expected, effective, loc = None):
"Check that `effective` is equal to `expected`."
......
......@@ -3,9 +3,7 @@
import vcsn
from test import *
ctx = vcsn.context('lal_char, q')
def check_mult(lhs, rhs, algo="standard"):
def check_mult(lhs, rhs):
if isinstance(lhs, list):
for aut in lhs:
check_mult(aut, rhs)
......@@ -13,37 +11,36 @@ def check_mult(lhs, rhs, algo="standard"):
for aut in rhs:
check_mult(lhs, aut)
else:
print("general")
gen = lhs.multiply(rhs, "general")
aut = lhs.multiply(rhs, algo)
CHECK_EQUIV(gen, aut)
def check_mult_deter(lhs, rhs):
if isinstance(lhs, list):
for aut in lhs:
check_mult(aut, rhs)
elif isinstance(rhs, list):
for aut in rhs:
check_mult(lhs, aut)
else:
gen = lhs.multiply(rhs, "general")
det = lhs.multiply(rhs, "deterministic")
CHECK_EQUIV(gen, det)
if isinstance(rhs, vcsn.automaton):
print("deterministic")
det = lhs.multiply(rhs, "deterministic")
CHECK(det.is_deterministic(), "is deterministic")
CHECK_EQUIV(gen, det)
print("standard")
std = lhs.multiply(rhs, "standard")
if (lhs.is_standard()
and (not isinstance(rhs, vcsn.automaton) or rhs.is_standard())):
CHECK(std.is_standard(), "is standard")
CHECK_EQUIV(gen, std)
ctx = vcsn.context('lal_char, q')
auts = [ctx.expression('a').standard(),
ctx.expression('ab').standard(),
ctx.expression('a+b').standard(),
ctx.expression('a<2>', identities='none').standard()]
check_mult(auts, [1, 3, (-1, 5), (2, 4), (2, -1)])
# We want the determinization to terminate.
ctx = vcsn.context('lal_char, b')
auts = [auts,
ctx.expression('a(ba)*').automaton('derived_term'),
ctx.expression('a+b').derived_term(breaking=True),
ctx.expression('a*').derived_term()]
check_mult(auts, auts, "deterministic")
auts = [auts,
ctx.expression('a*').derived_term()]
check_mult_deter(auts, auts)
check_mult(auts, auts)
ab = vcsn.context('lal_char(ab), b').expression('(a+b)*')
bc = vcsn.context('lal_char(bc), b').expression('(b+c)*')
......
......@@ -33,7 +33,7 @@ namespace vcsn
multiply_here(Aut1& res, const Aut2& b, deterministic_tag = {})
{
multiply_here(res, b, standard_tag{});
determinize(res);
res = determinize(res)->strip();
return res;
}
......
......@@ -2,6 +2,7 @@
#include <map>
#include <vcsn/algos/determinize.hh>
#include <vcsn/algos/standard.hh> // is_standard
#include <vcsn/algos/tags.hh>
#include <vcsn/core/join-automata.hh>
......@@ -28,8 +29,10 @@ namespace vcsn
Aut1&
sum_here(Aut1& res, const Aut2& b, deterministic_tag)
{
// FIXME: this is a stub: there are better algorithms to compute a
// deterministic sum of deterministic automata.
sum_here(res, b, standard_tag{});
assert(0);
res = determinize(res)->strip();
return res;
}
......
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