Skip to content

Commit 695abcc

Browse files
committed
test: refator gcd and lcm
1 parent 982e21f commit 695abcc

File tree

3 files changed

+135
-19
lines changed

3 files changed

+135
-19
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,9 @@ Math.pow(2.71, 3.15), Math.round(3.14), Math.sqrt(225)
920920

921921
- Combination: [cpp](/cpp-algorithm/src/math/combination.h)(`GenerateCombination`) | Find the number of ways to choose $k$ items from $n$ items.
922922
- Fast Fourier transform: Fast Fourier transform is a mathematical algorithm that finds the discrete Fourier transform of a set of real numbers.
923-
- Greatest common divisor (GCD), CLRS#31.2: [python](/python-algorithm/algorithm/math/greatest_common_divisor.py), [java](/java-algorithm/src/main/java/com/example/algorithm/math/GreatestCommonDivisor.java), [golang](/go-algorithm/pkg/math/greatest_common_divisor.go) | Find the greatest common divisor of two numbers.
923+
- Greatest common divisor (GCD), CLRS#31.2: [python](/python-algorithm/algorithm/math/greatest_common_divisor.py), [golang](/go-algorithm/pkg/math/greatest_common_divisor.go), [java](/java-algorithm/src/main/java/com/example/algorithm/math/GreatestCommonDivisor.java) | Find the greatest common divisor of two numbers.
924924
- Integer factorization: [java](/java-algorithm/src/main/java/com/example/algorithm/math/IntegerFactorization.java) | Integer factorization is the process of determining which prime numbers divide a given positive integer.
925-
- Least common multiple (LCM): [python](/python-algorithm/algorithm/math/least_common_multiple.py), [java](/java-algorithm/src/main/java/com/example/algorithm/math/LeastCommonMultiple.java), [golang](/go-algorithm/pkg/math/least_common_multiple.go) | Find the least common multiple of two numbers.
925+
- Least common multiple (LCM): [python](/python-algorithm/algorithm/math/least_common_multiple.py), [golang](/go-algorithm/pkg/math/least_common_multiple.go), [java](/java-algorithm/src/main/java/com/example/algorithm/math/LeastCommonMultiple.java) | Find the least common multiple of two numbers.
926926
- Miller-Rabin primality test, CLRS#31.8: [cpp](/cpp-algorithm/src/math/miller_rabin.h) | Miller-Rabin primality test is a mathematical algorithm that finds whether a given number is prime.
927927
- Permutation
928928
- given string: [cpp](/cpp-algorithm/src/math/permutation.h)(`Permutation`) | Find the permutation of a set of items from given string.
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,97 @@
11
package math
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

8-
func TestGCDEuclidean(t *testing.T) {
9-
assert.Equal(t, 12, GCDEuclidean(24, 36))
10-
assert.Equal(t, 1, GCDEuclidean(17, 22))
11-
assert.Equal(t, 20, GCDEuclidean(120, 500))
9+
func Test_GCDEuclidean(t *testing.T) {
10+
type args struct {
11+
a int
12+
b int
13+
}
14+
tests := []struct {
15+
name string
16+
args args
17+
want int
18+
}{
19+
{
20+
name: "Case 1",
21+
args: args{
22+
a: 24,
23+
b: 36,
24+
},
25+
want: 12,
26+
},
27+
{
28+
name: "Case 2",
29+
args: args{
30+
a: 17,
31+
b: 22,
32+
},
33+
want: 1,
34+
},
35+
{
36+
name: "Case 3",
37+
args: args{
38+
a: 120,
39+
b: 500,
40+
},
41+
want: 20,
42+
},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
got := GCDEuclidean(tt.args.a, tt.args.b)
48+
49+
assert.Equalf(t, tt.want, got, "GCDEuclidean(%v, %v)", tt.args.a, tt.args.b)
50+
})
51+
}
1252
}
1353

14-
func TestGCDEuclideanExtended(t *testing.T) {
15-
d1, _, _ := GCDEuclideanExtended(24, 36)
16-
assert.Equal(t, 12, d1)
17-
d2, _, _ := GCDEuclideanExtended(17, 22)
18-
assert.Equal(t, 1, d2)
19-
d3, _, _ := GCDEuclideanExtended(120, 500)
20-
assert.Equal(t, 20, d3)
54+
func Test_GCDEuclideanExtended(t *testing.T) {
55+
type args struct {
56+
a int
57+
b int
58+
}
59+
tests := []struct {
60+
name string
61+
args args
62+
want int
63+
}{
64+
{
65+
name: "Case 1",
66+
args: args{
67+
a: 24,
68+
b: 36,
69+
},
70+
want: 12,
71+
},
72+
{
73+
name: "Case 2",
74+
args: args{
75+
a: 17,
76+
b: 22,
77+
},
78+
want: 1,
79+
},
80+
{
81+
name: "Case 3",
82+
args: args{
83+
a: 120,
84+
b: 500,
85+
},
86+
want: 20,
87+
},
88+
}
89+
90+
for _, tt := range tests {
91+
t.Run(tt.name, func(t *testing.T) {
92+
got, _, _ := GCDEuclideanExtended(tt.args.a, tt.args.b)
93+
94+
assert.Equalf(t, tt.want, got, "GCDEuclideanExtended(%v, %v)", tt.args.a, tt.args.b)
95+
})
96+
}
2197
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,52 @@
11
package math
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

8-
func TestLCM(t *testing.T) {
9-
assert.Equal(t, 72, LCM(24, 36))
10-
assert.Equal(t, 374, LCM(17, 22))
11-
assert.Equal(t, 3000, LCM(120, 500))
9+
func Test_LCM(t *testing.T) {
10+
type args struct {
11+
a int
12+
b int
13+
}
14+
tests := []struct {
15+
name string
16+
args args
17+
want int
18+
}{
19+
{
20+
name: "Case 1",
21+
args: args{
22+
a: 24,
23+
b: 36,
24+
},
25+
want: 72,
26+
},
27+
{
28+
name: "Case 2",
29+
args: args{
30+
a: 17,
31+
b: 22,
32+
},
33+
want: 374,
34+
},
35+
{
36+
name: "Case 3",
37+
args: args{
38+
a: 120,
39+
b: 500,
40+
},
41+
want: 3000,
42+
},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
got := LCM(tt.args.a, tt.args.b)
48+
49+
assert.Equalf(t, tt.want, got, "LCM() got = %v, want %v", got, tt.want)
50+
})
51+
}
1252
}

0 commit comments

Comments
 (0)