github.com/sandwich-go/boost@v1.3.29/xcontainer/smap/gen_string_any_test.go (about) 1 // Code generated by gotemplate. DO NOT EDIT. 2 3 // smap 包提供了一个分片的协程安全的映射 4 // NewWithSharedCount 返回一个线程安全的映射实例 5 // New 返回一个线程安全的映射实例 6 package smap 7 8 import ( 9 . "github.com/smartystreets/goconvey/convey" 10 11 "testing" 12 ) 13 14 func TestSMapStringAny(t *testing.T) { 15 Convey("test sync array", t, func() { 16 tr := NewStringAny() 17 So(tr.Len(), ShouldEqual, 0) 18 So(tr.IsEmpty(), ShouldBeTrue) 19 tr.Set(__formatKTypeToStringAny(1), __formatVTypeToStringAny(1)) 20 So(tr.Len(), ShouldEqual, 1) 21 22 tr.Set(__formatKTypeToStringAny(1), __formatVTypeToStringAny(2)) 23 So(tr.Len(), ShouldEqual, 1) 24 tr.Set(__formatKTypeToStringAny(2), __formatVTypeToStringAny(2)) 25 So(tr.Len(), ShouldEqual, 2) 26 So(tr.Count(), ShouldEqual, 2) 27 So(tr.Size(), ShouldEqual, 2) 28 29 So(tr.Keys(), ShouldContain, __formatKTypeToStringAny(1)) 30 So(tr.Keys(), ShouldContain, __formatKTypeToStringAny(2)) 31 32 So(tr.GetAll(), ShouldContainKey, __formatKTypeToStringAny(1)) 33 So(tr.GetAll(), ShouldContainKey, __formatKTypeToStringAny(2)) 34 35 tr.Clear() 36 So(tr.Len(), ShouldEqual, 0) 37 38 tr.Set(__formatKTypeToStringAny(1), __formatVTypeToStringAny(2)) 39 tr.Set(__formatKTypeToStringAny(2), __formatVTypeToStringAny(2)) 40 So(func() { 41 tr.ClearWithFuncLock(func(key string, val interface{}) { 42 return 43 }) 44 }, ShouldNotPanic) 45 46 tr.Set(__formatKTypeToStringAny(1), __formatVTypeToStringAny(1)) 47 tr.Set(__formatKTypeToStringAny(2), __formatVTypeToStringAny(2)) 48 tr.Set(__formatKTypeToStringAny(3), __formatVTypeToStringAny(3)) 49 tr.Set(__formatKTypeToStringAny(4), __formatVTypeToStringAny(4)) 50 mk := []string{__formatKTypeToStringAny(1), __formatKTypeToStringAny(2), __formatKTypeToStringAny(3)} 51 m := tr.MGet(mk...) 52 for _, k := range mk { 53 So(m, ShouldContainKey, k) 54 } 55 56 tr2 := NewStringAny() 57 tr2.MSet(m) 58 So(tr2.Len(), ShouldEqual, len(mk)) 59 60 So(tr2.SetNX(__formatKTypeToStringAny(5), __formatVTypeToStringAny(5)), ShouldBeTrue) 61 So(tr2.SetNX(__formatKTypeToStringAny(1), __formatVTypeToStringAny(5)), ShouldBeFalse) 62 63 So(func() { 64 tr2.LockFuncWithKey(__formatKTypeToStringAny(5), func(shardData map[string]interface{}) { 65 return 66 }) 67 }, ShouldNotPanic) 68 So(func() { 69 tr2.RLockFuncWithKey(__formatKTypeToStringAny(5), func(shardData map[string]interface{}) { 70 return 71 }) 72 }, ShouldNotPanic) 73 So(func() { 74 tr2.LockFunc(func(shardData map[string]interface{}) { 75 return 76 }) 77 }, ShouldNotPanic) 78 So(func() { 79 tr2.RLockFunc(func(shardData map[string]interface{}) { 80 return 81 }) 82 }, ShouldNotPanic) 83 84 dfv := __formatVTypeToStringAny(1) 85 r, ret := tr2.GetOrSetFunc(__formatKTypeToStringAny(1), func(key string) interface{} { 86 return dfv 87 }) 88 So(r, ShouldEqual, dfv) 89 So(ret, ShouldBeFalse) 90 r, ret = tr2.GetOrSetFuncLock(__formatKTypeToStringAny(1), func(key string) interface{} { 91 return dfv 92 }) 93 So(r, ShouldEqual, dfv) 94 So(ret, ShouldBeFalse) 95 96 _, ret = tr2.GetOrSet(__formatKTypeToStringAny(1), __formatVTypeToStringAny(1)) 97 So(ret, ShouldBeFalse) 98 r, ret = tr2.GetOrSet(__formatKTypeToStringAny(10), __formatVTypeToStringAny(10)) 99 So(r, ShouldEqual, __formatVTypeToStringAny(10)) 100 So(ret, ShouldBeTrue) 101 102 So(tr.Has(__formatKTypeToStringAny(1)), ShouldBeTrue) 103 104 tr2.Remove(__formatKTypeToStringAny(1)) 105 v, ret := tr2.GetAndRemove(__formatKTypeToStringAny(10)) 106 So(v, ShouldEqual, __formatVTypeToStringAny(10)) 107 So(ret, ShouldBeTrue) 108 109 for _, f := range []func() <-chan TupleStringAny{ 110 tr2.Iter, tr2.IterBuffered, 111 } { 112 cnt := 0 113 for v := range f() { 114 cnt++ 115 So(v.Key, ShouldBeIn, []string{__formatKTypeToStringAny(2), __formatKTypeToStringAny(3), __formatKTypeToStringAny(5)}) 116 So(v.Val, ShouldBeIn, []interface{}{__formatVTypeToStringAny(2), __formatVTypeToStringAny(3), __formatVTypeToStringAny(5)}) 117 } 118 So(cnt, ShouldEqual, 3) 119 } 120 121 }) 122 }