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