-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcd_lcm_test.go
154 lines (143 loc) · 5.35 KB
/
gcd_lcm_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package maths
import (
"fmt"
"math"
"math/big"
"testing"
)
func TestGCD(t *testing.T) {
testCases := []struct {
input []int
expectedResult int
expectedError bool
}{
{[]int{-6, 10, 0}, 2, false},
{[]int{-12, -6, -4}, 2, false},
{[]int{-2, 3, 4}, 1, false},
{[]int{-3, -3}, 3, false},
{[]int{-1, 2}, 1, false},
{[]int{0, -1}, 1, false},
{[]int{-2}, 2, false},
{[]int{-1}, 1, false},
{[]int{1}, 1, false},
{[]int{2}, 2, false},
{[]int{0, 1}, 1, false},
{[]int{1, 2}, 1, false},
{[]int{3, 3}, 3, false},
{[]int{2, 3, 4}, 1, false},
{[]int{12, 6, 4}, 2, false},
{[]int{6, 9}, 3, false},
{[]int{6, 10}, 2, false},
{[]int{6, 10, 0}, 2, false},
{[]int{130, 65, 10}, 5, false},
{[]int{4950, 3750, 450, 225}, 75, false},
{[]int{527592, 91, 455}, 13, false},
{[]int{math.MaxInt}, math.MaxInt, false},
{[]int{}, 0, false},
{[]int{0}, 0, false},
{[]int{0, 0, 0}, 0, false},
{[]int{0, 0, 0, 10}, 10, false},
{[]int{math.MinInt}, 0, true},
}
for _, tC := range testCases {
testName := fmt.Sprintf("Input: %v", tC.input)
t.Run(testName, func(t *testing.T) {
actualResult, actualError := GCD(tC.input...)
// Check if an error was returned and matches if an error was expected.
if gotError := actualError != nil; gotError != tC.expectedError {
t.Errorf("Expected error: %t, got error: %t, error: %v", tC.expectedError, gotError, actualError)
}
// Check if the actual result matches the expected result.
if actualResult != tC.expectedResult {
t.Errorf("Expected GCD: %d, actual GCD: %d", tC.expectedResult, actualResult)
}
})
}
}
func TestLCM(t *testing.T) {
testCases := []struct {
input []int
expectedResult int
expectedError bool
}{
{[]int{-2, -3, -4}, 12, false},
{[]int{-3, -3}, 3, false},
{[]int{-1, 2}, 2, false},
{[]int{-1, -1}, 1, false},
{[]int{-2}, 2, false},
{[]int{-1}, 1, false},
{[]int{}, 0, false},
{[]int{0}, 0, false},
{[]int{0, 0, 0}, 0, false},
{[]int{1}, 1, false},
{[]int{2}, 2, false},
{[]int{1, 2}, 2, false},
{[]int{3, 3}, 3, false},
{[]int{2, 3, 4}, 12, false},
{[]int{6, 10}, 30, false},
{[]int{5, 10, 65}, 130, false},
{[]int{75, 330, 225, 450}, 4950, false},
{[]int{13, 89, 456}, 527592, false},
{[]int{-2, -3, 4, 0}, 0, false},
{[]int{2, 3, 4, 0}, 0, false},
{[]int{1, 0}, 0, false},
{[]int{math.MinInt, 2}, 0, true}, // Fail GCD.
{[]int{math.MinInt}, 0, true}, // Fail Abs.
{[]int{math.MaxInt}, math.MaxInt, false}, // Overflow.
{[]int{math.MaxInt, 2}, 0, true}, // Overflow.
}
for _, tC := range testCases {
testName := fmt.Sprintf("Input: %v", tC.input)
t.Run(testName, func(t *testing.T) {
actualResult, actualError := LCM(tC.input...)
// Check if an error was returned and matches if an error was expected.
if gotError := actualError != nil; gotError != tC.expectedError {
t.Errorf("Expected error: %t, got error: %t, error: %v", tC.expectedError, gotError, actualError)
}
// Check if the actual result matches the expected result.
if actualResult != tC.expectedResult {
t.Errorf("Expected LCM: %d, actual LCM: %d", tC.expectedResult, actualResult)
}
})
}
}
func TestLCMBig(t *testing.T) {
testCases := []struct {
input []*big.Int
expectedResult *big.Int
}{
{[]*big.Int{big.NewInt(-2), big.NewInt(-3), big.NewInt(-4)}, big.NewInt(12)},
{[]*big.Int{big.NewInt(-3), big.NewInt(-3)}, big.NewInt(3)},
{[]*big.Int{big.NewInt(-1), big.NewInt(2)}, big.NewInt(2)},
{[]*big.Int{big.NewInt(-1), big.NewInt(-1)}, big.NewInt(1)},
{[]*big.Int{big.NewInt(-2)}, big.NewInt(2)},
{[]*big.Int{big.NewInt(-1)}, big.NewInt(1)},
{[]*big.Int{}, big.NewInt(0)},
{[]*big.Int{big.NewInt(0)}, big.NewInt(0)},
{[]*big.Int{big.NewInt(0), big.NewInt(0), big.NewInt(0)}, big.NewInt(0)},
{[]*big.Int{big.NewInt(1)}, big.NewInt(1)},
{[]*big.Int{big.NewInt(2)}, big.NewInt(2)},
{[]*big.Int{big.NewInt(1), big.NewInt(2)}, big.NewInt(2)},
{[]*big.Int{big.NewInt(3), big.NewInt(3)}, big.NewInt(3)},
{[]*big.Int{big.NewInt(2), big.NewInt(3), big.NewInt(4)}, big.NewInt(12)},
{[]*big.Int{big.NewInt(6), big.NewInt(10)}, big.NewInt(30)},
{[]*big.Int{big.NewInt(5), big.NewInt(10), big.NewInt(65)}, big.NewInt(130)},
{[]*big.Int{big.NewInt(75), big.NewInt(330), big.NewInt(225), big.NewInt(450)}, big.NewInt(4950)},
{[]*big.Int{big.NewInt(13), big.NewInt(89), big.NewInt(456)}, big.NewInt(527592)},
{[]*big.Int{big.NewInt(-2), big.NewInt(-3), big.NewInt(4), big.NewInt(0)}, big.NewInt(0)},
{[]*big.Int{big.NewInt(2), big.NewInt(3), big.NewInt(4), big.NewInt(0)}, big.NewInt(0)},
{[]*big.Int{big.NewInt(1), big.NewInt(0)}, big.NewInt(0)},
{[]*big.Int{big.NewInt(math.MinInt), big.NewInt(2)}, big.NewInt(0).Abs(big.NewInt(math.MinInt))},
{[]*big.Int{big.NewInt(math.MinInt), big.NewInt(0).Abs(big.NewInt(math.MinInt))}, big.NewInt(0).Abs(big.NewInt(math.MinInt))},
{[]*big.Int{big.NewInt(math.MaxInt), big.NewInt(2)}, big.NewInt(0).Mul(big.NewInt(math.MaxInt), big.NewInt(2))},
}
for _, tC := range testCases {
testName := fmt.Sprintf("Input: %v", tC.input)
t.Run(testName, func(t *testing.T) {
// Check if the actual result matches the expected result.
if actualResult := LCMBig(tC.input...); actualResult.Cmp(tC.expectedResult) != 0 {
t.Errorf("Expected LCM: %v, actual LCM: %v", tC.expectedResult, actualResult) // Can print the big.Int values OK.
}
})
}
}