Esempio n. 1
0
 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;
 }
Esempio n. 2
0
 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());
 }
Esempio n. 3
0
 @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
 }
Esempio n. 4
0
 @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
 }
Esempio n. 5
0
 @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);
 }
Esempio n. 6
0
 @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);
 }
Esempio n. 7
0
 @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);
 }
Esempio n. 8
0
 @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());
 }
Esempio n. 9
0
 @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());
 }
Esempio n. 10
0
  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;
    }
  }