diff --git a/avltree/avltree_test.go b/avltree/avltree_test.go index 9b55490..7d2cec3 100644 --- a/avltree/avltree_test.go +++ b/avltree/avltree_test.go @@ -2,22 +2,14 @@ package avltree import ( "testing" -) -func compareInts(a, b int) int { - if a < b { - return -1 - } - if a > b { - return 1 - } - return 0 -} + "github.com/idsulik/go-collections/v2/internal/cmp" +) func TestAVLTree(t *testing.T) { t.Run( "Basic Operations", func(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) // Test Insert and Search tree.Insert(5) @@ -40,7 +32,7 @@ func TestAVLTree(t *testing.T) { t.Run( "Balance After Insert", func(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) // Left-Left case tree.Insert(30) @@ -66,7 +58,7 @@ func TestAVLTree(t *testing.T) { t.Run( "Delete Operations", func(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) // Insert some values values := []int{10, 5, 15, 3, 7, 12, 17} @@ -96,7 +88,7 @@ func TestAVLTree(t *testing.T) { t.Run( "InOrder Traversal", func(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) values := []int{5, 3, 7, 1, 4, 6, 8} expected := []int{1, 3, 4, 5, 6, 7, 8} @@ -125,7 +117,7 @@ func TestAVLTree(t *testing.T) { t.Run( "Height Calculation", func(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) if h := tree.Height(); h != 0 { t.Errorf("Expected height 0 for empty tree, got %d", h) @@ -146,7 +138,7 @@ func TestAVLTree(t *testing.T) { t.Run( "Clear and IsEmpty", func(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) if !tree.IsEmpty() { t.Error("New tree should be empty") @@ -173,7 +165,7 @@ func TestAVLTree(t *testing.T) { t.Run( "Complex Balancing", func(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) values := []int{10, 20, 30, 40, 50, 25} for _, v := range values { diff --git a/internal/cmp/cmp.go b/internal/cmp/cmp.go index 0c388ec..214dd61 100644 --- a/internal/cmp/cmp.go +++ b/internal/cmp/cmp.go @@ -6,3 +6,13 @@ type Ordered interface { ~float32 | ~float64 | ~string } + +func CompareInts(a, b int) int { + if a < b { + return -1 + } + if a > b { + return 1 + } + return 0 +} diff --git a/rbtree/rbtree_test.go b/rbtree/rbtree_test.go index 526b299..f4087d0 100644 --- a/rbtree/rbtree_test.go +++ b/rbtree/rbtree_test.go @@ -5,18 +5,9 @@ import ( "math/rand" "testing" "time" -) -// compareInts is a helper function for comparing integers -func compareInts(a, b int) int { - if a < b { - return -1 - } - if a > b { - return 1 - } - return 0 -} + "github.com/idsulik/go-collections/v2/internal/cmp" +) // verifyRedBlackProperties checks if the tree maintains Red-Black properties func verifyRedBlackProperties[T any](t *RedBlackTree[T]) bool { @@ -77,7 +68,7 @@ func verifyNodeProperties[T any](n *node[T], parent *node[T]) (int, bool) { } func TestNewRedBlackTree(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) if tree == nil { t.Error("Expected non-nil tree") } @@ -105,7 +96,7 @@ func TestRedBlackTree_Insert(t *testing.T) { for _, tt := range tests { t.Run( tt.name, func(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) uniqueValues := make(map[int]bool) for _, v := range tt.values { @@ -171,7 +162,7 @@ func TestRedBlackTree_Delete(t *testing.T) { for _, tt := range tests { t.Run( tt.name, func(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) // Insert values for _, v := range tt.insertOrder { @@ -232,7 +223,7 @@ func TestRedBlackTree_InOrderTraversal(t *testing.T) { for _, tt := range tests { t.Run( tt.name, func(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) for _, v := range tt.values { tree.Insert(v) } @@ -274,7 +265,7 @@ func TestRedBlackTree_Height(t *testing.T) { for _, tt := range tests { t.Run( tt.name, func(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) for _, v := range tt.values { tree.Insert(v) } @@ -289,7 +280,7 @@ func TestRedBlackTree_Height(t *testing.T) { } func TestRedBlackTree_Clear(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) values := []int{5, 3, 7, 1, 9} for _, v := range values { @@ -317,7 +308,7 @@ func TestRedBlackTree_Clear(t *testing.T) { } func TestRedBlackTree_RandomOperations(t *testing.T) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) rng := rand.New(rand.NewSource(time.Now().UnixNano())) operations := 1000 @@ -370,7 +361,7 @@ func BenchmarkRedBlackTree(b *testing.B) { b.Run( fmt.Sprintf("Insert_%s", bm.name), func(b *testing.B) { for i := 0; i < b.N; i++ { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) for j := 0; j < bm.size; j++ { tree.Insert(j) } @@ -380,7 +371,7 @@ func BenchmarkRedBlackTree(b *testing.B) { b.Run( fmt.Sprintf("Search_%s", bm.name), func(b *testing.B) { - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) for i := 0; i < bm.size; i++ { tree.Insert(i) } @@ -400,7 +391,7 @@ func BenchmarkRedBlackTree(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { b.StopTimer() - tree := New[int](compareInts) + tree := New[int](cmp.CompareInts) for _, v := range values { tree.Insert(v) }