Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/golang_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ jobs:
version: v1.51.0
args: --disable-all -E gofmt --print-linter-name
skip-build-cache: true
- name: run math unit testing
run: go test ./math/...
- name: run math implementation unit testing
run: go test ./math_implementation/...
- name: run unit testing
run: go test $(go list ./... | grep -v 'basic\|intermediate\|utilities')
2 changes: 1 addition & 1 deletion math_implementation/armstrong/is_armstrong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var testCase = []struct {
},
}

func testingArmstrong(t *testing.T) {
func TestArmstrong(t *testing.T) {
for _, test := range testCase {
t.Run(test.name, func(t *testing.T) {
funcResult := cekArmstrong(test.input)
Expand Down
66 changes: 66 additions & 0 deletions math_implementation/faktorial/faktorial.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package faktorial

import "errors"

var ArgumentNegatif = errors.New("argumen input harus berupa bilangan bulat non-negatif")

// fungsi untuk menghitung faktorial secara bruteforce
func Iterasi(n int) (int, error) {
if n < 0 {
return 0, ArgumentNegatif
}

hasil := 1
for i := 2; i <= n; i++ {
hasil *= i
}
return hasil, nil
}

// fungsi menghitung faktorial secara rekursif
func Rekursif(n int) (int, error) {
if n < 0 {
return 0, ArgumentNegatif
}

if n <= 1 {
return 1, nil
}

prev, _ := Rekursif(n - 1)
return n * prev, nil
}

// menghitung faktorial menggunakan tree biner (divide and conquer)
func TreeBin(n int) (int, error) {
if n < 0 {
return 0, ArgumentNegatif
}

if n == 0 {
return 1, nil
}

if n == 1 || n == 2 {
return n, nil
}

return produkTree(2, n), nil
}

func produkTree(l int, r int) int {
if l > r {
return 1
}

if l == r {
return l
}

if r-l == 1 {
return l * r
}

m := (l + r) / 2
return produkTree(l, m) * produkTree(m+1, r)
}
47 changes: 47 additions & 0 deletions math_implementation/faktorial/faktorial_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package faktorial

import (
"fmt"
"testing"
)

type fungsiFaktorial func(int) (int, error)

var implementasi = map[string]fungsiFaktorial{
"Iterasi": Iterasi,
"Rekursif": Rekursif,
"Tree": TreeBin,
}

var testCase = []struct {
n int
ekspetasi int
}{
{0, 1},
{1, 1},
{6, 720},
{9, 362880},
}

func TestFaktorial(t *testing.T) {
for implName, implFunction := range implementasi {
t.Run(implName+" error input negatif", func(t *testing.T) {
_, error := implFunction(-1)
if error != ArgumentNegatif {
t.Errorf("tidak ada error dari input negatif")
}
})

for _, tc := range testCase {
t.Run(fmt.Sprintf("%s dengan input %d", implName, tc.n), func(t *testing.T) {
aktual, err := implFunction(tc.n)
if err != nil {
t.Errorf("add error")
}
if aktual != tc.ekspetasi {
t.Errorf("ekspetasi: %d, aktual: %d", tc.ekspetasi, aktual)
}
})
}
}
}
Loading