Split RepeatFive, handle counts, add examples

This commit is contained in:
2026-03-15 23:59:21 +01:00
parent be5fafa655
commit 083ff01e53
2 changed files with 40 additions and 8 deletions

View File

@@ -1,11 +1,36 @@
package iteration
import "testing"
import (
"fmt"
"testing"
)
func TestRepeat(t *testing.T) {
got := Repeat("a")
want := "aaaaa"
assertResult(t, got, want)
t.Run("RepeatFive a", func(t *testing.T) {
got := RepeatFive("a")
want := "aaaaa"
assertResult(t, got, want)
})
t.Run("RepeatFive z", func(t *testing.T) {
got := RepeatFive("z")
want := "zzzzz"
assertResult(t, got, want)
})
t.Run("repeat z, 11 times", func(t *testing.T) {
got := Repeat("z", 11)
want := "zzzzzzzzzzz"
assertResult(t, got, want)
})
}
func ExampleRepeatFive() {
fmt.Println(RepeatFive("y"))
// Output: yyyyy
}
func ExampleRepeat() {
fmt.Println(Repeat("t", 7))
// Output: ttttttt
}
func assertResult(t testing.TB, got, want string) {
@@ -17,6 +42,6 @@ func assertResult(t testing.TB, got, want string) {
func BenchmarkRepeat(b *testing.B) {
for b.Loop() {
Repeat("a")
RepeatFive("a")
}
}