Compare commits
11 Commits
3172b5db04
...
feature/ar
| Author | SHA1 | Date | |
|---|---|---|---|
| 141bc3098c | |||
| 7e5515c862 | |||
| cf93bb215a | |||
| 3932b66634 | |||
| c9cc104fe5 | |||
| 083ff01e53 | |||
| be5fafa655 | |||
| cfab2d02a9 | |||
| 0ac0075904 | |||
| 2b554d42c7 | |||
| 803c9d1be9 |
35
arrays/sum.go
Normal file
35
arrays/sum.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func Sum(numbers []int) int {
|
||||||
|
sum := 0
|
||||||
|
// example: for ... in equivalent, first var has index
|
||||||
|
for _, number := range numbers {
|
||||||
|
sum += number
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
func SumAll(numbersToSum ...[]int) []int {
|
||||||
|
var sums []int
|
||||||
|
|
||||||
|
for _, numbers := range numbersToSum {
|
||||||
|
sums = append(sums, Sum(numbers))
|
||||||
|
}
|
||||||
|
|
||||||
|
return sums
|
||||||
|
}
|
||||||
|
|
||||||
|
func SumAllTails(numbersToSum ...[]int) []int {
|
||||||
|
var sums []int
|
||||||
|
|
||||||
|
for _, numbers := range numbersToSum {
|
||||||
|
if len(numbers) == 0 {
|
||||||
|
sums = append(sums, 0)
|
||||||
|
} else {
|
||||||
|
tail := numbers[1:]
|
||||||
|
sums = append(sums, Sum(tail))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sums
|
||||||
|
}
|
||||||
60
arrays/sum_test.go
Normal file
60
arrays/sum_test.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSum(t *testing.T) {
|
||||||
|
t.Run("collection of 5 numbers", func(t *testing.T) {
|
||||||
|
numbers := []int{1, 2, 3, 4, 5}
|
||||||
|
|
||||||
|
got := Sum(numbers)
|
||||||
|
want := 15
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %d want %d given, %v", got, want, numbers)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("collection of any size", func(t *testing.T) {
|
||||||
|
numbers := []int{1, 2, 3}
|
||||||
|
|
||||||
|
got := Sum(numbers)
|
||||||
|
want := 6
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %d want %d given, %v", got, want, numbers)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSumAll(t *testing.T) {
|
||||||
|
got := SumAll([]int{1, 2}, []int{0, 9})
|
||||||
|
want := []int{3, 9}
|
||||||
|
|
||||||
|
if !slices.Equal(got, want) {
|
||||||
|
t.Errorf("got %d want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSumAllTails(t *testing.T) {
|
||||||
|
checkSums := func(t testing.TB, got, want []int) {
|
||||||
|
t.Helper()
|
||||||
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("got %v want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("sums of slices", func(t *testing.T) {
|
||||||
|
got := SumAllTails([]int{1, 2}, []int{0, 9})
|
||||||
|
want := []int{2, 9}
|
||||||
|
checkSums(t, got, want)
|
||||||
|
})
|
||||||
|
t.Run("sums with some empty slices", func(t *testing.T) {
|
||||||
|
got := SumAllTails([]int{}, []int{3, 4, 5})
|
||||||
|
want := []int{0, 9}
|
||||||
|
checkSums(t, got, want)
|
||||||
|
})
|
||||||
|
}
|
||||||
3
go.mod
Normal file
3
go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ak-homelab.duckdns.org/gitea/hoborg/go_with_tests
|
||||||
|
|
||||||
|
go 1.26.1
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
module go_with_tests
|
|
||||||
|
|
||||||
go 1.26.1
|
|
||||||
22
integers/adder_test.go
Normal file
22
integers/adder_test.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package integers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAdder(t *testing.T) {
|
||||||
|
sum := Add(2, 2)
|
||||||
|
// Output: 9
|
||||||
|
expected := 4
|
||||||
|
|
||||||
|
if sum != expected {
|
||||||
|
t.Errorf("expected '%d' but got '%d'", expected, sum)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Runnable example testing
|
||||||
|
func ExampleAdd() {
|
||||||
|
fmt.Println(Add(2, 4))
|
||||||
|
// Output: 6
|
||||||
|
}
|
||||||
6
integers/integers.go
Normal file
6
integers/integers.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package integers
|
||||||
|
|
||||||
|
// Add two numbers together
|
||||||
|
func Add(x, y int) int {
|
||||||
|
return x + y
|
||||||
|
}
|
||||||
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