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), :-)