mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-15 02:57:04 +02:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package syncmap
|
|
|
|
import (
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type stringID struct {
|
|
val string
|
|
id string
|
|
}
|
|
|
|
func (s stringID) ID() string {
|
|
return s.id
|
|
}
|
|
|
|
var _ IDer = stringID{"", ""}
|
|
|
|
// TestSyncMap_Get tests that basic getting and putting works.
|
|
func TestSyncMap_Get(t *testing.T) {
|
|
m := NewSyncMap[string, stringID]()
|
|
m.Put("a", stringID{"b", "b"})
|
|
assert.Equal(t, stringID{"b", "b"}, m.Get("a"))
|
|
assert.Equal(t, stringID{"", ""}, m.Get("c"))
|
|
}
|
|
|
|
// TestSyncMap_Pop tests that basic Pop operations work.
|
|
func TestSyncMap_Pop(t *testing.T) {
|
|
m := NewSyncMap[string, stringID]()
|
|
m.Put("a", stringID{"b", "b"})
|
|
assert.Equal(t, stringID{"b", "b"}, m.Pop("a"))
|
|
assert.Equal(t, stringID{"", ""}, m.Pop("a"))
|
|
assert.Equal(t, stringID{"", ""}, m.Pop("c"))
|
|
}
|
|
|
|
// TestSyncMap_PopIfEqual tests that basic PopIfEqual operations pop only if the IDs are equal.
|
|
func TestSyncMap_PopIfEqual(t *testing.T) {
|
|
m := NewSyncMap[string, stringID]()
|
|
m.Put("a", stringID{"b", "c"})
|
|
assert.Equal(t, stringID{"", ""}, m.PopIfEqual("a", "b"))
|
|
assert.Equal(t, stringID{"b", "c"}, m.PopIfEqual("a", "c"))
|
|
assert.Equal(t, stringID{"", ""}, m.PopIfEqual("a", "c"))
|
|
}
|
|
|
|
// TestSyncMap_Clear checks that clearing works as expected and returns a copy of the original map.
|
|
func TestSyncMap_Clear(t *testing.T) {
|
|
m := NewSyncMap[string, stringID]()
|
|
assert.Equal(t, map[string]stringID{}, m.data)
|
|
oldMap := m.Clear()
|
|
assert.Equal(t, map[string]stringID{}, m.data)
|
|
assert.Equal(t, map[string]stringID{}, oldMap)
|
|
|
|
m.Put("a", stringID{"b", "b"})
|
|
m.Put("c", stringID{"d", "d"})
|
|
oldMap = m.Clear()
|
|
|
|
assert.Equal(t, map[string]stringID{"a": {"b", "b"}, "c": {"d", "d"}}, oldMap)
|
|
assert.Equal(t, map[string]stringID{}, m.data)
|
|
}
|
|
|
|
// TestSyncMap_Values checks that the Values method returns an array of the values.
|
|
func TestSyncMap_Values(t *testing.T) {
|
|
m := NewSyncMap[string, stringID]()
|
|
assert.Equal(t, []stringID{}, m.Values())
|
|
m.Put("a", stringID{"b", "b"})
|
|
assert.Equal(t, []stringID{{"b", "b"}}, m.Values())
|
|
m.Put("c", stringID{"d", "d"})
|
|
values := m.Values()
|
|
sort.Slice(values, func(i, j int) bool {
|
|
return values[i].val < values[j].val
|
|
})
|
|
assert.Equal(t, []stringID{{"b", "b"}, {"d", "d"}}, values)
|
|
}
|