Files
aethera/backend/pkg/slices/map_test.go

84 lines
1.7 KiB
Go

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)
}