From ab6656e2862897edbd55dc7efb41986a54e6953f Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Fri, 24 Jun 2016 20:23:54 +0200 Subject: [PATCH] - refactor trees' tests --- trees/binaryheap/binaryheap_test.go | 50 +++---- trees/redblacktree/redblacktree_test.go | 187 +++++++++++------------- 2 files changed, 113 insertions(+), 124 deletions(-) diff --git a/trees/binaryheap/binaryheap_test.go b/trees/binaryheap/binaryheap_test.go index 66c80e1..7699634 100644 --- a/trees/binaryheap/binaryheap_test.go +++ b/trees/binaryheap/binaryheap_test.go @@ -31,63 +31,65 @@ import ( "testing" ) -func TestBinaryHeap(t *testing.T) { - +func TestBinaryHeapPush(t *testing.T) { heap := NewWithIntComparator() if actualValue := heap.Empty(); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - // insertions - heap.Push(3) - // [3] - heap.Push(2) - // [2,3] - heap.Push(1) - // [1,3,2](2 swapped with 1, hence last) + heap.Push(3) // [3] + heap.Push(2) // [2,3] + heap.Push(1) // [1,3,2](2 swapped with 1, hence last) if actualValue := heap.Values(); actualValue[0].(int) != 1 || actualValue[1].(int) != 3 || actualValue[2].(int) != 2 { t.Errorf("Got %v expected %v", actualValue, "[1,2,3]") } - if actualValue := heap.Empty(); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) } - if actualValue := heap.Size(); actualValue != 3 { t.Errorf("Got %v expected %v", actualValue, 3) } - if actualValue, ok := heap.Peek(); actualValue != 1 || !ok { t.Errorf("Got %v expected %v", actualValue, 1) } +} + +func TestBinaryHeapPop(t *testing.T) { + heap := NewWithIntComparator() + + if actualValue := heap.Empty(); actualValue != true { + t.Errorf("Got %v expected %v", actualValue, true) + } - heap.Pop() + heap.Push(3) // [3] + heap.Push(2) // [2,3] + heap.Push(1) // [1,3,2](2 swapped with 1, hence last) + heap.Pop() // [3,2] if actualValue, ok := heap.Peek(); actualValue != 2 || !ok { t.Errorf("Got %v expected %v", actualValue, 2) } - if actualValue, ok := heap.Pop(); actualValue != 2 || !ok { t.Errorf("Got %v expected %v", actualValue, 2) } - if actualValue, ok := heap.Pop(); actualValue != 3 || !ok { t.Errorf("Got %v expected %v", actualValue, 3) } - if actualValue, ok := heap.Pop(); actualValue != nil || ok { t.Errorf("Got %v expected %v", actualValue, nil) } - if actualValue := heap.Empty(); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := heap.Values(); len(actualValue) != 0 { t.Errorf("Got %v expected %v", actualValue, "[]") } +} + +func TestBinaryHeapRandom(t *testing.T) { + heap := NewWithIntComparator() rand.Seed(3) for i := 0; i < 10000; i++ { @@ -112,15 +114,10 @@ func TestBinaryHeapIterator(t *testing.T) { t.Errorf("Got %v expected %v", actualValue, true) } - // insertions - heap.Push(3) - // [3] - heap.Push(2) - // [2,3] - heap.Push(1) - // [1,3,2](2 swapped with 1, hence last) + heap.Push(3) // [3] + heap.Push(2) // [2,3] + heap.Push(1) // [1,3,2](2 swapped with 1, hence last) - // Iterator it := heap.Iterator() for it.Next() { index := it.Index() @@ -142,6 +139,7 @@ func TestBinaryHeapIterator(t *testing.T) { t.Errorf("Too many") } } + heap.Clear() it = heap.Iterator() for it.Next() { diff --git a/trees/redblacktree/redblacktree_test.go b/trees/redblacktree/redblacktree_test.go index eee8955..480afa7 100644 --- a/trees/redblacktree/redblacktree_test.go +++ b/trees/redblacktree/redblacktree_test.go @@ -31,11 +31,8 @@ import ( "testing" ) -func TestRedBlackTree(t *testing.T) { - +func TestRedBlackTreePut(t *testing.T) { tree := NewWithIntComparator() - - // insertions tree.Put(5, "e") tree.Put(6, "f") tree.Put(7, "g") @@ -45,54 +42,16 @@ func TestRedBlackTree(t *testing.T) { tree.Put(2, "b") tree.Put(1, "a") //overwrite - // Test Size() if actualValue := tree.Size(); actualValue != 7 { t.Errorf("Got %v expected %v", actualValue, 7) } - - // test Keys() if actualValue, expectedValue := fmt.Sprintf("%d%d%d%d%d%d%d", tree.Keys()...), "1234567"; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } - - // test Values() if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s%s%s%s", tree.Values()...), "abcdefg"; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } - // test Left() - if actualValue, expectedValue := fmt.Sprintf("%d", tree.Left().Key), "1"; actualValue != expectedValue { - t.Errorf("Got %v expected %v", actualValue, expectedValue) - } - if actualValue, expectedValue := fmt.Sprintf("%s", tree.Left().Value), "a"; actualValue != expectedValue { - t.Errorf("Got %v expected %v", actualValue, expectedValue) - } - - // test Right() - if actualValue, expectedValue := fmt.Sprintf("%d", tree.Right().Key), "7"; actualValue != expectedValue { - t.Errorf("Got %v expected %v", actualValue, expectedValue) - } - if actualValue, expectedValue := fmt.Sprintf("%s", tree.Right().Value), "g"; actualValue != expectedValue { - t.Errorf("Got %v expected %v", actualValue, expectedValue) - } - - // test Floor() - if node, found := tree.Floor(4); node.Key != 4 || !found { - t.Errorf("Got %v expected %v", node.Key, 4) - } - if node, found := tree.Floor(0); node != nil || found { - t.Errorf("Got %v expected %v", node, "") - } - - // test Ceiling() - if node, found := tree.Ceiling(4); node.Key != 4 || !found { - t.Errorf("Got %v expected %v", node.Key, 4) - } - if node, found := tree.Ceiling(8); node != nil || found { - t.Errorf("Got %v expected %v", node, "") - } - - // key,expectedValue,expectedFound tests1 := [][]interface{}{ {1, "a", true}, {2, "b", true}, @@ -111,30 +70,34 @@ func TestRedBlackTree(t *testing.T) { t.Errorf("Got %v expected %v", actualValue, test[1]) } } +} + +func TestRedBlackTreeRemove(t *testing.T) { + tree := NewWithIntComparator() + tree.Put(5, "e") + tree.Put(6, "f") + tree.Put(7, "g") + tree.Put(3, "c") + tree.Put(4, "d") + tree.Put(1, "x") + tree.Put(2, "b") + tree.Put(1, "a") //overwrite - // removals tree.Remove(5) tree.Remove(6) tree.Remove(7) tree.Remove(8) tree.Remove(5) - // Test Keys() if actualValue, expectedValue := fmt.Sprintf("%d%d%d%d", tree.Keys()...), "1234"; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } - - // test Values() if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s", tree.Values()...), "abcd"; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } - - // test Values() if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s", tree.Values()...), "abcd"; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } - - // Test Size() if actualValue := tree.Size(); actualValue != 4 { t.Errorf("Got %v expected %v", actualValue, 7) } @@ -151,14 +114,12 @@ func TestRedBlackTree(t *testing.T) { } for _, test := range tests2 { - // retrievals actualValue, actualFound := tree.Get(test[0]) if actualValue != test[1] || actualFound != test[2] { t.Errorf("Got %v expected %v", actualValue, test[1]) } } - // removals tree.Remove(1) tree.Remove(4) tree.Remove(2) @@ -166,60 +127,87 @@ func TestRedBlackTree(t *testing.T) { tree.Remove(2) tree.Remove(2) - // Test Keys() if actualValue, expectedValue := fmt.Sprintf("%s", tree.Keys()), "[]"; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } - - // test Values() if actualValue, expectedValue := fmt.Sprintf("%s", tree.Values()), "[]"; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } - - // Test Size() - if actualValue := tree.Size(); actualValue != 0 { - t.Errorf("Got %v expected %v", actualValue, 0) + if empty, size := tree.Empty(), tree.Size(); empty != true || size != -0 { + t.Errorf("Got %v expected %v", empty, true) } - // Test Empty() - if actualValue := tree.Empty(); actualValue != true { - t.Errorf("Got %v expected %v", actualValue, true) +} + +func TestRedBlackTreeLeftAndRight(t *testing.T) { + tree := NewWithIntComparator() + + if actualValue := tree.Left(); actualValue != nil { + t.Errorf("Got %v expected %v", actualValue, nil) + } + if actualValue := tree.Right(); actualValue != nil { + t.Errorf("Got %v expected %v", actualValue, nil) } tree.Put(1, "a") + tree.Put(5, "e") + tree.Put(6, "f") + tree.Put(7, "g") + tree.Put(3, "c") + tree.Put(4, "d") + tree.Put(1, "x") // overwrite tree.Put(2, "b") - tree.Clear() - // Test Empty() - if actualValue := tree.Empty(); actualValue != true { - t.Errorf("Got %v expected %v", actualValue, true) + if actualValue, expectedValue := fmt.Sprintf("%d", tree.Left().Key), "1"; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if actualValue, expectedValue := fmt.Sprintf("%s", tree.Left().Value), "x"; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) } - // test Left() - if actualValue, expectedValue := fmt.Sprintf("%s", tree.Left()), ""; actualValue != expectedValue { - t.Errorf("Got %s expected %s", actualValue, expectedValue) + if actualValue, expectedValue := fmt.Sprintf("%d", tree.Right().Key), "7"; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if actualValue, expectedValue := fmt.Sprintf("%s", tree.Right().Value), "g"; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) } +} + +func TestRedBlackTreeCeilingAndFloor(t *testing.T) { + tree := NewWithIntComparator() - // test Right() - if actualValue, expectedValue := fmt.Sprintf("%s", tree.Right()), ""; actualValue != expectedValue { - t.Errorf("Got %s expected %s", actualValue, expectedValue) + if node, found := tree.Floor(0); node != nil || found { + t.Errorf("Got %v expected %v", node, "") + } + if node, found := tree.Ceiling(0); node != nil || found { + t.Errorf("Got %v expected %v", node, "") } - // test Floor() - if node, found := tree.Floor(1); node != nil || found { + tree.Put(5, "e") + tree.Put(6, "f") + tree.Put(7, "g") + tree.Put(3, "c") + tree.Put(4, "d") + tree.Put(1, "x") + tree.Put(2, "b") + + if node, found := tree.Floor(4); node.Key != 4 || !found { + t.Errorf("Got %v expected %v", node.Key, 4) + } + if node, found := tree.Floor(0); node != nil || found { t.Errorf("Got %v expected %v", node, "") } - // test Ceiling() - if node, found := tree.Ceiling(1); node != nil || found { + if node, found := tree.Ceiling(4); node.Key != 4 || !found { + t.Errorf("Got %v expected %v", node.Key, 4) + } + if node, found := tree.Ceiling(8); node != nil || found { t.Errorf("Got %v expected %v", node, "") } } -func TestRedBlackTreeIterator(t *testing.T) { +func TestRedBlackTreeIterator1(t *testing.T) { tree := NewWithIntComparator() - - // insertions tree.Put(5, "e") tree.Put(6, "f") tree.Put(7, "g") @@ -229,7 +217,6 @@ func TestRedBlackTreeIterator(t *testing.T) { tree.Put(2, "b") tree.Put(1, "a") //overwrite - // Iterator it := tree.Iterator() count := 0 for it.Next() { @@ -249,14 +236,15 @@ func TestRedBlackTreeIterator(t *testing.T) { if actualValue, expectedValue := count, 7; actualValue != expectedValue { t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue) } +} - // Iterator - tree.Clear() +func TestRedBlackTreeIterator2(t *testing.T) { + tree := NewWithIntComparator() tree.Put(3, "c") tree.Put(1, "a") tree.Put(2, "b") - it = tree.Iterator() - count = 0 + it := tree.Iterator() + count := 0 for it.Next() { count += 1 index := it.Key() @@ -274,12 +262,20 @@ func TestRedBlackTreeIterator(t *testing.T) { if actualValue, expectedValue := count, 3; actualValue != expectedValue { t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue) } +} + +func TestRedBlackTreeIterator3(t *testing.T) { + tree := NewWithIntComparator() + + it := tree.Iterator() + for it.Next() { + t.Errorf("Shouldn't iterate on empty stack") + } - // Iterator - tree.Clear() tree.Put(1, "a") + it = tree.Iterator() - count = 0 + count := 0 for it.Next() { count += 1 index := it.Key() @@ -297,16 +293,10 @@ func TestRedBlackTreeIterator(t *testing.T) { if actualValue, expectedValue := count, 1; actualValue != expectedValue { t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue) } +} - // Iterator on empty - tree.Clear() - it = tree.Iterator() - for it.Next() { - t.Errorf("Shouldn't iterate on empty stack") - } - - // Iterator (from image) - tree.Clear() +func TestRedBlackTreeIterator4(t *testing.T) { + tree := NewWithIntComparator() tree.Put(13, 5) tree.Put(8, 3) tree.Put(17, 7) @@ -317,8 +307,9 @@ func TestRedBlackTreeIterator(t *testing.T) { tree.Put(6, 2) tree.Put(22, 8) tree.Put(27, 10) - it = tree.Iterator() - count = 0 + + it := tree.Iterator() + count := 0 for it.Next() { count += 1 value := it.Value()