-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime_factorisation_test.go
79 lines (72 loc) · 2.17 KB
/
prime_factorisation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package maths
import (
"fmt"
"math"
"testing"
)
func TestPrimeFactorisation(t *testing.T) {
testCases := []struct {
input int
expectedResult []PrimeFactor
}{
{math.MinInt, []PrimeFactor{{2, 63}}},
{-5, []PrimeFactor{{5, 1}}},
{-4, []PrimeFactor{{2, 2}}},
{-3, []PrimeFactor{{3, 1}}},
{-2, []PrimeFactor{{2, 1}}},
{-1, []PrimeFactor{{1, 1}}},
{0, []PrimeFactor{{0, 1}}},
{1, []PrimeFactor{{1, 1}}},
{2, []PrimeFactor{{2, 1}}},
{3, []PrimeFactor{{3, 1}}},
{4, []PrimeFactor{{2, 2}}},
{5, []PrimeFactor{{5, 1}}},
{6, []PrimeFactor{{2, 1}, {3, 1}}},
{7, []PrimeFactor{{7, 1}}},
{8, []PrimeFactor{{2, 3}}},
{9, []PrimeFactor{{3, 2}}},
{10, []PrimeFactor{{2, 1}, {5, 1}}},
{100, []PrimeFactor{{2, 2}, {5, 2}}},
{101, []PrimeFactor{{101, 1}}},
{1000, []PrimeFactor{{2, 3}, {5, 3}}},
{4561356, []PrimeFactor{{2, 2}, {3, 1}, {593, 1}, {641, 1}}},
{600851475143, []PrimeFactor{{71, 1}, {839, 1}, {1471, 1}, {6857, 1}}},
{math.MaxInt, []PrimeFactor{{7, 2}, {73, 1}, {127, 1}, {337, 1}, {92737, 1}, {649657, 1}}},
}
for _, tC := range testCases {
testName := fmt.Sprintf("Input: %d", tC.input)
t.Run(testName, func(t *testing.T) {
primeFactorisationCh := PrimeFactorisation(tC.input)
for _, expectedPrimeFactor := range tC.expectedResult {
if actualPrimeFactor := <-primeFactorisationCh; actualPrimeFactor != expectedPrimeFactor {
t.Errorf("Actual factor: %v. Expected factor: %v.", actualPrimeFactor, expectedPrimeFactor)
}
}
if factor, more := <-primeFactorisationCh; more {
t.Errorf("Received more prime factors than expected. Unexpected prime factor: %v", factor)
}
})
}
}
func FuzzPrimeFactorisation(f *testing.F) {
testcases := []int{2, 5, 10, 20, 50, 100}
for _, tc := range testcases {
f.Add(tc) // Use f.Add to provide a seed corpus
}
f.Fuzz(func(t *testing.T, orig int) {
resultCh := PrimeFactorisation(orig)
result := 1
for factor := range resultCh {
for i := 0; i < factor.Index; i++ {
result *= factor.Value
}
}
absOrig, err := Abs(orig)
if err != nil {
t.Skip("Failed to get Abs of input")
}
if result != absOrig {
t.Errorf("Expected %d, got %d", orig, result)
}
})
}