diff options
Diffstat (limited to 'pkg/rbtree/rbtree_test.go')
-rw-r--r-- | pkg/rbtree/rbtree_test.go | 44 |
1 files changed, 8 insertions, 36 deletions
diff --git a/pkg/rbtree/rbtree_test.go b/pkg/rbtree/rbtree_test.go index 6c49127..7b3987f 100644 --- a/pkg/rbtree/rbtree_test.go +++ b/pkg/rbtree/rbtree_test.go @@ -1,8 +1,6 @@ package rbtree import ( - "fmt" - "strings" "testing" "github.com/stretchr/testify/assert" @@ -10,37 +8,6 @@ import ( "golang.org/x/exp/constraints" ) -func printTree[K constraints.Ordered, V any](t *testing.T, tree *Tree[K, V]) { - fmtBareNode := func(node *Node[V]) string { - if node == nil { - return "nil" - } - if node.Color == Red { - return fmt.Sprintf("R(%v)", node.Value) - } else { - return fmt.Sprintf("B(%v)", node.Value) - } - } - addIndent := func(indent string, lines []string) []string { - ret := make([]string, 0, len(lines)) - for _, line := range lines { - ret = append(ret, indent+line) - } - return ret - } - var fmtNode func(node *Node[V]) []string - fmtNode = func(node *Node[V]) []string { - if node == nil { - return []string{"nil"} - } - ret := addIndent(" ", fmtNode(node.Right)) - ret = append(ret, fmtBareNode(node)) - ret = append(ret, addIndent(" ", fmtNode(node.Left))...) - return ret - } - t.Log("\n" + strings.Join(fmtNode(tree.root), "\n")) -} - func checkTree[K constraints.Ordered, V any](t *testing.T, tree *Tree[K, V]) { // 1. Every node is either red or black @@ -79,9 +46,8 @@ func checkTree[K constraints.Ordered, V any](t *testing.T, tree *Tree[K, V]) { }) for i := range cnts { if cnts[0] != cnts[i] { - if !assert.Truef(t, false, "node %v: not all leafs have same black-count: %v", node.Value, cnts) { - printTree(t, tree) - } + assert.Truef(t, false, "node %v: not all leafs have same black-count: %v", node.Value, cnts) + break } } }) @@ -125,7 +91,13 @@ func FuzzTree(f *testing.F) { assert.Equal(t, val, node.Value) } else { t.Logf("Delete(%v)", val) + if val == 25 { + t.Logf("before:\n\n%s\n", tree.ASCIIArt()) + } tree.Delete(val) + if val == 25 { + t.Logf("after:\n\n%s\n", tree.ASCIIArt()) + } assert.Nil(t, tree.Lookup(val)) } checkTree(t, tree) |