Add unit test for orderedmap

This commit is contained in:
armirveliaj
2024-05-16 08:26:43 -04:00
committed by Dan Christian Bogos
parent 8e23794576
commit 1ef9ea768e
2 changed files with 86 additions and 19 deletions

View File

@@ -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