Files
gf/container/gmap/gmap_z_unit_hash_str_any_test.go

438 lines
9.5 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with gm file,
// You can obtain one at https://github.com/gogf/gf.
2019-04-11 17:33:52 +08:00
package gmap_test
import (
"strconv"
2019-04-11 17:33:52 +08:00
"testing"
2019-07-29 21:01:19 +08:00
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/container/garray"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/gmap"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/test/gtest"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/util/gconv"
2019-04-11 17:33:52 +08:00
)
func Test_StrAnyMap_Var(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var m gmap.StrAnyMap
m.Set("a", 1)
t.Assert(m.Get("a"), 1)
t.Assert(m.Size(), 1)
t.Assert(m.IsEmpty(), false)
t.Assert(m.GetOrSet("b", "2"), "2")
t.Assert(m.SetIfNotExist("b", "2"), false)
t.Assert(m.SetIfNotExist("c", 3), true)
t.Assert(m.Remove("b"), "2")
t.Assert(m.Contains("b"), false)
t.AssertIN("c", m.Keys())
t.AssertIN("a", m.Keys())
t.AssertIN(3, m.Values())
t.AssertIN(1, m.Values())
m.Flip()
t.Assert(m.Map(), map[string]interface{}{"1": "a", "3": "c"})
m.Clear()
t.Assert(m.Size(), 0)
t.Assert(m.IsEmpty(), true)
})
2019-04-11 17:33:52 +08:00
}
func Test_StrAnyMap_Basic(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMap()
2019-04-11 17:33:52 +08:00
m.Set("a", 1)
2020-03-19 22:56:12 +08:00
t.Assert(m.Get("a"), 1)
t.Assert(m.Size(), 1)
t.Assert(m.IsEmpty(), false)
2019-04-11 17:33:52 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(m.GetOrSet("b", "2"), "2")
t.Assert(m.SetIfNotExist("b", "2"), false)
2019-04-11 17:33:52 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(m.SetIfNotExist("c", 3), true)
2019-04-11 17:33:52 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(m.Remove("b"), "2")
t.Assert(m.Contains("b"), false)
2019-04-11 17:33:52 +08:00
2020-03-19 22:56:12 +08:00
t.AssertIN("c", m.Keys())
t.AssertIN("a", m.Keys())
t.AssertIN(3, m.Values())
t.AssertIN(1, m.Values())
2019-04-11 17:33:52 +08:00
m.Flip()
2020-03-19 22:56:12 +08:00
t.Assert(m.Map(), map[string]interface{}{"1": "a", "3": "c"})
2019-04-11 17:33:52 +08:00
m.Clear()
2020-03-19 22:56:12 +08:00
t.Assert(m.Size(), 0)
t.Assert(m.IsEmpty(), true)
2019-04-11 17:33:52 +08:00
m2 := gmap.NewStrAnyMapFrom(map[string]interface{}{"a": 1, "b": "2"})
2020-03-19 22:56:12 +08:00
t.Assert(m2.Map(), map[string]interface{}{"a": 1, "b": "2"})
2019-04-11 17:33:52 +08:00
})
}
func Test_StrAnyMap_Set_Fun(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMap()
2019-04-12 10:59:05 +08:00
2020-03-19 23:53:03 +08:00
m.GetOrSetFunc("a", getAny)
m.GetOrSetFuncLock("b", getAny)
t.Assert(m.Get("a"), 123)
t.Assert(m.Get("b"), 123)
t.Assert(m.SetIfNotExistFunc("a", getAny), false)
t.Assert(m.SetIfNotExistFunc("c", getAny), true)
2019-04-12 10:59:05 +08:00
2020-03-19 23:53:03 +08:00
t.Assert(m.SetIfNotExistFuncLock("b", getAny), false)
t.Assert(m.SetIfNotExistFuncLock("d", getAny), true)
})
2019-04-11 17:33:52 +08:00
}
func Test_StrAnyMap_Batch(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMap()
2019-04-11 17:33:52 +08:00
2020-03-19 23:53:03 +08:00
m.Sets(map[string]interface{}{"a": 1, "b": "2", "c": 3})
t.Assert(m.Map(), map[string]interface{}{"a": 1, "b": "2", "c": 3})
m.Removes([]string{"a", "b"})
t.Assert(m.Map(), map[string]interface{}{"c": 3})
})
2019-04-11 17:33:52 +08:00
}
func Test_StrAnyMap_Iterator_Deadlock(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMapFrom(map[string]interface{}{"1": "1", "2": "2", "3": "3", "4": "4"}, true)
m.Iterator(func(k string, _ interface{}) bool {
kInt, _ := strconv.Atoi(k)
if kInt%2 == 0 {
m.Remove(k)
}
return true
})
t.Assert(m.Map(), map[string]interface{}{
"1": "1",
"3": "3",
})
})
}
func Test_StrAnyMap_Iterator(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
expect := map[string]interface{}{"a": true, "b": false}
m := gmap.NewStrAnyMapFrom(expect)
m.Iterator(func(k string, v interface{}) bool {
t.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k string, v interface{}) bool {
i++
return true
})
m.Iterator(func(k string, v interface{}) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
2019-04-16 14:28:25 +08:00
})
2019-04-12 10:59:05 +08:00
}
func Test_StrAnyMap_Lock(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
expect := map[string]interface{}{"a": true, "b": false}
2019-04-16 14:28:25 +08:00
2020-03-19 23:53:03 +08:00
m := gmap.NewStrAnyMapFrom(expect)
m.LockFunc(func(m map[string]interface{}) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[string]interface{}) {
t.Assert(m, expect)
})
2019-04-16 14:28:25 +08:00
})
2019-04-12 10:59:05 +08:00
}
func Test_StrAnyMap_Clone(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
2021-11-13 23:23:55 +08:00
// clone 方法是深克隆
2020-03-19 23:53:03 +08:00
m := gmap.NewStrAnyMapFrom(map[string]interface{}{"a": 1, "b": "2"})
2019-04-11 17:33:52 +08:00
2020-03-19 23:53:03 +08:00
m_clone := m.Clone()
m.Remove("a")
2021-11-13 23:23:55 +08:00
// 修改原 map,clone 后的 map 不影响
2020-03-19 23:53:03 +08:00
t.AssertIN("a", m_clone.Keys())
2019-04-11 17:33:52 +08:00
2020-03-19 23:53:03 +08:00
m_clone.Remove("b")
2021-11-13 23:23:55 +08:00
// 修改clone map,原 map 不影响
2020-03-19 23:53:03 +08:00
t.AssertIN("b", m.Keys())
})
2019-04-11 17:33:52 +08:00
}
func Test_StrAnyMap_Merge(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
m1 := gmap.NewStrAnyMap()
m2 := gmap.NewStrAnyMap()
m1.Set("a", 1)
m2.Set("b", "2")
m1.Merge(m2)
t.Assert(m1.Map(), map[string]interface{}{"a": 1, "b": "2"})
m3 := gmap.NewStrAnyMapFrom(nil)
m3.Merge(m2)
t.Assert(m3.Map(), m2.Map())
2020-03-19 23:53:03 +08:00
})
2019-04-11 17:33:52 +08:00
}
func Test_StrAnyMap_Map(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMap()
m.Set("1", 1)
m.Set("2", 2)
t.Assert(m.Get("1"), 1)
t.Assert(m.Get("2"), 2)
data := m.Map()
t.Assert(data["1"], 1)
t.Assert(data["2"], 2)
data["3"] = 3
t.Assert(m.Get("3"), 3)
m.Set("4", 4)
t.Assert(data["4"], 4)
})
}
func Test_StrAnyMap_MapCopy(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMap()
m.Set("1", 1)
m.Set("2", 2)
t.Assert(m.Get("1"), 1)
t.Assert(m.Get("2"), 2)
data := m.MapCopy()
t.Assert(data["1"], 1)
t.Assert(data["2"], 2)
data["3"] = 3
t.Assert(m.Get("3"), nil)
m.Set("4", 4)
t.Assert(data["4"], nil)
})
}
func Test_StrAnyMap_FilterEmpty(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMap()
m.Set("1", 0)
m.Set("2", 2)
t.Assert(m.Size(), 2)
t.Assert(m.Get("1"), 0)
t.Assert(m.Get("2"), 2)
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get("2"), 2)
})
}
func Test_StrAnyMap_Json(t *testing.T) {
// Marshal
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
data := g.MapStrAny{
"k1": "v1",
"k2": "v2",
}
m1 := gmap.NewStrAnyMapFrom(data)
b1, err1 := json.Marshal(m1)
b2, err2 := json.Marshal(data)
2020-03-19 22:56:12 +08:00
t.Assert(err1, err2)
t.Assert(b1, b2)
})
// Unmarshal
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
data := g.MapStrAny{
"k1": "v1",
"k2": "v2",
}
b, err := json.Marshal(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
m := gmap.NewStrAnyMap()
err = json.UnmarshalUseNumber(b, m)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(m.Get("k1"), data["k1"])
t.Assert(m.Get("k2"), data["k2"])
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
data := g.MapStrAny{
"k1": "v1",
"k2": "v2",
}
b, err := json.Marshal(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
var m gmap.StrAnyMap
err = json.UnmarshalUseNumber(b, &m)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(m.Get("k1"), data["k1"])
t.Assert(m.Get("k2"), data["k2"])
})
}
func Test_StrAnyMap_Pop(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMapFrom(g.MapStrAny{
"k1": "v1",
"k2": "v2",
})
2020-03-19 22:56:12 +08:00
t.Assert(m.Size(), 2)
k1, v1 := m.Pop()
2020-03-19 22:56:12 +08:00
t.AssertIN(k1, g.Slice{"k1", "k2"})
t.AssertIN(v1, g.Slice{"v1", "v2"})
t.Assert(m.Size(), 1)
k2, v2 := m.Pop()
2020-03-19 22:56:12 +08:00
t.AssertIN(k2, g.Slice{"k1", "k2"})
t.AssertIN(v2, g.Slice{"v1", "v2"})
t.Assert(m.Size(), 0)
2020-03-19 22:56:12 +08:00
t.AssertNE(k1, k2)
t.AssertNE(v1, v2)
k3, v3 := m.Pop()
t.Assert(k3, "")
t.Assert(v3, "")
})
}
func Test_StrAnyMap_Pops(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMapFrom(g.MapStrAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
})
2020-03-19 22:56:12 +08:00
t.Assert(m.Size(), 3)
kArray := garray.New()
vArray := garray.New()
for k, v := range m.Pops(1) {
2020-03-19 22:56:12 +08:00
t.AssertIN(k, g.Slice{"k1", "k2", "k3"})
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
kArray.Append(k)
vArray.Append(v)
}
2020-03-19 22:56:12 +08:00
t.Assert(m.Size(), 2)
for k, v := range m.Pops(2) {
2020-03-19 22:56:12 +08:00
t.AssertIN(k, g.Slice{"k1", "k2", "k3"})
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
kArray.Append(k)
vArray.Append(v)
}
2020-03-19 22:56:12 +08:00
t.Assert(m.Size(), 0)
2020-03-19 22:56:12 +08:00
t.Assert(kArray.Unique().Len(), 3)
t.Assert(vArray.Unique().Len(), 3)
v := m.Pops(1)
t.AssertNil(v)
v = m.Pops(-1)
t.AssertNil(v)
})
}
func TestStrAnyMap_UnmarshalValue(t *testing.T) {
2020-03-19 23:53:03 +08:00
type V struct {
Name string
Map *gmap.StrAnyMap
}
// JSON
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2020-03-19 23:53:03 +08:00
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": []byte(`{"k1":"v1","k2":"v2"}`),
2020-03-19 23:53:03 +08:00
}, &v)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 23:53:03 +08:00
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("k1"), "v1")
t.Assert(v.Map.Get("k2"), "v2")
})
// Map
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2020-03-19 23:53:03 +08:00
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": g.Map{
"k1": "v1",
"k2": "v2",
},
2020-03-19 23:53:03 +08:00
}, &v)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 23:53:03 +08:00
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("k1"), "v1")
t.Assert(v.Map.Get("k2"), "v2")
})
}
func Test_StrAnyMap_DeepCopy(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMapFrom(g.MapStrAny{
"key1": "val1",
"key2": "val2",
})
t.Assert(m.Size(), 2)
n := m.DeepCopy().(*gmap.StrAnyMap)
n.Set("key1", "v1")
t.AssertNE(m.Get("key1"), n.Get("key1"))
})
}
2023-03-08 14:12:51 +08:00
func Test_StrAnyMap_IsSubOf(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
m1 := gmap.NewStrAnyMapFrom(g.MapStrAny{
"k1": "v1",
"k2": "v2",
})
m2 := gmap.NewStrAnyMapFrom(g.MapStrAny{
"k2": "v2",
})
t.Assert(m1.IsSubOf(m2), false)
t.Assert(m2.IsSubOf(m1), true)
t.Assert(m2.IsSubOf(m2), true)
})
}
2023-07-20 20:07:43 +08:00
func Test_StrAnyMap_Diff(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
m1 := gmap.NewStrAnyMapFrom(g.MapStrAny{
"0": "v0",
"1": "v1",
"2": "v2",
"3": 3,
})
m2 := gmap.NewStrAnyMapFrom(g.MapStrAny{
"0": "v0",
"2": "v2",
"3": "v3",
"4": "v4",
})
addedKeys, removedKeys, updatedKeys := m1.Diff(m2)
t.Assert(addedKeys, []string{"4"})
t.Assert(removedKeys, []string{"1"})
t.Assert(updatedKeys, []string{"3"})
})
}