Add orderemap.FromPairs() for instantiating a pre-populated ordered map.

Improve test coverage.
This commit is contained in:
Shawn Poulson
2023-09-08 11:57:11 -04:00
parent ded9215b89
commit 9d93ee3a55
2 changed files with 118 additions and 0 deletions

View File

@@ -72,6 +72,26 @@ func (o *wrapOrderedMap[K, V]) First() Pair[K, V] {
}
}
// NewPair instantiates a `Pair` object for use with `FromPairs()`.
func NewPair[K comparable, V any](key K, value V) Pair[K, V] {
return &wrapPair[K, V]{
Pair: &wk8orderedmap.Pair[K, V]{
Key: key,
Value: value,
},
}
}
// FromPairs creates an `OrderedMap` from an array of pairs.
// Use `NewPair()` to generate input parameters.
func FromPairs[K comparable, V any](pairs ...Pair[K, V]) Map[K, V] {
om := New[K, V]()
for _, pair := range pairs {
om.Set(pair.Key(), pair.Value())
}
return om
}
// IsZero is required to support `omitempty` tag for YAML/JSON marshaling.
func (o *wrapOrderedMap[K, V]) IsZero() bool {
return o.Len() == 0

View File

@@ -264,6 +264,104 @@ func TestMap(t *testing.T) {
})
}
func TestFirst(t *testing.T) {
t.Run("Nil", func(t *testing.T) {
pair := orderedmap.First[string, int](nil)
require.Nil(t, pair)
})
t.Run("Single item", func(t *testing.T) {
m := orderedmap.New[string, int]()
m.Set("key", 1)
var count int
for pair := orderedmap.First(m); pair != nil; pair = pair.Next() {
count++
}
assert.Equal(t, 1, count)
})
t.Run("Many items", func(t *testing.T) {
const mapSize = 100
m := orderedmap.New[string, int]()
for i := 0; i < mapSize; i++ {
m.Set(fmt.Sprintf("key%d", i), i+1000)
}
var count int
for pair := orderedmap.First(m); pair != nil; pair = pair.Next() {
count++
}
assert.Equal(t, mapSize, count)
})
}
func TestLen(t *testing.T) {
t.Run("Nil", func(t *testing.T) {
require.Zero(t, orderedmap.Len(nil))
})
t.Run("Single item", func(t *testing.T) {
m := orderedmap.New[string, int]()
m.Set("key", 1)
assert.Equal(t, 1, orderedmap.Len(m))
})
t.Run("Many items", func(t *testing.T) {
const mapSize = 100
m := orderedmap.New[string, int]()
for i := 0; i < mapSize; i++ {
m.Set(fmt.Sprintf("key%d", i), i+1000)
}
assert.Equal(t, mapSize, orderedmap.Len(m))
})
}
func TestFromPairs(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
m := orderedmap.FromPairs[string, int]()
require.NotNil(t, m)
assert.Zero(t, m.Len())
})
t.Run("Single item", func(t *testing.T) {
m := orderedmap.FromPairs(
orderedmap.NewPair[string, int]("key", 1),
)
require.NotNil(t, m)
assert.Equal(t, 1, m.Len())
pair := m.First()
assert.Equal(t, "key", pair.Key())
assert.Equal(t, 1, pair.Value())
assert.Nil(t, pair.Next())
})
t.Run("Many items", func(t *testing.T) {
const mapSize = 100
var pairs []orderedmap.Pair[string, int]
for i := 0; i < mapSize; i++ {
key := fmt.Sprintf("key%d", i)
pairs = append(pairs, orderedmap.NewPair[string, int](key, i+1000))
}
m := orderedmap.FromPairs(pairs...)
require.NotNil(t, m)
assert.Equal(t, mapSize, m.Len())
var count int
for pair := m.First(); pair != nil; pair = pair.Next() {
expectedKey := fmt.Sprintf("key%d", count)
assert.Equal(t, expectedKey, pair.Key())
assert.Equal(t, count+1000, pair.Value())
count++
require.LessOrEqual(t, count, mapSize)
}
assert.Equal(t, mapSize, count)
})
}
func requireClosed[K comparable, V any](t *testing.T, c <-chan orderedmap.Pair[K, V]) {
select {
case pair := <-c: