38 lines
877 B
Go
38 lines
877 B
Go
package util
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestSumSlice(t *testing.T) {
|
|
// Test with integers
|
|
intSlice := []int64{1, 2, 3, 4, 5}
|
|
intSum := SumSlice(intSlice, func(i int64) int64 { return i })
|
|
if intSum != 15 {
|
|
t.Errorf("Expected sum of integers to be 15, got %d", intSum)
|
|
}
|
|
|
|
// Test with floats
|
|
floatSlice := []float64{1.1, 2.2, 3.3, 4.4, 5.5}
|
|
floatSum := SumSlice(floatSlice, func(f float64) float64 { return f })
|
|
expectedFloatSum := 16.5
|
|
if floatSum != expectedFloatSum {
|
|
t.Errorf("Expected sum of floats to be %f, got %f", expectedFloatSum, floatSum)
|
|
}
|
|
|
|
// Test with custom struct
|
|
type Person struct {
|
|
Name string
|
|
Age int64
|
|
}
|
|
people := []Person{
|
|
{"Alice", 25},
|
|
{"Bob", 30},
|
|
{"Charlie", 35},
|
|
}
|
|
ageSum := SumSlice(people, func(p Person) int64 { return p.Age })
|
|
if ageSum != 90 {
|
|
t.Errorf("Expected sum of ages to be 90, got %d", ageSum)
|
|
}
|
|
}
|