github.com/sandwich-go/boost@v1.3.29/xcontainer/smap/gen_int32_string_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 TestSMapInt32String(t *testing.T) { 15 Convey("test sync array", t, func() { 16 tr := NewInt32String() 17 So(tr.Len(), ShouldEqual, 0) 18 So(tr.IsEmpty(), ShouldBeTrue) 19 tr.Set(__formatKTypeToInt32String(1), __formatVTypeToInt32String(1)) 20 So(tr.Len(), ShouldEqual, 1) 21 22 tr.Set(__formatKTypeToInt32String(1), __formatVTypeToInt32String(2)) 23 So(tr.Len(), ShouldEqual, 1) 24 tr.Set(__formatKTypeToInt32String(2), __formatVTypeToInt32String(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, __formatKTypeToInt32String(1)) 30 So(tr.Keys(), ShouldContain, __formatKTypeToInt32String(2)) 31 32 So(tr.GetAll(), ShouldContainKey, __formatKTypeToInt32String(1)) 33 So(tr.GetAll(), ShouldContainKey, __formatKTypeToInt32String(2)) 34 35 tr.Clear() 36 So(tr.Len(), ShouldEqual, 0) 37 38 tr.Set(__formatKTypeToInt32String(1), __formatVTypeToInt32String(2)) 39 tr.Set(__formatKTypeToInt32String(2), __formatVTypeToInt32String(2)) 40 So(func() { 41 tr.ClearWithFuncLock(func(key int32, val string) { 42 return 43 }) 44 }, ShouldNotPanic) 45 46 tr.Set(__formatKTypeToInt32String(1), __formatVTypeToInt32String(1)) 47 tr.Set(__formatKTypeToInt32String(2), __formatVTypeToInt32String(2)) 48 tr.Set(__formatKTypeToInt32String(3), __formatVTypeToInt32String(3)) 49 tr.Set(__formatKTypeToInt32String(4), __formatVTypeToInt32String(4)) 50 mk := []int32{__formatKTypeToInt32String(1), __formatKTypeToInt32String(2), __formatKTypeToInt32String(3)} 51 m := tr.MGet(mk...) 52 for _, k := range mk { 53 So(m, ShouldContainKey, k) 54 } 55 56 tr2 := NewInt32String() 57 tr2.MSet(m) 58 So(tr2.Len(), ShouldEqual, len(mk)) 59 60 So(tr2.SetNX(__formatKTypeToInt32String(5), __formatVTypeToInt32String(5)), ShouldBeTrue) 61 So(tr2.SetNX(__formatKTypeToInt32String(1), __formatVTypeToInt32String(5)), ShouldBeFalse) 62 63 So(func() { 64 tr2.LockFuncWithKey(__formatKTypeToInt32String(5), func(shardData map[int32]string) { 65 return 66 }) 67 }, ShouldNotPanic) 68 So(func() { 69 tr2.RLockFuncWithKey(__formatKTypeToInt32String(5), func(shardData map[int32]string) { 70 return 71 }) 72 }, ShouldNotPanic) 73 So(func() { 74 tr2.LockFunc(func(shardData map[int32]string) { 75 return 76 }) 77 }, ShouldNotPanic) 78 So(func() { 79 tr2.RLockFunc(func(shardData map[int32]string) { 80 return 81 }) 82 }, ShouldNotPanic) 83 84 dfv := __formatVTypeToInt32String(1) 85 r, ret := tr2.GetOrSetFunc(__formatKTypeToInt32String(1), func(key int32) string { 86 return dfv 87 }) 88 So(r, ShouldEqual, dfv) 89 So(ret, ShouldBeFalse) 90 r, ret = tr2.GetOrSetFuncLock(__formatKTypeToInt32String(1), func(key int32) string { 91 return dfv 92 }) 93 So(r, ShouldEqual, dfv) 94 So(ret, ShouldBeFalse) 95 96 _, ret = tr2.GetOrSet(__formatKTypeToInt32String(1), __formatVTypeToInt32String(1)) 97 So(ret, ShouldBeFalse) 98 r, ret = tr2.GetOrSet(__formatKTypeToInt32String(10), __formatVTypeToInt32String(10)) 99 So(r, ShouldEqual, __formatVTypeToInt32String(10)) 100 So(ret, ShouldBeTrue) 101 102 So(tr.Has(__formatKTypeToInt32String(1)), ShouldBeTrue) 103 104 tr2.Remove(__formatKTypeToInt32String(1)) 105 v, ret := tr2.GetAndRemove(__formatKTypeToInt32String(10)) 106 So(v, ShouldEqual, __formatVTypeToInt32String(10)) 107 So(ret, ShouldBeTrue) 108 109 for _, f := range []func() <-chan TupleInt32String{ 110 tr2.Iter, tr2.IterBuffered, 111 } { 112 cnt := 0 113 for v := range f() { 114 cnt++ 115 So(v.Key, ShouldBeIn, []int32{__formatKTypeToInt32String(2), __formatKTypeToInt32String(3), __formatKTypeToInt32String(5)}) 116 So(v.Val, ShouldBeIn, []string{__formatVTypeToInt32String(2), __formatVTypeToInt32String(3), __formatVTypeToInt32String(5)}) 117 } 118 So(cnt, ShouldEqual, 3) 119 } 120 121 }) 122 }