Skip to content

Commit bd5fed8

Browse files
authored
chore: menambahkan fungsi faktorial (#45)
- membuat go test dengan exclude folder basic dan intermediate karena bagian dari tutorial golang - merubah fungsi test armstrong jadi `TestArmstrong` dari `testingArmstrong` Signed-off-by: slowy07 <[email protected]>
1 parent 8bc3903 commit bd5fed8

File tree

4 files changed

+116
-5
lines changed

4 files changed

+116
-5
lines changed

.github/workflows/golang_test.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,5 @@ jobs:
2222
version: v1.51.0
2323
args: --disable-all -E gofmt --print-linter-name
2424
skip-build-cache: true
25-
- name: run math unit testing
26-
run: go test ./math/...
27-
- name: run math implementation unit testing
28-
run: go test ./math_implementation/...
25+
- name: run unit testing
26+
run: go test $(go list ./... | grep -v 'basic\|intermediate\|utilities')

math_implementation/armstrong/is_armstrong_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var testCase = []struct {
3434
},
3535
}
3636

37-
func testingArmstrong(t *testing.T) {
37+
func TestArmstrong(t *testing.T) {
3838
for _, test := range testCase {
3939
t.Run(test.name, func(t *testing.T) {
4040
funcResult := cekArmstrong(test.input)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package faktorial
2+
3+
import "errors"
4+
5+
var ArgumentNegatif = errors.New("argumen input harus berupa bilangan bulat non-negatif")
6+
7+
// fungsi untuk menghitung faktorial secara bruteforce
8+
func Iterasi(n int) (int, error) {
9+
if n < 0 {
10+
return 0, ArgumentNegatif
11+
}
12+
13+
hasil := 1
14+
for i := 2; i <= n; i++ {
15+
hasil *= i
16+
}
17+
return hasil, nil
18+
}
19+
20+
// fungsi menghitung faktorial secara rekursif
21+
func Rekursif(n int) (int, error) {
22+
if n < 0 {
23+
return 0, ArgumentNegatif
24+
}
25+
26+
if n <= 1 {
27+
return 1, nil
28+
}
29+
30+
prev, _ := Rekursif(n - 1)
31+
return n * prev, nil
32+
}
33+
34+
// menghitung faktorial menggunakan tree biner (divide and conquer)
35+
func TreeBin(n int) (int, error) {
36+
if n < 0 {
37+
return 0, ArgumentNegatif
38+
}
39+
40+
if n == 0 {
41+
return 1, nil
42+
}
43+
44+
if n == 1 || n == 2 {
45+
return n, nil
46+
}
47+
48+
return produkTree(2, n), nil
49+
}
50+
51+
func produkTree(l int, r int) int {
52+
if l > r {
53+
return 1
54+
}
55+
56+
if l == r {
57+
return l
58+
}
59+
60+
if r-l == 1 {
61+
return l * r
62+
}
63+
64+
m := (l + r) / 2
65+
return produkTree(l, m) * produkTree(m+1, r)
66+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package faktorial
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type fungsiFaktorial func(int) (int, error)
9+
10+
var implementasi = map[string]fungsiFaktorial{
11+
"Iterasi": Iterasi,
12+
"Rekursif": Rekursif,
13+
"Tree": TreeBin,
14+
}
15+
16+
var testCase = []struct {
17+
n int
18+
ekspetasi int
19+
}{
20+
{0, 1},
21+
{1, 1},
22+
{6, 720},
23+
{9, 362880},
24+
}
25+
26+
func TestFaktorial(t *testing.T) {
27+
for implName, implFunction := range implementasi {
28+
t.Run(implName+" error input negatif", func(t *testing.T) {
29+
_, error := implFunction(-1)
30+
if error != ArgumentNegatif {
31+
t.Errorf("tidak ada error dari input negatif")
32+
}
33+
})
34+
35+
for _, tc := range testCase {
36+
t.Run(fmt.Sprintf("%s dengan input %d", implName, tc.n), func(t *testing.T) {
37+
aktual, err := implFunction(tc.n)
38+
if err != nil {
39+
t.Errorf("add error")
40+
}
41+
if aktual != tc.ekspetasi {
42+
t.Errorf("ekspetasi: %d, aktual: %d", tc.ekspetasi, aktual)
43+
}
44+
})
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)