Skip to content

Commit

Permalink
prime-factors: use slices package to sort and compare test cases (#2771)
Browse files Browse the repository at this point in the history
[no important files changed]
  • Loading branch information
zamai authored Jul 27, 2024
1 parent c549923 commit c825ad5
Showing 1 changed file with 4 additions and 24 deletions.
28 changes: 4 additions & 24 deletions exercises/practice/prime-factors/prime_factors_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package prime

import (
"sort"
"slices"
"testing"
)

func TestPrimeFactors(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
actual := Factors(tc.input)
sort.Slice(actual, ascending(actual))
sort.Slice(tc.expected, ascending(tc.expected))
if !slicesEqual(actual, tc.expected) {
slices.Sort(actual)
slices.Sort(tc.expected)
if !slices.Equal(actual, tc.expected) {
t.Fatalf("Factors(%d)\n got:%#v\nwant:%#v", tc.input, actual, tc.expected)
}
})
Expand All @@ -28,23 +28,3 @@ func BenchmarkPrimeFactors(b *testing.B) {
}
}
}

func slicesEqual(a, b []int64) bool {
if len(a) != len(b) {
return false
}

for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}

return true
}

func ascending(list []int64) func(int, int) bool {
return func(i, j int) bool {
return list[i] < list[j]
}
}

0 comments on commit c825ad5

Please sign in to comment.