Given a *Node, verify that all the pointers (parent, first child, next sibling, etc.) of - the node itself, - all its child nodes, and - pointers along the silbling chain are valid.
(t *testing.T, n *Node)
| 81 | // - pointers along the silbling chain |
| 82 | // are valid. |
| 83 | func verifyNodePointers(t *testing.T, n *Node) { |
| 84 | if n == nil { |
| 85 | return |
| 86 | } |
| 87 | if n.FirstChild != nil { |
| 88 | testValue(t, n, n.FirstChild.Parent) |
| 89 | } |
| 90 | if n.LastChild != nil { |
| 91 | testValue(t, n, n.LastChild.Parent) |
| 92 | } |
| 93 | |
| 94 | verifyNodePointers(t, n.FirstChild) |
| 95 | // There is no need to call verifyNodePointers(t, n.LastChild) |
| 96 | // because verifyNodePointers(t, n.FirstChild) will traverse all its |
| 97 | // siblings to the end, and if the last one isn't n.LastChild then it will fail. |
| 98 | |
| 99 | parent := n.Parent // parent could be nil if n is the root of a tree. |
| 100 | |
| 101 | // Verify the PrevSibling chain |
| 102 | cur, prev := n, n.PrevSibling |
| 103 | for ; prev != nil; cur, prev = prev, prev.PrevSibling { |
| 104 | testValue(t, prev.Parent, parent) |
| 105 | testValue(t, prev.NextSibling, cur) |
| 106 | } |
| 107 | testTrue(t, cur.PrevSibling == nil) |
| 108 | testTrue(t, parent == nil || parent.FirstChild == cur) |
| 109 | |
| 110 | // Verify the NextSibling chain |
| 111 | cur, next := n, n.NextSibling |
| 112 | for ; next != nil; cur, next = next, next.NextSibling { |
| 113 | testValue(t, next.Parent, parent) |
| 114 | testValue(t, next.PrevSibling, cur) |
| 115 | } |
| 116 | testTrue(t, cur.NextSibling == nil) |
| 117 | testTrue(t, parent == nil || parent.LastChild == cur) |
| 118 | } |
| 119 | |
| 120 | func TestChildNodes(t *testing.T) { |
| 121 | t.Run("Has 3 children", func(t *testing.T) { |
no test coverage detected
searching dependent graphs…