diff --git a/golang-basic/latihan/01-arius-galileo.go b/golang-basic/latihan/01-arius-galileo.go new file mode 100644 index 0000000..6873257 --- /dev/null +++ b/golang-basic/latihan/01-arius-galileo.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +// polindrone +func polindrone(x string) bool { + var perbedaanHuruf byte + perbedaanHuruf = 0 + var hasilAkhir bool + for i := 0; i < len(x) && perbedaanHuruf == 0; i++ { + perbedaanHuruf = x[i] - x[len(x)-i-1] + if perbedaanHuruf == 0 { + hasilAkhir = true + } else { + hasilAkhir = false + } + } + return hasilAkhir +} + +func main() { + var inputKata string + fmt.Scan(&inputKata) + fmt.Println(polindrone(inputKata)) +} diff --git a/golang-basic/latihan/01-arius-galileo_test.go b/golang-basic/latihan/01-arius-galileo_test.go new file mode 100644 index 0000000..57b4148 --- /dev/null +++ b/golang-basic/latihan/01-arius-galileo_test.go @@ -0,0 +1,42 @@ +package main + +import "testing" + +func Test_polindrone(t *testing.T) { + type args struct { + x string + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "Test polindrome true", + args: args{ + x: "madam", + }, + want: true, + }, + { + name: "Test polindrome true", + args: args{ + x: "kaka", + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := polindrone(tt.args.x); got != tt.want { + t.Errorf("polindrone() = %v, want %v", got, tt.want) + } + }) + } +} + +func BenchmarkPolindrome(b *testing.B) { + for i := 0; i < b.N; i++ { + polindrone("madam") + } +} diff --git a/golang-basic/latihan/02-arius-galileo.go b/golang-basic/latihan/02-arius-galileo.go new file mode 100644 index 0000000..2708ebb --- /dev/null +++ b/golang-basic/latihan/02-arius-galileo.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "strings" +) + +//konsonan + +func vokalDetection(x string) string { + var hurufVokal = [5]string{"a", "i", "u", "e", "o"} + var huruf = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} + var isHuruf bool + var jawaban string + jawaban = "Konsonan" + x = strings.ToLower(x) + if len(x) == 1 { + for i := 0; i < len(huruf); i++ { + if huruf[i] == x { + isHuruf = true + } + } + if isHuruf { + for i := 0; i < len(hurufVokal); i++ { + if hurufVokal[i] == x { + jawaban = "Vokal" + } + } + } else { + jawaban = "Inputan Bukan Huruf, Tolng Input Huruf" + } + } else { + jawaban = "Tolong input 1 karakter saja" + } + + return jawaban +} + +func main() { + var inputHuruf string + fmt.Scan(&inputHuruf) + fmt.Println(vokalDetection(inputHuruf)) +} diff --git a/golang-basic/latihan/02-arius-galileo_test.go b/golang-basic/latihan/02-arius-galileo_test.go new file mode 100644 index 0000000..0938306 --- /dev/null +++ b/golang-basic/latihan/02-arius-galileo_test.go @@ -0,0 +1,70 @@ +package main + +import "testing" + +func Test_vokalDetection(t *testing.T) { + type args struct { + x string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "Test lebih dari 1 karakter", + args: args{ + x: "ac", + }, + want: "Tolong input 1 karakter saja", + }, + { + name: "Test bukan menggunakan huruf", + args: args{ + x: "1", + }, + want: "Inputan Bukan Huruf, Tolng Input Huruf", + }, + { + name: "Test menggunakan konsonan", + args: args{ + x: "g", + }, + want: "Konsonan", + }, + { + name: "Test menggunakan Vokal", + args: args{ + x: "a", + }, + want: "Vokal", + }, + { + name: "Test menggunakan vokal tapi huruf besar", + args: args{ + x: "A", + }, + want: "Vokal", + }, + { + name: "Test menggunakan Konsonan tapi huruf besar", + args: args{ + x: "G", + }, + want: "Konsonan", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := vokalDetection(tt.args.x); got != tt.want { + t.Errorf("vokalDetection() = %v, want %v", got, tt.want) + } + }) + } +} + +func BenchmarkVokalDetection(b *testing.B) { + for i := 0; i < b.N; i++ { + vokalDetection("x") + } +} diff --git a/gomodule-unit-test/latihan/latihan-moq/main.go b/gomodule-unit-test/latihan/latihan-moq/main.go index 0cbd3d4..473875b 100644 --- a/gomodule-unit-test/latihan/latihan-moq/main.go +++ b/gomodule-unit-test/latihan/latihan-moq/main.go @@ -8,6 +8,8 @@ type Student struct { Class int `json:"class"` } +//go:generate moq -out main_mock_test.go . StudentRepositoryInterface + type StudentRepositoryInterface interface { GetAllStudents() ([]Student, error) } diff --git a/gomodule-unit-test/latihan/latihan-moq/main_mock_test.go b/gomodule-unit-test/latihan/latihan-moq/main_mock_test.go new file mode 100644 index 0000000..0e3811d --- /dev/null +++ b/gomodule-unit-test/latihan/latihan-moq/main_mock_test.go @@ -0,0 +1,67 @@ +// Code generated by moq; DO NOT EDIT. +// github.com/matryer/moq + +package main + +import ( + "sync" +) + +// Ensure, that StudentRepositoryInterfaceMock does implement StudentRepositoryInterface. +// If this is not the case, regenerate this file with moq. +var _ StudentRepositoryInterface = &StudentRepositoryInterfaceMock{} + +// StudentRepositoryInterfaceMock is a mock implementation of StudentRepositoryInterface. +// +// func TestSomethingThatUsesStudentRepositoryInterface(t *testing.T) { +// +// // make and configure a mocked StudentRepositoryInterface +// mockedStudentRepositoryInterface := &StudentRepositoryInterfaceMock{ +// GetAllStudentsFunc: func() ([]Student, error) { +// panic("mock out the GetAllStudents method") +// }, +// } +// +// // use mockedStudentRepositoryInterface in code that requires StudentRepositoryInterface +// // and then make assertions. +// +// } +type StudentRepositoryInterfaceMock struct { + // GetAllStudentsFunc mocks the GetAllStudents method. + GetAllStudentsFunc func() ([]Student, error) + + // calls tracks calls to the methods. + calls struct { + // GetAllStudents holds details about calls to the GetAllStudents method. + GetAllStudents []struct { + } + } + lockGetAllStudents sync.RWMutex +} + +// GetAllStudents calls GetAllStudentsFunc. +func (mock *StudentRepositoryInterfaceMock) GetAllStudents() ([]Student, error) { + if mock.GetAllStudentsFunc == nil { + panic("StudentRepositoryInterfaceMock.GetAllStudentsFunc: method is nil but StudentRepositoryInterface.GetAllStudents was just called") + } + callInfo := struct { + }{} + mock.lockGetAllStudents.Lock() + mock.calls.GetAllStudents = append(mock.calls.GetAllStudents, callInfo) + mock.lockGetAllStudents.Unlock() + return mock.GetAllStudentsFunc() +} + +// GetAllStudentsCalls gets all the calls that were made to GetAllStudents. +// Check the length with: +// +// len(mockedStudentRepositoryInterface.GetAllStudentsCalls()) +func (mock *StudentRepositoryInterfaceMock) GetAllStudentsCalls() []struct { +} { + var calls []struct { + } + mock.lockGetAllStudents.RLock() + calls = mock.calls.GetAllStudents + mock.lockGetAllStudents.RUnlock() + return calls +} diff --git a/gomodule-unit-test/latihan/latihan-moq/main_test.go b/gomodule-unit-test/latihan/latihan-moq/main_test.go new file mode 100644 index 0000000..17a2b38 --- /dev/null +++ b/gomodule-unit-test/latihan/latihan-moq/main_test.go @@ -0,0 +1,62 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestStudentService_GetStudent(t *testing.T) { + tests := []struct { + name string + s StudentService + want []Student + wantErr bool + }{ + { + name: "sukses", + s: StudentService{ + StudentRepositoryInterface: &StudentRepositoryInterfaceMock{ + GetAllStudentsFunc: func() ([]Student, error) { + return []Student{ + { + FullName: "Ihsan Arief", + Grade: "B", + Class: 1, + }, + { + FullName: "Tono", + Grade: "A", + Class: 2, + }, + }, nil + }, + }, + }, + wantErr: false, + want: []Student{ + { + FullName: "Ihsan Arief", + Grade: "B", + Class: 1, + }, + { + FullName: "Tono", + Grade: "A", + Class: 2, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.s.GetStudent() + if (err != nil) != tt.wantErr { + t.Errorf("StudentService.GetStudent() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("StudentService.GetStudent() = %v, want %v", got, tt.want) + } + }) + } +}