mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-14 20:59:53 +05:00
Merge branch 'master' of https://github.com/cgrates/cgrates
This commit is contained in:
@@ -41,3 +41,14 @@ func SliceMemberHasPrefix(ss []string, prfx string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func Avg(values []float64) float64 {
|
||||
if len(values) == 0 {
|
||||
return 0.0
|
||||
}
|
||||
var sum float64
|
||||
for _, val := range values {
|
||||
sum += val
|
||||
}
|
||||
return sum / float64(len(values))
|
||||
}
|
||||
|
||||
@@ -436,3 +436,21 @@ func TestConcatenatedKey(t *testing.T) {
|
||||
t.Error("Unexpected key value received: ", key)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAvg(t *testing.T) {
|
||||
values := []float64{1, 2, 3}
|
||||
result := Avg(values)
|
||||
expected := 2.0
|
||||
if expected != result {
|
||||
t.Errorf("Wrong Avg: expected %v got %v", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAvgEmpty(t *testing.T) {
|
||||
values := []float64{}
|
||||
result := Avg(values)
|
||||
expected := 0.0
|
||||
if expected != result {
|
||||
t.Errorf("Wrong Avg: expected %v got %v", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user