mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Remove previos sum from utils
This commit is contained in:
committed by
Dan Christian Bogos
parent
12b6807950
commit
ace31f8256
@@ -320,50 +320,6 @@ func GreaterThan(item, oItem interface{}, orEqual bool) (gte bool, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Sum attempts to sum two items
|
||||
// returns the result or error if not comparable
|
||||
func Sum(item, oItem interface{}) (sum interface{}, err error) {
|
||||
valItm := reflect.ValueOf(item)
|
||||
valOtItm := reflect.ValueOf(oItem)
|
||||
// convert to wider type so we can be compatible with StringToInterface function
|
||||
switch valItm.Kind() {
|
||||
case reflect.Float32:
|
||||
item = valItm.Float()
|
||||
valItm = reflect.ValueOf(item)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
|
||||
item = valItm.Int()
|
||||
valItm = reflect.ValueOf(item)
|
||||
}
|
||||
switch valOtItm.Kind() {
|
||||
case reflect.Float32:
|
||||
oItem = valOtItm.Float()
|
||||
valOtItm = reflect.ValueOf(oItem)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
|
||||
oItem = valOtItm.Int()
|
||||
valOtItm = reflect.ValueOf(oItem)
|
||||
}
|
||||
typItem := reflect.TypeOf(item)
|
||||
typOItem := reflect.TypeOf(oItem)
|
||||
if !typItem.Comparable() ||
|
||||
!typOItem.Comparable() ||
|
||||
typItem != typOItem {
|
||||
return false, errors.New("incomparable")
|
||||
}
|
||||
switch item.(type) {
|
||||
case float64:
|
||||
sum = valItm.Float() + valOtItm.Float()
|
||||
case int64:
|
||||
sum = valItm.Int() + valOtItm.Int()
|
||||
case time.Duration:
|
||||
tVal := item.(time.Duration)
|
||||
tOVal := oItem.(time.Duration)
|
||||
sum = tVal + tOVal
|
||||
default: // unsupported comparison
|
||||
err = fmt.Errorf("unsupported comparison type: %v, kind: %v", typItem, typItem.Kind())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// MultipleSum attempts to sum multiple items
|
||||
// returns the result or error if not comparable
|
||||
func MultipleSum(items ...interface{}) (sum interface{}, err error) {
|
||||
|
||||
@@ -419,43 +419,6 @@ func TestIfaceAsBool(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSum(t *testing.T) {
|
||||
if _, err := Sum(1, 1.2); err == nil || err.Error() != "incomparable" {
|
||||
t.Error(err)
|
||||
}
|
||||
if _, err := Sum(struct{}{},
|
||||
map[string]interface{}{"a": "a"}); err == nil || err.Error() != "incomparable" {
|
||||
t.Error(err)
|
||||
}
|
||||
if sum, err := Sum(1.2, 1.2); err != nil {
|
||||
t.Error(err)
|
||||
} else if sum != 2.4 {
|
||||
t.Errorf("Expecting: 2.4, received: %+v", sum)
|
||||
}
|
||||
if sum, err := Sum(2, 4); err != nil {
|
||||
t.Error(err)
|
||||
} else if sum != int64(6) {
|
||||
t.Errorf("Expecting: 6, received: %+v", sum)
|
||||
}
|
||||
if sum, err := Sum(0.5, 1.23); err != nil {
|
||||
t.Error(err)
|
||||
} else if sum != 1.73 {
|
||||
t.Errorf("Expecting: 1.73, received: %+v", sum)
|
||||
}
|
||||
if sum, err := Sum(time.Duration(2*time.Second),
|
||||
time.Duration(1*time.Second)); err != nil {
|
||||
t.Error(err)
|
||||
} else if sum != time.Duration(3*time.Second) {
|
||||
t.Errorf("Expecting: 3s, received: %+v", sum)
|
||||
}
|
||||
if sum, err := Sum(time.Duration(2*time.Second),
|
||||
time.Duration(10*time.Millisecond)); err != nil {
|
||||
t.Error(err)
|
||||
} else if sum != time.Duration(2*time.Second+10*time.Millisecond) {
|
||||
t.Errorf("Expecting: 2s10ms, received: %+v", sum)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultipleSum(t *testing.T) {
|
||||
if _, err := MultipleSum(1); err == nil || err.Error() != "Not enought parameters" {
|
||||
t.Error(err)
|
||||
|
||||
Reference in New Issue
Block a user