2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-05-08 17:21:18 +08:00
|
|
|
//
|
|
|
|
// 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 (
|
|
|
|
"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 getInt() int {
|
|
|
|
return 123
|
|
|
|
}
|
2020-04-07 20:41:49 +08:00
|
|
|
|
2019-04-11 17:33:52 +08:00
|
|
|
func intIntCallBack(int, int) bool {
|
|
|
|
return true
|
|
|
|
}
|
2020-04-07 20:41:49 +08:00
|
|
|
|
|
|
|
func Test_IntIntMap_Var(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var m gmap.IntIntMap
|
|
|
|
m.Set(1, 1)
|
|
|
|
|
|
|
|
t.Assert(m.Get(1), 1)
|
|
|
|
t.Assert(m.Size(), 1)
|
|
|
|
t.Assert(m.IsEmpty(), false)
|
|
|
|
|
|
|
|
t.Assert(m.GetOrSet(2, 2), 2)
|
|
|
|
t.Assert(m.SetIfNotExist(2, 2), false)
|
|
|
|
|
|
|
|
t.Assert(m.SetIfNotExist(3, 3), true)
|
|
|
|
|
|
|
|
t.Assert(m.Remove(2), 2)
|
|
|
|
t.Assert(m.Contains(2), false)
|
|
|
|
|
|
|
|
t.AssertIN(3, m.Keys())
|
|
|
|
t.AssertIN(1, m.Keys())
|
|
|
|
t.AssertIN(3, m.Values())
|
|
|
|
t.AssertIN(1, m.Values())
|
|
|
|
m.Flip()
|
|
|
|
t.Assert(m.Map(), map[int]int{1: 1, 3: 3})
|
|
|
|
|
|
|
|
m.Clear()
|
|
|
|
t.Assert(m.Size(), 0)
|
|
|
|
t.Assert(m.IsEmpty(), true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-11 17:33:52 +08:00
|
|
|
func Test_IntIntMap_Basic(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-04-11 17:33:52 +08:00
|
|
|
m := gmap.NewIntIntMap()
|
|
|
|
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-04-11 17:33:52 +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-04-11 17:33:52 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(m.SetIfNotExist(3, 3), true)
|
2019-04-11 17:33:52 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(m.Remove(2), 2)
|
|
|
|
t.Assert(m.Contains(2), false)
|
2019-04-11 17:33:52 +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-04-11 17:33:52 +08:00
|
|
|
m.Flip()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(m.Map(), map[int]int{1: 1, 3: 3})
|
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.NewIntIntMapFrom(map[int]int{1: 1, 2: 2})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(m2.Map(), map[int]int{1: 1, 2: 2})
|
2019-04-11 17:33:52 +08:00
|
|
|
})
|
|
|
|
}
|
2020-04-07 20:41:49 +08:00
|
|
|
|
2019-04-11 17:33:52 +08:00
|
|
|
func Test_IntIntMap_Set_Fun(t *testing.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
m := gmap.NewIntIntMap()
|
2019-04-12 10:59:05 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
m.GetOrSetFunc(1, getInt)
|
|
|
|
m.GetOrSetFuncLock(2, getInt)
|
|
|
|
t.Assert(m.Get(1), 123)
|
|
|
|
t.Assert(m.Get(2), 123)
|
|
|
|
t.Assert(m.SetIfNotExistFunc(1, getInt), false)
|
|
|
|
t.Assert(m.SetIfNotExistFunc(3, getInt), true)
|
2019-04-12 10:59:05 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
t.Assert(m.SetIfNotExistFuncLock(2, getInt), false)
|
|
|
|
t.Assert(m.SetIfNotExistFuncLock(4, getInt), true)
|
|
|
|
})
|
2019-04-11 17:33:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_IntIntMap_Batch(t *testing.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
m := gmap.NewIntIntMap()
|
2019-04-11 17:33:52 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
m.Sets(map[int]int{1: 1, 2: 2, 3: 3})
|
|
|
|
m.Iterator(intIntCallBack)
|
|
|
|
t.Assert(m.Map(), map[int]int{1: 1, 2: 2, 3: 3})
|
|
|
|
m.Removes([]int{1, 2})
|
|
|
|
t.Assert(m.Map(), map[int]int{3: 3})
|
|
|
|
})
|
2019-04-11 17:33:52 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func Test_IntIntMap_Iterator(t *testing.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
expect := map[int]int{1: 1, 2: 2}
|
|
|
|
m := gmap.NewIntIntMapFrom(expect)
|
|
|
|
m.Iterator(func(k int, v int) bool {
|
|
|
|
t.Assert(expect[k], v)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
// 断言返回值对遍历控制
|
|
|
|
i := 0
|
|
|
|
j := 0
|
|
|
|
m.Iterator(func(k int, v int) bool {
|
|
|
|
i++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
m.Iterator(func(k int, v int) 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
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func Test_IntIntMap_Lock(t *testing.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
expect := map[int]int{1: 1, 2: 2}
|
|
|
|
m := gmap.NewIntIntMapFrom(expect)
|
|
|
|
m.LockFunc(func(m map[int]int) {
|
|
|
|
t.Assert(m, expect)
|
|
|
|
})
|
|
|
|
m.RLockFunc(func(m map[int]int) {
|
|
|
|
t.Assert(m, expect)
|
|
|
|
})
|
2019-04-16 14:28:25 +08:00
|
|
|
})
|
2019-04-12 10:59:05 +08:00
|
|
|
}
|
2019-09-29 14:27:09 +08:00
|
|
|
|
2019-04-11 17:33:52 +08:00
|
|
|
func Test_IntIntMap_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.NewIntIntMapFrom(map[int]int{1: 1, 2: 2})
|
2019-04-11 17:33:52 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
m_clone := m.Clone()
|
|
|
|
m.Remove(1)
|
2021-11-13 23:23:55 +08:00
|
|
|
// 修改原 map,clone 后的 map 不影响
|
2020-03-19 23:53:03 +08:00
|
|
|
t.AssertIN(1, m_clone.Keys())
|
2019-04-11 17:33:52 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
m_clone.Remove(2)
|
2021-11-13 23:23:55 +08:00
|
|
|
// 修改clone map,原 map 不影响
|
2020-03-19 23:53:03 +08:00
|
|
|
t.AssertIN(2, m.Keys())
|
|
|
|
})
|
2019-04-11 17:33:52 +08:00
|
|
|
}
|
2019-09-29 14:27:09 +08:00
|
|
|
|
2019-04-11 17:33:52 +08:00
|
|
|
func Test_IntIntMap_Merge(t *testing.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
m1 := gmap.NewIntIntMap()
|
|
|
|
m2 := gmap.NewIntIntMap()
|
|
|
|
m1.Set(1, 1)
|
|
|
|
m2.Set(2, 2)
|
|
|
|
m1.Merge(m2)
|
|
|
|
t.Assert(m1.Map(), map[int]int{1: 1, 2: 2})
|
|
|
|
})
|
2019-04-11 17:33:52 +08:00
|
|
|
}
|
2019-09-29 14:27:09 +08:00
|
|
|
|
|
|
|
func Test_IntIntMap_Map(t *testing.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
m := gmap.NewIntIntMap()
|
|
|
|
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)
|
|
|
|
})
|
2019-09-29 14:27:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_IntIntMap_MapCopy(t *testing.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
m := gmap.NewIntIntMap()
|
|
|
|
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), 0)
|
|
|
|
m.Set(4, 4)
|
|
|
|
t.Assert(data[4], 0)
|
|
|
|
})
|
2019-09-29 14:27:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_IntIntMap_FilterEmpty(t *testing.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
m := gmap.NewIntIntMap()
|
|
|
|
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)
|
|
|
|
})
|
2019-09-29 14:27:09 +08:00
|
|
|
}
|
2019-09-30 14:23:15 +08:00
|
|
|
|
|
|
|
func Test_IntIntMap_Json(t *testing.T) {
|
|
|
|
// Marshal
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-30 14:23:15 +08:00
|
|
|
data := g.MapIntInt{
|
|
|
|
1: 10,
|
|
|
|
2: 20,
|
|
|
|
}
|
|
|
|
m1 := gmap.NewIntIntMapFrom(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)
|
2019-09-30 14:23:15 +08:00
|
|
|
})
|
|
|
|
// Unmarshal
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-30 14:23:15 +08:00
|
|
|
data := g.MapIntInt{
|
|
|
|
1: 10,
|
|
|
|
2: 20,
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(data)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-09-30 14:23:15 +08:00
|
|
|
|
|
|
|
m := gmap.NewIntIntMap()
|
2021-05-15 22:38:07 +08:00
|
|
|
err = json.UnmarshalUseNumber(b, m)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(m.Get(1), data[1])
|
|
|
|
t.Assert(m.Get(2), data[2])
|
2019-09-30 14:23:15 +08:00
|
|
|
})
|
|
|
|
}
|
2019-10-16 23:33:06 +08:00
|
|
|
|
|
|
|
func Test_IntIntMap_Pop(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-16 23:33:06 +08:00
|
|
|
m := gmap.NewIntIntMapFrom(g.MapIntInt{
|
|
|
|
1: 11,
|
|
|
|
2: 22,
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(m.Size(), 2)
|
2019-10-16 23:33:06 +08:00
|
|
|
|
|
|
|
k1, v1 := m.Pop()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertIN(k1, g.Slice{1, 2})
|
|
|
|
t.AssertIN(v1, g.Slice{11, 22})
|
|
|
|
t.Assert(m.Size(), 1)
|
2019-10-16 23:33:06 +08:00
|
|
|
k2, v2 := m.Pop()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertIN(k2, g.Slice{1, 2})
|
|
|
|
t.AssertIN(v2, g.Slice{11, 22})
|
|
|
|
t.Assert(m.Size(), 0)
|
2019-10-16 23:33:06 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(k1, k2)
|
|
|
|
t.AssertNE(v1, v2)
|
2019-10-16 23:33:06 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_IntIntMap_Pops(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-16 23:33:06 +08:00
|
|
|
m := gmap.NewIntIntMapFrom(g.MapIntInt{
|
|
|
|
1: 11,
|
|
|
|
2: 22,
|
|
|
|
3: 33,
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(m.Size(), 3)
|
2019-10-16 23:33:06 +08:00
|
|
|
|
|
|
|
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{1, 2, 3})
|
|
|
|
t.AssertIN(v, g.Slice{11, 22, 33})
|
2019-10-16 23:33:06 +08:00
|
|
|
kArray.Append(k)
|
|
|
|
vArray.Append(v)
|
|
|
|
}
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(m.Size(), 2)
|
2019-10-16 23:33:06 +08:00
|
|
|
for k, v := range m.Pops(2) {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertIN(k, g.Slice{1, 2, 3})
|
|
|
|
t.AssertIN(v, g.Slice{11, 22, 33})
|
2019-10-16 23:33:06 +08:00
|
|
|
kArray.Append(k)
|
|
|
|
vArray.Append(v)
|
|
|
|
}
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(m.Size(), 0)
|
2019-10-16 23:33:06 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(kArray.Unique().Len(), 3)
|
|
|
|
t.Assert(vArray.Unique().Len(), 3)
|
2019-10-16 23:33:06 +08:00
|
|
|
})
|
|
|
|
}
|
2020-01-20 19:56:42 +08:00
|
|
|
|
|
|
|
func TestIntIntMap_UnmarshalValue(t *testing.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
type V struct {
|
2020-01-20 19:56:42 +08:00
|
|
|
Name string
|
|
|
|
Map *gmap.IntIntMap
|
|
|
|
}
|
|
|
|
// 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
|
2020-01-20 19:56:42 +08:00
|
|
|
err := gconv.Struct(map[string]interface{}{
|
|
|
|
"name": "john",
|
|
|
|
"map": []byte(`{"1":1,"2":2}`),
|
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(1), "1")
|
|
|
|
t.Assert(v.Map.Get(2), "2")
|
2020-01-20 19:56:42 +08:00
|
|
|
})
|
|
|
|
// 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
|
2020-01-20 19:56:42 +08:00
|
|
|
err := gconv.Struct(map[string]interface{}{
|
|
|
|
"name": "john",
|
|
|
|
"map": g.MapIntAny{
|
|
|
|
1: 1,
|
|
|
|
2: 2,
|
|
|
|
},
|
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(1), "1")
|
|
|
|
t.Assert(v.Map.Get(2), "2")
|
2020-01-20 19:56:42 +08:00
|
|
|
})
|
|
|
|
}
|