gf/container/gmap/gmap_z_unit_any_any_test.go

329 lines
7.1 KiB
Go
Raw Normal View History

2019-09-26 20:01:48 +08:00
// Copyright 2017-2019 gf Author(https://github.com/gogf/gf). 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.
package gmap_test
import (
"encoding/json"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
2019-09-26 20:01:48 +08:00
"testing"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/test/gtest"
)
func anyAnyCallBack(int, interface{}) bool {
return true
}
func Test_AnyAnyMap_Basic(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-09-26 20:01:48 +08:00
m := gmap.NewAnyAnyMap()
m.Set(1, 1)
2020-03-19 22:56:12 +08:00
t.Assert(m.Get(1), 1)
t.Assert(m.Size(), 1)
t.Assert(m.IsEmpty(), false)
2019-09-26 20:01:48 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(m.GetOrSet(2, "2"), "2")
t.Assert(m.SetIfNotExist(2, "2"), false)
2019-09-26 20:01:48 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(m.SetIfNotExist(3, 3), true)
2019-09-26 20:01:48 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(m.Remove(2), "2")
t.Assert(m.Contains(2), false)
2019-09-26 20:01:48 +08:00
2020-03-19 22:56:12 +08:00
t.AssertIN(3, m.Keys())
t.AssertIN(1, m.Keys())
t.AssertIN(3, m.Values())
t.AssertIN(1, m.Values())
2019-09-26 20:01:48 +08:00
m.Flip()
2020-03-19 22:56:12 +08:00
t.Assert(m.Map(), map[interface{}]int{1: 1, 3: 3})
2019-09-26 20:01:48 +08:00
m.Clear()
2020-03-19 22:56:12 +08:00
t.Assert(m.Size(), 0)
t.Assert(m.IsEmpty(), true)
2019-09-26 20:01:48 +08:00
m2 := gmap.NewAnyAnyMapFrom(map[interface{}]interface{}{1: 1, 2: "2"})
2020-03-19 22:56:12 +08:00
t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, 2: "2"})
2019-09-26 20:01:48 +08:00
})
}
func Test_AnyAnyMap_Set_Fun(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMap()
2019-09-26 20:01:48 +08:00
2020-03-19 23:53:03 +08:00
m.GetOrSetFunc(1, getAny)
m.GetOrSetFuncLock(2, getAny)
t.Assert(m.Get(1), 123)
t.Assert(m.Get(2), 123)
2019-09-26 20:01:48 +08:00
2020-03-19 23:53:03 +08:00
t.Assert(m.SetIfNotExistFunc(1, getAny), false)
t.Assert(m.SetIfNotExistFunc(3, getAny), true)
2019-09-26 20:01:48 +08:00
2020-03-19 23:53:03 +08:00
t.Assert(m.SetIfNotExistFuncLock(2, getAny), false)
t.Assert(m.SetIfNotExistFuncLock(4, getAny), true)
})
2019-09-26 20:01:48 +08:00
}
func Test_AnyAnyMap_Batch(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMap()
2019-09-26 20:01:48 +08:00
2020-03-19 23:53:03 +08:00
m.Sets(map[interface{}]interface{}{1: 1, 2: "2", 3: 3})
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, 2: "2", 3: 3})
m.Removes([]interface{}{1, 2})
t.Assert(m.Map(), map[interface{}]interface{}{3: 3})
})
2019-09-26 20:01:48 +08:00
}
func Test_AnyAnyMap_Iterator(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
expect := map[interface{}]interface{}{1: 1, 2: "2"}
m := gmap.NewAnyAnyMapFrom(expect)
m.Iterator(func(k interface{}, v interface{}) bool {
t.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k interface{}, v interface{}) bool {
i++
return true
})
m.Iterator(func(k interface{}, v interface{}) bool {
j++
return false
})
t.Assert(i, "2")
t.Assert(j, 1)
2019-09-26 20:01:48 +08:00
})
}
func Test_AnyAnyMap_Lock(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
expect := map[interface{}]interface{}{1: 1, 2: "2"}
m := gmap.NewAnyAnyMapFrom(expect)
m.LockFunc(func(m map[interface{}]interface{}) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[interface{}]interface{}) {
t.Assert(m, expect)
})
2019-09-26 20:01:48 +08:00
})
}
func Test_AnyAnyMap_Clone(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
//clone 方法是深克隆
m := gmap.NewAnyAnyMapFrom(map[interface{}]interface{}{1: 1, 2: "2"})
2019-09-26 20:01:48 +08:00
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
2020-03-19 22:56:12 +08:00
t.AssertIN(1, m_clone.Keys())
2019-09-26 20:01:48 +08:00
m_clone.Remove(2)
//修改clone map,原 map 不影响
2020-03-19 22:56:12 +08:00
t.AssertIN(2, m.Keys())
})
2019-09-26 20:01:48 +08:00
}
func Test_AnyAnyMap_Merge(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
m1 := gmap.NewAnyAnyMap()
m2 := gmap.NewAnyAnyMap()
m1.Set(1, 1)
m2.Set(2, "2")
m1.Merge(m2)
t.Assert(m1.Map(), map[interface{}]interface{}{1: 1, 2: "2"})
})
2019-09-26 20:01:48 +08:00
}
func Test_AnyAnyMap_Map(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.Map()
t.Assert(data[1], 0)
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_AnyAnyMap_MapCopy(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.MapCopy()
t.Assert(data[1], 0)
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_AnyAnyMap_FilterEmpty(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
m.FilterEmpty()
t.Assert(m.Get(1), nil)
t.Assert(m.Get(2), 2)
})
}
func Test_AnyAnyMap_Json(t *testing.T) {
// Marshal
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
data := g.MapAnyAny{
"k1": "v1",
"k2": "v2",
}
m1 := gmap.NewAnyAnyMapFrom(data)
b1, err1 := json.Marshal(m1)
b2, err2 := json.Marshal(gconv.Map(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.MapAnyAny{
"k1": "v1",
"k2": "v2",
}
b, err := json.Marshal(gconv.Map(data))
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
m := gmap.New()
err = json.Unmarshal(b, m)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
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.MapAnyAny{
"k1": "v1",
"k2": "v2",
}
b, err := json.Marshal(gconv.Map(data))
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
var m gmap.Map
err = json.Unmarshal(b, &m)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(m.Get("k1"), data["k1"])
t.Assert(m.Get("k2"), data["k2"])
})
}
func Test_AnyAnyMap_Pop(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMapFrom(g.MapAnyAny{
"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)
})
}
func Test_AnyAnyMap_Pops(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMapFrom(g.MapAnyAny{
"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)
})
}
func TestAnyAnyMap_UnmarshalValue(t *testing.T) {
2020-03-19 23:53:03 +08:00
type V struct {
Name string
Map *gmap.Map
}
// 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)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
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)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
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")
})
}