Verified Commit 93d731b2 authored by Vladimír Štill's avatar Vladimír Štill
Browse files

test: Add some basic tests for CFG

parent 620bcbaa
......@@ -4,11 +4,16 @@ PY = $(wildcard *.py)
MYPY ?= mypy
all : check
all : test
check : $(PY:%=%.mypy)
typecheck : $(PY:%=%.mypy)
test: typecheck unit
%.mypy : %
$(MYPY) --check-untyped-defs --warn-redundant-casts --warn-unused-ignores --warn-return-any $<
$(MYPY) --typecheck-untyped-defs --warn-redundant-casts --warn-unused-ignores --warn-return-any $<
.PHONY: %.mypy check all
.PHONY: %.mypy typecheck test unit all
from ...common import Nonterminal, Terminal
from ... import cfl
from copy import deepcopy
A = Nonterminal("A")
B = Nonterminal("B")
C = Nonterminal("C")
a = Terminal("a")
b = Terminal("b")
Rules = cfl.CFG.Rules
rules0: Rules = dict()
g0 = cfl.CFG({A}, {a, b}, rules0, A)
rules1: Rules = {A: {(a,), (a, A), (b, B), (b,)},
B: {(b,), (b, B)}}
g1 = cfl.CFG({A, B}, {a, b}, rules1, A)
rules2: Rules = {A: {(a,), (a, A), (b, B)},
B: {(b, B), ()}}
g2 = cfl.CFG({A, B}, {a, b}, rules2, A)
def test_empty():
assert g0.is_empty()
def test_basics():
assert g1.init == A
assert g1.terminals == {a, b}
assert g1.nonterminals == {A, B}
assert g1.rules == rules1
collected_prods = set(
assert collected_prods == {(src, dst) for src, prods in rules1.items()
for dst in prods}
def test_generates():
def go(g):
assert g.generates("a")
assert g.generates("b")
assert g.generates("aa")
assert g.generates("ab")
assert g.generates("abbb")
assert g.generates("aabbb")
assert not g.generates("")
assert not g.generates("ba")
def test_remove_eps():
g = g2.epsilon_normal_form()
assert g.init == g1.init
assert g.terminals == g1.terminals
assert g.nonterminals == g1.nonterminals
assert g.rules == g1.rules
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