github.com/lingyao2333/mo-zero@v1.4.1/core/collection/set_test.go (about) 1 package collection 2 3 import ( 4 "sort" 5 "testing" 6 7 "github.com/lingyao2333/mo-zero/core/logx" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func init() { 12 logx.Disable() 13 } 14 15 func BenchmarkRawSet(b *testing.B) { 16 m := make(map[interface{}]struct{}) 17 for i := 0; i < b.N; i++ { 18 m[i] = struct{}{} 19 _ = m[i] 20 } 21 } 22 23 func BenchmarkUnmanagedSet(b *testing.B) { 24 s := NewUnmanagedSet() 25 for i := 0; i < b.N; i++ { 26 s.Add(i) 27 _ = s.Contains(i) 28 } 29 } 30 31 func BenchmarkSet(b *testing.B) { 32 s := NewSet() 33 for i := 0; i < b.N; i++ { 34 s.AddInt(i) 35 _ = s.Contains(i) 36 } 37 } 38 39 func TestAdd(t *testing.T) { 40 // given 41 set := NewUnmanagedSet() 42 values := []interface{}{1, 2, 3} 43 44 // when 45 set.Add(values...) 46 47 // then 48 assert.True(t, set.Contains(1) && set.Contains(2) && set.Contains(3)) 49 assert.Equal(t, len(values), len(set.Keys())) 50 } 51 52 func TestAddInt(t *testing.T) { 53 // given 54 set := NewSet() 55 values := []int{1, 2, 3} 56 57 // when 58 set.AddInt(values...) 59 60 // then 61 assert.True(t, set.Contains(1) && set.Contains(2) && set.Contains(3)) 62 keys := set.KeysInt() 63 sort.Ints(keys) 64 assert.EqualValues(t, values, keys) 65 } 66 67 func TestAddInt64(t *testing.T) { 68 // given 69 set := NewSet() 70 values := []int64{1, 2, 3} 71 72 // when 73 set.AddInt64(values...) 74 75 // then 76 assert.True(t, set.Contains(int64(1)) && set.Contains(int64(2)) && set.Contains(int64(3))) 77 assert.Equal(t, len(values), len(set.KeysInt64())) 78 } 79 80 func TestAddUint(t *testing.T) { 81 // given 82 set := NewSet() 83 values := []uint{1, 2, 3} 84 85 // when 86 set.AddUint(values...) 87 88 // then 89 assert.True(t, set.Contains(uint(1)) && set.Contains(uint(2)) && set.Contains(uint(3))) 90 assert.Equal(t, len(values), len(set.KeysUint())) 91 } 92 93 func TestAddUint64(t *testing.T) { 94 // given 95 set := NewSet() 96 values := []uint64{1, 2, 3} 97 98 // when 99 set.AddUint64(values...) 100 101 // then 102 assert.True(t, set.Contains(uint64(1)) && set.Contains(uint64(2)) && set.Contains(uint64(3))) 103 assert.Equal(t, len(values), len(set.KeysUint64())) 104 } 105 106 func TestAddStr(t *testing.T) { 107 // given 108 set := NewSet() 109 values := []string{"1", "2", "3"} 110 111 // when 112 set.AddStr(values...) 113 114 // then 115 assert.True(t, set.Contains("1") && set.Contains("2") && set.Contains("3")) 116 assert.Equal(t, len(values), len(set.KeysStr())) 117 } 118 119 func TestContainsWithoutElements(t *testing.T) { 120 // given 121 set := NewSet() 122 123 // then 124 assert.False(t, set.Contains(1)) 125 } 126 127 func TestContainsUnmanagedWithoutElements(t *testing.T) { 128 // given 129 set := NewUnmanagedSet() 130 131 // then 132 assert.False(t, set.Contains(1)) 133 } 134 135 func TestRemove(t *testing.T) { 136 // given 137 set := NewSet() 138 set.Add([]interface{}{1, 2, 3}...) 139 140 // when 141 set.Remove(2) 142 143 // then 144 assert.True(t, set.Contains(1) && !set.Contains(2) && set.Contains(3)) 145 } 146 147 func TestCount(t *testing.T) { 148 // given 149 set := NewSet() 150 set.Add([]interface{}{1, 2, 3}...) 151 152 // then 153 assert.Equal(t, set.Count(), 3) 154 } 155 156 func TestKeysIntMismatch(t *testing.T) { 157 set := NewSet() 158 set.add(int64(1)) 159 set.add(2) 160 vals := set.KeysInt() 161 assert.EqualValues(t, []int{2}, vals) 162 } 163 164 func TestKeysInt64Mismatch(t *testing.T) { 165 set := NewSet() 166 set.add(1) 167 set.add(int64(2)) 168 vals := set.KeysInt64() 169 assert.EqualValues(t, []int64{2}, vals) 170 } 171 172 func TestKeysUintMismatch(t *testing.T) { 173 set := NewSet() 174 set.add(1) 175 set.add(uint(2)) 176 vals := set.KeysUint() 177 assert.EqualValues(t, []uint{2}, vals) 178 } 179 180 func TestKeysUint64Mismatch(t *testing.T) { 181 set := NewSet() 182 set.add(1) 183 set.add(uint64(2)) 184 vals := set.KeysUint64() 185 assert.EqualValues(t, []uint64{2}, vals) 186 } 187 188 func TestKeysStrMismatch(t *testing.T) { 189 set := NewSet() 190 set.add(1) 191 set.add("2") 192 vals := set.KeysStr() 193 assert.EqualValues(t, []string{"2"}, vals) 194 } 195 196 func TestSetType(t *testing.T) { 197 set := NewUnmanagedSet() 198 set.add(1) 199 set.add("2") 200 vals := set.Keys() 201 assert.ElementsMatch(t, []interface{}{1, "2"}, vals) 202 }