github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/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/wangyougui/gf/v2/container/garray" 18 "github.com/wangyougui/gf/v2/container/gset" 19 "github.com/wangyougui/gf/v2/frame/g" 20 "github.com/wangyougui/gf/v2/internal/json" 21 "github.com/wangyougui/gf/v2/test/gtest" 22 "github.com/wangyougui/gf/v2/util/gconv" 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: {}, 98 2: {}, 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 s4 := gset.NewIntSet() 110 s1.Add(1, 2, 3) 111 s2.Add(1, 2, 3) 112 s3.Add(1, 2, 3, 4) 113 s4.Add(4, 5, 6) 114 t.Assert(s1.Equal(s2), true) 115 t.Assert(s1.Equal(s3), false) 116 t.Assert(s1.Equal(s4), false) 117 s5 := s1 118 t.Assert(s1.Equal(s5), true) 119 }) 120 } 121 122 func TestIntSet_IsSubsetOf(t *testing.T) { 123 gtest.C(t, func(t *gtest.T) { 124 s1 := gset.NewIntSet() 125 s2 := gset.NewIntSet() 126 s3 := gset.NewIntSet() 127 s1.Add(1, 2) 128 s2.Add(1, 2, 3) 129 s3.Add(1, 2, 3, 4) 130 t.Assert(s1.IsSubsetOf(s2), true) 131 t.Assert(s2.IsSubsetOf(s3), true) 132 t.Assert(s1.IsSubsetOf(s3), true) 133 t.Assert(s2.IsSubsetOf(s1), false) 134 t.Assert(s3.IsSubsetOf(s2), false) 135 136 s4 := s1 137 t.Assert(s1.IsSubsetOf(s4), true) 138 }) 139 } 140 141 func TestIntSet_Union(t *testing.T) { 142 gtest.C(t, func(t *gtest.T) { 143 s1 := gset.NewIntSet() 144 s2 := gset.NewIntSet() 145 s1.Add(1, 2) 146 s2.Add(3, 4) 147 s3 := s1.Union(s2) 148 t.Assert(s3.Contains(1), true) 149 t.Assert(s3.Contains(2), true) 150 t.Assert(s3.Contains(3), true) 151 t.Assert(s3.Contains(4), true) 152 }) 153 } 154 155 func TestIntSet_Diff(t *testing.T) { 156 gtest.C(t, func(t *gtest.T) { 157 s1 := gset.NewIntSet() 158 s2 := gset.NewIntSet() 159 s1.Add(1, 2, 3) 160 s2.Add(3, 4, 5) 161 s3 := s1.Diff(s2) 162 t.Assert(s3.Contains(1), true) 163 t.Assert(s3.Contains(2), true) 164 t.Assert(s3.Contains(3), false) 165 t.Assert(s3.Contains(4), false) 166 167 s4 := s1 168 s5 := s1.Diff(s2, s4) 169 t.Assert(s5.Contains(1), true) 170 t.Assert(s5.Contains(2), true) 171 t.Assert(s5.Contains(3), false) 172 t.Assert(s5.Contains(4), false) 173 }) 174 } 175 176 func TestIntSet_Intersect(t *testing.T) { 177 gtest.C(t, func(t *gtest.T) { 178 s1 := gset.NewIntSet() 179 s2 := gset.NewIntSet() 180 s1.Add(1, 2, 3) 181 s2.Add(3, 4, 5) 182 s3 := s1.Intersect(s2) 183 t.Assert(s3.Contains(1), false) 184 t.Assert(s3.Contains(2), false) 185 t.Assert(s3.Contains(3), true) 186 t.Assert(s3.Contains(4), false) 187 }) 188 } 189 190 func TestIntSet_Complement(t *testing.T) { 191 gtest.C(t, func(t *gtest.T) { 192 s1 := gset.NewIntSet() 193 s2 := gset.NewIntSet() 194 s1.Add(1, 2, 3) 195 s2.Add(3, 4, 5) 196 s3 := s1.Complement(s2) 197 t.Assert(s3.Contains(1), false) 198 t.Assert(s3.Contains(2), false) 199 t.Assert(s3.Contains(4), true) 200 t.Assert(s3.Contains(5), true) 201 }) 202 } 203 204 func TestIntSet_Size(t *testing.T) { 205 gtest.C(t, func(t *gtest.T) { 206 s1 := gset.NewIntSet(true) 207 s1.Add(1, 2, 3) 208 t.Assert(s1.Size(), 3) 209 210 }) 211 212 } 213 214 func TestIntSet_Merge(t *testing.T) { 215 gtest.C(t, func(t *gtest.T) { 216 s1 := gset.NewIntSet() 217 s2 := gset.NewIntSet() 218 s1.Add(1, 2, 3) 219 s2.Add(3, 4, 5) 220 s3 := s1.Merge(s2) 221 t.Assert(s3.Contains(1), true) 222 t.Assert(s3.Contains(5), true) 223 t.Assert(s3.Contains(6), false) 224 }) 225 } 226 227 func TestIntSet_Join(t *testing.T) { 228 gtest.C(t, func(t *gtest.T) { 229 s1 := gset.NewIntSet() 230 t.Assert(s1.Join(","), "") 231 s1.Add(1, 2, 3) 232 s3 := s1.Join(",") 233 t.Assert(strings.Contains(s3, "1"), true) 234 t.Assert(strings.Contains(s3, "2"), true) 235 t.Assert(strings.Contains(s3, "3"), true) 236 }) 237 } 238 239 func TestIntSet_String(t *testing.T) { 240 gtest.C(t, func(t *gtest.T) { 241 s1 := gset.NewIntSet() 242 s1.Add(1, 2, 3) 243 s3 := s1.String() 244 t.Assert(strings.Contains(s3, "["), true) 245 t.Assert(strings.Contains(s3, "]"), true) 246 t.Assert(strings.Contains(s3, "1"), true) 247 t.Assert(strings.Contains(s3, "2"), true) 248 t.Assert(strings.Contains(s3, "3"), true) 249 s1 = nil 250 t.Assert(s1.String(), "") 251 }) 252 } 253 254 func TestIntSet_Sum(t *testing.T) { 255 gtest.C(t, func(t *gtest.T) { 256 s1 := gset.NewIntSet() 257 s1.Add(1, 2, 3) 258 s2 := gset.NewIntSet() 259 s2.Add(5, 6, 7) 260 t.Assert(s2.Sum(), 18) 261 262 }) 263 264 } 265 266 func TestIntSet_Pop(t *testing.T) { 267 gtest.C(t, func(t *gtest.T) { 268 s := gset.NewIntSet() 269 t.Assert(s.Pop(), 0) 270 s.Add(4, 2, 3) 271 t.Assert(s.Size(), 3) 272 t.AssertIN(s.Pop(), []int{4, 2, 3}) 273 t.AssertIN(s.Pop(), []int{4, 2, 3}) 274 t.Assert(s.Size(), 1) 275 }) 276 } 277 278 func TestIntSet_Pops(t *testing.T) { 279 gtest.C(t, func(t *gtest.T) { 280 s := gset.NewIntSet() 281 s.Add(1, 4, 2, 3) 282 t.Assert(s.Size(), 4) 283 t.Assert(s.Pops(0), nil) 284 t.AssertIN(s.Pops(1), []int{1, 4, 2, 3}) 285 t.Assert(s.Size(), 3) 286 a := s.Pops(2) 287 t.Assert(len(a), 2) 288 t.AssertIN(a, []int{1, 4, 2, 3}) 289 t.Assert(s.Size(), 1) 290 }) 291 292 gtest.C(t, func(t *gtest.T) { 293 s := gset.NewIntSet(true) 294 a := []int{1, 2, 3, 4} 295 s.Add(a...) 296 t.Assert(s.Size(), 4) 297 t.Assert(s.Pops(-2), nil) 298 t.AssertIN(s.Pops(-1), a) 299 }) 300 } 301 302 func TestIntSet_AddIfNotExist(t *testing.T) { 303 gtest.C(t, func(t *gtest.T) { 304 s := gset.NewIntSet(true) 305 s.Add(1) 306 t.Assert(s.Contains(1), true) 307 t.Assert(s.AddIfNotExist(1), false) 308 t.Assert(s.AddIfNotExist(2), true) 309 t.Assert(s.Contains(2), true) 310 t.Assert(s.AddIfNotExist(2), false) 311 t.Assert(s.Contains(2), true) 312 }) 313 gtest.C(t, func(t *gtest.T) { 314 s := gset.IntSet{} 315 t.Assert(s.AddIfNotExist(1), true) 316 }) 317 } 318 319 func TestIntSet_AddIfNotExistFunc(t *testing.T) { 320 gtest.C(t, func(t *gtest.T) { 321 s := gset.NewIntSet(true) 322 s.Add(1) 323 t.Assert(s.Contains(1), true) 324 t.Assert(s.Contains(2), false) 325 t.Assert(s.AddIfNotExistFunc(2, func() bool { return false }), false) 326 t.Assert(s.Contains(2), false) 327 t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), true) 328 t.Assert(s.Contains(2), true) 329 t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), false) 330 t.Assert(s.Contains(2), true) 331 }) 332 gtest.C(t, func(t *gtest.T) { 333 s := gset.NewIntSet(true) 334 wg := sync.WaitGroup{} 335 wg.Add(1) 336 go func() { 337 defer wg.Done() 338 r := s.AddIfNotExistFunc(1, func() bool { 339 time.Sleep(100 * time.Millisecond) 340 return true 341 }) 342 t.Assert(r, false) 343 }() 344 s.Add(1) 345 wg.Wait() 346 }) 347 gtest.C(t, func(t *gtest.T) { 348 s := gset.IntSet{} 349 t.Assert(s.AddIfNotExistFunc(1, func() bool { return true }), true) 350 }) 351 } 352 353 func TestIntSet_AddIfNotExistFuncLock(t *testing.T) { 354 gtest.C(t, func(t *gtest.T) { 355 s := gset.NewIntSet(true) 356 wg := sync.WaitGroup{} 357 wg.Add(2) 358 go func() { 359 defer wg.Done() 360 r := s.AddIfNotExistFuncLock(1, func() bool { 361 time.Sleep(500 * time.Millisecond) 362 return true 363 }) 364 t.Assert(r, true) 365 }() 366 time.Sleep(100 * time.Millisecond) 367 go func() { 368 defer wg.Done() 369 r := s.AddIfNotExistFuncLock(1, func() bool { 370 return true 371 }) 372 t.Assert(r, false) 373 }() 374 wg.Wait() 375 }) 376 gtest.C(t, func(t *gtest.T) { 377 s := gset.IntSet{} 378 t.Assert(s.AddIfNotExistFuncLock(1, func() bool { return true }), true) 379 }) 380 } 381 382 func TestIntSet_Json(t *testing.T) { 383 gtest.C(t, func(t *gtest.T) { 384 s1 := []int{1, 3, 2, 4} 385 a1 := gset.NewIntSetFrom(s1) 386 b1, err1 := json.Marshal(a1) 387 b2, err2 := json.Marshal(s1) 388 t.Assert(len(b1), len(b2)) 389 t.Assert(err1, err2) 390 391 a2 := gset.NewIntSet() 392 err2 = json.UnmarshalUseNumber(b2, &a2) 393 t.Assert(err2, nil) 394 t.Assert(a2.Contains(1), true) 395 t.Assert(a2.Contains(2), true) 396 t.Assert(a2.Contains(3), true) 397 t.Assert(a2.Contains(4), true) 398 t.Assert(a2.Contains(5), false) 399 400 var a3 gset.IntSet 401 err := json.UnmarshalUseNumber(b2, &a3) 402 t.AssertNil(err) 403 t.Assert(a2.Contains(1), true) 404 t.Assert(a2.Contains(2), true) 405 t.Assert(a2.Contains(3), true) 406 t.Assert(a2.Contains(4), true) 407 t.Assert(a2.Contains(5), false) 408 }) 409 } 410 411 func TestIntSet_Walk(t *testing.T) { 412 gtest.C(t, func(t *gtest.T) { 413 var set gset.IntSet 414 set.Add(g.SliceInt{1, 2}...) 415 set.Walk(func(item int) int { 416 return item + 10 417 }) 418 t.Assert(set.Size(), 2) 419 t.Assert(set.Contains(11), true) 420 t.Assert(set.Contains(12), true) 421 }) 422 } 423 424 func TestIntSet_UnmarshalValue(t *testing.T) { 425 type V struct { 426 Name string 427 Set *gset.IntSet 428 } 429 // JSON 430 gtest.C(t, func(t *gtest.T) { 431 var v *V 432 err := gconv.Struct(g.Map{ 433 "name": "john", 434 "set": []byte(`[1,2,3]`), 435 }, &v) 436 t.AssertNil(err) 437 t.Assert(v.Name, "john") 438 t.Assert(v.Set.Size(), 3) 439 t.Assert(v.Set.Contains(1), true) 440 t.Assert(v.Set.Contains(2), true) 441 t.Assert(v.Set.Contains(3), true) 442 t.Assert(v.Set.Contains(4), false) 443 }) 444 // Map 445 gtest.C(t, func(t *gtest.T) { 446 var v *V 447 err := gconv.Struct(g.Map{ 448 "name": "john", 449 "set": g.Slice{1, 2, 3}, 450 }, &v) 451 t.AssertNil(err) 452 t.Assert(v.Name, "john") 453 t.Assert(v.Set.Size(), 3) 454 t.Assert(v.Set.Contains(1), true) 455 t.Assert(v.Set.Contains(2), true) 456 t.Assert(v.Set.Contains(3), true) 457 t.Assert(v.Set.Contains(4), false) 458 }) 459 } 460 461 func TestIntSet_DeepCopy(t *testing.T) { 462 gtest.C(t, func(t *gtest.T) { 463 set := gset.NewIntSet() 464 set.Add(1, 2, 3) 465 466 copySet := set.DeepCopy().(*gset.IntSet) 467 copySet.Add(4) 468 t.AssertNE(set.Size(), copySet.Size()) 469 t.AssertNE(set.String(), copySet.String()) 470 471 set = nil 472 t.AssertNil(set.DeepCopy()) 473 }) 474 }