github.com/sandwich-go/boost@v1.3.29/xcontainer/smap/any_test.go (about)

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