github.com/shuguocloud/go-zero@v1.3.0/core/collection/safemap_test.go (about) 1 package collection 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/shuguocloud/go-zero/core/stringx" 8 ) 9 10 func TestSafeMap(t *testing.T) { 11 tests := []struct { 12 size int 13 exception int 14 }{ 15 { 16 100000, 17 2000, 18 }, 19 { 20 100000, 21 50, 22 }, 23 } 24 for _, test := range tests { 25 t.Run(stringx.Rand(), func(t *testing.T) { 26 testSafeMapWithParameters(t, test.size, test.exception) 27 }) 28 } 29 } 30 31 func TestSafeMap_CopyNew(t *testing.T) { 32 const ( 33 size = 100000 34 exception1 = 5 35 exception2 = 500 36 ) 37 m := NewSafeMap() 38 39 for i := 0; i < size; i++ { 40 m.Set(i, i) 41 } 42 for i := 0; i < size; i++ { 43 if i%exception1 == 0 { 44 m.Del(i) 45 } 46 } 47 48 for i := size; i < size<<1; i++ { 49 m.Set(i, i) 50 } 51 for i := size; i < size<<1; i++ { 52 if i%exception2 != 0 { 53 m.Del(i) 54 } 55 } 56 57 for i := 0; i < size; i++ { 58 val, ok := m.Get(i) 59 if i%exception1 != 0 { 60 assert.True(t, ok) 61 assert.Equal(t, i, val.(int)) 62 } else { 63 assert.False(t, ok) 64 } 65 } 66 for i := size; i < size<<1; i++ { 67 val, ok := m.Get(i) 68 if i%exception2 == 0 { 69 assert.True(t, ok) 70 assert.Equal(t, i, val.(int)) 71 } else { 72 assert.False(t, ok) 73 } 74 } 75 } 76 77 func testSafeMapWithParameters(t *testing.T, size, exception int) { 78 m := NewSafeMap() 79 80 for i := 0; i < size; i++ { 81 m.Set(i, i) 82 } 83 for i := 0; i < size; i++ { 84 if i%exception != 0 { 85 m.Del(i) 86 } 87 } 88 89 assert.Equal(t, size/exception, m.Size()) 90 91 for i := size; i < size<<1; i++ { 92 m.Set(i, i) 93 } 94 for i := size; i < size<<1; i++ { 95 if i%exception != 0 { 96 m.Del(i) 97 } 98 } 99 100 for i := 0; i < size<<1; i++ { 101 val, ok := m.Get(i) 102 if i%exception == 0 { 103 assert.True(t, ok) 104 assert.Equal(t, i, val.(int)) 105 } else { 106 assert.False(t, ok) 107 } 108 } 109 }