mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 20:04:43 +01:00
code kata 230815
This commit is contained in:
45
code_kata_230815/prime_factors_test.go
Normal file
45
code_kata_230815/prime_factors_test.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PrimeFactorsOf(n int) []int {
|
||||||
|
ret := make([]int, 0)
|
||||||
|
d := 2
|
||||||
|
for d < n {
|
||||||
|
for n%d == 0 {
|
||||||
|
ret = append(ret, d)
|
||||||
|
n /= d
|
||||||
|
}
|
||||||
|
d++
|
||||||
|
}
|
||||||
|
if n > 1 {
|
||||||
|
ret = append(ret, n)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrimeFactorsOf(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
input int
|
||||||
|
want []int
|
||||||
|
}{
|
||||||
|
{"1", 1, []int{}},
|
||||||
|
{"2", 2, []int{2}},
|
||||||
|
{"4", 4, []int{2, 2}},
|
||||||
|
{"8", 8, []int{2, 2, 2}},
|
||||||
|
{"9", 9, []int{3, 3}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got := PrimeFactorsOf(tc.input)
|
||||||
|
if !reflect.DeepEqual(got, tc.want) {
|
||||||
|
t.Errorf("PrimeFactors(%d) = %v, want %v\n", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user