Beispiel #1
0
    def to_fsm(self, alphabet=None):
        if alphabet is None:
            alphabet = self.alphabet()

        return reduce(lambda x, y: x | y,
                      map(lambda x: x.to_fsm(alphabet), self.concs),
                      fsm.null(alphabet))
Beispiel #2
0
def test_new_set_methods(a, b):
    # A whole bunch of new methods were added to the FSM module to enable FSMs to
    # function exactly as if they were sets of strings (symbol lists), see:
    # https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset
    # But do they work?
    assert len(a) == 1
    assert len((a | b) * 4) == 16
    try:
        len(a.star())
        assert False
    except OverflowError:
        pass

    # "in"
    assert "a" in a
    assert not "a" in b
    assert "a" not in b

    # List comprehension!
    four = (a | b) * 2
    for string in four:
        assert string == ["a", "a"]
        break
    assert [s for s in four] == [["a", "a"], ["a", "b"], ["b", "a"],
                                 ["b", "b"]]

    # set.union() imitation
    assert FSM.union(a, b) == a.union(b)
    assert len(FSM.union()) == 0
    assert FSM.intersection(a, b) == a.intersection(b)

    # This takes a little explaining. In general, `a & b & c` is equivalent to
    # `EVERYTHING & a & b & c` where `EVERYTHING` is an FSM accepting every
    # possible string. Similarly `a` is equivalent to `EVERYTHING & a`, and the
    # intersection of no sets at all is... `EVERYTHING`.
    # However, since we compute the union of alphabets, and there are no
    # alphabets, the union is the empty set. So the only string which `EVERYTHING`
    # actually recognises is the empty string, [] (or "" if you prefer).
    int_none = FSM.intersection()
    assert len(int_none) == 1
    assert [] in int_none

    assert (a | b).difference(a) == FSM.difference(
        (a | b), a) == (a | b) - a == b
    assert (a | b).difference(a, b) == FSM.difference(
        (a | b), a, b) == (a | b) - a - b == null("ab")
    assert a.symmetric_difference(b) == FSM.symmetric_difference(a, b) == a ^ b
    assert a.isdisjoint(b)
    assert a <= (a | b)
    assert a < (a | b)
    assert a != (a | b)
    assert (a | b) > a
    assert (a | b) >= a

    assert list(a.concatenate(a, a).strings()) == [["a", "a", "a"]]
    assert list(a.concatenate().strings()) == [["a"]]
    assert list(FSM.concatenate(b, a, b).strings()) == [["b", "a", "b"]]
    assert list(FSM.concatenate().strings()) == []
    assert not a.copy() is a
Beispiel #3
0
def test_derive(a, b):
    # Just some basic tests because this is mainly a regex thing.
    assert a.derive("a") == epsilon({"a", "b"})
    assert a.derive("b") == null({"a", "b"})
    try:
        a.derive("c")
        assert False
    except KeyError:
        assert True
    assert (a * 3).derive("a") == a * 2
    assert (a.star() - epsilon({"a", "b"})).derive("a") == a.star()
Beispiel #4
0
def test_builtins():
    assert not null("a").accepts("a")
    assert epsilon("a").accepts("")
    assert not epsilon("a").accepts("a")
Beispiel #5
0
def test_alternation_a(a):
    altA = a | null({"a", "b"})
    assert not altA.accepts("")
    assert altA.accepts("a")