def test_pop_double(): d = Derivation() d.add("bbs", -1) d.add("bSa", 2) assert ("bSa", 2) == d.pop() assert ("bbs", -1) == d.pop() assert d.isEmpty()
def test_all_derivations_two(): cfg = ({"E"}, {"a", "b"}, {"E": ["E+E", "E*E", "(E)", "a", "b"]}, "E") string = "a+b+a" firstDerivation = Derivation(entries=[("E", 0), ("E+E", 0), ("E+E+E", 0), ("a+E+E", 2), ("a+b+E", 4), ("a+b+a", -1)]) secondDerivation = Derivation(entries=[("E", 0), ("E+E", 0), ("E+E+E", 0), ("a+E+E", 4), ("a+E+a", 2), ("a+b+a", -1)]) derivations = d.getAllDerivations(cfg, string, MAX_SUBSTITUTIONS=5, MAX_DERIVATIONS=36) # for de in derivations: print str(de) assert firstDerivation in derivations assert secondDerivation in derivations
def test_simple_derivations(): cfg = (["S", "A"], ["a", "b"], {"S": ["aA"], "A": ["a", "b"]}, "S") string = "aa" firstDerivation = Derivation(entries=[("S", 0), ("aA", 1), ("aa", -1)]) derivations = d.getAllDerivations(cfg, string, MAX_SUBSTITUTIONS=5, MAX_DERIVATIONS=2) for de in derivations: print str(de) assert firstDerivation in derivations
def test_get_left_derivations(): cfg = ({"S", "A"}, {"a", "b"}, {"S": ["aAS", "a"], "A": ["SbA", "SS", "ba"]}, "S") string = "aabbaa" expected = Derivation(entries=[("S", 0), ("aAS", 1), ("aSbAS", 1), ("aabAS", 3), ("aabbaS", 5), ("aabbaa", -1)]) actual = d.getLeftDerivation(cfg, string) print actual print expected assert expected == actual
def test_add(): d = Derivation() d.add("AAs", 1)
def test_clear(): d = Derivation() d.add("bbs", -1) d.add("bSa", 2) d.clear() assert d.isEmpty()
def test_empty(): d = Derivation() assert d.isEmpty()
def test_pop_simple(): d = Derivation() d.add("bBs", 2) d.pop() assert d.isEmpty()
def test_add_no_variable(): d = Derivation() d.add("aas", -1)