github.com/gogf/gf@v1.16.9/container/gset/gset_z_unit_int_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // go test *.go 8 9 package gset_test 10 11 import ( 12 "github.com/gogf/gf/frame/g" 13 "github.com/gogf/gf/internal/json" 14 "github.com/gogf/gf/util/gconv" 15 "strings" 16 "sync" 17 "testing" 18 "time" 19 20 "github.com/gogf/gf/container/garray" 21 "github.com/gogf/gf/container/gset" 22 "github.com/gogf/gf/test/gtest" 23 ) 24 25 func TestIntSet_Var(t *testing.T) { 26 gtest.C(t, func(t *gtest.T) { 27 var s gset.IntSet 28 s.Add(1, 1, 2) 29 s.Add([]int{3, 4}...) 30 t.Assert(s.Size(), 4) 31 t.AssertIN(1, s.Slice()) 32 t.AssertIN(2, s.Slice()) 33 t.AssertIN(3, s.Slice()) 34 t.AssertIN(4, s.Slice()) 35 t.AssertNI(0, s.Slice()) 36 t.Assert(s.Contains(4), true) 37 t.Assert(s.Contains(5), false) 38 s.Remove(1) 39 t.Assert(s.Size(), 3) 40 s.Clear() 41 t.Assert(s.Size(), 0) 42 }) 43 } 44 45 func TestIntSet_Basic(t *testing.T) { 46 gtest.C(t, func(t *gtest.T) { 47 s := gset.NewIntSet() 48 s.Add(1, 1, 2) 49 s.Add([]int{3, 4}...) 50 t.Assert(s.Size(), 4) 51 t.AssertIN(1, s.Slice()) 52 t.AssertIN(2, s.Slice()) 53 t.AssertIN(3, s.Slice()) 54 t.AssertIN(4, s.Slice()) 55 t.AssertNI(0, s.Slice()) 56 t.Assert(s.Contains(4), true) 57 t.Assert(s.Contains(5), false) 58 s.Remove(1) 59 t.Assert(s.Size(), 3) 60 s.Clear() 61 t.Assert(s.Size(), 0) 62 }) 63 } 64 65 func TestIntSet_Iterator(t *testing.T) { 66 gtest.C(t, func(t *gtest.T) { 67 s := gset.NewIntSet() 68 s.Add(1, 2, 3) 69 t.Assert(s.Size(), 3) 70 71 a1 := garray.New(true) 72 a2 := garray.New(true) 73 s.Iterator(func(v int) bool { 74 a1.Append(1) 75 return false 76 }) 77 s.Iterator(func(v int) bool { 78 a2.Append(1) 79 return true 80 }) 81 t.Assert(a1.Len(), 1) 82 t.Assert(a2.Len(), 3) 83 }) 84 } 85 86 func TestIntSet_LockFunc(t *testing.T) { 87 gtest.C(t, func(t *gtest.T) { 88 s := gset.NewIntSet() 89 s.Add(1, 2, 3) 90 t.Assert(s.Size(), 3) 91 s.LockFunc(func(m map[int]struct{}) { 92 delete(m, 1) 93 }) 94 t.Assert(s.Size(), 2) 95 s.RLockFunc(func(m map[int]struct{}) { 96 t.Assert(m, map[int]struct{}{ 97 3: struct{}{}, 98 2: struct{}{}, 99 }) 100 }) 101 }) 102 } 103 104 func TestIntSet_Equal(t *testing.T) { 105 gtest.C(t, func(t *gtest.T) { 106 s1 := gset.NewIntSet() 107 s2 := gset.NewIntSet() 108 s3 := gset.NewIntSet() 109 s1.Add(1, 2, 3) 110 s2.Add(1, 2, 3) 111 s3.Add(1, 2, 3, 4) 112 t.Assert(s1.Equal(s2), true) 113 t.Assert(s1.Equal(s3), false) 114 }) 115 } 116 117 func TestIntSet_IsSubsetOf(t *testing.T) { 118 gtest.C(t, func(t *gtest.T) { 119 s1 := gset.NewIntSet() 120 s2 := gset.NewIntSet() 121 s3 := gset.NewIntSet() 122 s1.Add(1, 2) 123 s2.Add(1, 2, 3) 124 s3.Add(1, 2, 3, 4) 125 t.Assert(s1.IsSubsetOf(s2), true) 126 t.Assert(s2.IsSubsetOf(s3), true) 127 t.Assert(s1.IsSubsetOf(s3), true) 128 t.Assert(s2.IsSubsetOf(s1), false) 129 t.Assert(s3.IsSubsetOf(s2), false) 130 }) 131 } 132 133 func TestIntSet_Union(t *testing.T) { 134 gtest.C(t, func(t *gtest.T) { 135 s1 := gset.NewIntSet() 136 s2 := gset.NewIntSet() 137 s1.Add(1, 2) 138 s2.Add(3, 4) 139 s3 := s1.Union(s2) 140 t.Assert(s3.Contains(1), true) 141 t.Assert(s3.Contains(2), true) 142 t.Assert(s3.Contains(3), true) 143 t.Assert(s3.Contains(4), true) 144 }) 145 } 146 147 func TestIntSet_Diff(t *testing.T) { 148 gtest.C(t, func(t *gtest.T) { 149 s1 := gset.NewIntSet() 150 s2 := gset.NewIntSet() 151 s1.Add(1, 2, 3) 152 s2.Add(3, 4, 5) 153 s3 := s1.Diff(s2) 154 t.Assert(s3.Contains(1), true) 155 t.Assert(s3.Contains(2), true) 156 t.Assert(s3.Contains(3), false) 157 t.Assert(s3.Contains(4), false) 158 }) 159 } 160 161 func TestIntSet_Intersect(t *testing.T) { 162 gtest.C(t, func(t *gtest.T) { 163 s1 := gset.NewIntSet() 164 s2 := gset.NewIntSet() 165 s1.Add(1, 2, 3) 166 s2.Add(3, 4, 5) 167 s3 := s1.Intersect(s2) 168 t.Assert(s3.Contains(1), false) 169 t.Assert(s3.Contains(2), false) 170 t.Assert(s3.Contains(3), true) 171 t.Assert(s3.Contains(4), false) 172 }) 173 } 174 175 func TestIntSet_Complement(t *testing.T) { 176 gtest.C(t, func(t *gtest.T) { 177 s1 := gset.NewIntSet() 178 s2 := gset.NewIntSet() 179 s1.Add(1, 2, 3) 180 s2.Add(3, 4, 5) 181 s3 := s1.Complement(s2) 182 t.Assert(s3.Contains(1), false) 183 t.Assert(s3.Contains(2), false) 184 t.Assert(s3.Contains(4), true) 185 t.Assert(s3.Contains(5), true) 186 }) 187 } 188 189 func TestIntSet_Size(t *testing.T) { 190 gtest.C(t, func(t *gtest.T) { 191 s1 := gset.NewIntSet(true) 192 s1.Add(1, 2, 3) 193 t.Assert(s1.Size(), 3) 194 195 }) 196 197 } 198 199 func TestIntSet_Merge(t *testing.T) { 200 gtest.C(t, func(t *gtest.T) { 201 s1 := gset.NewIntSet() 202 s2 := gset.NewIntSet() 203 s1.Add(1, 2, 3) 204 s2.Add(3, 4, 5) 205 s3 := s1.Merge(s2) 206 t.Assert(s3.Contains(1), true) 207 t.Assert(s3.Contains(5), true) 208 t.Assert(s3.Contains(6), false) 209 }) 210 } 211 212 func TestIntSet_Join(t *testing.T) { 213 gtest.C(t, func(t *gtest.T) { 214 s1 := gset.NewIntSet() 215 s1.Add(1, 2, 3) 216 s3 := s1.Join(",") 217 t.Assert(strings.Contains(s3, "1"), true) 218 t.Assert(strings.Contains(s3, "2"), true) 219 t.Assert(strings.Contains(s3, "3"), true) 220 }) 221 } 222 223 func TestIntSet_String(t *testing.T) { 224 gtest.C(t, func(t *gtest.T) { 225 s1 := gset.NewIntSet() 226 s1.Add(1, 2, 3) 227 s3 := s1.String() 228 t.Assert(strings.Contains(s3, "["), true) 229 t.Assert(strings.Contains(s3, "]"), true) 230 t.Assert(strings.Contains(s3, "1"), true) 231 t.Assert(strings.Contains(s3, "2"), true) 232 t.Assert(strings.Contains(s3, "3"), true) 233 }) 234 } 235 236 func TestIntSet_Sum(t *testing.T) { 237 gtest.C(t, func(t *gtest.T) { 238 s1 := gset.NewIntSet() 239 s1.Add(1, 2, 3) 240 s2 := gset.NewIntSet() 241 s2.Add(5, 6, 7) 242 t.Assert(s2.Sum(), 18) 243 244 }) 245 246 } 247 248 func TestIntSet_Pop(t *testing.T) { 249 gtest.C(t, func(t *gtest.T) { 250 s := gset.NewIntSet() 251 s.Add(4, 2, 3) 252 t.Assert(s.Size(), 3) 253 t.AssertIN(s.Pop(), []int{4, 2, 3}) 254 t.AssertIN(s.Pop(), []int{4, 2, 3}) 255 t.Assert(s.Size(), 1) 256 }) 257 } 258 259 func TestIntSet_Pops(t *testing.T) { 260 gtest.C(t, func(t *gtest.T) { 261 s := gset.NewIntSet() 262 s.Add(1, 4, 2, 3) 263 t.Assert(s.Size(), 4) 264 t.Assert(s.Pops(0), nil) 265 t.AssertIN(s.Pops(1), []int{1, 4, 2, 3}) 266 t.Assert(s.Size(), 3) 267 a := s.Pops(2) 268 t.Assert(len(a), 2) 269 t.AssertIN(a, []int{1, 4, 2, 3}) 270 t.Assert(s.Size(), 1) 271 }) 272 273 gtest.C(t, func(t *gtest.T) { 274 s := gset.NewIntSet(true) 275 a := []int{1, 2, 3, 4} 276 s.Add(a...) 277 t.Assert(s.Size(), 4) 278 t.Assert(s.Pops(-2), nil) 279 t.AssertIN(s.Pops(-1), a) 280 }) 281 } 282 283 func TestIntSet_AddIfNotExist(t *testing.T) { 284 gtest.C(t, func(t *gtest.T) { 285 s := gset.NewIntSet(true) 286 s.Add(1) 287 t.Assert(s.Contains(1), true) 288 t.Assert(s.AddIfNotExist(1), false) 289 t.Assert(s.AddIfNotExist(2), true) 290 t.Assert(s.Contains(2), true) 291 t.Assert(s.AddIfNotExist(2), false) 292 t.Assert(s.Contains(2), true) 293 }) 294 } 295 296 func TestIntSet_AddIfNotExistFunc(t *testing.T) { 297 gtest.C(t, func(t *gtest.T) { 298 s := gset.NewIntSet(true) 299 s.Add(1) 300 t.Assert(s.Contains(1), true) 301 t.Assert(s.Contains(2), false) 302 t.Assert(s.AddIfNotExistFunc(2, func() bool { return false }), false) 303 t.Assert(s.Contains(2), false) 304 t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), true) 305 t.Assert(s.Contains(2), true) 306 t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), false) 307 t.Assert(s.Contains(2), true) 308 }) 309 gtest.C(t, func(t *gtest.T) { 310 s := gset.NewIntSet(true) 311 wg := sync.WaitGroup{} 312 wg.Add(1) 313 go func() { 314 defer wg.Done() 315 r := s.AddIfNotExistFunc(1, func() bool { 316 time.Sleep(100 * time.Millisecond) 317 return true 318 }) 319 t.Assert(r, false) 320 }() 321 s.Add(1) 322 wg.Wait() 323 }) 324 } 325 326 func TestIntSet_AddIfNotExistFuncLock(t *testing.T) { 327 gtest.C(t, func(t *gtest.T) { 328 s := gset.NewIntSet(true) 329 wg := sync.WaitGroup{} 330 wg.Add(2) 331 go func() { 332 defer wg.Done() 333 r := s.AddIfNotExistFuncLock(1, func() bool { 334 time.Sleep(500 * time.Millisecond) 335 return true 336 }) 337 t.Assert(r, true) 338 }() 339 time.Sleep(100 * time.Millisecond) 340 go func() { 341 defer wg.Done() 342 r := s.AddIfNotExistFuncLock(1, func() bool { 343 return true 344 }) 345 t.Assert(r, false) 346 }() 347 wg.Wait() 348 }) 349 } 350 351 func TestIntSet_Json(t *testing.T) { 352 gtest.C(t, func(t *gtest.T) { 353 s1 := []int{1, 3, 2, 4} 354 a1 := gset.NewIntSetFrom(s1) 355 b1, err1 := json.Marshal(a1) 356 b2, err2 := json.Marshal(s1) 357 t.Assert(len(b1), len(b2)) 358 t.Assert(err1, err2) 359 360 a2 := gset.NewIntSet() 361 err2 = json.UnmarshalUseNumber(b2, &a2) 362 t.Assert(err2, nil) 363 t.Assert(a2.Contains(1), true) 364 t.Assert(a2.Contains(2), true) 365 t.Assert(a2.Contains(3), true) 366 t.Assert(a2.Contains(4), true) 367 t.Assert(a2.Contains(5), false) 368 369 var a3 gset.IntSet 370 err := json.UnmarshalUseNumber(b2, &a3) 371 t.Assert(err, nil) 372 t.Assert(a2.Contains(1), true) 373 t.Assert(a2.Contains(2), true) 374 t.Assert(a2.Contains(3), true) 375 t.Assert(a2.Contains(4), true) 376 t.Assert(a2.Contains(5), false) 377 }) 378 } 379 380 func TestIntSet_Walk(t *testing.T) { 381 gtest.C(t, func(t *gtest.T) { 382 var set gset.IntSet 383 set.Add(g.SliceInt{1, 2}...) 384 set.Walk(func(item int) int { 385 return item + 10 386 }) 387 t.Assert(set.Size(), 2) 388 t.Assert(set.Contains(11), true) 389 t.Assert(set.Contains(12), true) 390 }) 391 } 392 393 func TestIntSet_UnmarshalValue(t *testing.T) { 394 type V struct { 395 Name string 396 Set *gset.IntSet 397 } 398 // JSON 399 gtest.C(t, func(t *gtest.T) { 400 var v *V 401 err := gconv.Struct(g.Map{ 402 "name": "john", 403 "set": []byte(`[1,2,3]`), 404 }, &v) 405 t.Assert(err, nil) 406 t.Assert(v.Name, "john") 407 t.Assert(v.Set.Size(), 3) 408 t.Assert(v.Set.Contains(1), true) 409 t.Assert(v.Set.Contains(2), true) 410 t.Assert(v.Set.Contains(3), true) 411 t.Assert(v.Set.Contains(4), false) 412 }) 413 // Map 414 gtest.C(t, func(t *gtest.T) { 415 var v *V 416 err := gconv.Struct(g.Map{ 417 "name": "john", 418 "set": g.Slice{1, 2, 3}, 419 }, &v) 420 t.Assert(err, nil) 421 t.Assert(v.Name, "john") 422 t.Assert(v.Set.Size(), 3) 423 t.Assert(v.Set.Contains(1), true) 424 t.Assert(v.Set.Contains(2), true) 425 t.Assert(v.Set.Contains(3), true) 426 t.Assert(v.Set.Contains(4), false) 427 }) 428 }