Add iteration module
This commit is contained in:
12
iteration/iteration.go
Normal file
12
iteration/iteration.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package iteration
|
||||||
|
|
||||||
|
const repeatCount = 5
|
||||||
|
|
||||||
|
func Repeat(character string) string {
|
||||||
|
var repeated string
|
||||||
|
for range repeatCount {
|
||||||
|
repeated = repeated + character
|
||||||
|
}
|
||||||
|
|
||||||
|
return repeated
|
||||||
|
}
|
||||||
22
iteration/iteration_test.go
Normal file
22
iteration/iteration_test.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package iteration
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestRepeat(t *testing.T) {
|
||||||
|
got := Repeat("a")
|
||||||
|
want := "aaaaa"
|
||||||
|
assertResult(t, got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertResult(t testing.TB, got, want string) {
|
||||||
|
t.Helper()
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %q want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkRepeat(b *testing.B) {
|
||||||
|
for b.Loop() {
|
||||||
|
Repeat("a")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user