From 36e1982464bf4cb16fa6c73b4f3658156528f7cf Mon Sep 17 00:00:00 2001 From: adragusin Date: Mon, 24 Feb 2020 19:02:36 +0200 Subject: [PATCH] Updated tests in utils --- utils/map_test.go | 4 ++ utils/set_test.go | 91 +++++++++++++++++++++++++++++++++++++++++++++ utils/slice.go | 7 +++- utils/slice_test.go | 85 +++++++++++++++++++++++++++++++++++------- 4 files changed, 172 insertions(+), 15 deletions(-) diff --git a/utils/map_test.go b/utils/map_test.go index dcdf57fd1..8d7081821 100644 --- a/utils/map_test.go +++ b/utils/map_test.go @@ -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) { diff --git a/utils/set_test.go b/utils/set_test.go index 33f64e9e7..ed5ffed66 100644 --- a/utils/set_test.go +++ b/utils/set_test.go @@ -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) + } +} diff --git a/utils/slice.go b/utils/slice.go index eac95488e..60b79db20 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -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 { diff --git a/utils/slice_test.go b/utils/slice_test.go index 4d4b50c12..5c86aea00 100644 --- a/utils/slice_test.go +++ b/utils/slice_test.go @@ -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)) } }