This code is now maintained on GitHub
[TestClass]
public class NodeTest
{
[TestMethod]
public void Node_DeepCopy_CopySuccessful()
{
// Arrange
var root = new Node(null);
var node1 = new Node(null);
var node2 = new Node(null);
var copyNode = new Node(null);
// Act
root.AddChild(node1);
root.AddChild(node2);
copyNode = root.DeepCopy();
var actual = copyNode.HasChildren;
// Assert
Assert.AreEqual(true, actual);
}
[TestMethod]
public void Node_DeepCopy_CopyIsIndependent()
{
// Arrange
var root = new Node(null);
var node1 = new Node(null);
var node2 = new Node(null);
var copyNode = new Node(null);
// Act
root.AddChild(node1);
root.AddChild(node2);
copyNode = root.DeepCopy();
root.AddChild(new Node(null));
var actual = root.Count != copyNode.Count;
// Assert
Assert.AreEqual(true, actual);
}
[TestMethod]
public void Node_Search_ReturnsAllElements()
{
// Arrange
const int EXPECTED_CHILDREN_COUNT = 3;
var root = new Node(null);
var root_child1 = new Node(null);
var root_child2 = new Node(null);
var root_child3 = new Node(null);
// Act
root.AddChild(root_child1);
root.AddChild(root_child2);
root.AddChild(root_child3);
int actual = root.Count;
// Assert
Assert.AreEqual(EXPECTED_CHILDREN_COUNT, actual);
}
[TestMethod]
public void Node_RecursiveSearch_ReturnsAllElements()
{
// Arrange
const int EXPECTED_CHILDREN_COUNT = 9;
var root = new Node("Root node");
var rc1 = new Node("[Gen 1] 1st child of: root");
var rc2 = new Node("[Gen 1] 2nd child of: root");
var rc3 = new Node("[Gen 1] 3rd child of: root");
var rc2_1 = new Node("[Gen 2] 1st child of: root's 2nd child");
var rc2_2 = new Node("[Gen 2] 2nd child of: root's 2nd child");
var rc3_1 = new Node("[Gen 2] 1st child of: root's 3rd child");
var rc2_1_1 = new Node("[Gen 3] 1st child of: root's 2nd child's 1st child");
var rc3_1_1 = new Node("[Gen 3] 1st child of: root's 3rd child's 1st child");
var rc3_1_1_1 = new Node("[Gen 4] 1st child of: root's 3rd child's 1st child's 1st child");
// Act
rc2_1.AddChild(rc2_1_1);
rc2.AddChild(rc2_1);
rc2.AddChild(rc2_2);
rc3_1_1.AddChild(rc3_1_1_1);
rc3_1.AddChild(rc3_1_1);
rc3.AddChild(rc3_1);
root.AddChild(rc1);
root.AddChild(rc2);
root.AddChild(rc3);
int actual = new List<Node>(root.GetChildrenRecursive()).Count;
// Assert
Assert.AreEqual(EXPECTED_CHILDREN_COUNT, actual);
}
}
```