Esempio n. 1
0
 def test_postorder(self):
     a = ParseTree(
         1, [ParseTree(2),
             ParseTree(3, [ParseTree(4)]),
             ParseTree(5)])
     for i, j in zip(a.postorder(), [2, 4, 3, 5, 1]):
         self.assertEqual(i, j)
Esempio n. 2
0
 def test_preorder(self):
     a = ParseTree(
         1, [ParseTree(2),
             ParseTree(3, [ParseTree(4)]),
             ParseTree(5)])
     for i, j in zip(a.preorder(), [1, 2, 3, 4, 5]):
         self.assertEqual(i, j)
 def test_postorder_subtrees(self):
     two = ParseTree(2)
     four = ParseTree(4)
     three = ParseTree(3, [four])
     five = ParseTree(5)
     one = ParseTree(1, [two, three, five])
     for i, j in zip(one.postorder(values=False),
                     [two, four, three, five, one]):
         self.assertEqual(i, j)
Esempio n. 4
0
 def test_expand_excludes_terminals(self):
     tree = ParseTree(self.A)
     self.assertEqual(frontier_values(tree), [self.A])
     tree.expand([self.B, "hello", self.B])
     self.assertEqual(frontier_values(tree), [self.B, self.B])
     tree.frontier[1].expand([self.A, "world", self.A])
     self.assertEqual(frontier_values(tree), [self.B, self.A, self.A])
Esempio n. 5
0
 def test_expand_propagates_to_ancestors(self):
     tree = ParseTree(self.A)
     self.assertEqual(frontier_values(tree), [self.A])
     tree.expand([self.B, self.B])
     self.assertEqual(frontier_values(tree), [self.B, self.B])
     tree.frontier[1].expand([self.A, self.B, self.A])
     self.assertEqual(frontier_values(tree),
                      [self.B, self.A, self.B, self.A])
Esempio n. 6
0
 def setUp(self):
     self.A = Nonterminal("A")
     self.B = Nonterminal("B")
     self.t1 = ParseTree(self.A)
     self.t2 = ParseTree(self.A)
     self.t3 = ParseTree(self.B)
     self.t4 = ParseTree(self.A, [self.t2, ParseTree("a"), self.t3])
     self.t = ParseTree(self.A, [ParseTree("a"), self.t1, self.t4])
Esempio n. 7
0
 def test_string_excludes_childless_nonterminals(self):
     tree = ParseTree(self.A, [])
     self.assertEqual(tree.string(), "")
Esempio n. 8
0
 def test_string_traverses_tree(self):
     tree = ParseTree(self.A, ["Hello", "world!"])
     self.assertEqual(tree.string(), "Hello world!")
Esempio n. 9
0
 def test_expand_empty_production_shrinks_frontier(self):
     tree = ParseTree(self.A)
     self.assertEqual(frontier_values(tree), [self.A])
     tree.expand(())
     self.assertEqual(frontier_values(tree), [])
Esempio n. 10
0
 def test_isleaf(self):
     a = ParseTree(5)
     self.assertTrue(a.isleaf())
     b = ParseTree(4, [ParseTree(6)])
     self.assertFalse(b.isleaf())