private Tree buildTree(AstNode node) { Tree t = new Tree(node.getType(), Tree.NO_LABEL, Token.typeToName(node.getType())); t.setPos(node.getAbsolutePosition()); t.setLength(node.getLength()); trees.put(node, t); return t; }
void compareListIterator(Iterator<Tree> it, String... expected) { for (String e : expected) { Tree n = it.next(); assertEquals(e, n.getLabel()); } assertFalse("Iterator has next", it.hasNext()); }
@Test public void testHeight() { assertEquals(2, root.getHeight()); // depth of a assertEquals(1, root.getChildren().get(0).getHeight()); // depth of b assertEquals(0, root.getChildren().get(0).getChildren().get(0).getHeight()); // depth of c assertEquals(0, root.getChildren().get(0).getChildren().get(1).getHeight()); // depth of d assertEquals(0, root.getChildren().get(1).getHeight()); // depth of e }
@Test public void testPreOrderNumbering() { TreeUtils.preOrderNumbering(root); assertEquals(0, root.getId()); // id of a assertEquals(1, root.getChildren().get(0).getId()); // id of b assertEquals(2, root.getChildren().get(0).getChildren().get(0).getId()); // id of c assertEquals(3, root.getChildren().get(0).getChildren().get(1).getId()); // id of d assertEquals(4, root.getChildren().get(1).getId()); // id of e }
@Test public void testDepth() { TreeUtils.computeDepth(root); assertEquals(root.getDepth(), 0); assertEquals(root.getChildren().get(0).getDepth(), 1); assertEquals(root.getChildren().get(0).getChildren().get(0).getDepth(), 2); assertEquals(root.getChildren().get(0).getChildren().get(1).getDepth(), 2); assertEquals(root.getChildren().get(1).getDepth(), 1); }
@Test public void testPostOrderNumbering() { TreeUtils.postOrderNumbering(root); assertEquals(root.getId(), 4); assertEquals(root.getChildren().get(0).getId(), 2); assertEquals(root.getChildren().get(0).getChildren().get(0).getId(), 0); assertEquals(root.getChildren().get(0).getChildren().get(1).getId(), 1); assertEquals(root.getChildren().get(1).getId(), 3); }
@Test public void testRemoveCompletelyMappedDescendants() { dst.getChildren().get(0).setMatched(true); dst.getChildren().get(0).getChildren().get(0).getChildren().get(0).setMatched(true); dst.getChildren().get(1).setMatched(true); dst.getChildren().get(1).getChildren().get(0).setMatched(true); dst = TreeUtils.removeCompletelyMapped(dst); TreeUtils.computeSize(dst); assertTrue(dst.getSize() == 5); }
@Test public void testBreadthFirstNumbering() { System.out.println(root.toCompleteTreeString()); TreeUtils.breadthFirstNumbering(root); System.out.println(root.toCompleteTreeString()); assertEquals(0, root.getId()); assertEquals(1, root.getChildren().get(0).getId()); assertEquals(2, root.getChildren().get(1).getId()); assertEquals(3, root.getChildren().get(0).getChildren().get(0).getId()); assertEquals(4, root.getChildren().get(0).getChildren().get(1).getId()); }
@Test public void testDigest() { Tree croot = root.deepCopy(); TreeUtils.computeDigest(root); TreeUtils.computeDigest(croot); assertTrue(root.getDigest() == croot.getDigest()); croot.getChildren().get(0).getChildren().get(0).setLabel("x"); TreeUtils.computeDigest(croot); assertFalse(root.getDigest() == croot.getDigest()); assertEquals("[(a@@0[(b@@1[(c@@3)][(d@@3)])][(e@@2)])]", root.toDigestTreeString()); }
public boolean visit(AstNode node) { if (node instanceof AstRoot) return true; else { Tree t = buildTree(node); Tree p = trees.get(node.getParent()); p.addChild(t); if (node instanceof Name) { Name name = (Name) node; t.setLabel(name.getIdentifier()); } else if (node instanceof StringLiteral) { StringLiteral literal = (StringLiteral) node; t.setLabel(literal.getValue()); } else if (node instanceof NumberLiteral) { NumberLiteral l = (NumberLiteral) node; t.setLabel(l.getValue()); } return true; } }