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