2023-05-19 13:05:18 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-11-10 20:36:15 +00:00
|
|
|
|
|
|
|
"git.sr.ht/~ashkeel/containers/sync"
|
2023-05-19 13:05:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMergeMap(t *testing.T) {
|
|
|
|
// Create two map and merge them.
|
|
|
|
m1 := map[string]string{
|
|
|
|
"a": "1",
|
|
|
|
"b": "2",
|
|
|
|
"c": "3",
|
|
|
|
}
|
|
|
|
m2 := map[string]string{
|
|
|
|
"a": "4",
|
|
|
|
"d": "5",
|
|
|
|
"e": "6",
|
|
|
|
}
|
|
|
|
MergeMap(m1, m2)
|
|
|
|
|
|
|
|
// Check if the merged map is correct.
|
|
|
|
if m1["a"] != "4" {
|
|
|
|
t.Error("MergeMap failed: wrong value in overwritten key")
|
|
|
|
}
|
|
|
|
if len(m1) != 5 {
|
|
|
|
t.Error("MergeMap failed: wrong length")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeSyncMap(t *testing.T) {
|
|
|
|
// Create two map and merge them.
|
|
|
|
m1 := sync.NewMap[string, string]()
|
|
|
|
m1.Set(map[string]string{
|
|
|
|
"a": "1",
|
|
|
|
"b": "2",
|
|
|
|
"c": "3",
|
|
|
|
})
|
|
|
|
m2 := sync.NewMap[string, string]()
|
|
|
|
m2.Set(map[string]string{
|
|
|
|
"a": "4",
|
|
|
|
"d": "5",
|
|
|
|
"e": "6",
|
|
|
|
})
|
|
|
|
MergeSyncMap(m1, m2)
|
|
|
|
|
|
|
|
// Check if the merged map is correct.
|
|
|
|
if val, ok := m1.GetKey("a"); !ok || val != "4" {
|
|
|
|
t.Error("MergeSyncMap failed: wrong value in overwritten key")
|
|
|
|
}
|
|
|
|
if len(m1.Copy()) != 5 {
|
|
|
|
t.Error("MergeSyncMap failed: wrong length")
|
|
|
|
}
|
|
|
|
}
|