Unit test creating the proper data structure

How to check if the data structure is built correctly? I am implementing the look of a modified base tree and wondering how you verify the correctness of the data structure.

Consider a node tree TreeNode {String, Int}. You always want to add a new child to the deepest node of a value of 0, as in the following example:

Root, 0
- Child_1, 5
- Child_2, 0
   - Child_3, 1

The question is, how is unit test, if a tree structure is created according to your desire? TreeNode has only one method that will be insert.

My idea so far has been to write TreeVisitorthat would go through a tree and convert each node to a string. The tree from the above example might look like this:

[Root, 0 [Child_1, 5] [Child_2, 0 [Child_3, 1]]]

Knowing the algorithm that builds the tree, I can create such a string manually, if I have an idea which elements I am inserting. My unit test will look like this (using the same example).

TreeNode root = new TreeNode("Root", 0);
root.insert(new TreeNode("Child_1", 5));
root.insert(new TreeNode("Child_2", 0));
root.insert(new TreeNode("Child_3", 1));
TreeVisitor visitor = new TreeVisitor();
String expected = "[Root, 0 [Child_1, 5][Child_2, 0 [Child_3, 1]]]";
asssertEquals(expected, visitor.visit(root));

I have a feeling that this is not the best approach. To start, as soon as the visitor changes, all tests will fail (just put change [ ]in ( )). In addition, this approach allows me to test quite small trees (as large as I can calculate manually). How would you test larger ones?

General question: how to write tests, checking the correctness of the construction of the data structure ?

, , , , , , .Sum(a, b), :-)

+3
3

Test Driven Design . "" , . , - , , , .

, , ( , - ). .

, (.. Java TreeMap, , TreeView), :

  • debugVerifyTree.
  • : 36542 , , , .

, , , , : " , ?". , .

+3

, TreeNode unit test , , . TreeNode , .

+1

, , , , . , .

TreeNode root = new MyTreeNode("0.0", 0);
root.insert(new MyTreeNode("1.0", 5));
root.insert(new MyTreeNode("1.1", 0));
root.insert(new MyTreeNode("2.0", 1));
verify(root, 0, 0);
...
public void verify(TreeNode node, int depth, int index) {
   verifyName(node, depth, index);
   int numChildren = node.getChildCount();
   depth++;
   for (int i = 0; i < numChildren; i++) {
      verify(node.getChildAt(i), depth, i);
   }
}
public void verifyName(TreeNode node, int depth, int index) {
   StringBuilder expectedName = new StringBuilder();
   expectedName.append(depth).append('.').append(index);
   assertEquals("Tree node not in expected place",
                expectedName.toString(), node.getName());
}

. , .

public void verify(TreeNode root) {
   Stack<TreeNode> toBeVerified = new Stack<TreeNode>();
   verifyName(root, 0, 0);
   toBeVerified.push(root);

   while(!toBeVerified.isEmpty()) {
     TreeNode node = toBeVerified.pop();
     int depth = getDepth(node.getName()) + 1;
     int numChildren = node.getChildCount();
     for (int i = 0; i < numChildren; i++) {
        verifyName(node, depth, i);
        toBeVerified.push(node);
     }
   }
}
public int getDepth(String name) {
   return Integer.parseInt(name.substring(0, name.indexOf('.')));
}
+1

Source: https://habr.com/ru/post/1717506/


All Articles