tests: add backend tests

This commit is contained in:
2026-01-19 12:46:07 -05:00
parent 5e8ac5e4b6
commit 627fcbafe1
9 changed files with 664 additions and 7 deletions

View File

@@ -0,0 +1,83 @@
package slices
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMap(t *testing.T) {
// Test with integers
ints := []int{1, 2, 3, 4, 5}
result := Map(ints, func(x int) int {
return x * 2
})
assert.Equal(t, []int{2, 4, 6, 8, 10}, result)
// Test with strings
strings := []string{"a", "b", "c"}
resultStr := Map(strings, func(s string) string {
return s + "_suffix"
})
assert.Equal(t, []string{"a_suffix", "b_suffix", "c_suffix"}, resultStr)
// Test with empty slice
empty := []int{}
resultEmpty := Map(empty, func(x int) int {
return x * 2
})
assert.Equal(t, []int{}, resultEmpty)
}
func TestFirst(t *testing.T) {
// Test with non-empty slice
ints := []int{1, 2, 3}
item, found := First(ints)
assert.True(t, found)
assert.Equal(t, 1, item)
// Test with empty slice
empty := []int{}
item, found = First(empty)
assert.False(t, found)
assert.Equal(t, 0, item)
}
func TestLast(t *testing.T) {
// Test with non-empty slice
ints := []int{1, 2, 3}
item, found := Last(ints)
assert.True(t, found)
assert.Equal(t, 3, item)
// Test with empty slice
empty := []int{}
item, found = Last(empty)
assert.False(t, found)
assert.Equal(t, 0, item)
}
func TestFindFirst(t *testing.T) {
// Test with matching element
ints := []int{1, 2, 3, 4, 5}
item, found := FindFirst(ints, func(x int) bool {
return x > 3
})
assert.True(t, found)
assert.Equal(t, 4, item)
// Test with no matching element
item, found = FindFirst(ints, func(x int) bool {
return x > 10
})
assert.False(t, found)
assert.Equal(t, 0, item)
// Test with empty slice
empty := []int{}
item, found = FindFirst(empty, func(x int) bool {
return x > 3
})
assert.False(t, found)
assert.Equal(t, 0, item)
}