mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Updated tests in utils
This commit is contained in:
committed by
Dan Christian Bogos
parent
25cd02e126
commit
36e1982464
@@ -127,6 +127,10 @@ func MapKeysStringMapParse(t *testing.T) {
|
||||
if include, has := sm["5"]; include != false && has != false {
|
||||
t.Error("Error detecting missing: ", sm)
|
||||
}
|
||||
eOut := make(StringMap)
|
||||
if rcv := ParseStringMap(ZERO); !reflect.DeepEqual(eOut, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapMergeMapsStringIface(t *testing.T) {
|
||||
|
||||
@@ -20,6 +20,7 @@ package utils
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -82,3 +83,93 @@ func TestHas(t *testing.T) {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddSlice(t *testing.T) {
|
||||
s := &StringSet{data: map[string]struct{}{
|
||||
"test": struct{}{}}}
|
||||
eOut := &StringSet{data: map[string]struct{}{
|
||||
"test": struct{}{},
|
||||
"test1": struct{}{},
|
||||
"test2": struct{}{}}}
|
||||
s.AddSlice([]string{"test1", "test2"})
|
||||
if !reflect.DeepEqual(eOut, s) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAsSlice(t *testing.T) {
|
||||
s := &StringSet{}
|
||||
eOut := make([]string, 0)
|
||||
if rcv := s.AsSlice(); !reflect.DeepEqual(eOut, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
s = &StringSet{data: map[string]struct{}{
|
||||
"test": struct{}{},
|
||||
"test1": struct{}{},
|
||||
"test2": struct{}{}}}
|
||||
eOut = []string{"test", "test1", "test2"}
|
||||
rcv := s.AsSlice()
|
||||
sort.Strings(rcv)
|
||||
if !reflect.DeepEqual(eOut, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestData(t *testing.T) {
|
||||
s := &StringSet{data: map[string]struct{}{
|
||||
"test": struct{}{},
|
||||
"test1": struct{}{},
|
||||
"test2": struct{}{}}}
|
||||
eOut := map[string]struct{}{
|
||||
"test": struct{}{},
|
||||
"test1": struct{}{},
|
||||
"test2": struct{}{}}
|
||||
if rcv := s.Data(); !reflect.DeepEqual(eOut, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSize(t *testing.T) {
|
||||
s := &StringSet{}
|
||||
if rcv := s.Size(); rcv != 0 {
|
||||
t.Errorf("Expecting: 0, received %+v", rcv)
|
||||
}
|
||||
s = &StringSet{data: map[string]struct{}{
|
||||
"test0": struct{}{},
|
||||
"test1": struct{}{},
|
||||
"test2": struct{}{}}}
|
||||
if rcv := s.Size(); rcv != 3 {
|
||||
t.Errorf("Expecting: 3, received %+v", rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntersect(t *testing.T) {
|
||||
s1 := &StringSet{data: map[string]struct{}{
|
||||
"test0": struct{}{},
|
||||
"test1": struct{}{},
|
||||
"test2": struct{}{}}}
|
||||
s2 := &StringSet{data: map[string]struct{}{
|
||||
"test0": struct{}{},
|
||||
"test2": struct{}{},
|
||||
"test3": struct{}{}}}
|
||||
eOut := &StringSet{data: map[string]struct{}{
|
||||
"test0": struct{}{},
|
||||
"test2": struct{}{}}}
|
||||
s1.Intersect(s2)
|
||||
if !reflect.DeepEqual(eOut, s1) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, s1)
|
||||
}
|
||||
s1 = &StringSet{data: map[string]struct{}{
|
||||
"test0": struct{}{},
|
||||
"test1": struct{}{},
|
||||
"test2": struct{}{}}}
|
||||
s2 = &StringSet{data: map[string]struct{}{
|
||||
"test3": struct{}{},
|
||||
"test4": struct{}{},
|
||||
"test5": struct{}{}}}
|
||||
s1.Intersect(s2)
|
||||
eOut = &StringSet{data: map[string]struct{}{}}
|
||||
if !reflect.DeepEqual(eOut, s1) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, s1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ func SliceHasMember(ss []string, s string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SliceWithoutMember removes s string from slice ss (if found)
|
||||
func SliceWithoutMember(ss []string, s string) []string {
|
||||
sort.Strings(ss)
|
||||
if i := sort.SearchStrings(ss, s); i < len(ss) && ss[i] == s {
|
||||
@@ -49,7 +50,7 @@ func SliceWithoutMember(ss []string, s string) []string {
|
||||
return ss
|
||||
}
|
||||
|
||||
//Iterates over slice members and returns true if one starts with prefix
|
||||
// Iterates over slice members and returns true if one starts with prefix
|
||||
func SliceMemberHasPrefix(ss []string, prfx string) bool {
|
||||
for _, mbr := range ss {
|
||||
if strings.HasPrefix(mbr, prfx) {
|
||||
@@ -59,6 +60,7 @@ func SliceMemberHasPrefix(ss []string, prfx string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Avg returns the average of a float64 slice
|
||||
func Avg(values []float64) float64 {
|
||||
if len(values) == 0 {
|
||||
return 0.0
|
||||
@@ -70,6 +72,8 @@ func Avg(values []float64) float64 {
|
||||
return sum / float64(len(values))
|
||||
}
|
||||
|
||||
// AvgNegative returns the average of a float64 slice
|
||||
// if is called with an empty slice return -1
|
||||
func AvgNegative(values []float64) float64 {
|
||||
if len(values) == 0 {
|
||||
return -1 // return -1 if no data
|
||||
@@ -77,6 +81,7 @@ func AvgNegative(values []float64) float64 {
|
||||
return Avg(values)
|
||||
}
|
||||
|
||||
// PrefixSliceItems iterates through slice and add a prefix before every element
|
||||
func PrefixSliceItems(slc []string, prfx string) (out []string) {
|
||||
out = make([]string, len(slc))
|
||||
for i, itm := range slc {
|
||||
|
||||
@@ -19,24 +19,82 @@ package utils
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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 TestIsSliceMember(t *testing.T) {
|
||||
if !IsSliceMember([]string{"1", "2", "3", "4", "5"}, "5") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
if IsSliceMember([]string{"1", "2", "3", "4", "5"}, "6") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
func TestSliceHasMember(t *testing.T) {
|
||||
if !SliceHasMember([]string{"1", "2", "3", "4", "5"}, "5") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
if SliceHasMember([]string{"1", "2", "3", "4", "5"}, "6") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSliceWithoutMember(t *testing.T) {
|
||||
rcv := SliceWithoutMember([]string{"1", "2", "3", "4", "5"}, "5")
|
||||
sort.Strings(rcv)
|
||||
eOut := []string{"1", "2", "3", "4"}
|
||||
if !reflect.DeepEqual(eOut, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
rcv = SliceWithoutMember([]string{"1", "2", "3", "4", "5"}, "6")
|
||||
sort.Strings(rcv)
|
||||
eOut = []string{"1", "2", "3", "4", "5"}
|
||||
if !reflect.DeepEqual(eOut, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSliceMemberHasPrefix(t *testing.T) {
|
||||
if !SliceMemberHasPrefix([]string{"1", "*2", "3", "4", "5"}, "*") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
if SliceMemberHasPrefix([]string{"1", "2", "3", "4", "5"}, "*") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAvg(t *testing.T) {
|
||||
if rcv := Avg([]float64{}); rcv != 0 {
|
||||
t.Errorf("Expecting: 0, received: %+v", rcv)
|
||||
}
|
||||
if rcv := Avg([]float64{1, 2, 3}); rcv != 2 {
|
||||
t.Errorf("Expecting: 2, received: %+v", rcv)
|
||||
}
|
||||
if rcv := Avg([]float64{1.5, 2.75, 3.25}); rcv != 2.5 {
|
||||
t.Errorf("Expecting: 2.5, received: %+v", rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAvgNegative(t *testing.T) {
|
||||
if rcv := AvgNegative([]float64{}); rcv != -1 {
|
||||
t.Errorf("Expecting: -1, received: %+v", rcv)
|
||||
}
|
||||
if rcv := AvgNegative([]float64{1, 2, 3}); rcv != 2 {
|
||||
t.Errorf("Expecting: 2, received: %+v", rcv)
|
||||
}
|
||||
if rcv := Avg([]float64{1.5, 2.75, 3.25}); rcv != 2.5 {
|
||||
t.Errorf("Expecting: 2.5, received: %+v", rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefixSliceItems(t *testing.T) {
|
||||
rcv := PrefixSliceItems([]string{"1", "2", "3", "4", "5"}, "*")
|
||||
sort.Strings(rcv)
|
||||
eOut := []string{"*1", "*2", "*3", "*4", "*5"}
|
||||
if !reflect.DeepEqual(eOut, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,9 +114,8 @@ func TestStripSlicePrefix(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSliceStringToIface(t *testing.T) {
|
||||
args := []string{"*default", "ToR", "*voice"}
|
||||
exp := []interface{}{"*default", "ToR", "*voice"}
|
||||
if rply := SliceStringToIface(args); !reflect.DeepEqual(exp, rply) {
|
||||
if rply := SliceStringToIface([]string{"*default", "ToR", "*voice"}); !reflect.DeepEqual(exp, rply) {
|
||||
t.Errorf("Expected: %s ,received: %s", ToJSON(exp), ToJSON(rply))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user