github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bmap/any_test.go (about) 1 package bmap 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "sort" 6 "testing" 7 ) 8 9 type tInterface interface { 10 test() any 11 } 12 13 type tStruct struct { 14 t any 15 } 16 17 func (t tStruct) test() any { 18 return t 19 } 20 21 func TestAnyBMap(t *testing.T) { 22 t1 := tStruct{t: 1} 23 t2 := tStruct{t: 2} 24 t3 := tInterface(nil) 25 26 anyBMap := NewUnsafeAnyBMap[int, tInterface]() 27 // map[int]tInterface{} 28 29 assert.Equal(t, anyBMap.Keys(), []int{}) 30 assert.Equal(t, anyBMap.Values(), []tInterface{}) 31 assert.Equal(t, anyBMap.Size(), 0) 32 assert.Equal(t, false, anyBMap.EqualFuncByMap(map[int]tInterface{1: t1, 2: t2, 3: t3}, func(v1, v2 tInterface) bool { 33 return v1.test() == v2.test() 34 })) 35 assert.Equal(t, false, anyBMap.EqualFuncByBMap(NewUnsafeAnyBMapByMap(map[int]tInterface{1: t1, 2: t2, 3: t3}), func(v1, v2 tInterface) bool { 36 return v1.test() == v2.test() 37 })) 38 assert.Equal(t, true, anyBMap.IsEmpty()) 39 40 assert.Equal(t, false, anyBMap.IsExist(1)) 41 assert.Equal(t, false, anyBMap.ContainsKey(1)) 42 assert.Equal(t, false, anyBMap.ContainsValue(t1)) 43 44 // map[int]tInterface{} 45 v1, ok := anyBMap.Get(1) 46 assert.Equal(t, false, ok) 47 if v1 != nil { 48 t.Error("v1 should be nil") 49 } 50 v2 := anyBMap.GetOrDefault(1, t2) 51 assert.Equal(t, t2, v2) 52 53 // map[int]tInterface{1:t1} 54 55 anyBMap.Put(1, t1) 56 assert.Equal(t, anyBMap.Keys(), []int{1}) 57 assert.Equal(t, anyBMap.Values(), []tInterface{t1}) 58 assert.Equal(t, anyBMap.Size(), 1) 59 assert.Equal(t, true, anyBMap.EqualFuncByMap(map[int]tInterface{1: t1}, func(v1, v2 tInterface) bool { 60 return v1.test() == v2.test() 61 })) 62 assert.Equal(t, true, anyBMap.EqualFuncByBMap(NewUnsafeAnyBMapByMap(map[int]tInterface{1: t1}), func(v1, v2 tInterface) bool { 63 return v1.test() == v2.test() 64 })) 65 assert.Equal(t, false, anyBMap.IsEmpty()) 66 assert.Equal(t, true, anyBMap.IsExist(1)) 67 assert.Equal(t, true, anyBMap.ContainsKey(1)) 68 assert.Equal(t, true, anyBMap.ContainsValue(t1)) 69 70 // map[int]tInterface{1:t1} 71 v1, ok = anyBMap.Get(1) 72 assert.Equal(t, true, ok) 73 assert.Equal(t, t1, v1) 74 v2 = anyBMap.GetOrDefault(1, t2) 75 assert.Equal(t, t1, v2) 76 77 assert.Equal(t, false, anyBMap.PuTIfAbsent(1, t2)) 78 assert.Equal(t, anyBMap.Keys(), []int{1}) 79 assert.Equal(t, anyBMap.Values(), []tInterface{t1}) 80 assert.Equal(t, anyBMap.Size(), 1) 81 assert.Equal(t, true, anyBMap.EqualFuncByMap(map[int]tInterface{1: t1}, func(v1, v2 tInterface) bool { 82 return v1.test() == v2.test() 83 })) 84 assert.Equal(t, true, anyBMap.EqualFuncByBMap(NewUnsafeAnyBMapByMap(map[int]tInterface{1: t1}), func(v1, v2 tInterface) bool { 85 return v1.test() == v2.test() 86 })) 87 assert.Equal(t, false, anyBMap.IsEmpty()) 88 assert.Equal(t, true, anyBMap.IsExist(1)) 89 assert.Equal(t, true, anyBMap.ContainsKey(1)) 90 assert.Equal(t, true, anyBMap.ContainsValue(t1)) 91 v1, ok = anyBMap.Get(1) 92 assert.Equal(t, true, ok) 93 assert.Equal(t, t1, v1) 94 95 // map[int]tInterface{1:t1} 96 97 v2 = anyBMap.GetOrDefault(1, t2) 98 assert.Equal(t, t1, v2) 99 100 assert.Equal(t, true, anyBMap.PuTIfAbsent(2, t2)) 101 102 // map[int]tInterface{1:t1, 2:t2} 103 ks := anyBMap.Keys() 104 vs := anyBMap.Values() 105 sort.Ints(ks) 106 sort.Slice(vs, func(i, j int) bool { 107 if vs[i] != nil && vs[j] != nil { 108 return vs[i].test().(tStruct).t.(int) < vs[j].test().(tStruct).t.(int) 109 } 110 return false 111 }) 112 113 assert.Equal(t, ks, []int{1, 2}) 114 assert.Equal(t, vs, []tInterface{t1, t2}) 115 assert.Equal(t, anyBMap.Size(), 2) 116 assert.Equal(t, true, anyBMap.EqualFuncByMap(map[int]tInterface{1: t1, 2: t2}, func(v1, v2 tInterface) bool { 117 return v1.test() == v2.test() 118 })) 119 assert.Equal(t, true, anyBMap.EqualFuncByBMap(NewUnsafeAnyBMapByMap(map[int]tInterface{1: t1, 2: t2}), func(v1, v2 tInterface) bool { 120 return v1.test() == v2.test() 121 })) 122 assert.Equal(t, false, anyBMap.IsEmpty()) 123 assert.Equal(t, true, anyBMap.IsExist(2)) 124 assert.Equal(t, true, anyBMap.ContainsKey(2)) 125 assert.Equal(t, true, anyBMap.ContainsValue(t2)) 126 // map[int]tInterface{1:t1, 2:t2} 127 v1, ok = anyBMap.Get(2) 128 assert.Equal(t, true, ok) 129 assert.Equal(t, t2, v1) 130 v2 = anyBMap.GetOrDefault(2, t1) 131 assert.Equal(t, t2, v2) 132 133 anyBMap.Delete(3) 134 // map[int]tInterface{1:t1, 2:t2} 135 136 ks = anyBMap.Keys() 137 vs = anyBMap.Values() 138 sort.Ints(ks) 139 sort.Slice(vs, func(i, j int) bool { 140 if vs[i] != nil && vs[j] != nil { 141 return vs[i].test().(tStruct).t.(int) < vs[j].test().(tStruct).t.(int) 142 } 143 return false 144 }) 145 146 assert.Equal(t, ks, []int{1, 2}) 147 assert.Equal(t, vs, []tInterface{t1, t2}) 148 assert.Equal(t, anyBMap.Size(), 2) 149 assert.Equal(t, true, anyBMap.EqualFuncByMap(map[int]tInterface{1: t1, 2: t2}, func(v1, v2 tInterface) bool { 150 return v1.test() == v2.test() 151 })) 152 assert.Equal(t, true, anyBMap.EqualFuncByBMap(NewUnsafeAnyBMapByMap(map[int]tInterface{1: t1, 2: t2}), func(v1, v2 tInterface) bool { 153 return v1.test() == v2.test() 154 })) 155 assert.Equal(t, false, anyBMap.IsEmpty()) 156 157 d, ok := anyBMap.DeleteIfPresent(3) 158 159 // map[int]tInterface{1:t1, 2:t2} 160 161 assert.Equal(t, false, ok) 162 if d != nil { 163 t.Error("d should be nil") 164 } 165 166 ks = anyBMap.Keys() 167 vs = anyBMap.Values() 168 sort.Ints(ks) 169 sort.Slice(vs, func(i, j int) bool { 170 if vs[i] != nil && vs[j] != nil { 171 return vs[i].test().(tStruct).t.(int) < vs[j].test().(tStruct).t.(int) 172 } 173 return false 174 }) 175 assert.Equal(t, ks, []int{1, 2}) 176 assert.Equal(t, vs, []tInterface{t1, t2}) 177 assert.Equal(t, anyBMap.Size(), 2) 178 assert.Equal(t, true, anyBMap.EqualFuncByMap(map[int]tInterface{1: t1, 2: t2}, func(v1, v2 tInterface) bool { 179 return v1.test() == v2.test() 180 })) 181 assert.Equal(t, true, anyBMap.EqualFuncByBMap(NewUnsafeAnyBMapByMap(map[int]tInterface{1: t1, 2: t2}), func(v1, v2 tInterface) bool { 182 return v1.test() == v2.test() 183 })) 184 185 d2, ok := anyBMap.DeleteIfPresent(1) 186 187 // map[int]tInterface{2:t2} 188 assert.Equal(t, true, ok) 189 assert.Equal(t, t1, d2) 190 191 assert.Equal(t, anyBMap.Keys(), []int{2}) 192 assert.Equal(t, anyBMap.Values(), []tInterface{t2}) 193 assert.Equal(t, anyBMap.Size(), 1) 194 assert.Equal(t, true, anyBMap.EqualFuncByMap(map[int]tInterface{2: t2}, func(v1, v2 tInterface) bool { 195 return v1.test() == v2.test() 196 })) 197 assert.Equal(t, true, anyBMap.EqualFuncByBMap(NewUnsafeAnyBMapByMap(map[int]tInterface{2: t2}), func(v1, v2 tInterface) bool { 198 return v1.test() == v2.test() 199 })) 200 201 assert.Equal(t, false, anyBMap.ContainsKey(1)) 202 assert.Equal(t, false, anyBMap.ContainsValue(t1)) 203 204 // map[int]tInterface{2:t2} 205 206 assert.Equal(t, false, anyBMap.Replace(2, t1, t3)) 207 assert.Equal(t, true, anyBMap.Replace(2, t2, t3)) 208 assert.Equal(t, true, anyBMap.ContainsValue(t3)) 209 210 anyBMap.MergeByMap(map[int]tInterface{2: t1}, func(k int, v tInterface) bool { 211 return k != 2 212 }) 213 214 assert.Equal(t, anyBMap.Keys(), []int{2}) 215 assert.Equal(t, anyBMap.Values(), []tInterface{t3}) 216 assert.Equal(t, true, anyBMap.Replace(2, t3, t1)) 217 assert.Equal(t, anyBMap.Size(), 1) 218 assert.Equal(t, true, anyBMap.EqualFuncByMap(map[int]tInterface{2: t1}, func(v1, v2 tInterface) bool { 219 return v1.test() == v2.test() 220 })) 221 assert.Equal(t, true, anyBMap.EqualFuncByBMap(NewUnsafeAnyBMapByMap(map[int]tInterface{2: t1}), func(v1, v2 tInterface) bool { 222 return v1.test() == v2.test() 223 })) 224 225 // map[int]tInterface{2:t2} 226 anyBMap.MergeByMap(map[int]tInterface{1: t1, 2: t2, 3: t3}, func(k int, v tInterface) bool { 227 return k != 2 228 }) 229 230 assert.Equal(t, len(anyBMap.Keys()), 3) 231 assert.Equal(t, len(anyBMap.Values()), 3) 232 assert.Equal(t, anyBMap.Size(), 3) 233 v1, ok = anyBMap.Get(2) 234 assert.Equal(t, true, ok) 235 assert.Equal(t, v1, t1) 236 237 c1 := anyBMap.CloneToMap() 238 assert.Equal(t, true, anyBMap.EqualFuncByMap(c1, func(v1, v2 tInterface) bool { 239 if v1 == v2 { 240 return true 241 } 242 return v1.test() == v2.test() 243 })) 244 c2 := anyBMap.CloneToBMap() 245 assert.Equal(t, true, anyBMap.EqualFuncByBMap(c2, func(v1, v2 tInterface) bool { 246 if v1 == v2 { 247 return true 248 } 249 return v1.test() == v2.test() 250 })) 251 252 mp := make(map[int]tInterface) 253 anyBMap.CopyByMap(mp) 254 assert.Equal(t, true, anyBMap.EqualFuncByMap(mp, func(v1, v2 tInterface) bool { 255 if v1 == v2 { 256 return true 257 } 258 return v1.test() == v2.test() 259 })) 260 bmp := NewUnsafeAnyBMap[int, tInterface]() 261 anyBMap.CopyByBMap(bmp) 262 assert.Equal(t, true, anyBMap.EqualFuncByBMap(bmp, func(v1, v2 tInterface) bool { 263 if v1 == v2 { 264 return true 265 } 266 return v1.test() == v2.test() 267 })) 268 269 anyBMap.Clear() 270 assert.Equal(t, anyBMap.Keys(), []int{}) 271 assert.Equal(t, anyBMap.Values(), []tInterface{}) 272 assert.Equal(t, anyBMap.Size(), 0) 273 assert.Equal(t, false, anyBMap.EqualFuncByMap(map[int]tInterface{1: t1, 2: t2, 3: t3}, func(v1, v2 tInterface) bool { 274 return v1.test() == v2.test() 275 })) 276 assert.Equal(t, false, anyBMap.EqualFuncByBMap(NewUnsafeAnyBMapByMap(map[int]tInterface{1: t1, 2: t2, 3: t3}), func(v1, v2 tInterface) bool { 277 return v1.test() == v2.test() 278 })) 279 assert.Equal(t, true, anyBMap.IsEmpty()) 280 } 281 282 func TestUnsafeAnyBMap_ToMetaMap(t *testing.T) { 283 type fields struct { 284 mp map[int]int 285 } 286 tests := []struct { 287 name string 288 fields fields 289 want map[int]int 290 }{ 291 { 292 name: "nil", 293 fields: fields{ 294 mp: nil, 295 }, 296 want: map[int]int{}, 297 }, 298 { 299 name: "", 300 fields: fields{ 301 mp: map[int]int{1: 1, 2: 2, 3: 3}, 302 }, 303 want: map[int]int{1: 1, 2: 2, 3: 3}, 304 }, 305 } 306 for _, tt := range tests { 307 t.Run(tt.name, func(t *testing.T) { 308 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 309 assert.Equalf(t, tt.want, x.ToMetaMap(), "ToMetaMap()") 310 }) 311 } 312 } 313 314 func TestUnsafeAnyBMap_Keys(t *testing.T) { 315 type fields struct { 316 mp map[int]int 317 } 318 tests := []struct { 319 name string 320 fields fields 321 want []int 322 }{ 323 { 324 name: "nil", 325 fields: fields{ 326 mp: nil, 327 }, 328 want: []int{}, 329 }, 330 { 331 name: "", 332 fields: fields{ 333 mp: map[int]int{1: 1, 2: 2, 3: 3}, 334 }, 335 want: []int{1, 2, 3}, 336 }, 337 } 338 for _, tt := range tests { 339 t.Run(tt.name, func(t *testing.T) { 340 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 341 v := x.Keys() 342 sort.Ints(v) 343 assert.Equalf(t, tt.want, v, "Keys()") 344 }) 345 } 346 } 347 348 func TestUnsafeAnyBMap_Values(t *testing.T) { 349 type fields struct { 350 mp map[int]int 351 } 352 tests := []struct { 353 name string 354 fields fields 355 want []int 356 }{ 357 { 358 name: "nil", 359 fields: fields{ 360 mp: nil, 361 }, 362 want: []int{}, 363 }, 364 { 365 name: "", 366 fields: fields{ 367 mp: map[int]int{1: 1, 2: 2, 3: 3}, 368 }, 369 want: []int{1, 2, 3}, 370 }, 371 } 372 for _, tt := range tests { 373 t.Run(tt.name, func(t *testing.T) { 374 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 375 v := x.Values() 376 sort.Ints(v) 377 assert.Equalf(t, tt.want, v, "Values()") 378 }) 379 } 380 } 381 382 func TestUnsafeAnyBMap_EqualFuncByMap(t *testing.T) { 383 type fields struct { 384 mp map[int]int 385 } 386 eq := func(v1, v2 int) bool { 387 return v1 == v2 388 } 389 type args struct { 390 m map[int]int 391 } 392 tests := []struct { 393 name string 394 fields fields 395 args args 396 want bool 397 }{ 398 { 399 name: "nil", 400 fields: fields{ 401 mp: nil, 402 }, 403 args: args{ 404 m: nil, 405 }, 406 want: true, 407 }, 408 { 409 name: "", 410 fields: fields{ 411 mp: nil, 412 }, 413 args: args{ 414 m: map[int]int{}, 415 }, 416 want: true, 417 }, 418 { 419 name: "", 420 fields: fields{ 421 mp: nil, 422 }, 423 args: args{ 424 m: map[int]int{1: 1}, 425 }, 426 want: false, 427 }, 428 { 429 name: "", 430 fields: fields{ 431 mp: map[int]int{1: 1}, 432 }, 433 args: args{ 434 m: nil, 435 }, 436 want: false, 437 }, 438 { 439 name: "", 440 fields: fields{ 441 mp: map[int]int{1: 1}, 442 }, 443 args: args{ 444 m: map[int]int{1: 1}, 445 }, 446 want: true, 447 }, 448 { 449 name: "", 450 fields: fields{ 451 mp: map[int]int{1: 1}, 452 }, 453 args: args{ 454 m: map[int]int{1: 1, 2: 2}, 455 }, 456 want: false, 457 }, 458 { 459 name: "", 460 fields: fields{ 461 mp: map[int]int{1: 1, 2: 2}, 462 }, 463 args: args{ 464 m: map[int]int{1: 1}, 465 }, 466 want: false, 467 }, 468 } 469 for _, tt := range tests { 470 t.Run(tt.name, func(t *testing.T) { 471 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 472 assert.Equalf(t, tt.want, x.EqualFuncByMap(tt.args.m, eq), "EqualFuncByMap(%v)", tt.args.m) 473 }) 474 } 475 } 476 477 func TestUnsafeAnyBMap_EqualFuncByBMap(t *testing.T) { 478 type fields struct { 479 mp map[int]int 480 } 481 eq := func(v1, v2 int) bool { 482 return v1 == v2 483 } 484 type args struct { 485 m map[int]int 486 } 487 tests := []struct { 488 name string 489 fields fields 490 args args 491 want bool 492 }{ 493 { 494 name: "nil", 495 fields: fields{ 496 mp: nil, 497 }, 498 args: args{ 499 m: nil, 500 }, 501 want: true, 502 }, 503 { 504 name: "", 505 fields: fields{ 506 mp: nil, 507 }, 508 args: args{ 509 m: map[int]int{}, 510 }, 511 want: true, 512 }, 513 { 514 name: "", 515 fields: fields{ 516 mp: nil, 517 }, 518 args: args{ 519 m: map[int]int{1: 1}, 520 }, 521 want: false, 522 }, 523 { 524 name: "", 525 fields: fields{ 526 mp: map[int]int{1: 1}, 527 }, 528 args: args{ 529 m: nil, 530 }, 531 want: false, 532 }, 533 { 534 name: "", 535 fields: fields{ 536 mp: map[int]int{1: 1}, 537 }, 538 args: args{ 539 m: map[int]int{1: 1}, 540 }, 541 want: true, 542 }, 543 { 544 name: "", 545 fields: fields{ 546 mp: map[int]int{1: 1}, 547 }, 548 args: args{ 549 m: map[int]int{1: 1, 2: 2}, 550 }, 551 want: false, 552 }, 553 { 554 name: "", 555 fields: fields{ 556 mp: map[int]int{1: 1, 2: 2}, 557 }, 558 args: args{ 559 m: map[int]int{1: 1}, 560 }, 561 want: false, 562 }, 563 } 564 for _, tt := range tests { 565 t.Run(tt.name, func(t *testing.T) { 566 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 567 assert.Equalf(t, tt.want, x.EqualFuncByBMap(NewUnsafeAnyBMapByMap(tt.args.m), eq), "EqualFuncByBMap(%v)", tt.args.m) 568 }) 569 } 570 } 571 572 func TestUnsafeAnyBMap_Clear(t *testing.T) { 573 type fields struct { 574 mp map[int]int 575 } 576 tests := []struct { 577 name string 578 fields fields 579 }{ 580 { 581 name: "nil", 582 fields: fields{ 583 mp: nil, 584 }, 585 }, 586 { 587 name: "", 588 fields: fields{ 589 mp: map[int]int{}, 590 }, 591 }, 592 { 593 name: "", 594 fields: fields{ 595 mp: map[int]int{1: 1}, 596 }, 597 }, 598 } 599 for _, tt := range tests { 600 t.Run(tt.name, func(t *testing.T) { 601 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 602 x.Clear() 603 assert.Equalf(t, map[int]int{}, x.ToMetaMap(), "Clear()") 604 }) 605 } 606 } 607 608 func TestUnsafeAnyBMap_CloneToMap(t *testing.T) { 609 type fields struct { 610 mp map[int]int 611 } 612 tests := []struct { 613 name string 614 fields fields 615 want map[int]int 616 }{ 617 { 618 name: "nil", 619 fields: fields{ 620 mp: nil, 621 }, 622 want: map[int]int{}, 623 }, 624 { 625 name: "", 626 fields: fields{ 627 mp: map[int]int{}, 628 }, 629 want: map[int]int{}, 630 }, 631 { 632 name: "", 633 fields: fields{ 634 mp: map[int]int{1: 1}, 635 }, 636 want: map[int]int{1: 1}, 637 }, 638 } 639 for _, tt := range tests { 640 t.Run(tt.name, func(t *testing.T) { 641 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 642 assert.Equalf(t, tt.want, x.CloneToMap(), "CloneToMap()") 643 }) 644 } 645 } 646 647 func TestUnsafeAnyBMap_CloneToBMap(t *testing.T) { 648 type fields struct { 649 mp map[int]int 650 } 651 tests := []struct { 652 name string 653 fields fields 654 want AnyBMap[int, int] 655 }{ 656 { 657 name: "nil", 658 fields: fields{ 659 mp: nil, 660 }, 661 want: NewUnsafeAnyBMapByMap[int, int](nil), 662 }, 663 { 664 name: "", 665 fields: fields{ 666 mp: map[int]int{}, 667 }, 668 want: NewUnsafeAnyBMapByMap[int, int](map[int]int{}), 669 }, 670 { 671 name: "", 672 fields: fields{ 673 mp: map[int]int{1: 1}, 674 }, 675 want: NewUnsafeAnyBMapByMap[int, int](map[int]int{1: 1}), 676 }, 677 } 678 for _, tt := range tests { 679 t.Run(tt.name, func(t *testing.T) { 680 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 681 assert.Equalf(t, tt.want, x.CloneToBMap(), "CloneToBMap()") 682 }) 683 } 684 } 685 686 func TestUnsafeAnyBMap_CopyByMap(t *testing.T) { 687 type fields struct { 688 mp map[int]int 689 } 690 type args struct { 691 dst map[int]int 692 } 693 tests := []struct { 694 name string 695 fields fields 696 args args 697 }{ 698 { 699 name: "nil", 700 fields: fields{ 701 mp: nil, 702 }, 703 args: args{ 704 dst: map[int]int{}, 705 }, 706 }, 707 { 708 name: "", 709 fields: fields{ 710 mp: map[int]int{}, 711 }, 712 args: args{ 713 dst: map[int]int{}, 714 }, 715 }, 716 { 717 name: "", 718 fields: fields{ 719 mp: map[int]int{1: 1}, 720 }, 721 args: args{ 722 dst: map[int]int{1: 1}, 723 }, 724 }, 725 } 726 for _, tt := range tests { 727 t.Run(tt.name, func(t *testing.T) { 728 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 729 x.CopyByMap(tt.args.dst) 730 assert.Equalf(t, tt.args.dst, x.ToMetaMap(), "CopyByMap()") 731 }) 732 } 733 } 734 735 func TestUnsafeAnyBMap_CopyByBMap(t *testing.T) { 736 type fields struct { 737 mp map[int]int 738 } 739 type args struct { 740 dst AnyBMap[int, int] 741 } 742 tests := []struct { 743 name string 744 fields fields 745 args args 746 }{ 747 { 748 name: "nil", 749 fields: fields{ 750 mp: nil, 751 }, 752 args: args{ 753 dst: NewUnsafeAnyBMapByMap[int, int](nil), 754 }, 755 }, 756 { 757 name: "", 758 fields: fields{ 759 mp: map[int]int{}, 760 }, 761 args: args{ 762 dst: NewUnsafeAnyBMapByMap(map[int]int{}), 763 }, 764 }, 765 { 766 name: "", 767 fields: fields{ 768 mp: map[int]int{1: 1}, 769 }, 770 args: args{ 771 dst: NewUnsafeAnyBMapByMap(map[int]int{1: 1}), 772 }, 773 }, 774 } 775 for _, tt := range tests { 776 t.Run(tt.name, func(t *testing.T) { 777 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 778 x.CopyByBMap(tt.args.dst) 779 assert.Equalf(t, tt.args.dst, x, "CopyByBMap()") 780 }) 781 } 782 } 783 784 func TestUnsafeAnyBMap_DeleteFunc(t *testing.T) { 785 type fields struct { 786 mp map[int]int 787 } 788 type args struct { 789 del func(int, int) bool 790 } 791 tests := []struct { 792 name string 793 fields fields 794 args args 795 want map[int]int 796 }{ 797 { 798 name: "nil", 799 fields: fields{ 800 mp: nil, 801 }, 802 args: args{ 803 del: func(i int, i2 int) bool { 804 return true 805 }, 806 }, 807 want: map[int]int{}, 808 }, 809 { 810 name: "", 811 fields: fields{ 812 mp: map[int]int{}, 813 }, 814 args: args{ 815 del: func(i int, i2 int) bool { 816 return true 817 }, 818 }, 819 want: map[int]int{}, 820 }, 821 { 822 name: "", 823 fields: fields{ 824 mp: map[int]int{1: 1, 2: 2, 3: 3}, 825 }, 826 args: args{ 827 del: func(i int, i2 int) bool { 828 return true 829 }, 830 }, 831 want: map[int]int{}, 832 }, 833 { 834 name: "", 835 fields: fields{ 836 mp: map[int]int{1: 1, 2: 2, 3: 3}, 837 }, 838 args: args{ 839 del: func(i int, i2 int) bool { 840 return i == 1 841 }, 842 }, 843 want: map[int]int{2: 2, 3: 3}, 844 }, 845 { 846 name: "", 847 fields: fields{ 848 mp: map[int]int{1: 1, 2: 2, 3: 3}, 849 }, 850 args: args{ 851 del: func(i int, i2 int) bool { 852 return i2 == 1 853 }, 854 }, 855 want: map[int]int{2: 2, 3: 3}, 856 }, 857 } 858 for _, tt := range tests { 859 t.Run(tt.name, func(t *testing.T) { 860 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 861 x.DeleteFunc(tt.args.del) 862 assert.Equalf(t, tt.want, x.ToMetaMap(), "DeleteFunc()") 863 }) 864 } 865 } 866 867 func TestUnsafeAnyBMap_Marshal(t *testing.T) { 868 type fields struct { 869 mp map[int]int 870 } 871 tests := []struct { 872 name string 873 fields fields 874 want []byte 875 }{ 876 { 877 name: "nil", 878 fields: fields{ 879 mp: nil, 880 }, 881 want: []byte("{}"), 882 }, 883 { 884 name: "", 885 fields: fields{ 886 mp: map[int]int{}, 887 }, 888 want: []byte("{}"), 889 }, 890 { 891 name: "", 892 fields: fields{ 893 mp: map[int]int{1: 1, 2: 2}, 894 }, 895 want: []byte("{\"1\":1,\"2\":2}"), 896 }, 897 } 898 for _, tt := range tests { 899 t.Run(tt.name, func(t *testing.T) { 900 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 901 got, err := x.Marshal() 902 assert.NoError(t, err) 903 assert.Equalf(t, tt.want, got, "Marshal()") 904 }) 905 } 906 } 907 908 func TestUnsafeAnyBMap_Unmarshal(t *testing.T) { 909 type fields struct { 910 mp map[int]int 911 } 912 type args struct { 913 data []byte 914 } 915 tests := []struct { 916 name string 917 fields fields 918 args args 919 want map[int]int 920 }{ 921 { 922 name: "", 923 fields: fields{ 924 mp: nil, 925 }, 926 args: args{ 927 data: []byte("{}"), 928 }, 929 want: map[int]int{}, 930 }, 931 { 932 name: "", 933 fields: fields{ 934 mp: nil, 935 }, 936 args: args{ 937 data: []byte("{\"1\":1,\"2\":2}"), 938 }, 939 want: map[int]int{1: 1, 2: 2}, 940 }, 941 { 942 name: "", 943 fields: fields{ 944 mp: map[int]int{}, 945 }, 946 args: args{ 947 data: []byte("{\"1\":1,\"2\":2}"), 948 }, 949 want: map[int]int{1: 1, 2: 2}, 950 }, 951 } 952 for _, tt := range tests { 953 t.Run(tt.name, func(t *testing.T) { 954 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 955 err := x.Unmarshal(tt.args.data) 956 assert.NoError(t, err) 957 assert.Equalf(t, tt.want, x.ToMetaMap(), "Unmarshal()") 958 }) 959 } 960 } 961 962 func TestUnsafeAnyBMap_Size(t *testing.T) { 963 type fields struct { 964 mp map[int]int 965 } 966 tests := []struct { 967 name string 968 fields fields 969 want int 970 }{ 971 { 972 name: "nil", 973 fields: fields{ 974 mp: nil, 975 }, 976 want: 0, 977 }, 978 { 979 name: "", 980 fields: fields{ 981 mp: map[int]int{}, 982 }, 983 want: 0, 984 }, 985 { 986 name: "", 987 fields: fields{ 988 mp: map[int]int{1: 1}, 989 }, 990 want: 1, 991 }, 992 { 993 name: "", 994 fields: fields{ 995 mp: map[int]int{1: 1, 2: 2}, 996 }, 997 want: 2, 998 }, 999 } 1000 for _, tt := range tests { 1001 t.Run(tt.name, func(t *testing.T) { 1002 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1003 assert.Equalf(t, tt.want, x.Size(), "Size()") 1004 }) 1005 } 1006 } 1007 1008 func TestUnsafeAnyBMap_IsEmpty(t *testing.T) { 1009 type fields struct { 1010 mp map[int]int 1011 } 1012 tests := []struct { 1013 name string 1014 fields fields 1015 want bool 1016 }{ 1017 { 1018 name: "nil", 1019 fields: fields{ 1020 mp: nil, 1021 }, 1022 want: true, 1023 }, 1024 { 1025 name: "", 1026 fields: fields{ 1027 mp: map[int]int{}, 1028 }, 1029 want: true, 1030 }, 1031 { 1032 name: "", 1033 fields: fields{ 1034 mp: map[int]int{1: 1}, 1035 }, 1036 want: false, 1037 }, 1038 { 1039 name: "", 1040 fields: fields{ 1041 mp: map[int]int{1: 1, 2: 2}, 1042 }, 1043 want: false, 1044 }, 1045 } 1046 for _, tt := range tests { 1047 t.Run(tt.name, func(t *testing.T) { 1048 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1049 assert.Equalf(t, tt.want, x.IsEmpty(), "IsEmpty()") 1050 }) 1051 } 1052 } 1053 1054 func TestUnsafeAnyBMap_IsExist(t *testing.T) { 1055 type fields struct { 1056 mp map[int]int 1057 } 1058 type args struct { 1059 k int 1060 } 1061 tests := []struct { 1062 name string 1063 fields fields 1064 args args 1065 want bool 1066 }{ 1067 { 1068 name: "nil", 1069 fields: fields{ 1070 mp: nil, 1071 }, 1072 args: args{ 1073 k: 0, 1074 }, 1075 want: false, 1076 }, 1077 { 1078 name: "", 1079 fields: fields{ 1080 mp: map[int]int{}, 1081 }, 1082 args: args{ 1083 k: 0, 1084 }, 1085 want: false, 1086 }, 1087 { 1088 name: "", 1089 fields: fields{ 1090 mp: map[int]int{1: 1}, 1091 }, 1092 args: args{ 1093 k: 0, 1094 }, 1095 want: false, 1096 }, 1097 { 1098 name: "", 1099 fields: fields{ 1100 mp: map[int]int{1: 1}, 1101 }, 1102 args: args{ 1103 k: 1, 1104 }, 1105 want: true, 1106 }, 1107 } 1108 for _, tt := range tests { 1109 t.Run(tt.name, func(t *testing.T) { 1110 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1111 assert.Equalf(t, tt.want, x.IsExist(tt.args.k), "IsExist(%v)", tt.args.k) 1112 }) 1113 } 1114 } 1115 1116 func TestUnsafeAnyBMap_ContainsKey(t *testing.T) { 1117 type fields struct { 1118 mp map[int]int 1119 } 1120 type args struct { 1121 k int 1122 } 1123 tests := []struct { 1124 name string 1125 fields fields 1126 args args 1127 want bool 1128 }{ 1129 { 1130 name: "nil", 1131 fields: fields{ 1132 mp: nil, 1133 }, 1134 args: args{ 1135 k: 0, 1136 }, 1137 want: false, 1138 }, 1139 { 1140 name: "", 1141 fields: fields{ 1142 mp: map[int]int{}, 1143 }, 1144 args: args{ 1145 k: 0, 1146 }, 1147 want: false, 1148 }, 1149 { 1150 name: "", 1151 fields: fields{ 1152 mp: map[int]int{1: 1}, 1153 }, 1154 args: args{ 1155 k: 0, 1156 }, 1157 want: false, 1158 }, 1159 { 1160 name: "", 1161 fields: fields{ 1162 mp: map[int]int{1: 1}, 1163 }, 1164 args: args{ 1165 k: 1, 1166 }, 1167 want: true, 1168 }, 1169 } 1170 for _, tt := range tests { 1171 t.Run(tt.name, func(t *testing.T) { 1172 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1173 assert.Equalf(t, tt.want, x.ContainsKey(tt.args.k), "ContainsKey(%v)", tt.args.k) 1174 }) 1175 } 1176 } 1177 1178 func TestUnsafeAnyBMap_ContainsValue(t *testing.T) { 1179 type fields struct { 1180 mp map[int]tInterface 1181 } 1182 type args struct { 1183 v tInterface 1184 } 1185 tests := []struct { 1186 name string 1187 fields fields 1188 args args 1189 want bool 1190 }{ 1191 { 1192 name: "nil", 1193 fields: fields{ 1194 mp: nil, 1195 }, 1196 args: args{ 1197 v: nil, 1198 }, 1199 want: false, 1200 }, 1201 { 1202 name: "", 1203 fields: fields{ 1204 mp: map[int]tInterface{}, 1205 }, 1206 args: args{ 1207 v: nil, 1208 }, 1209 want: false, 1210 }, 1211 { 1212 name: "", 1213 fields: fields{ 1214 mp: map[int]tInterface{1: nil}, 1215 }, 1216 args: args{ 1217 v: nil, 1218 }, 1219 want: true, 1220 }, 1221 { 1222 name: "", 1223 fields: fields{ 1224 mp: map[int]tInterface{1: nil}, 1225 }, 1226 args: args{ 1227 v: &tStruct{ 1228 t: nil, 1229 }, 1230 }, 1231 want: false, 1232 }, 1233 { 1234 name: "", 1235 fields: fields{ 1236 mp: map[int]tInterface{1: nil}, 1237 }, 1238 args: args{ 1239 v: &tStruct{ 1240 t: 1, 1241 }, 1242 }, 1243 want: false, 1244 }, 1245 { 1246 name: "", 1247 fields: fields{ 1248 mp: map[int]tInterface{1: nil, 2: &tStruct{ 1249 t: 1, 1250 }}, 1251 }, 1252 args: args{ 1253 v: &tStruct{ 1254 t: 1, 1255 }, 1256 }, 1257 want: true, 1258 }, 1259 } 1260 for _, tt := range tests { 1261 t.Run(tt.name, func(t *testing.T) { 1262 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1263 assert.Equalf(t, tt.want, x.ContainsValue(tt.args.v), "ContainsValue(%v)", tt.args.v) 1264 }) 1265 } 1266 } 1267 1268 func TestUnsafeAnyBMap_Get(t *testing.T) { 1269 type fields struct { 1270 mp map[int]int 1271 } 1272 type args struct { 1273 k int 1274 } 1275 tests := []struct { 1276 name string 1277 fields fields 1278 args args 1279 want int 1280 want1 bool 1281 }{ 1282 { 1283 name: "nil", 1284 fields: fields{ 1285 mp: nil, 1286 }, 1287 args: args{ 1288 k: 1, 1289 }, 1290 want: 0, 1291 want1: false, 1292 }, 1293 { 1294 name: "", 1295 fields: fields{ 1296 mp: map[int]int{}, 1297 }, 1298 args: args{ 1299 k: 1, 1300 }, 1301 want: 0, 1302 want1: false, 1303 }, 1304 { 1305 name: "", 1306 fields: fields{ 1307 mp: map[int]int{1: 1, 2: 2}, 1308 }, 1309 args: args{ 1310 k: 1, 1311 }, 1312 want: 1, 1313 want1: true, 1314 }, 1315 { 1316 name: "", 1317 fields: fields{ 1318 mp: map[int]int{1: 1, 2: 2}, 1319 }, 1320 args: args{ 1321 k: 3, 1322 }, 1323 want: 0, 1324 want1: false, 1325 }, 1326 } 1327 for _, tt := range tests { 1328 t.Run(tt.name, func(t *testing.T) { 1329 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1330 got, got1 := x.Get(tt.args.k) 1331 assert.Equalf(t, tt.want, got, "Get(%v)", tt.args.k) 1332 assert.Equalf(t, tt.want1, got1, "Get(%v)", tt.args.k) 1333 }) 1334 } 1335 } 1336 1337 func TestUnsafeAnyBMap_GetOrDefault(t *testing.T) { 1338 type fields struct { 1339 mp map[int]int 1340 } 1341 type args struct { 1342 k int 1343 defaultValue int 1344 } 1345 tests := []struct { 1346 name string 1347 fields fields 1348 args args 1349 want int 1350 }{ 1351 { 1352 name: "nil", 1353 fields: fields{ 1354 mp: nil, 1355 }, 1356 args: args{ 1357 k: 0, 1358 defaultValue: 1, 1359 }, 1360 want: 1, 1361 }, 1362 { 1363 name: "", 1364 fields: fields{ 1365 mp: map[int]int{}, 1366 }, 1367 args: args{ 1368 k: 0, 1369 defaultValue: 1, 1370 }, 1371 want: 1, 1372 }, 1373 { 1374 name: "", 1375 fields: fields{ 1376 mp: map[int]int{1: 1}, 1377 }, 1378 args: args{ 1379 k: 1, 1380 defaultValue: 2, 1381 }, 1382 want: 1, 1383 }, 1384 } 1385 for _, tt := range tests { 1386 t.Run(tt.name, func(t *testing.T) { 1387 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1388 assert.Equalf(t, tt.want, x.GetOrDefault(tt.args.k, tt.args.defaultValue), "GetOrDefault(%v, %v)", tt.args.k, tt.args.defaultValue) 1389 }) 1390 } 1391 } 1392 1393 func TestUnsafeAnyBMap_Put(t *testing.T) { 1394 type fields struct { 1395 mp map[int]int 1396 } 1397 type args struct { 1398 k int 1399 v int 1400 } 1401 tests := []struct { 1402 name string 1403 fields fields 1404 args args 1405 }{ 1406 { 1407 name: "", 1408 fields: fields{ 1409 mp: nil, 1410 }, 1411 args: args{ 1412 k: 1, 1413 v: 1, 1414 }, 1415 }, 1416 { 1417 name: "", 1418 fields: fields{ 1419 mp: map[int]int{}, 1420 }, 1421 args: args{ 1422 k: 2, 1423 v: 2, 1424 }, 1425 }, 1426 } 1427 for _, tt := range tests { 1428 t.Run(tt.name, func(t *testing.T) { 1429 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1430 x.Put(tt.args.k, tt.args.v) 1431 v, ok := x.Get(tt.args.k) 1432 assert.Equal(t, true, ok) 1433 assert.Equal(t, tt.args.v, v) 1434 }) 1435 } 1436 } 1437 1438 func TestUnsafeAnyBMap_PuTIfAbsent(t *testing.T) { 1439 type fields struct { 1440 mp map[int]int 1441 } 1442 type args struct { 1443 k int 1444 v int 1445 } 1446 tests := []struct { 1447 name string 1448 fields fields 1449 args args 1450 want bool 1451 }{ 1452 { 1453 name: "nil", 1454 fields: fields{}, 1455 args: args{ 1456 k: 1, 1457 v: 1, 1458 }, 1459 want: true, 1460 }, 1461 { 1462 name: "", 1463 fields: fields{ 1464 mp: map[int]int{}, 1465 }, 1466 args: args{ 1467 k: 1, 1468 v: 1, 1469 }, 1470 want: true, 1471 }, 1472 { 1473 name: "", 1474 fields: fields{ 1475 mp: map[int]int{1: 2}, 1476 }, 1477 args: args{ 1478 k: 1, 1479 v: 1, 1480 }, 1481 want: false, 1482 }, 1483 } 1484 for _, tt := range tests { 1485 t.Run(tt.name, func(t *testing.T) { 1486 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1487 assert.Equalf(t, tt.want, x.PuTIfAbsent(tt.args.k, tt.args.v), "PuTIfAbsent(%v, %v)", tt.args.k, tt.args.v) 1488 }) 1489 } 1490 } 1491 1492 func TestUnsafeAnyBMap_Delete(t *testing.T) { 1493 type fields struct { 1494 mp map[int]int 1495 } 1496 type args struct { 1497 k int 1498 } 1499 tests := []struct { 1500 name string 1501 fields fields 1502 args args 1503 }{ 1504 { 1505 name: "nil", 1506 fields: fields{ 1507 mp: nil, 1508 }, 1509 args: args{ 1510 k: 0, 1511 }, 1512 }, 1513 { 1514 name: "", 1515 fields: fields{ 1516 mp: map[int]int{}, 1517 }, 1518 args: args{ 1519 k: 0, 1520 }, 1521 }, 1522 { 1523 name: "", 1524 fields: fields{ 1525 mp: map[int]int{1: 1, 2: 2}, 1526 }, 1527 args: args{ 1528 k: 1, 1529 }, 1530 }, 1531 } 1532 for _, tt := range tests { 1533 t.Run(tt.name, func(t *testing.T) { 1534 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1535 x.Delete(tt.args.k) 1536 _, ok := x.Get(tt.args.k) 1537 assert.Equal(t, false, ok) 1538 }) 1539 } 1540 } 1541 1542 func TestUnsafeAnyBMap_DeleteIfPresent(t *testing.T) { 1543 type fields struct { 1544 mp map[int]int 1545 } 1546 type args struct { 1547 k int 1548 } 1549 tests := []struct { 1550 name string 1551 fields fields 1552 args args 1553 want int 1554 want1 bool 1555 }{ 1556 { 1557 name: "nil", 1558 fields: fields{ 1559 mp: nil, 1560 }, 1561 args: args{ 1562 k: 0, 1563 }, 1564 want: 0, 1565 want1: false, 1566 }, 1567 { 1568 name: "", 1569 fields: fields{ 1570 mp: map[int]int{}, 1571 }, 1572 args: args{ 1573 k: 0, 1574 }, 1575 want: 0, 1576 want1: false, 1577 }, 1578 { 1579 name: "", 1580 fields: fields{ 1581 mp: map[int]int{1: 1, 2: 2}, 1582 }, 1583 args: args{ 1584 k: 1, 1585 }, 1586 want: 1, 1587 want1: true, 1588 }, 1589 } 1590 for _, tt := range tests { 1591 t.Run(tt.name, func(t *testing.T) { 1592 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1593 got, got1 := x.DeleteIfPresent(tt.args.k) 1594 assert.Equalf(t, tt.want, got, "DeleteIfPresent(%v)", tt.args.k) 1595 assert.Equalf(t, tt.want1, got1, "DeleteIfPresent(%v)", tt.args.k) 1596 _, ok := x.Get(tt.args.k) 1597 assert.Equal(t, false, ok) 1598 }) 1599 } 1600 } 1601 1602 func TestUnsafeAnyBMap_MergeByMap(t *testing.T) { 1603 type fields struct { 1604 mp map[int]int 1605 } 1606 type args struct { 1607 m map[int]int 1608 f func(int, int) bool 1609 } 1610 tests := []struct { 1611 name string 1612 fields fields 1613 args args 1614 want map[int]int 1615 }{ 1616 { 1617 name: "nil", 1618 fields: fields{ 1619 mp: nil, 1620 }, 1621 args: args{ 1622 m: nil, 1623 f: nil, 1624 }, 1625 want: map[int]int{}, 1626 }, 1627 { 1628 name: "nil", 1629 fields: fields{ 1630 mp: nil, 1631 }, 1632 args: args{ 1633 m: map[int]int{}, 1634 f: nil, 1635 }, 1636 want: map[int]int{}, 1637 }, 1638 { 1639 name: "nil", 1640 fields: fields{ 1641 mp: nil, 1642 }, 1643 args: args{ 1644 m: map[int]int{1: 1}, 1645 f: nil, 1646 }, 1647 want: map[int]int{1: 1}, 1648 }, 1649 { 1650 name: "nil", 1651 fields: fields{ 1652 mp: map[int]int{1: 1}, 1653 }, 1654 args: args{ 1655 m: map[int]int{1: 2}, 1656 f: nil, 1657 }, 1658 want: map[int]int{1: 1}, 1659 }, 1660 { 1661 name: "", 1662 fields: fields{ 1663 mp: map[int]int{1: 1}, 1664 }, 1665 args: args{ 1666 m: map[int]int{1: 2}, 1667 f: func(i int, i2 int) bool { 1668 return true 1669 }, 1670 }, 1671 want: map[int]int{1: 2}, 1672 }, 1673 { 1674 name: "", 1675 fields: fields{ 1676 mp: map[int]int{1: 1}, 1677 }, 1678 args: args{ 1679 m: map[int]int{1: 2}, 1680 f: func(i int, i2 int) bool { 1681 return false 1682 }, 1683 }, 1684 want: map[int]int{1: 1}, 1685 }, 1686 { 1687 name: "", 1688 fields: fields{ 1689 mp: map[int]int{1: 1, 2: 2, 3: 3}, 1690 }, 1691 args: args{ 1692 m: map[int]int{3: 4, 4: 4}, 1693 f: func(i int, i2 int) bool { 1694 return false 1695 }, 1696 }, 1697 want: map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, 1698 }, 1699 { 1700 name: "", 1701 fields: fields{ 1702 mp: map[int]int{1: 1, 2: 2, 3: 3}, 1703 }, 1704 args: args{ 1705 m: map[int]int{3: 4, 4: 4}, 1706 f: func(i int, i2 int) bool { 1707 return true 1708 }, 1709 }, 1710 want: map[int]int{1: 1, 2: 2, 3: 4, 4: 4}, 1711 }, 1712 } 1713 for _, tt := range tests { 1714 t.Run(tt.name, func(t *testing.T) { 1715 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1716 x.MergeByMap(tt.args.m, tt.args.f) 1717 assert.Equal(t, tt.want, x.ToMetaMap()) 1718 }) 1719 } 1720 } 1721 1722 func TestUnsafeAnyBMap_MergeByBMap(t *testing.T) { 1723 type fields struct { 1724 mp map[int]int 1725 } 1726 type args struct { 1727 m map[int]int 1728 f func(int, int) bool 1729 } 1730 tests := []struct { 1731 name string 1732 fields fields 1733 args args 1734 want map[int]int 1735 }{ 1736 { 1737 name: "nil", 1738 fields: fields{ 1739 mp: nil, 1740 }, 1741 args: args{ 1742 m: nil, 1743 f: nil, 1744 }, 1745 want: map[int]int{}, 1746 }, 1747 { 1748 name: "nil", 1749 fields: fields{ 1750 mp: nil, 1751 }, 1752 args: args{ 1753 m: map[int]int{}, 1754 f: nil, 1755 }, 1756 want: map[int]int{}, 1757 }, 1758 { 1759 name: "nil", 1760 fields: fields{ 1761 mp: nil, 1762 }, 1763 args: args{ 1764 m: map[int]int{1: 1}, 1765 f: nil, 1766 }, 1767 want: map[int]int{1: 1}, 1768 }, 1769 { 1770 name: "nil", 1771 fields: fields{ 1772 mp: map[int]int{1: 1}, 1773 }, 1774 args: args{ 1775 m: map[int]int{1: 2}, 1776 f: nil, 1777 }, 1778 want: map[int]int{1: 1}, 1779 }, 1780 { 1781 name: "", 1782 fields: fields{ 1783 mp: map[int]int{1: 1}, 1784 }, 1785 args: args{ 1786 m: map[int]int{1: 2}, 1787 f: func(i int, i2 int) bool { 1788 return true 1789 }, 1790 }, 1791 want: map[int]int{1: 2}, 1792 }, 1793 { 1794 name: "", 1795 fields: fields{ 1796 mp: map[int]int{1: 1}, 1797 }, 1798 args: args{ 1799 m: map[int]int{1: 2}, 1800 f: func(i int, i2 int) bool { 1801 return false 1802 }, 1803 }, 1804 want: map[int]int{1: 1}, 1805 }, 1806 { 1807 name: "", 1808 fields: fields{ 1809 mp: map[int]int{1: 1, 2: 2, 3: 3}, 1810 }, 1811 args: args{ 1812 m: map[int]int{3: 4, 4: 4}, 1813 f: func(i int, i2 int) bool { 1814 return false 1815 }, 1816 }, 1817 want: map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, 1818 }, 1819 { 1820 name: "", 1821 fields: fields{ 1822 mp: map[int]int{1: 1, 2: 2, 3: 3}, 1823 }, 1824 args: args{ 1825 m: map[int]int{3: 4, 4: 4}, 1826 f: func(i int, i2 int) bool { 1827 return true 1828 }, 1829 }, 1830 want: map[int]int{1: 1, 2: 2, 3: 4, 4: 4}, 1831 }, 1832 } 1833 for _, tt := range tests { 1834 t.Run(tt.name, func(t *testing.T) { 1835 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1836 x.MergeByBMap(NewUnsafeAnyBMapByMap(tt.args.m), tt.args.f) 1837 assert.Equal(t, tt.want, x.ToMetaMap()) 1838 }) 1839 } 1840 } 1841 1842 func TestUnsafeAnyBMap_Replace(t *testing.T) { 1843 type fields struct { 1844 mp map[int]int 1845 } 1846 type args struct { 1847 k int 1848 ov int 1849 nv int 1850 } 1851 tests := []struct { 1852 name string 1853 fields fields 1854 args args 1855 want bool 1856 }{ 1857 { 1858 name: "nil", 1859 fields: fields{ 1860 mp: nil, 1861 }, 1862 args: args{ 1863 k: 0, 1864 ov: 0, 1865 nv: 0, 1866 }, 1867 want: false, 1868 }, 1869 { 1870 name: "", 1871 fields: fields{ 1872 mp: map[int]int{}, 1873 }, 1874 args: args{ 1875 k: 0, 1876 ov: 0, 1877 nv: 0, 1878 }, 1879 want: false, 1880 }, 1881 { 1882 name: "nil", 1883 fields: fields{ 1884 mp: map[int]int{1: 1}, 1885 }, 1886 args: args{ 1887 k: 1, 1888 ov: 0, 1889 nv: 2, 1890 }, 1891 want: false, 1892 }, 1893 { 1894 name: "nil", 1895 fields: fields{ 1896 mp: map[int]int{1: 1}, 1897 }, 1898 args: args{ 1899 k: 1, 1900 ov: 1, 1901 nv: 2, 1902 }, 1903 want: true, 1904 }, 1905 } 1906 for _, tt := range tests { 1907 t.Run(tt.name, func(t *testing.T) { 1908 x := NewUnsafeAnyBMapByMap(tt.fields.mp) 1909 assert.Equalf(t, tt.want, x.Replace(tt.args.k, tt.args.ov, tt.args.nv), "Replace(%v, %v, %v)", tt.args.k, tt.args.ov, tt.args.nv) 1910 }) 1911 } 1912 } 1913 1914 func TestSafeAnyBMap_ToMetaMap(t *testing.T) { 1915 type fields struct { 1916 mp map[int]int 1917 } 1918 tests := []struct { 1919 name string 1920 fields fields 1921 want map[int]int 1922 }{ 1923 { 1924 name: "nil", 1925 fields: fields{ 1926 mp: nil, 1927 }, 1928 want: map[int]int{}, 1929 }, 1930 { 1931 name: "", 1932 fields: fields{ 1933 mp: map[int]int{1: 1, 2: 2, 3: 3}, 1934 }, 1935 want: map[int]int{1: 1, 2: 2, 3: 3}, 1936 }, 1937 } 1938 for _, tt := range tests { 1939 t.Run(tt.name, func(t *testing.T) { 1940 x := NewSafeAnyBMapByMap(tt.fields.mp) 1941 assert.Equalf(t, tt.want, x.ToMetaMap(), "ToMetaMap()") 1942 }) 1943 } 1944 } 1945 1946 func TestSafeAnyBMap_Keys(t *testing.T) { 1947 type fields struct { 1948 mp map[int]int 1949 } 1950 tests := []struct { 1951 name string 1952 fields fields 1953 want []int 1954 }{ 1955 { 1956 name: "nil", 1957 fields: fields{ 1958 mp: nil, 1959 }, 1960 want: []int{}, 1961 }, 1962 { 1963 name: "", 1964 fields: fields{ 1965 mp: map[int]int{1: 1, 2: 2, 3: 3}, 1966 }, 1967 want: []int{1, 2, 3}, 1968 }, 1969 } 1970 for _, tt := range tests { 1971 t.Run(tt.name, func(t *testing.T) { 1972 x := NewSafeAnyBMapByMap(tt.fields.mp) 1973 v := x.Keys() 1974 sort.Ints(v) 1975 assert.Equalf(t, tt.want, v, "Keys()") 1976 }) 1977 } 1978 } 1979 1980 func TestSafeAnyBMap_Values(t *testing.T) { 1981 type fields struct { 1982 mp map[int]int 1983 } 1984 tests := []struct { 1985 name string 1986 fields fields 1987 want []int 1988 }{ 1989 { 1990 name: "nil", 1991 fields: fields{ 1992 mp: nil, 1993 }, 1994 want: []int{}, 1995 }, 1996 { 1997 name: "", 1998 fields: fields{ 1999 mp: map[int]int{1: 1, 2: 2, 3: 3}, 2000 }, 2001 want: []int{1, 2, 3}, 2002 }, 2003 } 2004 for _, tt := range tests { 2005 t.Run(tt.name, func(t *testing.T) { 2006 x := NewSafeAnyBMapByMap(tt.fields.mp) 2007 v := x.Values() 2008 sort.Ints(v) 2009 assert.Equalf(t, tt.want, v, "Values()") 2010 }) 2011 } 2012 } 2013 2014 func TestSafeAnyBMap_EqualFuncByMap(t *testing.T) { 2015 type fields struct { 2016 mp map[int]int 2017 } 2018 eq := func(v1, v2 int) bool { 2019 return v1 == v2 2020 } 2021 type args struct { 2022 m map[int]int 2023 } 2024 tests := []struct { 2025 name string 2026 fields fields 2027 args args 2028 want bool 2029 }{ 2030 { 2031 name: "nil", 2032 fields: fields{ 2033 mp: nil, 2034 }, 2035 args: args{ 2036 m: nil, 2037 }, 2038 want: true, 2039 }, 2040 { 2041 name: "", 2042 fields: fields{ 2043 mp: nil, 2044 }, 2045 args: args{ 2046 m: map[int]int{}, 2047 }, 2048 want: true, 2049 }, 2050 { 2051 name: "", 2052 fields: fields{ 2053 mp: nil, 2054 }, 2055 args: args{ 2056 m: map[int]int{1: 1}, 2057 }, 2058 want: false, 2059 }, 2060 { 2061 name: "", 2062 fields: fields{ 2063 mp: map[int]int{1: 1}, 2064 }, 2065 args: args{ 2066 m: nil, 2067 }, 2068 want: false, 2069 }, 2070 { 2071 name: "", 2072 fields: fields{ 2073 mp: map[int]int{1: 1}, 2074 }, 2075 args: args{ 2076 m: map[int]int{1: 1}, 2077 }, 2078 want: true, 2079 }, 2080 { 2081 name: "", 2082 fields: fields{ 2083 mp: map[int]int{1: 1}, 2084 }, 2085 args: args{ 2086 m: map[int]int{1: 1, 2: 2}, 2087 }, 2088 want: false, 2089 }, 2090 { 2091 name: "", 2092 fields: fields{ 2093 mp: map[int]int{1: 1, 2: 2}, 2094 }, 2095 args: args{ 2096 m: map[int]int{1: 1}, 2097 }, 2098 want: false, 2099 }, 2100 } 2101 for _, tt := range tests { 2102 t.Run(tt.name, func(t *testing.T) { 2103 x := NewSafeAnyBMapByMap(tt.fields.mp) 2104 assert.Equalf(t, tt.want, x.EqualFuncByMap(tt.args.m, eq), "EqualFuncByMap(%v)", tt.args.m) 2105 }) 2106 } 2107 } 2108 2109 func TestSafeAnyBMap_EqualFuncByBMap(t *testing.T) { 2110 type fields struct { 2111 mp map[int]int 2112 } 2113 eq := func(v1, v2 int) bool { 2114 return v1 == v2 2115 } 2116 type args struct { 2117 m map[int]int 2118 } 2119 tests := []struct { 2120 name string 2121 fields fields 2122 args args 2123 want bool 2124 }{ 2125 { 2126 name: "nil", 2127 fields: fields{ 2128 mp: nil, 2129 }, 2130 args: args{ 2131 m: nil, 2132 }, 2133 want: true, 2134 }, 2135 { 2136 name: "", 2137 fields: fields{ 2138 mp: nil, 2139 }, 2140 args: args{ 2141 m: map[int]int{}, 2142 }, 2143 want: true, 2144 }, 2145 { 2146 name: "", 2147 fields: fields{ 2148 mp: nil, 2149 }, 2150 args: args{ 2151 m: map[int]int{1: 1}, 2152 }, 2153 want: false, 2154 }, 2155 { 2156 name: "", 2157 fields: fields{ 2158 mp: map[int]int{1: 1}, 2159 }, 2160 args: args{ 2161 m: nil, 2162 }, 2163 want: false, 2164 }, 2165 { 2166 name: "", 2167 fields: fields{ 2168 mp: map[int]int{1: 1}, 2169 }, 2170 args: args{ 2171 m: map[int]int{1: 1}, 2172 }, 2173 want: true, 2174 }, 2175 { 2176 name: "", 2177 fields: fields{ 2178 mp: map[int]int{1: 1}, 2179 }, 2180 args: args{ 2181 m: map[int]int{1: 1, 2: 2}, 2182 }, 2183 want: false, 2184 }, 2185 { 2186 name: "", 2187 fields: fields{ 2188 mp: map[int]int{1: 1, 2: 2}, 2189 }, 2190 args: args{ 2191 m: map[int]int{1: 1}, 2192 }, 2193 want: false, 2194 }, 2195 } 2196 for _, tt := range tests { 2197 t.Run(tt.name, func(t *testing.T) { 2198 x := NewSafeAnyBMapByMap(tt.fields.mp) 2199 assert.Equalf(t, tt.want, x.EqualFuncByBMap(NewSafeAnyBMapByMap(tt.args.m), eq), "EqualFuncByBMap(%v)", tt.args.m) 2200 }) 2201 } 2202 } 2203 2204 func TestSafeAnyBMap_Clear(t *testing.T) { 2205 type fields struct { 2206 mp map[int]int 2207 } 2208 tests := []struct { 2209 name string 2210 fields fields 2211 }{ 2212 { 2213 name: "nil", 2214 fields: fields{ 2215 mp: nil, 2216 }, 2217 }, 2218 { 2219 name: "", 2220 fields: fields{ 2221 mp: map[int]int{}, 2222 }, 2223 }, 2224 { 2225 name: "", 2226 fields: fields{ 2227 mp: map[int]int{1: 1}, 2228 }, 2229 }, 2230 } 2231 for _, tt := range tests { 2232 t.Run(tt.name, func(t *testing.T) { 2233 x := NewSafeAnyBMapByMap(tt.fields.mp) 2234 x.Clear() 2235 assert.Equalf(t, map[int]int{}, x.ToMetaMap(), "Clear()") 2236 }) 2237 } 2238 } 2239 2240 func TestSafeAnyBMap_CloneToMap(t *testing.T) { 2241 type fields struct { 2242 mp map[int]int 2243 } 2244 tests := []struct { 2245 name string 2246 fields fields 2247 want map[int]int 2248 }{ 2249 { 2250 name: "nil", 2251 fields: fields{ 2252 mp: nil, 2253 }, 2254 want: map[int]int{}, 2255 }, 2256 { 2257 name: "", 2258 fields: fields{ 2259 mp: map[int]int{}, 2260 }, 2261 want: map[int]int{}, 2262 }, 2263 { 2264 name: "", 2265 fields: fields{ 2266 mp: map[int]int{1: 1}, 2267 }, 2268 want: map[int]int{1: 1}, 2269 }, 2270 } 2271 for _, tt := range tests { 2272 t.Run(tt.name, func(t *testing.T) { 2273 x := NewSafeAnyBMapByMap(tt.fields.mp) 2274 assert.Equalf(t, tt.want, x.CloneToMap(), "CloneToMap()") 2275 }) 2276 } 2277 } 2278 2279 func TestSafeAnyBMap_CloneToBMap(t *testing.T) { 2280 type fields struct { 2281 mp map[int]int 2282 } 2283 tests := []struct { 2284 name string 2285 fields fields 2286 want AnyBMap[int, int] 2287 }{ 2288 { 2289 name: "nil", 2290 fields: fields{ 2291 mp: nil, 2292 }, 2293 want: NewSafeAnyBMapByMap[int, int](nil), 2294 }, 2295 { 2296 name: "", 2297 fields: fields{ 2298 mp: map[int]int{}, 2299 }, 2300 want: NewSafeAnyBMapByMap[int, int](map[int]int{}), 2301 }, 2302 { 2303 name: "", 2304 fields: fields{ 2305 mp: map[int]int{1: 1}, 2306 }, 2307 want: NewSafeAnyBMapByMap[int, int](map[int]int{1: 1}), 2308 }, 2309 } 2310 for _, tt := range tests { 2311 t.Run(tt.name, func(t *testing.T) { 2312 x := NewSafeAnyBMapByMap(tt.fields.mp) 2313 assert.Equalf(t, tt.want.ToMetaMap(), x.CloneToBMap().ToMetaMap(), "CloneToBMap()") 2314 }) 2315 } 2316 } 2317 2318 func TestSafeAnyBMap_CopyByMap(t *testing.T) { 2319 type fields struct { 2320 mp map[int]int 2321 } 2322 type args struct { 2323 dst map[int]int 2324 } 2325 tests := []struct { 2326 name string 2327 fields fields 2328 args args 2329 }{ 2330 { 2331 name: "nil", 2332 fields: fields{ 2333 mp: nil, 2334 }, 2335 args: args{ 2336 dst: map[int]int{}, 2337 }, 2338 }, 2339 { 2340 name: "", 2341 fields: fields{ 2342 mp: map[int]int{}, 2343 }, 2344 args: args{ 2345 dst: map[int]int{}, 2346 }, 2347 }, 2348 { 2349 name: "", 2350 fields: fields{ 2351 mp: map[int]int{1: 1}, 2352 }, 2353 args: args{ 2354 dst: map[int]int{1: 1}, 2355 }, 2356 }, 2357 } 2358 for _, tt := range tests { 2359 t.Run(tt.name, func(t *testing.T) { 2360 x := NewSafeAnyBMapByMap(tt.fields.mp) 2361 x.CopyByMap(tt.args.dst) 2362 assert.Equalf(t, tt.args.dst, x.ToMetaMap(), "CopyByMap()") 2363 }) 2364 } 2365 } 2366 2367 func TestSafeAnyBMap_CopyByBMap(t *testing.T) { 2368 type fields struct { 2369 mp map[int]int 2370 } 2371 type args struct { 2372 dst AnyBMap[int, int] 2373 } 2374 tests := []struct { 2375 name string 2376 fields fields 2377 args args 2378 }{ 2379 { 2380 name: "nil", 2381 fields: fields{ 2382 mp: nil, 2383 }, 2384 args: args{ 2385 dst: NewSafeAnyBMapByMap[int, int](nil), 2386 }, 2387 }, 2388 { 2389 name: "", 2390 fields: fields{ 2391 mp: map[int]int{}, 2392 }, 2393 args: args{ 2394 dst: NewSafeAnyBMapByMap(map[int]int{}), 2395 }, 2396 }, 2397 { 2398 name: "", 2399 fields: fields{ 2400 mp: map[int]int{1: 1}, 2401 }, 2402 args: args{ 2403 dst: NewSafeAnyBMapByMap(map[int]int{1: 1}), 2404 }, 2405 }, 2406 } 2407 for _, tt := range tests { 2408 t.Run(tt.name, func(t *testing.T) { 2409 x := NewSafeAnyBMapByMap(tt.fields.mp) 2410 x.CopyByBMap(tt.args.dst) 2411 assert.Equalf(t, tt.args.dst, x, "CopyByBMap()") 2412 }) 2413 } 2414 } 2415 2416 func TestSafeAnyBMap_DeleteFunc(t *testing.T) { 2417 type fields struct { 2418 mp map[int]int 2419 } 2420 type args struct { 2421 del func(int, int) bool 2422 } 2423 tests := []struct { 2424 name string 2425 fields fields 2426 args args 2427 want map[int]int 2428 }{ 2429 { 2430 name: "nil", 2431 fields: fields{ 2432 mp: nil, 2433 }, 2434 args: args{ 2435 del: func(i int, i2 int) bool { 2436 return true 2437 }, 2438 }, 2439 want: map[int]int{}, 2440 }, 2441 { 2442 name: "", 2443 fields: fields{ 2444 mp: map[int]int{}, 2445 }, 2446 args: args{ 2447 del: func(i int, i2 int) bool { 2448 return true 2449 }, 2450 }, 2451 want: map[int]int{}, 2452 }, 2453 { 2454 name: "", 2455 fields: fields{ 2456 mp: map[int]int{1: 1, 2: 2, 3: 3}, 2457 }, 2458 args: args{ 2459 del: func(i int, i2 int) bool { 2460 return true 2461 }, 2462 }, 2463 want: map[int]int{}, 2464 }, 2465 { 2466 name: "", 2467 fields: fields{ 2468 mp: map[int]int{1: 1, 2: 2, 3: 3}, 2469 }, 2470 args: args{ 2471 del: func(i int, i2 int) bool { 2472 return i == 1 2473 }, 2474 }, 2475 want: map[int]int{2: 2, 3: 3}, 2476 }, 2477 { 2478 name: "", 2479 fields: fields{ 2480 mp: map[int]int{1: 1, 2: 2, 3: 3}, 2481 }, 2482 args: args{ 2483 del: func(i int, i2 int) bool { 2484 return i2 == 1 2485 }, 2486 }, 2487 want: map[int]int{2: 2, 3: 3}, 2488 }, 2489 } 2490 for _, tt := range tests { 2491 t.Run(tt.name, func(t *testing.T) { 2492 x := NewSafeAnyBMapByMap(tt.fields.mp) 2493 x.DeleteFunc(tt.args.del) 2494 assert.Equalf(t, tt.want, x.ToMetaMap(), "DeleteFunc()") 2495 }) 2496 } 2497 } 2498 2499 func TestSafeAnyBMap_Marshal(t *testing.T) { 2500 type fields struct { 2501 mp map[int]int 2502 } 2503 tests := []struct { 2504 name string 2505 fields fields 2506 want []byte 2507 }{ 2508 { 2509 name: "nil", 2510 fields: fields{ 2511 mp: nil, 2512 }, 2513 want: []byte("{}"), 2514 }, 2515 { 2516 name: "", 2517 fields: fields{ 2518 mp: map[int]int{}, 2519 }, 2520 want: []byte("{}"), 2521 }, 2522 { 2523 name: "", 2524 fields: fields{ 2525 mp: map[int]int{1: 1, 2: 2}, 2526 }, 2527 want: []byte("{\"1\":1,\"2\":2}"), 2528 }, 2529 } 2530 for _, tt := range tests { 2531 t.Run(tt.name, func(t *testing.T) { 2532 x := NewSafeAnyBMapByMap(tt.fields.mp) 2533 got, err := x.Marshal() 2534 assert.NoError(t, err) 2535 assert.Equalf(t, tt.want, got, "Marshal()") 2536 }) 2537 } 2538 } 2539 2540 func TestSafeAnyBMap_Unmarshal(t *testing.T) { 2541 type fields struct { 2542 mp map[int]int 2543 } 2544 type args struct { 2545 data []byte 2546 } 2547 tests := []struct { 2548 name string 2549 fields fields 2550 args args 2551 want map[int]int 2552 }{ 2553 { 2554 name: "", 2555 fields: fields{ 2556 mp: nil, 2557 }, 2558 args: args{ 2559 data: []byte("{}"), 2560 }, 2561 want: map[int]int{}, 2562 }, 2563 { 2564 name: "", 2565 fields: fields{ 2566 mp: nil, 2567 }, 2568 args: args{ 2569 data: []byte("{\"1\":1,\"2\":2}"), 2570 }, 2571 want: map[int]int{1: 1, 2: 2}, 2572 }, 2573 { 2574 name: "", 2575 fields: fields{ 2576 mp: map[int]int{}, 2577 }, 2578 args: args{ 2579 data: []byte("{\"1\":1,\"2\":2}"), 2580 }, 2581 want: map[int]int{1: 1, 2: 2}, 2582 }, 2583 } 2584 for _, tt := range tests { 2585 t.Run(tt.name, func(t *testing.T) { 2586 x := NewSafeAnyBMapByMap(tt.fields.mp) 2587 err := x.Unmarshal(tt.args.data) 2588 assert.NoError(t, err) 2589 assert.Equalf(t, tt.want, x.ToMetaMap(), "Unmarshal()") 2590 }) 2591 } 2592 } 2593 2594 func TestSafeAnyBMap_Size(t *testing.T) { 2595 type fields struct { 2596 mp map[int]int 2597 } 2598 tests := []struct { 2599 name string 2600 fields fields 2601 want int 2602 }{ 2603 { 2604 name: "nil", 2605 fields: fields{ 2606 mp: nil, 2607 }, 2608 want: 0, 2609 }, 2610 { 2611 name: "", 2612 fields: fields{ 2613 mp: map[int]int{}, 2614 }, 2615 want: 0, 2616 }, 2617 { 2618 name: "", 2619 fields: fields{ 2620 mp: map[int]int{1: 1}, 2621 }, 2622 want: 1, 2623 }, 2624 { 2625 name: "", 2626 fields: fields{ 2627 mp: map[int]int{1: 1, 2: 2}, 2628 }, 2629 want: 2, 2630 }, 2631 } 2632 for _, tt := range tests { 2633 t.Run(tt.name, func(t *testing.T) { 2634 x := NewSafeAnyBMapByMap(tt.fields.mp) 2635 assert.Equalf(t, tt.want, x.Size(), "Size()") 2636 }) 2637 } 2638 } 2639 2640 func TestSafeAnyBMap_IsEmpty(t *testing.T) { 2641 type fields struct { 2642 mp map[int]int 2643 } 2644 tests := []struct { 2645 name string 2646 fields fields 2647 want bool 2648 }{ 2649 { 2650 name: "nil", 2651 fields: fields{ 2652 mp: nil, 2653 }, 2654 want: true, 2655 }, 2656 { 2657 name: "", 2658 fields: fields{ 2659 mp: map[int]int{}, 2660 }, 2661 want: true, 2662 }, 2663 { 2664 name: "", 2665 fields: fields{ 2666 mp: map[int]int{1: 1}, 2667 }, 2668 want: false, 2669 }, 2670 { 2671 name: "", 2672 fields: fields{ 2673 mp: map[int]int{1: 1, 2: 2}, 2674 }, 2675 want: false, 2676 }, 2677 } 2678 for _, tt := range tests { 2679 t.Run(tt.name, func(t *testing.T) { 2680 x := NewSafeAnyBMapByMap(tt.fields.mp) 2681 assert.Equalf(t, tt.want, x.IsEmpty(), "IsEmpty()") 2682 }) 2683 } 2684 } 2685 2686 func TestSafeAnyBMap_IsExist(t *testing.T) { 2687 type fields struct { 2688 mp map[int]int 2689 } 2690 type args struct { 2691 k int 2692 } 2693 tests := []struct { 2694 name string 2695 fields fields 2696 args args 2697 want bool 2698 }{ 2699 { 2700 name: "nil", 2701 fields: fields{ 2702 mp: nil, 2703 }, 2704 args: args{ 2705 k: 0, 2706 }, 2707 want: false, 2708 }, 2709 { 2710 name: "", 2711 fields: fields{ 2712 mp: map[int]int{}, 2713 }, 2714 args: args{ 2715 k: 0, 2716 }, 2717 want: false, 2718 }, 2719 { 2720 name: "", 2721 fields: fields{ 2722 mp: map[int]int{1: 1}, 2723 }, 2724 args: args{ 2725 k: 0, 2726 }, 2727 want: false, 2728 }, 2729 { 2730 name: "", 2731 fields: fields{ 2732 mp: map[int]int{1: 1}, 2733 }, 2734 args: args{ 2735 k: 1, 2736 }, 2737 want: true, 2738 }, 2739 } 2740 for _, tt := range tests { 2741 t.Run(tt.name, func(t *testing.T) { 2742 x := NewSafeAnyBMapByMap(tt.fields.mp) 2743 assert.Equalf(t, tt.want, x.IsExist(tt.args.k), "IsExist(%v)", tt.args.k) 2744 }) 2745 } 2746 } 2747 2748 func TestSafeAnyBMap_ContainsKey(t *testing.T) { 2749 type fields struct { 2750 mp map[int]int 2751 } 2752 type args struct { 2753 k int 2754 } 2755 tests := []struct { 2756 name string 2757 fields fields 2758 args args 2759 want bool 2760 }{ 2761 { 2762 name: "nil", 2763 fields: fields{ 2764 mp: nil, 2765 }, 2766 args: args{ 2767 k: 0, 2768 }, 2769 want: false, 2770 }, 2771 { 2772 name: "", 2773 fields: fields{ 2774 mp: map[int]int{}, 2775 }, 2776 args: args{ 2777 k: 0, 2778 }, 2779 want: false, 2780 }, 2781 { 2782 name: "", 2783 fields: fields{ 2784 mp: map[int]int{1: 1}, 2785 }, 2786 args: args{ 2787 k: 0, 2788 }, 2789 want: false, 2790 }, 2791 { 2792 name: "", 2793 fields: fields{ 2794 mp: map[int]int{1: 1}, 2795 }, 2796 args: args{ 2797 k: 1, 2798 }, 2799 want: true, 2800 }, 2801 } 2802 for _, tt := range tests { 2803 t.Run(tt.name, func(t *testing.T) { 2804 x := NewSafeAnyBMapByMap(tt.fields.mp) 2805 assert.Equalf(t, tt.want, x.ContainsKey(tt.args.k), "ContainsKey(%v)", tt.args.k) 2806 }) 2807 } 2808 } 2809 2810 func TestSafeAnyBMap_ContainsValue(t *testing.T) { 2811 type fields struct { 2812 mp map[int]tInterface 2813 } 2814 type args struct { 2815 v tInterface 2816 } 2817 tests := []struct { 2818 name string 2819 fields fields 2820 args args 2821 want bool 2822 }{ 2823 { 2824 name: "nil", 2825 fields: fields{ 2826 mp: nil, 2827 }, 2828 args: args{ 2829 v: nil, 2830 }, 2831 want: false, 2832 }, 2833 { 2834 name: "", 2835 fields: fields{ 2836 mp: map[int]tInterface{}, 2837 }, 2838 args: args{ 2839 v: nil, 2840 }, 2841 want: false, 2842 }, 2843 { 2844 name: "", 2845 fields: fields{ 2846 mp: map[int]tInterface{1: nil}, 2847 }, 2848 args: args{ 2849 v: nil, 2850 }, 2851 want: true, 2852 }, 2853 { 2854 name: "", 2855 fields: fields{ 2856 mp: map[int]tInterface{1: nil}, 2857 }, 2858 args: args{ 2859 v: &tStruct{ 2860 t: nil, 2861 }, 2862 }, 2863 want: false, 2864 }, 2865 { 2866 name: "", 2867 fields: fields{ 2868 mp: map[int]tInterface{1: nil}, 2869 }, 2870 args: args{ 2871 v: &tStruct{ 2872 t: 1, 2873 }, 2874 }, 2875 want: false, 2876 }, 2877 { 2878 name: "", 2879 fields: fields{ 2880 mp: map[int]tInterface{1: nil, 2: &tStruct{ 2881 t: 1, 2882 }}, 2883 }, 2884 args: args{ 2885 v: &tStruct{ 2886 t: 1, 2887 }, 2888 }, 2889 want: true, 2890 }, 2891 } 2892 for _, tt := range tests { 2893 t.Run(tt.name, func(t *testing.T) { 2894 x := NewSafeAnyBMapByMap(tt.fields.mp) 2895 assert.Equalf(t, tt.want, x.ContainsValue(tt.args.v), "ContainsValue(%v)", tt.args.v) 2896 }) 2897 } 2898 } 2899 2900 func TestSafeAnyBMap_Get(t *testing.T) { 2901 type fields struct { 2902 mp map[int]int 2903 } 2904 type args struct { 2905 k int 2906 } 2907 tests := []struct { 2908 name string 2909 fields fields 2910 args args 2911 want int 2912 want1 bool 2913 }{ 2914 { 2915 name: "nil", 2916 fields: fields{ 2917 mp: nil, 2918 }, 2919 args: args{ 2920 k: 1, 2921 }, 2922 want: 0, 2923 want1: false, 2924 }, 2925 { 2926 name: "", 2927 fields: fields{ 2928 mp: map[int]int{}, 2929 }, 2930 args: args{ 2931 k: 1, 2932 }, 2933 want: 0, 2934 want1: false, 2935 }, 2936 { 2937 name: "", 2938 fields: fields{ 2939 mp: map[int]int{1: 1, 2: 2}, 2940 }, 2941 args: args{ 2942 k: 1, 2943 }, 2944 want: 1, 2945 want1: true, 2946 }, 2947 { 2948 name: "", 2949 fields: fields{ 2950 mp: map[int]int{1: 1, 2: 2}, 2951 }, 2952 args: args{ 2953 k: 3, 2954 }, 2955 want: 0, 2956 want1: false, 2957 }, 2958 } 2959 for _, tt := range tests { 2960 t.Run(tt.name, func(t *testing.T) { 2961 x := NewSafeAnyBMapByMap(tt.fields.mp) 2962 got, got1 := x.Get(tt.args.k) 2963 assert.Equalf(t, tt.want, got, "Get(%v)", tt.args.k) 2964 assert.Equalf(t, tt.want1, got1, "Get(%v)", tt.args.k) 2965 }) 2966 } 2967 } 2968 2969 func TestSafeAnyBMap_GetOrDefault(t *testing.T) { 2970 type fields struct { 2971 mp map[int]int 2972 } 2973 type args struct { 2974 k int 2975 defaultValue int 2976 } 2977 tests := []struct { 2978 name string 2979 fields fields 2980 args args 2981 want int 2982 }{ 2983 { 2984 name: "nil", 2985 fields: fields{ 2986 mp: nil, 2987 }, 2988 args: args{ 2989 k: 0, 2990 defaultValue: 1, 2991 }, 2992 want: 1, 2993 }, 2994 { 2995 name: "", 2996 fields: fields{ 2997 mp: map[int]int{}, 2998 }, 2999 args: args{ 3000 k: 0, 3001 defaultValue: 1, 3002 }, 3003 want: 1, 3004 }, 3005 { 3006 name: "", 3007 fields: fields{ 3008 mp: map[int]int{1: 1}, 3009 }, 3010 args: args{ 3011 k: 1, 3012 defaultValue: 2, 3013 }, 3014 want: 1, 3015 }, 3016 } 3017 for _, tt := range tests { 3018 t.Run(tt.name, func(t *testing.T) { 3019 x := NewSafeAnyBMapByMap(tt.fields.mp) 3020 assert.Equalf(t, tt.want, x.GetOrDefault(tt.args.k, tt.args.defaultValue), "GetOrDefault(%v, %v)", tt.args.k, tt.args.defaultValue) 3021 }) 3022 } 3023 } 3024 3025 func TestSafeAnyBMap_Put(t *testing.T) { 3026 type fields struct { 3027 mp map[int]int 3028 } 3029 type args struct { 3030 k int 3031 v int 3032 } 3033 tests := []struct { 3034 name string 3035 fields fields 3036 args args 3037 }{ 3038 { 3039 name: "", 3040 fields: fields{ 3041 mp: nil, 3042 }, 3043 args: args{ 3044 k: 1, 3045 v: 1, 3046 }, 3047 }, 3048 { 3049 name: "", 3050 fields: fields{ 3051 mp: map[int]int{}, 3052 }, 3053 args: args{ 3054 k: 2, 3055 v: 2, 3056 }, 3057 }, 3058 } 3059 for _, tt := range tests { 3060 t.Run(tt.name, func(t *testing.T) { 3061 x := NewSafeAnyBMapByMap(tt.fields.mp) 3062 x.Put(tt.args.k, tt.args.v) 3063 v, ok := x.Get(tt.args.k) 3064 assert.Equal(t, true, ok) 3065 assert.Equal(t, tt.args.v, v) 3066 }) 3067 } 3068 } 3069 3070 func TestSafeAnyBMap_PuTIfAbsent(t *testing.T) { 3071 type fields struct { 3072 mp map[int]int 3073 } 3074 type args struct { 3075 k int 3076 v int 3077 } 3078 tests := []struct { 3079 name string 3080 fields fields 3081 args args 3082 want bool 3083 }{ 3084 { 3085 name: "nil", 3086 fields: fields{}, 3087 args: args{ 3088 k: 1, 3089 v: 1, 3090 }, 3091 want: true, 3092 }, 3093 { 3094 name: "", 3095 fields: fields{ 3096 mp: map[int]int{}, 3097 }, 3098 args: args{ 3099 k: 1, 3100 v: 1, 3101 }, 3102 want: true, 3103 }, 3104 { 3105 name: "", 3106 fields: fields{ 3107 mp: map[int]int{1: 2}, 3108 }, 3109 args: args{ 3110 k: 1, 3111 v: 1, 3112 }, 3113 want: false, 3114 }, 3115 } 3116 for _, tt := range tests { 3117 t.Run(tt.name, func(t *testing.T) { 3118 x := NewSafeAnyBMapByMap(tt.fields.mp) 3119 assert.Equalf(t, tt.want, x.PuTIfAbsent(tt.args.k, tt.args.v), "PuTIfAbsent(%v, %v)", tt.args.k, tt.args.v) 3120 }) 3121 } 3122 } 3123 3124 func TestSafeAnyBMap_Delete(t *testing.T) { 3125 type fields struct { 3126 mp map[int]int 3127 } 3128 type args struct { 3129 k int 3130 } 3131 tests := []struct { 3132 name string 3133 fields fields 3134 args args 3135 }{ 3136 { 3137 name: "nil", 3138 fields: fields{ 3139 mp: nil, 3140 }, 3141 args: args{ 3142 k: 0, 3143 }, 3144 }, 3145 { 3146 name: "", 3147 fields: fields{ 3148 mp: map[int]int{}, 3149 }, 3150 args: args{ 3151 k: 0, 3152 }, 3153 }, 3154 { 3155 name: "", 3156 fields: fields{ 3157 mp: map[int]int{1: 1, 2: 2}, 3158 }, 3159 args: args{ 3160 k: 1, 3161 }, 3162 }, 3163 } 3164 for _, tt := range tests { 3165 t.Run(tt.name, func(t *testing.T) { 3166 x := NewSafeAnyBMapByMap(tt.fields.mp) 3167 x.Delete(tt.args.k) 3168 _, ok := x.Get(tt.args.k) 3169 assert.Equal(t, false, ok) 3170 }) 3171 } 3172 } 3173 3174 func TestSafeAnyBMap_DeleteIfPresent(t *testing.T) { 3175 type fields struct { 3176 mp map[int]int 3177 } 3178 type args struct { 3179 k int 3180 } 3181 tests := []struct { 3182 name string 3183 fields fields 3184 args args 3185 want int 3186 want1 bool 3187 }{ 3188 { 3189 name: "nil", 3190 fields: fields{ 3191 mp: nil, 3192 }, 3193 args: args{ 3194 k: 0, 3195 }, 3196 want: 0, 3197 want1: false, 3198 }, 3199 { 3200 name: "", 3201 fields: fields{ 3202 mp: map[int]int{}, 3203 }, 3204 args: args{ 3205 k: 0, 3206 }, 3207 want: 0, 3208 want1: false, 3209 }, 3210 { 3211 name: "", 3212 fields: fields{ 3213 mp: map[int]int{1: 1, 2: 2}, 3214 }, 3215 args: args{ 3216 k: 1, 3217 }, 3218 want: 1, 3219 want1: true, 3220 }, 3221 } 3222 for _, tt := range tests { 3223 t.Run(tt.name, func(t *testing.T) { 3224 x := NewSafeAnyBMapByMap(tt.fields.mp) 3225 got, got1 := x.DeleteIfPresent(tt.args.k) 3226 assert.Equalf(t, tt.want, got, "DeleteIfPresent(%v)", tt.args.k) 3227 assert.Equalf(t, tt.want1, got1, "DeleteIfPresent(%v)", tt.args.k) 3228 _, ok := x.Get(tt.args.k) 3229 assert.Equal(t, false, ok) 3230 }) 3231 } 3232 } 3233 3234 func TestSafeAnyBMap_MergeByMap(t *testing.T) { 3235 type fields struct { 3236 mp map[int]int 3237 } 3238 type args struct { 3239 m map[int]int 3240 f func(int, int) bool 3241 } 3242 tests := []struct { 3243 name string 3244 fields fields 3245 args args 3246 want map[int]int 3247 }{ 3248 { 3249 name: "nil", 3250 fields: fields{ 3251 mp: nil, 3252 }, 3253 args: args{ 3254 m: nil, 3255 f: nil, 3256 }, 3257 want: map[int]int{}, 3258 }, 3259 { 3260 name: "nil", 3261 fields: fields{ 3262 mp: nil, 3263 }, 3264 args: args{ 3265 m: map[int]int{}, 3266 f: nil, 3267 }, 3268 want: map[int]int{}, 3269 }, 3270 { 3271 name: "nil", 3272 fields: fields{ 3273 mp: nil, 3274 }, 3275 args: args{ 3276 m: map[int]int{1: 1}, 3277 f: nil, 3278 }, 3279 want: map[int]int{1: 1}, 3280 }, 3281 { 3282 name: "nil", 3283 fields: fields{ 3284 mp: map[int]int{1: 1}, 3285 }, 3286 args: args{ 3287 m: map[int]int{1: 2}, 3288 f: nil, 3289 }, 3290 want: map[int]int{1: 1}, 3291 }, 3292 { 3293 name: "", 3294 fields: fields{ 3295 mp: map[int]int{1: 1}, 3296 }, 3297 args: args{ 3298 m: map[int]int{1: 2}, 3299 f: func(i int, i2 int) bool { 3300 return true 3301 }, 3302 }, 3303 want: map[int]int{1: 2}, 3304 }, 3305 { 3306 name: "", 3307 fields: fields{ 3308 mp: map[int]int{1: 1}, 3309 }, 3310 args: args{ 3311 m: map[int]int{1: 2}, 3312 f: func(i int, i2 int) bool { 3313 return false 3314 }, 3315 }, 3316 want: map[int]int{1: 1}, 3317 }, 3318 { 3319 name: "", 3320 fields: fields{ 3321 mp: map[int]int{1: 1, 2: 2, 3: 3}, 3322 }, 3323 args: args{ 3324 m: map[int]int{3: 4, 4: 4}, 3325 f: func(i int, i2 int) bool { 3326 return false 3327 }, 3328 }, 3329 want: map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, 3330 }, 3331 { 3332 name: "", 3333 fields: fields{ 3334 mp: map[int]int{1: 1, 2: 2, 3: 3}, 3335 }, 3336 args: args{ 3337 m: map[int]int{3: 4, 4: 4}, 3338 f: func(i int, i2 int) bool { 3339 return true 3340 }, 3341 }, 3342 want: map[int]int{1: 1, 2: 2, 3: 4, 4: 4}, 3343 }, 3344 } 3345 for _, tt := range tests { 3346 t.Run(tt.name, func(t *testing.T) { 3347 x := NewSafeAnyBMapByMap(tt.fields.mp) 3348 x.MergeByMap(tt.args.m, tt.args.f) 3349 assert.Equal(t, tt.want, x.ToMetaMap()) 3350 }) 3351 } 3352 } 3353 3354 func TestSafeAnyBMap_MergeByBMap(t *testing.T) { 3355 type fields struct { 3356 mp map[int]int 3357 } 3358 type args struct { 3359 m map[int]int 3360 f func(int, int) bool 3361 } 3362 tests := []struct { 3363 name string 3364 fields fields 3365 args args 3366 want map[int]int 3367 }{ 3368 { 3369 name: "nil", 3370 fields: fields{ 3371 mp: nil, 3372 }, 3373 args: args{ 3374 m: nil, 3375 f: nil, 3376 }, 3377 want: map[int]int{}, 3378 }, 3379 { 3380 name: "nil", 3381 fields: fields{ 3382 mp: nil, 3383 }, 3384 args: args{ 3385 m: map[int]int{}, 3386 f: nil, 3387 }, 3388 want: map[int]int{}, 3389 }, 3390 { 3391 name: "nil", 3392 fields: fields{ 3393 mp: nil, 3394 }, 3395 args: args{ 3396 m: map[int]int{1: 1}, 3397 f: nil, 3398 }, 3399 want: map[int]int{1: 1}, 3400 }, 3401 { 3402 name: "nil", 3403 fields: fields{ 3404 mp: map[int]int{1: 1}, 3405 }, 3406 args: args{ 3407 m: map[int]int{1: 2}, 3408 f: nil, 3409 }, 3410 want: map[int]int{1: 1}, 3411 }, 3412 { 3413 name: "", 3414 fields: fields{ 3415 mp: map[int]int{1: 1}, 3416 }, 3417 args: args{ 3418 m: map[int]int{1: 2}, 3419 f: func(i int, i2 int) bool { 3420 return true 3421 }, 3422 }, 3423 want: map[int]int{1: 2}, 3424 }, 3425 { 3426 name: "", 3427 fields: fields{ 3428 mp: map[int]int{1: 1}, 3429 }, 3430 args: args{ 3431 m: map[int]int{1: 2}, 3432 f: func(i int, i2 int) bool { 3433 return false 3434 }, 3435 }, 3436 want: map[int]int{1: 1}, 3437 }, 3438 { 3439 name: "", 3440 fields: fields{ 3441 mp: map[int]int{1: 1, 2: 2, 3: 3}, 3442 }, 3443 args: args{ 3444 m: map[int]int{3: 4, 4: 4}, 3445 f: func(i int, i2 int) bool { 3446 return false 3447 }, 3448 }, 3449 want: map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, 3450 }, 3451 { 3452 name: "", 3453 fields: fields{ 3454 mp: map[int]int{1: 1, 2: 2, 3: 3}, 3455 }, 3456 args: args{ 3457 m: map[int]int{3: 4, 4: 4}, 3458 f: func(i int, i2 int) bool { 3459 return true 3460 }, 3461 }, 3462 want: map[int]int{1: 1, 2: 2, 3: 4, 4: 4}, 3463 }, 3464 } 3465 for _, tt := range tests { 3466 t.Run(tt.name, func(t *testing.T) { 3467 x := NewSafeAnyBMapByMap(tt.fields.mp) 3468 x.MergeByBMap(NewSafeAnyBMapByMap(tt.args.m), tt.args.f) 3469 assert.Equal(t, tt.want, x.ToMetaMap()) 3470 }) 3471 } 3472 } 3473 3474 func TestSafeAnyBMap_Replace(t *testing.T) { 3475 type fields struct { 3476 mp map[int]int 3477 } 3478 type args struct { 3479 k int 3480 ov int 3481 nv int 3482 } 3483 tests := []struct { 3484 name string 3485 fields fields 3486 args args 3487 want bool 3488 }{ 3489 { 3490 name: "nil", 3491 fields: fields{ 3492 mp: nil, 3493 }, 3494 args: args{ 3495 k: 0, 3496 ov: 0, 3497 nv: 0, 3498 }, 3499 want: false, 3500 }, 3501 { 3502 name: "", 3503 fields: fields{ 3504 mp: map[int]int{}, 3505 }, 3506 args: args{ 3507 k: 0, 3508 ov: 0, 3509 nv: 0, 3510 }, 3511 want: false, 3512 }, 3513 { 3514 name: "nil", 3515 fields: fields{ 3516 mp: map[int]int{1: 1}, 3517 }, 3518 args: args{ 3519 k: 1, 3520 ov: 0, 3521 nv: 2, 3522 }, 3523 want: false, 3524 }, 3525 { 3526 name: "nil", 3527 fields: fields{ 3528 mp: map[int]int{1: 1}, 3529 }, 3530 args: args{ 3531 k: 1, 3532 ov: 1, 3533 nv: 2, 3534 }, 3535 want: true, 3536 }, 3537 } 3538 for _, tt := range tests { 3539 t.Run(tt.name, func(t *testing.T) { 3540 x := NewSafeAnyBMapByMap(tt.fields.mp) 3541 assert.Equalf(t, tt.want, x.Replace(tt.args.k, tt.args.ov, tt.args.nv), "Replace(%v, %v, %v)", tt.args.k, tt.args.ov, tt.args.nv) 3542 }) 3543 } 3544 }