gonum.org/v1/gonum@v0.14.0/graph/internal/set/set_test.go (about) 1 // Copyright ©2014 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package set 6 7 import ( 8 "reflect" 9 "testing" 10 ) 11 12 type node int64 13 14 func (n node) ID() int64 { return int64(n) } 15 16 // TestSame tests the assumption that pointer equality via unsafe conversion 17 // of a map[int]struct{} to uintptr is a valid test for perfect identity between 18 // set values. If any of the tests in TestSame fail, the package is broken and same 19 // must be reimplemented to conform to the runtime map implementation. The relevant 20 // code to look at (at least for gc) is the hmap type in runtime/map.go. 21 22 func TestSameInt64s(t *testing.T) { 23 var ( 24 a = make(Int64s) 25 b = make(Int64s) 26 c = a 27 ) 28 29 if int64sSame(a, b) { 30 t.Error("Independently created sets test as same") 31 } 32 if !int64sSame(a, c) { 33 t.Error("Set copy and original test as not same.") 34 } 35 a.Add(1) 36 if !int64sSame(a, c) { 37 t.Error("Set copy and original test as not same after addition.") 38 } 39 if !int64sSame(nil, nil) { 40 t.Error("nil sets test as not same.") 41 } 42 if int64sSame(b, nil) { 43 t.Error("nil and empty sets test as same.") 44 } 45 } 46 47 func TestAddInt64s(t *testing.T) { 48 s := make(Int64s) 49 if s.Count() != 0 { 50 t.Error("Set somehow contains new elements upon creation") 51 } 52 53 s.Add(1) 54 s.Add(3) 55 s.Add(5) 56 57 if s.Count() != 3 { 58 t.Error("Incorrect number of set elements after adding") 59 } 60 61 if !s.Has(1) || !s.Has(3) || !s.Has(5) { 62 t.Error("Set doesn't contain element that was added") 63 } 64 65 s.Add(1) 66 67 if s.Count() > 3 { 68 t.Error("Set double-adds element (element not unique)") 69 } else if s.Count() < 3 { 70 t.Error("Set double-add lowered len") 71 } 72 73 if !s.Has(1) { 74 t.Error("Set doesn't contain double-added element") 75 } 76 77 if !s.Has(3) || !s.Has(5) { 78 t.Error("Set removes element on double-add") 79 } 80 } 81 82 func TestRemoveInt64s(t *testing.T) { 83 s := make(Int64s) 84 85 s.Add(1) 86 s.Add(3) 87 s.Add(5) 88 89 s.Remove(1) 90 91 if s.Count() != 2 { 92 t.Error("Incorrect number of set elements after removing an element") 93 } 94 95 if s.Has(1) { 96 t.Error("Element present after removal") 97 } 98 99 if !s.Has(3) || !s.Has(5) { 100 t.Error("Set remove removed wrong element") 101 } 102 103 s.Remove(1) 104 105 if s.Count() != 2 || s.Has(1) { 106 t.Error("Double set remove does something strange") 107 } 108 109 s.Add(1) 110 111 if s.Count() != 3 || !s.Has(1) { 112 t.Error("Cannot add element after removal") 113 } 114 } 115 116 func TestSelfEqualInt64s(t *testing.T) { 117 s := make(Int64s) 118 119 if !Int64sEqual(s, s) { 120 t.Error("Set is not equal to itself") 121 } 122 123 s.Add(1) 124 125 if !Int64sEqual(s, s) { 126 t.Error("Set ceases self equality after adding element") 127 } 128 } 129 130 func TestEqualInt64s(t *testing.T) { 131 a := make(Int64s) 132 b := make(Int64s) 133 134 if !Int64sEqual(a, b) { 135 t.Error("Two different empty sets not equal") 136 } 137 138 a.Add(1) 139 if Int64sEqual(a, b) { 140 t.Error("Two different sets with different sizes equal") 141 } 142 143 b.Add(1) 144 if !Int64sEqual(a, b) { 145 t.Error("Two sets with same element not equal") 146 } 147 148 b.Remove(1) 149 b.Add(2) 150 if Int64sEqual(a, b) { 151 t.Error("Two different sets with different elements equal") 152 } 153 } 154 155 func TestSameInts(t *testing.T) { 156 var ( 157 a = make(Ints) 158 b = make(Ints) 159 c = a 160 ) 161 162 if intsSame(a, b) { 163 t.Error("Independently created sets test as same") 164 } 165 if !intsSame(a, c) { 166 t.Error("Set copy and original test as not same.") 167 } 168 a.Add(1) 169 if !intsSame(a, c) { 170 t.Error("Set copy and original test as not same after addition.") 171 } 172 if !intsSame(nil, nil) { 173 t.Error("nil sets test as not same.") 174 } 175 if intsSame(b, nil) { 176 t.Error("nil and empty sets test as same.") 177 } 178 } 179 180 func TestAddInts(t *testing.T) { 181 s := make(Ints) 182 if s.Count() != 0 { 183 t.Error("Set somehow contains new elements upon creation") 184 } 185 186 s.Add(1) 187 s.Add(3) 188 s.Add(5) 189 190 if s.Count() != 3 { 191 t.Error("Incorrect number of set elements after adding") 192 } 193 194 if !s.Has(1) || !s.Has(3) || !s.Has(5) { 195 t.Error("Set doesn't contain element that was added") 196 } 197 198 s.Add(1) 199 200 if s.Count() > 3 { 201 t.Error("Set double-adds element (element not unique)") 202 } else if s.Count() < 3 { 203 t.Error("Set double-add lowered len") 204 } 205 206 if !s.Has(1) { 207 t.Error("Set doesn't contain double-added element") 208 } 209 210 if !s.Has(3) || !s.Has(5) { 211 t.Error("Set removes element on double-add") 212 } 213 } 214 215 func TestRemoveInts(t *testing.T) { 216 s := make(Ints) 217 218 s.Add(1) 219 s.Add(3) 220 s.Add(5) 221 222 s.Remove(1) 223 224 if s.Count() != 2 { 225 t.Error("Incorrect number of set elements after removing an element") 226 } 227 228 if s.Has(1) { 229 t.Error("Element present after removal") 230 } 231 232 if !s.Has(3) || !s.Has(5) { 233 t.Error("Set remove removed wrong element") 234 } 235 236 s.Remove(1) 237 238 if s.Count() != 2 || s.Has(1) { 239 t.Error("Double set remove does something strange") 240 } 241 242 s.Add(1) 243 244 if s.Count() != 3 || !s.Has(1) { 245 t.Error("Cannot add element after removal") 246 } 247 } 248 249 func TestSelfEqualInts(t *testing.T) { 250 s := make(Ints) 251 252 if !IntsEqual(s, s) { 253 t.Error("Set is not equal to itself") 254 } 255 256 s.Add(1) 257 258 if !IntsEqual(s, s) { 259 t.Error("Set ceases self equality after adding element") 260 } 261 } 262 263 func TestEqualInts(t *testing.T) { 264 a := make(Ints) 265 b := make(Ints) 266 267 if !IntsEqual(a, b) { 268 t.Error("Two different empty sets not equal") 269 } 270 271 a.Add(1) 272 if IntsEqual(a, b) { 273 t.Error("Two different sets with different sizes equal") 274 } 275 276 b.Add(1) 277 if !IntsEqual(a, b) { 278 t.Error("Two sets with same element not equal") 279 } 280 281 b.Remove(1) 282 b.Add(2) 283 if IntsEqual(a, b) { 284 t.Error("Two different sets with different elements equal") 285 } 286 } 287 288 func TestSameNodes(t *testing.T) { 289 var ( 290 a = NewNodes() 291 b = NewNodes() 292 c = a 293 ) 294 295 if same(a, b) { 296 t.Error("Independently created sets test as same") 297 } 298 if !same(a, c) { 299 t.Error("Set copy and original test as not same.") 300 } 301 a.Add(node(1)) 302 if !same(a, c) { 303 t.Error("Set copy and original test as not same after addition.") 304 } 305 if !same(nil, nil) { 306 t.Error("nil sets test as not same.") 307 } 308 if same(b, nil) { 309 t.Error("nil and empty sets test as same.") 310 } 311 } 312 313 func TestAddNodes(t *testing.T) { 314 s := NewNodes() 315 if s == nil { 316 t.Fatal("Set cannot be created successfully") 317 } 318 319 if s.Count() != 0 { 320 t.Error("Set somehow contains new elements upon creation") 321 } 322 323 s.Add(node(1)) 324 s.Add(node(3)) 325 s.Add(node(5)) 326 327 if s.Count() != 3 { 328 t.Error("Incorrect number of set elements after adding") 329 } 330 331 if !s.Has(node(1)) || !s.Has(node(3)) || !s.Has(node(5)) { 332 t.Error("Set doesn't contain element that was added") 333 } 334 335 s.Add(node(1)) 336 337 if s.Count() > 3 { 338 t.Error("Set double-adds element (element not unique)") 339 } else if s.Count() < 3 { 340 t.Error("Set double-add lowered len") 341 } 342 343 if !s.Has(node(1)) { 344 t.Error("Set doesn't contain double-added element") 345 } 346 347 if !s.Has(node(3)) || !s.Has(node(5)) { 348 t.Error("Set removes element on double-add") 349 } 350 351 for e, n := range s { 352 if e != n.ID() { 353 t.Errorf("Element ID did not match key: %d != %d", e, n.ID()) 354 } 355 } 356 } 357 358 func TestRemoveNodes(t *testing.T) { 359 s := NewNodes() 360 361 s.Add(node(1)) 362 s.Add(node(3)) 363 s.Add(node(5)) 364 365 s.Remove(node(1)) 366 367 if s.Count() != 2 { 368 t.Error("Incorrect number of set elements after removing an element") 369 } 370 371 if s.Has(node(1)) { 372 t.Error("Element present after removal") 373 } 374 375 if !s.Has(node(3)) || !s.Has(node(5)) { 376 t.Error("Set remove removed wrong element") 377 } 378 379 s.Remove(node(1)) 380 381 if s.Count() != 2 || s.Has(node(1)) { 382 t.Error("Double set remove does something strange") 383 } 384 385 s.Add(node(1)) 386 387 if s.Count() != 3 || !s.Has(node(1)) { 388 t.Error("Cannot add element after removal") 389 } 390 } 391 392 func TestSelfEqualNodes(t *testing.T) { 393 s := NewNodes() 394 395 if !Equal(s, s) { 396 t.Error("Set is not equal to itself") 397 } 398 399 s.Add(node(1)) 400 401 if !Equal(s, s) { 402 t.Error("Set ceases self equality after adding element") 403 } 404 } 405 406 func TestEqualNodes(t *testing.T) { 407 a := NewNodes() 408 b := NewNodes() 409 410 if !Equal(a, b) { 411 t.Error("Two different empty sets not equal") 412 } 413 414 a.Add(node(1)) 415 if Equal(a, b) { 416 t.Error("Two different sets with different sizes equal") 417 } 418 419 b.Add(node(1)) 420 if !Equal(a, b) { 421 t.Error("Two sets with same element not equal") 422 } 423 424 b.Remove(node(1)) 425 b.Add(node(2)) 426 if Equal(a, b) { 427 t.Error("Two different sets with different elements equal") 428 } 429 } 430 431 func TestCopyNodes(t *testing.T) { 432 a := NewNodes() 433 434 a.Add(node(1)) 435 a.Add(node(2)) 436 a.Add(node(3)) 437 438 b := CloneNodes(a) 439 440 if !Equal(a, b) { 441 t.Fatalf("Two sets not equal after copy: %v != %v", a, b) 442 } 443 444 b.Remove(node(1)) 445 446 if Equal(a, b) { 447 t.Errorf("Mutating one set mutated another after copy: %v == %v", a, b) 448 } 449 } 450 451 func TestUnionSameNodes(t *testing.T) { 452 a := NewNodes() 453 b := NewNodes() 454 455 a.Add(node(1)) 456 a.Add(node(2)) 457 458 b.Add(node(1)) 459 b.Add(node(2)) 460 461 c := UnionOfNodes(a, b) 462 463 if c.Count() != 2 { 464 t.Error("Union of same sets yields set with wrong len") 465 } 466 467 if !c.Has(node(1)) || !c.Has(node(2)) { 468 t.Error("Union of same sets yields wrong elements") 469 } 470 471 for i, s := range []Nodes{a, b, c} { 472 for e, n := range s { 473 if e != n.ID() { 474 t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID()) 475 } 476 } 477 } 478 } 479 480 func TestUnionDiffNodes(t *testing.T) { 481 a := NewNodes() 482 b := NewNodes() 483 484 a.Add(node(1)) 485 a.Add(node(2)) 486 487 b.Add(node(3)) 488 489 c := UnionOfNodes(a, b) 490 491 if c.Count() != 3 { 492 t.Error("Union of different sets yields set with wrong len") 493 } 494 495 if !c.Has(node(1)) || !c.Has(node(2)) || !c.Has(node(3)) { 496 t.Error("Union of different sets yields set with wrong elements") 497 } 498 499 if a.Has(node(3)) || !a.Has(node(2)) || !a.Has(node(1)) || a.Count() != 2 { 500 t.Error("Union of sets mutates non-destination set (argument 1)") 501 } 502 503 if !b.Has(node(3)) || b.Has(node(1)) || b.Has(node(2)) || b.Count() != 1 { 504 t.Error("Union of sets mutates non-destination set (argument 2)") 505 } 506 507 for i, s := range []Nodes{a, b, c} { 508 for e, n := range s { 509 if e != n.ID() { 510 t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID()) 511 } 512 } 513 } 514 515 c = UnionOfNodes(a, a) 516 if !reflect.DeepEqual(c, a) { 517 t.Errorf("Union of equal sets not equal to sets: %v != %v", c, a) 518 } 519 } 520 521 func TestUnionOverlappingNodes(t *testing.T) { 522 a := NewNodes() 523 b := NewNodes() 524 525 a.Add(node(1)) 526 a.Add(node(2)) 527 528 b.Add(node(2)) 529 b.Add(node(3)) 530 531 c := UnionOfNodes(a, b) 532 533 if c.Count() != 3 { 534 t.Error("Union of overlapping sets yields set with wrong len") 535 } 536 537 if !c.Has(node(1)) || !c.Has(node(2)) || !c.Has(node(3)) { 538 t.Error("Union of overlapping sets yields set with wrong elements") 539 } 540 541 if a.Has(node(3)) || !a.Has(node(2)) || !a.Has(node(1)) || a.Count() != 2 { 542 t.Error("Union of sets mutates non-destination set (argument 1)") 543 } 544 545 if !b.Has(node(3)) || b.Has(node(1)) || !b.Has(node(2)) || b.Count() != 2 { 546 t.Error("Union of sets mutates non-destination set (argument 2)") 547 } 548 549 for i, s := range []Nodes{a, b, c} { 550 for e, n := range s { 551 if e != n.ID() { 552 t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID()) 553 } 554 } 555 } 556 557 c = IntersectionOfNodes(a, a) 558 if !reflect.DeepEqual(c, a) { 559 t.Errorf("Intersection of equal sets not equal to sets: %v != %v", c, a) 560 } 561 } 562 563 func TestIntersectSameNodes(t *testing.T) { 564 a := NewNodes() 565 b := NewNodes() 566 567 a.Add(node(2)) 568 a.Add(node(3)) 569 570 b.Add(node(2)) 571 b.Add(node(3)) 572 573 c := IntersectionOfNodes(a, b) 574 575 if card := c.Count(); card != 2 { 576 t.Errorf("Intersection of identical sets yields set of wrong len %d", card) 577 } 578 579 if !c.Has(node(2)) || !c.Has(node(3)) { 580 t.Error("Intersection of identical sets yields set of wrong elements") 581 } 582 583 for i, s := range []Nodes{a, b, c} { 584 for e, n := range s { 585 if e != n.ID() { 586 t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID()) 587 } 588 } 589 } 590 } 591 592 func TestIntersectDiffNodes(t *testing.T) { 593 a := NewNodes() 594 b := NewNodes() 595 596 a.Add(node(2)) 597 a.Add(node(3)) 598 599 b.Add(node(1)) 600 b.Add(node(4)) 601 602 c := IntersectionOfNodes(a, b) 603 604 if card := c.Count(); card != 0 { 605 t.Errorf("Intersection of different yields non-empty set %d", card) 606 } 607 608 if !a.Has(node(2)) || !a.Has(node(3)) || a.Has(node(1)) || a.Has(node(4)) || a.Count() != 2 { 609 t.Error("Intersection of sets mutates non-destination set (argument 1)") 610 } 611 612 if b.Has(node(2)) || b.Has(node(3)) || !b.Has(node(1)) || !b.Has(node(4)) || b.Count() != 2 { 613 t.Error("Intersection of sets mutates non-destination set (argument 1)") 614 } 615 616 for i, s := range []Nodes{a, b, c} { 617 for e, n := range s { 618 if e != n.ID() { 619 t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID()) 620 } 621 } 622 } 623 } 624 625 func TestIntersectOverlappingNodes(t *testing.T) { 626 a := NewNodes() 627 b := NewNodes() 628 629 a.Add(node(2)) 630 a.Add(node(3)) 631 632 b.Add(node(3)) 633 b.Add(node(4)) 634 635 c := IntersectionOfNodes(a, b) 636 637 if card := c.Count(); card != 1 { 638 t.Errorf("Intersection of overlapping sets yields set of incorrect len %d", card) 639 } 640 641 if !c.Has(node(3)) { 642 t.Errorf("Intersection of overlapping sets yields set with wrong element") 643 } 644 645 if !a.Has(node(2)) || !a.Has(node(3)) || a.Has(node(4)) || a.Count() != 2 { 646 t.Error("Intersection of sets mutates non-destination set (argument 1)") 647 } 648 649 if b.Has(node(2)) || !b.Has(node(3)) || !b.Has(node(4)) || b.Count() != 2 { 650 t.Error("Intersection of sets mutates non-destination set (argument 1)") 651 } 652 653 for i, s := range []Nodes{a, b, c} { 654 for e, n := range s { 655 if e != n.ID() { 656 t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID()) 657 } 658 } 659 } 660 661 c = IntersectionOfNodes(c, a) 662 want := Nodes{3: node(3)} 663 if !reflect.DeepEqual(c, want) { 664 t.Errorf("Intersection of sets with dst equal to a not equal: %v != %v", c, want) 665 } 666 c = IntersectionOfNodes(a, c) 667 if !reflect.DeepEqual(c, want) { 668 t.Errorf("Intersection of sets with dst equal to a not equal: %v != %v", c, want) 669 } 670 }