github.com/gogf/gf/v2@v2.7.4/container/gset/gset_z_unit_any_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 "strings" 13 "sync" 14 "testing" 15 "time" 16 17 "github.com/gogf/gf/v2/container/garray" 18 "github.com/gogf/gf/v2/container/gset" 19 "github.com/gogf/gf/v2/frame/g" 20 "github.com/gogf/gf/v2/internal/json" 21 "github.com/gogf/gf/v2/test/gtest" 22 "github.com/gogf/gf/v2/util/gconv" 23 ) 24 25 func TestSet_Var(t *testing.T) { 26 gtest.C(t, func(t *gtest.T) { 27 var s gset.Set 28 s.Add(1, 1, 2) 29 s.Add([]interface{}{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 TestSet_New(t *testing.T) { 46 gtest.C(t, func(t *gtest.T) { 47 s := gset.New() 48 s.Add(1, 1, 2) 49 s.Add([]interface{}{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 TestSet_Basic(t *testing.T) { 66 gtest.C(t, func(t *gtest.T) { 67 s := gset.NewSet() 68 s.Add(1, 1, 2) 69 s.Add([]interface{}{3, 4}...) 70 t.Assert(s.Size(), 4) 71 t.AssertIN(1, s.Slice()) 72 t.AssertIN(2, s.Slice()) 73 t.AssertIN(3, s.Slice()) 74 t.AssertIN(4, s.Slice()) 75 t.AssertNI(0, s.Slice()) 76 t.Assert(s.Contains(4), true) 77 t.Assert(s.Contains(5), false) 78 s.Remove(1) 79 t.Assert(s.Size(), 3) 80 s.Clear() 81 t.Assert(s.Size(), 0) 82 }) 83 } 84 85 func TestSet_Iterator_Deadlock(t *testing.T) { 86 gtest.C(t, func(t *gtest.T) { 87 set := gset.NewFrom([]interface{}{1, 2, 3, 4, 5}, true) 88 set.Iterator(func(k interface{}) bool { 89 if gconv.Int(k)%2 == 0 { 90 set.Remove(k) 91 } 92 return true 93 }) 94 t.Assert(set.Contains(1), true) 95 t.Assert(set.Contains(2), false) 96 t.Assert(set.Contains(3), true) 97 t.Assert(set.Contains(4), false) 98 t.Assert(set.Contains(5), true) 99 }) 100 } 101 102 func TestSet_Iterator(t *testing.T) { 103 gtest.C(t, func(t *gtest.T) { 104 s := gset.NewSet() 105 s.Add(1, 2, 3) 106 t.Assert(s.Size(), 3) 107 108 a1 := garray.New(true) 109 a2 := garray.New(true) 110 s.Iterator(func(v interface{}) bool { 111 a1.Append(1) 112 return false 113 }) 114 s.Iterator(func(v interface{}) bool { 115 a2.Append(1) 116 return true 117 }) 118 t.Assert(a1.Len(), 1) 119 t.Assert(a2.Len(), 3) 120 }) 121 } 122 123 func TestSet_LockFunc(t *testing.T) { 124 gtest.C(t, func(t *gtest.T) { 125 s := gset.NewSet() 126 s.Add(1, 2, 3) 127 t.Assert(s.Size(), 3) 128 s.LockFunc(func(m map[interface{}]struct{}) { 129 delete(m, 1) 130 }) 131 t.Assert(s.Size(), 2) 132 s.RLockFunc(func(m map[interface{}]struct{}) { 133 t.Assert(m, map[interface{}]struct{}{ 134 3: {}, 135 2: {}, 136 }) 137 }) 138 }) 139 } 140 141 func TestSet_Equal(t *testing.T) { 142 gtest.C(t, func(t *gtest.T) { 143 s1 := gset.NewSet() 144 s2 := gset.NewSet() 145 s3 := gset.NewSet() 146 s4 := gset.NewSet() 147 s1.Add(1, 2, 3) 148 s2.Add(1, 2, 3) 149 s3.Add(1, 2, 3, 4) 150 s4.Add(4, 5, 6) 151 t.Assert(s1.Equal(s2), true) 152 t.Assert(s1.Equal(s3), false) 153 t.Assert(s1.Equal(s4), false) 154 s5 := s1 155 t.Assert(s1.Equal(s5), true) 156 }) 157 } 158 159 func TestSet_IsSubsetOf(t *testing.T) { 160 gtest.C(t, func(t *gtest.T) { 161 s1 := gset.NewSet() 162 s2 := gset.NewSet() 163 s3 := gset.NewSet() 164 s1.Add(1, 2) 165 s2.Add(1, 2, 3) 166 s3.Add(1, 2, 3, 4) 167 t.Assert(s1.IsSubsetOf(s2), true) 168 t.Assert(s2.IsSubsetOf(s3), true) 169 t.Assert(s1.IsSubsetOf(s3), true) 170 t.Assert(s2.IsSubsetOf(s1), false) 171 t.Assert(s3.IsSubsetOf(s2), false) 172 173 s4 := s1 174 t.Assert(s1.IsSubsetOf(s4), true) 175 }) 176 } 177 178 func TestSet_Union(t *testing.T) { 179 gtest.C(t, func(t *gtest.T) { 180 s1 := gset.NewSet() 181 s2 := gset.NewSet() 182 s1.Add(1, 2) 183 s2.Add(3, 4) 184 s3 := s1.Union(s2) 185 t.Assert(s3.Contains(1), true) 186 t.Assert(s3.Contains(2), true) 187 t.Assert(s3.Contains(3), true) 188 t.Assert(s3.Contains(4), true) 189 }) 190 } 191 192 func TestSet_Diff(t *testing.T) { 193 gtest.C(t, func(t *gtest.T) { 194 s1 := gset.NewSet() 195 s2 := gset.NewSet() 196 s1.Add(1, 2, 3) 197 s2.Add(3, 4, 5) 198 s3 := s1.Diff(s2) 199 t.Assert(s3.Contains(1), true) 200 t.Assert(s3.Contains(2), true) 201 t.Assert(s3.Contains(3), false) 202 t.Assert(s3.Contains(4), false) 203 204 s4 := s1 205 s5 := s1.Diff(s2, s4) 206 t.Assert(s5.Contains(1), true) 207 t.Assert(s5.Contains(2), true) 208 t.Assert(s5.Contains(3), false) 209 t.Assert(s5.Contains(4), false) 210 }) 211 } 212 213 func TestSet_Intersect(t *testing.T) { 214 gtest.C(t, func(t *gtest.T) { 215 s1 := gset.NewSet() 216 s2 := gset.NewSet() 217 s1.Add(1, 2, 3) 218 s2.Add(3, 4, 5) 219 s3 := s1.Intersect(s2) 220 t.Assert(s3.Contains(1), false) 221 t.Assert(s3.Contains(2), false) 222 t.Assert(s3.Contains(3), true) 223 t.Assert(s3.Contains(4), false) 224 }) 225 } 226 227 func TestSet_Complement(t *testing.T) { 228 gtest.C(t, func(t *gtest.T) { 229 s1 := gset.NewSet() 230 s2 := gset.NewSet() 231 s1.Add(1, 2, 3) 232 s2.Add(3, 4, 5) 233 s3 := s1.Complement(s2) 234 t.Assert(s3.Contains(1), false) 235 t.Assert(s3.Contains(2), false) 236 t.Assert(s3.Contains(4), true) 237 t.Assert(s3.Contains(5), true) 238 }) 239 } 240 241 func TestNewFrom(t *testing.T) { 242 gtest.C(t, func(t *gtest.T) { 243 s1 := gset.NewFrom("a") 244 s2 := gset.NewFrom("b", false) 245 s3 := gset.NewFrom(3, true) 246 s4 := gset.NewFrom([]string{"s1", "s2"}, true) 247 t.Assert(s1.Contains("a"), true) 248 t.Assert(s2.Contains("b"), true) 249 t.Assert(s3.Contains(3), true) 250 t.Assert(s4.Contains("s1"), true) 251 t.Assert(s4.Contains("s3"), false) 252 253 }) 254 } 255 256 func TestNew(t *testing.T) { 257 gtest.C(t, func(t *gtest.T) { 258 s1 := gset.New() 259 s1.Add("a", 2) 260 s2 := gset.New(true) 261 s2.Add("b", 3) 262 t.Assert(s1.Contains("a"), true) 263 264 }) 265 } 266 267 func TestSet_Join(t *testing.T) { 268 gtest.C(t, func(t *gtest.T) { 269 s1 := gset.New(true) 270 s1.Add("a", "a1", "b", "c") 271 str1 := s1.Join(",") 272 t.Assert(strings.Contains(str1, "a1"), true) 273 }) 274 gtest.C(t, func(t *gtest.T) { 275 s1 := gset.New(true) 276 s1.Add("a", `"b"`, `\c`) 277 str1 := s1.Join(",") 278 t.Assert(strings.Contains(str1, `"b"`), true) 279 t.Assert(strings.Contains(str1, `\c`), true) 280 t.Assert(strings.Contains(str1, `a`), true) 281 }) 282 gtest.C(t, func(t *gtest.T) { 283 s1 := gset.Set{} 284 t.Assert(s1.Join(","), "") 285 }) 286 } 287 288 func TestSet_String(t *testing.T) { 289 gtest.C(t, func(t *gtest.T) { 290 s1 := gset.New(true) 291 s1.Add("a", "a2", "b", "c") 292 str1 := s1.String() 293 t.Assert(strings.Contains(str1, "["), true) 294 t.Assert(strings.Contains(str1, "]"), true) 295 t.Assert(strings.Contains(str1, "a2"), true) 296 297 s1 = nil 298 t.Assert(s1.String(), "") 299 300 s2 := gset.New() 301 s2.Add(1) 302 t.Assert(s2.String(), "[1]") 303 }) 304 } 305 306 func TestSet_Merge(t *testing.T) { 307 gtest.C(t, func(t *gtest.T) { 308 s1 := gset.New(true) 309 s2 := gset.New(true) 310 s1.Add("a", "a2", "b", "c") 311 s2.Add("b", "b1", "e", "f") 312 ss := s1.Merge(s2) 313 t.Assert(ss.Contains("a2"), true) 314 t.Assert(ss.Contains("b1"), true) 315 316 }) 317 } 318 319 func TestSet_Sum(t *testing.T) { 320 gtest.C(t, func(t *gtest.T) { 321 s1 := gset.New(true) 322 s1.Add(1, 2, 3, 4) 323 t.Assert(s1.Sum(), int(10)) 324 325 }) 326 } 327 328 func TestSet_Pop(t *testing.T) { 329 gtest.C(t, func(t *gtest.T) { 330 s := gset.New(true) 331 t.Assert(s.Pop(), nil) 332 s.Add(1, 2, 3, 4) 333 t.Assert(s.Size(), 4) 334 t.AssertIN(s.Pop(), []int{1, 2, 3, 4}) 335 t.Assert(s.Size(), 3) 336 }) 337 } 338 339 func TestSet_Pops(t *testing.T) { 340 gtest.C(t, func(t *gtest.T) { 341 s := gset.New(true) 342 s.Add(1, 2, 3, 4) 343 t.Assert(s.Size(), 4) 344 t.Assert(s.Pops(0), nil) 345 t.AssertIN(s.Pops(1), []int{1, 2, 3, 4}) 346 t.Assert(s.Size(), 3) 347 a := s.Pops(6) 348 t.Assert(len(a), 3) 349 t.AssertIN(a, []int{1, 2, 3, 4}) 350 t.Assert(s.Size(), 0) 351 }) 352 353 gtest.C(t, func(t *gtest.T) { 354 s := gset.New(true) 355 a := []interface{}{1, 2, 3, 4} 356 s.Add(a...) 357 t.Assert(s.Size(), 4) 358 t.Assert(s.Pops(-2), nil) 359 t.AssertIN(s.Pops(-1), a) 360 }) 361 } 362 363 func TestSet_Json(t *testing.T) { 364 gtest.C(t, func(t *gtest.T) { 365 s1 := []interface{}{"a", "b", "d", "c"} 366 a1 := gset.NewFrom(s1) 367 b1, err1 := json.Marshal(a1) 368 b2, err2 := json.Marshal(s1) 369 t.Assert(len(b1), len(b2)) 370 t.Assert(err1, err2) 371 372 a2 := gset.New() 373 err2 = json.UnmarshalUseNumber(b2, &a2) 374 t.Assert(err2, nil) 375 t.Assert(a2.Contains("a"), true) 376 t.Assert(a2.Contains("b"), true) 377 t.Assert(a2.Contains("c"), true) 378 t.Assert(a2.Contains("d"), true) 379 t.Assert(a2.Contains("e"), false) 380 381 var a3 gset.Set 382 err := json.UnmarshalUseNumber(b2, &a3) 383 t.AssertNil(err) 384 t.Assert(a3.Contains("a"), true) 385 t.Assert(a3.Contains("b"), true) 386 t.Assert(a3.Contains("c"), true) 387 t.Assert(a3.Contains("d"), true) 388 t.Assert(a3.Contains("e"), false) 389 }) 390 } 391 392 func TestSet_AddIfNotExist(t *testing.T) { 393 gtest.C(t, func(t *gtest.T) { 394 s := gset.New(true) 395 s.Add(1) 396 t.Assert(s.Contains(1), true) 397 t.Assert(s.AddIfNotExist(1), false) 398 t.Assert(s.AddIfNotExist(2), true) 399 t.Assert(s.Contains(2), true) 400 t.Assert(s.AddIfNotExist(2), false) 401 t.Assert(s.AddIfNotExist(nil), false) 402 t.Assert(s.Contains(2), true) 403 }) 404 } 405 406 func TestSet_AddIfNotExistFunc(t *testing.T) { 407 gtest.C(t, func(t *gtest.T) { 408 s := gset.New(true) 409 s.Add(1) 410 t.Assert(s.Contains(1), true) 411 t.Assert(s.Contains(2), false) 412 t.Assert(s.AddIfNotExistFunc(2, func() bool { return false }), false) 413 t.Assert(s.Contains(2), false) 414 t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), true) 415 t.Assert(s.Contains(2), true) 416 t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), false) 417 t.Assert(s.Contains(2), true) 418 t.Assert(s.AddIfNotExistFunc(nil, func() bool { return false }), false) 419 }) 420 gtest.C(t, func(t *gtest.T) { 421 s := gset.New(true) 422 wg := sync.WaitGroup{} 423 wg.Add(1) 424 go func() { 425 defer wg.Done() 426 r := s.AddIfNotExistFunc(1, func() bool { 427 time.Sleep(100 * time.Millisecond) 428 return true 429 }) 430 t.Assert(r, false) 431 }() 432 s.Add(1) 433 wg.Wait() 434 }) 435 gtest.C(t, func(t *gtest.T) { 436 s := gset.Set{} 437 t.Assert(s.AddIfNotExistFunc(1, func() bool { return true }), true) 438 }) 439 } 440 441 func TestSet_Walk(t *testing.T) { 442 gtest.C(t, func(t *gtest.T) { 443 var set gset.Set 444 set.Add(g.Slice{1, 2}...) 445 set.Walk(func(item interface{}) interface{} { 446 return gconv.Int(item) + 10 447 }) 448 t.Assert(set.Size(), 2) 449 t.Assert(set.Contains(11), true) 450 t.Assert(set.Contains(12), true) 451 }) 452 } 453 454 func TestSet_AddIfNotExistFuncLock(t *testing.T) { 455 gtest.C(t, func(t *gtest.T) { 456 s := gset.New(true) 457 wg := sync.WaitGroup{} 458 wg.Add(2) 459 go func() { 460 defer wg.Done() 461 r := s.AddIfNotExistFuncLock(1, func() bool { 462 time.Sleep(500 * time.Millisecond) 463 return true 464 }) 465 t.Assert(r, true) 466 }() 467 time.Sleep(100 * time.Millisecond) 468 go func() { 469 defer wg.Done() 470 r := s.AddIfNotExistFuncLock(1, func() bool { 471 return true 472 }) 473 t.Assert(r, false) 474 }() 475 wg.Wait() 476 }) 477 gtest.C(t, func(t *gtest.T) { 478 s := gset.New(true) 479 t.Assert(s.AddIfNotExistFuncLock(nil, func() bool { return true }), false) 480 s1 := gset.Set{} 481 t.Assert(s1.AddIfNotExistFuncLock(1, func() bool { return true }), true) 482 }) 483 } 484 485 func TestSet_UnmarshalValue(t *testing.T) { 486 type V struct { 487 Name string 488 Set *gset.Set 489 } 490 // JSON 491 gtest.C(t, func(t *gtest.T) { 492 var v *V 493 err := gconv.Struct(g.Map{ 494 "name": "john", 495 "set": []byte(`["k1","k2","k3"]`), 496 }, &v) 497 t.AssertNil(err) 498 t.Assert(v.Name, "john") 499 t.Assert(v.Set.Size(), 3) 500 t.Assert(v.Set.Contains("k1"), true) 501 t.Assert(v.Set.Contains("k2"), true) 502 t.Assert(v.Set.Contains("k3"), true) 503 t.Assert(v.Set.Contains("k4"), false) 504 }) 505 // Map 506 gtest.C(t, func(t *gtest.T) { 507 var v *V 508 err := gconv.Struct(g.Map{ 509 "name": "john", 510 "set": g.Slice{"k1", "k2", "k3"}, 511 }, &v) 512 t.AssertNil(err) 513 t.Assert(v.Name, "john") 514 t.Assert(v.Set.Size(), 3) 515 t.Assert(v.Set.Contains("k1"), true) 516 t.Assert(v.Set.Contains("k2"), true) 517 t.Assert(v.Set.Contains("k3"), true) 518 t.Assert(v.Set.Contains("k4"), false) 519 }) 520 } 521 522 func TestSet_DeepCopy(t *testing.T) { 523 gtest.C(t, func(t *gtest.T) { 524 set := gset.New() 525 set.Add(1, 2, 3) 526 527 copySet := set.DeepCopy().(*gset.Set) 528 copySet.Add(4) 529 t.AssertNE(set.Size(), copySet.Size()) 530 t.AssertNE(set.String(), copySet.String()) 531 532 set = nil 533 t.AssertNil(set.DeepCopy()) 534 }) 535 }