Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prime-factors: use slices package to sort and compare test cases #2771

Merged
merged 1 commit into from
Jul 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
}
}
Loading