Skip to content

Commit

Permalink
use slices.Equal instead of reflrect.DeepEqual where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
spring1843 committed Jun 28, 2024
1 parent e7f87f5 commit 8b27a0e
Show file tree
Hide file tree
Showing 30 changed files with 61 additions and 65 deletions.
4 changes: 2 additions & 2 deletions array/add_two_numbers_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package array

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -26,7 +26,7 @@ func TestAddTwoNumbers(t *testing.T) {
{[]int{9, 9, 9}, []int{9, 9, 9}, []int{1, 9, 9, 8}},
}
for i, test := range tests {
if got := AddTwoNumbers(test.num1, test.num2); !reflect.DeepEqual(got, test.sum) {
if got := AddTwoNumbers(test.num1, test.num2); !slices.Equal(got, test.sum) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sum, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions array/bubble_sort_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package array

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -27,7 +27,7 @@ func TestBubbleSort(t *testing.T) {
}
for i, test := range tests {
BubbleSort(test.input)
if !reflect.DeepEqual(test.input, test.sorted) {
if !slices.Equal(test.input, test.sorted) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sorted, test.input)
}
}
Expand Down
4 changes: 2 additions & 2 deletions array/insertion_sort_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package array

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -28,7 +28,7 @@ func TestInsertionSort(t *testing.T) {
}
for i, test := range tests {
InsertionSort(test.input)
if !reflect.DeepEqual(test.input, test.sorted) {
if !slices.Equal(test.input, test.sorted) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sorted, test.input)
}
}
Expand Down
4 changes: 2 additions & 2 deletions array/product_of_all_other_elements_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package array

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -26,7 +26,7 @@ func TestProductOfAllOtherElements(t *testing.T) {
}

for i, test := range tests {
if got := ProductOfAllOtherElements(test.list); !reflect.DeepEqual(got, test.products) {
if got := ProductOfAllOtherElements(test.list); !slices.Equal(got, test.products) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.products, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions array/reverse_inplace_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package array

import (
"reflect"
"slices"
"testing"
)

Expand Down Expand Up @@ -31,7 +31,7 @@ func TestReverseInPlace(t *testing.T) {

for i, test := range tests {
ReverseInPlace(test.list, test.start, test.end)
if !reflect.DeepEqual(test.list, test.reversed) {
if !slices.Equal(test.list, test.reversed) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.reversed, test.list)
}
}
Expand Down
4 changes: 2 additions & 2 deletions array/rotate_k_steps_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package array

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -27,7 +27,7 @@ func TestRotateKSteps(t *testing.T) {

for i, test := range tests {
RotateKSteps(test.list, test.steps)
if !reflect.DeepEqual(test.list, test.rotatedList) {
if !slices.Equal(test.list, test.rotatedList) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.rotatedList, test.list)
}
}
Expand Down
4 changes: 2 additions & 2 deletions backtracking/generate_parentheses_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package backtracking

import (
"reflect"
"slices"
"sort"
"testing"
)
Expand Down Expand Up @@ -32,7 +32,7 @@ func TestGenerateParentheses(t *testing.T) {
if len(got) > 0 {
sort.Strings(got)
}
if !reflect.DeepEqual(test.validParentheses, got) {
if !slices.Equal(test.validParentheses, got) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.validParentheses, got)
}
}
Expand Down
3 changes: 1 addition & 2 deletions backtracking/maze_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package backtracking

import (
"reflect"
"testing"
)

Expand Down Expand Up @@ -39,7 +38,7 @@ func TestMaze(t *testing.T) {
}

for i, test := range tests {
if got := Maze(test.m, test.n, test.walls, test.start, test.finish); !reflect.DeepEqual(test.moves, got) {
if got := Maze(test.m, test.n, test.walls, test.start, test.finish); test.moves != got {
t.Fatalf("Failed test case #%d. Want %s got %s", i, test.moves, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions backtracking/phone_letter_combinations_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package backtracking

import (
"reflect"
"slices"
"sort"
"testing"
)
Expand Down Expand Up @@ -33,7 +33,7 @@ func TestPhoneLetterCombinations(t *testing.T) {
if len(got) > 0 {
sort.Strings(got)
}
if !reflect.DeepEqual(test.combinations, got) {
if !slices.Equal(test.combinations, got) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.combinations, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions dnc/merge_sort_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dnc

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -27,7 +27,7 @@ func TestMergeSort(t *testing.T) {
}

for i, test := range tests {
if got := MergeSort(test.list); !reflect.DeepEqual(got, test.sorted) {
if got := MergeSort(test.list); !slices.Equal(got, test.sorted) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sorted, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions dnc/quick_sort_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dnc

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -27,7 +27,7 @@ func TestQuickSort(t *testing.T) {
}

for i, test := range tests {
if got := QuickSort(test.list); !reflect.DeepEqual(got, test.sorted) {
if got := QuickSort(test.list); !slices.Equal(got, test.sorted) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sorted, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions dnc/rate_limit_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dnc

import (
"reflect"
"slices"
"testing"
"time"
)
Expand Down Expand Up @@ -43,7 +43,7 @@ func TestRateLimiter(t *testing.T) {
got = append(got, IsAllowed(test.limitPerSecond))
}

if !reflect.DeepEqual(got, test.want) {
if !slices.Equal(got, test.want) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.want, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions dnc/towers_of_hanoi_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dnc

import (
"reflect"
"slices"
"testing"
)

Expand Down Expand Up @@ -29,7 +29,7 @@ func TestTowerOfHanoi(t *testing.T) {
}

for i, test := range tests {
if got := TowerOfHanoi(test.n, test.start, test.end); !reflect.DeepEqual(got, test.moves) {
if got := TowerOfHanoi(test.n, test.start, test.end); !slices.Equal(got, test.moves) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.moves, got)
}
}
Expand Down
7 changes: 2 additions & 5 deletions dp/rod_cutting_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package dp

import (
"reflect"
"testing"
)
import "testing"

/*
TestRodCutting tests solution(s) with the following signature and problem description:
Expand All @@ -30,7 +27,7 @@ func TestRodCutting(t *testing.T) {
}

for i, test := range tests {
if got := CutRod(test.snacks, test.n); !reflect.DeepEqual(got, test.solution) {
if got := CutRod(test.snacks, test.n); got != test.solution {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.solution, got)
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/spring1843/go-dsa

go 1.20
go 1.22

require github.com/spf13/cobra v1.7.0

Expand Down
6 changes: 3 additions & 3 deletions graph/iterative_traversal_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package graph

import (
"reflect"
"slices"
"testing"
)

Expand Down Expand Up @@ -33,10 +33,10 @@ func TestIterativeTraversal(t *testing.T) {

for i, test := range tests {
bfs, dfs := IterativeTraversal(toGraph(test.graph))
if !reflect.DeepEqual(bfs, test.bfs) {
if !slices.Equal(bfs, test.bfs) {
t.Fatalf("Failed BFS test case #%d. Want %#v got %#v", i, test.bfs, bfs)
}
if !reflect.DeepEqual(dfs, test.dfs) {
if !slices.Equal(dfs, test.dfs) {
t.Fatalf("Failed DFS test case #%d. Want %#v got %#v", i, test.dfs, dfs)
}
}
Expand Down
4 changes: 2 additions & 2 deletions graph/remove_invalid_parentheses_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package graph

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -28,7 +28,7 @@ func TestRemoveInvalidParentheses(t *testing.T) {
}

for i, test := range tests {
if got := RemoveInvalidParentheses(test.input); !reflect.DeepEqual(got, test.outputs) {
if got := RemoveInvalidParentheses(test.input); !slices.Equal(got, test.outputs) {
t.Errorf("Failed test case #%d. Want %#v got %#v", i, test.outputs, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions graph/topological_sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package graph

import (
"errors"
"reflect"
"slices"
"testing"
)

Expand Down Expand Up @@ -64,7 +64,7 @@ func TestTopologicalSort(t *testing.T) {
t.Fatalf("Failed test case #%d. Expected error %s did not occur", i, test.expectedErr)
}

if !reflect.DeepEqual(got, test.topologicalSort) {
if !slices.Equal(got, test.topologicalSort) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.topologicalSort, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions greedy/activity_selector_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package greedy

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -27,7 +27,7 @@ func TestActivitySelector(t *testing.T) {
{[]int{1, 3, 0, 5, 3, 5, 6, 7, 8, 2, 12}, []int{4, 5, 6, 7, 9, 9, 10, 11, 12, 14, 16}, []int{1, 4, 8, 11}},
}
for i, test := range tests {
if got := ActivitySelector(test.start, test.finish); !reflect.DeepEqual(got, test.activities) {
if got := ActivitySelector(test.start, test.finish); !slices.Equal(got, test.activities) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.activities, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions greedy/max_number_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package greedy

import (
"reflect"
"slices"
"testing"
)

Expand Down Expand Up @@ -39,7 +39,7 @@ func TestMaxNumber(t *testing.T) {
}

for i, test := range tests {
if got := MaxNumber(test.number1, test.number2, test.k); !reflect.DeepEqual(got, test.largestNumber) {
if got := MaxNumber(test.number1, test.number2, test.k); !slices.Equal(got, test.largestNumber) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.largestNumber, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions greedy/task_scheduling_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package greedy

import (
"reflect"
"slices"
"testing"
)

Expand Down Expand Up @@ -29,7 +29,7 @@ func TestScheduleEvents(t *testing.T) {
}

for i, test := range tests {
if got := ScheduleEvents(test.events); !reflect.DeepEqual(got, test.schedule) {
if got := ScheduleEvents(test.events); !slices.Equal(got, test.schedule) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.schedule, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions hashtable/sum_up_to_k_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hashtable

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -25,7 +25,7 @@ func TestSumUpToK(t *testing.T) {

for i, test := range tests {
got := SumUpToK(test.numbers, test.k)
if !reflect.DeepEqual(got, test.indices) {
if !slices.Equal(got, test.indices) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.indices, got)
}
}
Expand Down
Loading

0 comments on commit 8b27a0e

Please sign in to comment.