Compare commits
5 Commits
2b554d42c7
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| c9cc104fe5 | |||
| 083ff01e53 | |||
| be5fafa655 | |||
| cfab2d02a9 | |||
| 0ac0075904 |
2
go.mod
2
go.mod
@@ -1,3 +1,3 @@
|
||||
module go_with_tests
|
||||
module ak-homelab.duckdns.org/gitea/hoborg/go_with_tests
|
||||
|
||||
go 1.26.1
|
||||
|
||||
17
iteration/iteration.go
Normal file
17
iteration/iteration.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package iteration
|
||||
|
||||
import "strings"
|
||||
|
||||
const defaultRepeatCount = 5
|
||||
|
||||
func RepeatFive(character string) string {
|
||||
return Repeat(character, defaultRepeatCount)
|
||||
}
|
||||
|
||||
func Repeat(character string, count int) string {
|
||||
if count == 0 {
|
||||
count = defaultRepeatCount
|
||||
}
|
||||
|
||||
return strings.Repeat(character, count)
|
||||
}
|
||||
47
iteration/iteration_test.go
Normal file
47
iteration/iteration_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package iteration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRepeat(t *testing.T) {
|
||||
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) {
|
||||
t.Helper()
|
||||
if got != want {
|
||||
t.Errorf("got %q want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRepeat(b *testing.B) {
|
||||
for b.Loop() {
|
||||
RepeatFive("a")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user