-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlcm_test.go
77 lines (72 loc) · 1.12 KB
/
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
package numbertheory
import (
"fmt"
"testing"
)
func ExampleLCM() {
lcm := LCM(48, 18)
fmt.Printf("The LCM of 18 and 48 is %v.", lcm)
// Output:
// The LCM of 18 and 48 is 144.
}
func TestLCM(t *testing.T) {
cases := []struct {
name string
a int
b int
expected string
}{
{
name: "48,18",
a: 48,
b: 18,
expected: "144",
},
{
name: "18,48",
a: 18,
b: 48,
expected: "144",
},
{
name: "-18,48",
a: -18,
b: 48,
expected: "-144",
},
{
name: "11,13",
a: 11,
b: 13,
expected: "143",
},
{
name: "0,11",
a: 0,
b: 11,
expected: "0",
},
{
name: "0,0",
a: 0,
b: 0,
expected: "0",
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
actual := LCM(c.a, c.b)
if fmt.Sprintf("%v", actual) != c.expected {
t.Logf("expected %v, got %v\n", c.expected, actual)
t.FailNow()
}
})
}
}
func BenchmarkLCM(b *testing.B) {
for i := 0; i < b.N; i++ {
LCM(i, i+30)
}
}