Skip to content

Commit

Permalink
refactor: Extract CompareInts into cmp package and use it
Browse files Browse the repository at this point in the history
  • Loading branch information
idsulik committed Nov 10, 2024
1 parent c4b3dcb commit 6e9ee91
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
26 changes: 9 additions & 17 deletions avltree/avltree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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}
Expand Down Expand Up @@ -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}

Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions internal/cmp/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
33 changes: 12 additions & 21 deletions rbtree/rbtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 6e9ee91

Please sign in to comment.