From 1ef9ea768e8416a6cb155f78577c30425bbfff1c Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Thu, 16 May 2024 08:26:43 -0400 Subject: [PATCH] Add unit test for orderedmap --- config/radiuscfg_test.go | 38 +++++++++++------------ utils/orderedmap_test.go | 67 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 19 deletions(-) diff --git a/config/radiuscfg_test.go b/config/radiuscfg_test.go index 5bc0d9c50..fbcd8ea5e 100644 --- a/config/radiuscfg_test.go +++ b/config/radiuscfg_test.go @@ -61,15 +61,15 @@ func TestRadiusAgentCfgloadFromJsonCfgCase1(t *testing.T) { }, }, }, - // ClientDaAddresses: map[string]DAClientOptsJson{ + ClientDaAddresses: map[string]DAClientOptsJson{ - // "fsfdsz": { - // Transport: utils.StringPointer("http"), - // Host: utils.StringPointer("localhost"), - // Port: utils.IntPointer(6768), - // Flags: []string{"*sessions", "*routes"}, - // }, - // }, + "fsfdsz": { + Transport: utils.StringPointer("http"), + Host: utils.StringPointer("localhost"), + Port: utils.IntPointer(6768), + Flags: []string{"*sessions", "*routes"}, + }, + }, } expected := &RadiusAgentCfg{ Enabled: true, @@ -85,17 +85,17 @@ func TestRadiusAgentCfgloadFromJsonCfgCase1(t *testing.T) { SessionSConns: []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaSessionS)}, DMRTemplate: "*dmr", CoATemplate: "*coa", - // ClientDaAddresses: map[string]DAClientOpts{ - // "fsfdsz": { - // Transport: "http", - // Host: "localhost", - // Port: 6768, - // Flags: utils.FlagsWithParams{ - // utils.MetaSessionS: utils.FlagParams{}, - // utils.MetaRoutes: utils.FlagParams{}, - // }, - // }, - // }, + ClientDaAddresses: map[string]DAClientOpts{ + "fsfdsz": { + Transport: "http", + Host: "localhost", + Port: 6768, + Flags: utils.FlagsWithParams{ + utils.MetaSessionS: utils.FlagParams{}, + utils.MetaRoutes: utils.FlagParams{}, + }, + }, + }, RequestProcessors: []*RequestProcessor{ { ID: "OutboundAUTHDryRun", diff --git a/utils/orderedmap_test.go b/utils/orderedmap_test.go index 02d3b5bb1..f39253079 100644 --- a/utils/orderedmap_test.go +++ b/utils/orderedmap_test.go @@ -21,6 +21,8 @@ import ( "crypto/rand" "reflect" "testing" + + "github.com/google/go-cmp/cmp" ) func TestOrderedMapSetGetDelete(t *testing.T) { @@ -94,6 +96,71 @@ func TestOrderedMapKeysValues(t *testing.T) { } } +func TestOrderedMapMap(t *testing.T) { + + // Initialize a new OrderedMap + nm := NewOrderedMap[string, []string]() + + // Set multiple key-value pairs + nm.Set("key1", []string{"value1", "value21"}) + nm.Set("key2", []string{"value2", "value22"}) + nm.Set("key3", []string{"value3", "value23"}) + + // Return a deep copy of the ordered map's key-value pairs. + val := nm.Map() + expected := map[string][]string{ + "key1": {"value1", "value21"}, + "key2": {"value2", "value22"}, + "key3": {"value3", "value23"}} + + // Compare the enerated map + if diff := cmp.Diff(expected, val); diff != "" { + t.Errorf("OrderedMap.Map() returned an unexpected value(-want +got): \n%s", diff) + } +} + +func TestOrderMapGetByIndex(t *testing.T) { + testcases := []struct { + index int + value string + key string + received bool + }{ + { + + index: 0, + value: "value1", + key: "key1", + received: true, + }, + + { + + index: 1, + received: false, + }, + } + nom := NewOrderedMap[string, string]() + nom.Set("key1", "value1") + + for _, tc := range testcases { + key, val, has := nom.GetByIndex(tc.index) + if tc.received != has { + t.Errorf("index is out of bounds") + return + } + if key != tc.key { + t.Errorf(" expected key %v, received %v", tc.key, key) + } + + if val != tc.value { + t.Errorf(" expected value %v, received %v", tc.value, val) + } + + } + +} + // Benchmark that emphasizes the difference in performance for simple values between // OrderedNavigableMap and the generic OrderedMap. // Sample usage: go test -bench=. -run=Benchmark_NavigableMaps -benchtime=5s -count 3 -benchmem