gonum.org/v1/gonum@v0.15.1-0.20240517103525-f853624cb1bb/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  // TestSameInts 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 TestSameInts(t *testing.T) {
    23  	var (
    24  		a = make(Ints[int64])
    25  		b = make(Ints[int64])
    26  		c = a
    27  	)
    28  
    29  	if intsSame(a, b) {
    30  		t.Error("Independently created sets test as same")
    31  	}
    32  	if !intsSame(a, c) {
    33  		t.Error("Ints copy and original test as not same.")
    34  	}
    35  	a.Add(1)
    36  	if !intsSame(a, c) {
    37  		t.Error("Ints copy and original test as not same after addition.")
    38  	}
    39  	if !intsSame[int64](nil, nil) {
    40  		t.Error("nil sets test as not same.")
    41  	}
    42  	if intsSame(b, nil) {
    43  		t.Error("nil and empty sets test as same.")
    44  	}
    45  }
    46  
    47  func TestAddInts(t *testing.T) {
    48  	s := make(Ints[int64])
    49  	if s.Count() != 0 {
    50  		t.Error("Ints 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("Ints doesn't contain element that was added")
    63  	}
    64  
    65  	s.Add(1)
    66  
    67  	if s.Count() > 3 {
    68  		t.Error("Ints double-adds element (element not unique)")
    69  	} else if s.Count() < 3 {
    70  		t.Error("Ints double-add lowered len")
    71  	}
    72  
    73  	if !s.Has(1) {
    74  		t.Error("Ints doesn't contain double-added element")
    75  	}
    76  
    77  	if !s.Has(3) || !s.Has(5) {
    78  		t.Error("Ints removes element on double-add")
    79  	}
    80  }
    81  
    82  func TestRemoveInts(t *testing.T) {
    83  	s := make(Ints[int64])
    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("Ints 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 TestSelfEqualInts(t *testing.T) {
   117  	s := make(Ints[int64])
   118  
   119  	if !IntsEqual(s, s) {
   120  		t.Error("Ints is not equal to itself")
   121  	}
   122  
   123  	s.Add(1)
   124  
   125  	if !IntsEqual(s, s) {
   126  		t.Error("Ints ceases self equality after adding element")
   127  	}
   128  }
   129  
   130  func TestEqualInts(t *testing.T) {
   131  	a := make(Ints[int64])
   132  	b := make(Ints[int64])
   133  
   134  	if !IntsEqual(a, b) {
   135  		t.Error("Two different empty sets not equal")
   136  	}
   137  
   138  	a.Add(1)
   139  	if IntsEqual(a, b) {
   140  		t.Error("Two different sets with different sizes equal")
   141  	}
   142  
   143  	b.Add(1)
   144  	if !IntsEqual(a, b) {
   145  		t.Error("Two sets with same element not equal")
   146  	}
   147  
   148  	b.Remove(1)
   149  	b.Add(2)
   150  	if IntsEqual(a, b) {
   151  		t.Error("Two different sets with different elements equal")
   152  	}
   153  }
   154  
   155  func TestSameNodes(t *testing.T) {
   156  	var (
   157  		a = NewNodes()
   158  		b = NewNodes()
   159  		c = a
   160  	)
   161  
   162  	if same(a, b) {
   163  		t.Error("Independently created sets test as same")
   164  	}
   165  	if !same(a, c) {
   166  		t.Error("Ints copy and original test as not same.")
   167  	}
   168  	a.Add(node(1))
   169  	if !same(a, c) {
   170  		t.Error("Ints copy and original test as not same after addition.")
   171  	}
   172  	if !same(nil, nil) {
   173  		t.Error("nil sets test as not same.")
   174  	}
   175  	if same(b, nil) {
   176  		t.Error("nil and empty sets test as same.")
   177  	}
   178  }
   179  
   180  func TestAddNodes(t *testing.T) {
   181  	s := NewNodes()
   182  	if s == nil {
   183  		t.Fatal("Ints cannot be created successfully")
   184  	}
   185  
   186  	if s.Count() != 0 {
   187  		t.Error("Ints somehow contains new elements upon creation")
   188  	}
   189  
   190  	s.Add(node(1))
   191  	s.Add(node(3))
   192  	s.Add(node(5))
   193  
   194  	if s.Count() != 3 {
   195  		t.Error("Incorrect number of set elements after adding")
   196  	}
   197  
   198  	if !s.Has(node(1)) || !s.Has(node(3)) || !s.Has(node(5)) {
   199  		t.Error("Ints doesn't contain element that was added")
   200  	}
   201  
   202  	s.Add(node(1))
   203  
   204  	if s.Count() > 3 {
   205  		t.Error("Ints double-adds element (element not unique)")
   206  	} else if s.Count() < 3 {
   207  		t.Error("Ints double-add lowered len")
   208  	}
   209  
   210  	if !s.Has(node(1)) {
   211  		t.Error("Ints doesn't contain double-added element")
   212  	}
   213  
   214  	if !s.Has(node(3)) || !s.Has(node(5)) {
   215  		t.Error("Ints removes element on double-add")
   216  	}
   217  
   218  	for e, n := range s {
   219  		if e != n.ID() {
   220  			t.Errorf("Element ID did not match key: %d != %d", e, n.ID())
   221  		}
   222  	}
   223  }
   224  
   225  func TestRemoveNodes(t *testing.T) {
   226  	s := NewNodes()
   227  
   228  	s.Add(node(1))
   229  	s.Add(node(3))
   230  	s.Add(node(5))
   231  
   232  	s.Remove(node(1))
   233  
   234  	if s.Count() != 2 {
   235  		t.Error("Incorrect number of set elements after removing an element")
   236  	}
   237  
   238  	if s.Has(node(1)) {
   239  		t.Error("Element present after removal")
   240  	}
   241  
   242  	if !s.Has(node(3)) || !s.Has(node(5)) {
   243  		t.Error("Ints remove removed wrong element")
   244  	}
   245  
   246  	s.Remove(node(1))
   247  
   248  	if s.Count() != 2 || s.Has(node(1)) {
   249  		t.Error("Double set remove does something strange")
   250  	}
   251  
   252  	s.Add(node(1))
   253  
   254  	if s.Count() != 3 || !s.Has(node(1)) {
   255  		t.Error("Cannot add element after removal")
   256  	}
   257  }
   258  
   259  func TestSelfEqualNodes(t *testing.T) {
   260  	s := NewNodes()
   261  
   262  	if !Equal(s, s) {
   263  		t.Error("Ints is not equal to itself")
   264  	}
   265  
   266  	s.Add(node(1))
   267  
   268  	if !Equal(s, s) {
   269  		t.Error("Ints ceases self equality after adding element")
   270  	}
   271  }
   272  
   273  func TestEqualNodes(t *testing.T) {
   274  	a := NewNodes()
   275  	b := NewNodes()
   276  
   277  	if !Equal(a, b) {
   278  		t.Error("Two different empty sets not equal")
   279  	}
   280  
   281  	a.Add(node(1))
   282  	if Equal(a, b) {
   283  		t.Error("Two different sets with different sizes equal")
   284  	}
   285  
   286  	b.Add(node(1))
   287  	if !Equal(a, b) {
   288  		t.Error("Two sets with same element not equal")
   289  	}
   290  
   291  	b.Remove(node(1))
   292  	b.Add(node(2))
   293  	if Equal(a, b) {
   294  		t.Error("Two different sets with different elements equal")
   295  	}
   296  }
   297  
   298  func TestCopyNodes(t *testing.T) {
   299  	a := NewNodes()
   300  
   301  	a.Add(node(1))
   302  	a.Add(node(2))
   303  	a.Add(node(3))
   304  
   305  	b := CloneNodes(a)
   306  
   307  	if !Equal(a, b) {
   308  		t.Fatalf("Two sets not equal after copy: %v != %v", a, b)
   309  	}
   310  
   311  	b.Remove(node(1))
   312  
   313  	if Equal(a, b) {
   314  		t.Errorf("Mutating one set mutated another after copy: %v == %v", a, b)
   315  	}
   316  }
   317  
   318  func TestUnionSameNodes(t *testing.T) {
   319  	a := NewNodes()
   320  	b := NewNodes()
   321  
   322  	a.Add(node(1))
   323  	a.Add(node(2))
   324  
   325  	b.Add(node(1))
   326  	b.Add(node(2))
   327  
   328  	c := UnionOfNodes(a, b)
   329  
   330  	if c.Count() != 2 {
   331  		t.Error("Union of same sets yields set with wrong len")
   332  	}
   333  
   334  	if !c.Has(node(1)) || !c.Has(node(2)) {
   335  		t.Error("Union of same sets yields wrong elements")
   336  	}
   337  
   338  	for i, s := range []Nodes{a, b, c} {
   339  		for e, n := range s {
   340  			if e != n.ID() {
   341  				t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID())
   342  			}
   343  		}
   344  	}
   345  }
   346  
   347  func TestUnionDiffNodes(t *testing.T) {
   348  	a := NewNodes()
   349  	b := NewNodes()
   350  
   351  	a.Add(node(1))
   352  	a.Add(node(2))
   353  
   354  	b.Add(node(3))
   355  
   356  	c := UnionOfNodes(a, b)
   357  
   358  	if c.Count() != 3 {
   359  		t.Error("Union of different sets yields set with wrong len")
   360  	}
   361  
   362  	if !c.Has(node(1)) || !c.Has(node(2)) || !c.Has(node(3)) {
   363  		t.Error("Union of different sets yields set with wrong elements")
   364  	}
   365  
   366  	if a.Has(node(3)) || !a.Has(node(2)) || !a.Has(node(1)) || a.Count() != 2 {
   367  		t.Error("Union of sets mutates non-destination set (argument 1)")
   368  	}
   369  
   370  	if !b.Has(node(3)) || b.Has(node(1)) || b.Has(node(2)) || b.Count() != 1 {
   371  		t.Error("Union of sets mutates non-destination set (argument 2)")
   372  	}
   373  
   374  	for i, s := range []Nodes{a, b, c} {
   375  		for e, n := range s {
   376  			if e != n.ID() {
   377  				t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID())
   378  			}
   379  		}
   380  	}
   381  
   382  	c = UnionOfNodes(a, a)
   383  	if !reflect.DeepEqual(c, a) {
   384  		t.Errorf("Union of equal sets not equal to sets: %v != %v", c, a)
   385  	}
   386  }
   387  
   388  func TestUnionOverlappingNodes(t *testing.T) {
   389  	a := NewNodes()
   390  	b := NewNodes()
   391  
   392  	a.Add(node(1))
   393  	a.Add(node(2))
   394  
   395  	b.Add(node(2))
   396  	b.Add(node(3))
   397  
   398  	c := UnionOfNodes(a, b)
   399  
   400  	if c.Count() != 3 {
   401  		t.Error("Union of overlapping sets yields set with wrong len")
   402  	}
   403  
   404  	if !c.Has(node(1)) || !c.Has(node(2)) || !c.Has(node(3)) {
   405  		t.Error("Union of overlapping sets yields set with wrong elements")
   406  	}
   407  
   408  	if a.Has(node(3)) || !a.Has(node(2)) || !a.Has(node(1)) || a.Count() != 2 {
   409  		t.Error("Union of sets mutates non-destination set (argument 1)")
   410  	}
   411  
   412  	if !b.Has(node(3)) || b.Has(node(1)) || !b.Has(node(2)) || b.Count() != 2 {
   413  		t.Error("Union of sets mutates non-destination set (argument 2)")
   414  	}
   415  
   416  	for i, s := range []Nodes{a, b, c} {
   417  		for e, n := range s {
   418  			if e != n.ID() {
   419  				t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID())
   420  			}
   421  		}
   422  	}
   423  
   424  	c = IntersectionOfNodes(a, a)
   425  	if !reflect.DeepEqual(c, a) {
   426  		t.Errorf("Intersection of equal sets not equal to sets: %v != %v", c, a)
   427  	}
   428  }
   429  
   430  func TestIntersectSameNodes(t *testing.T) {
   431  	a := NewNodes()
   432  	b := NewNodes()
   433  
   434  	a.Add(node(2))
   435  	a.Add(node(3))
   436  
   437  	b.Add(node(2))
   438  	b.Add(node(3))
   439  
   440  	c := IntersectionOfNodes(a, b)
   441  
   442  	if card := c.Count(); card != 2 {
   443  		t.Errorf("Intersection of identical sets yields set of wrong len %d", card)
   444  	}
   445  
   446  	if !c.Has(node(2)) || !c.Has(node(3)) {
   447  		t.Error("Intersection of identical sets yields set of wrong elements")
   448  	}
   449  
   450  	for i, s := range []Nodes{a, b, c} {
   451  		for e, n := range s {
   452  			if e != n.ID() {
   453  				t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID())
   454  			}
   455  		}
   456  	}
   457  }
   458  
   459  func TestIntersectDiffNodes(t *testing.T) {
   460  	a := NewNodes()
   461  	b := NewNodes()
   462  
   463  	a.Add(node(2))
   464  	a.Add(node(3))
   465  
   466  	b.Add(node(1))
   467  	b.Add(node(4))
   468  
   469  	c := IntersectionOfNodes(a, b)
   470  
   471  	if card := c.Count(); card != 0 {
   472  		t.Errorf("Intersection of different yields non-empty set %d", card)
   473  	}
   474  
   475  	if !a.Has(node(2)) || !a.Has(node(3)) || a.Has(node(1)) || a.Has(node(4)) || a.Count() != 2 {
   476  		t.Error("Intersection of sets mutates non-destination set (argument 1)")
   477  	}
   478  
   479  	if b.Has(node(2)) || b.Has(node(3)) || !b.Has(node(1)) || !b.Has(node(4)) || b.Count() != 2 {
   480  		t.Error("Intersection of sets mutates non-destination set (argument 1)")
   481  	}
   482  
   483  	for i, s := range []Nodes{a, b, c} {
   484  		for e, n := range s {
   485  			if e != n.ID() {
   486  				t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID())
   487  			}
   488  		}
   489  	}
   490  }
   491  
   492  func TestIntersectOverlappingNodes(t *testing.T) {
   493  	a := NewNodes()
   494  	b := NewNodes()
   495  
   496  	a.Add(node(2))
   497  	a.Add(node(3))
   498  
   499  	b.Add(node(3))
   500  	b.Add(node(4))
   501  
   502  	c := IntersectionOfNodes(a, b)
   503  
   504  	if card := c.Count(); card != 1 {
   505  		t.Errorf("Intersection of overlapping sets yields set of incorrect len %d", card)
   506  	}
   507  
   508  	if !c.Has(node(3)) {
   509  		t.Errorf("Intersection of overlapping sets yields set with wrong element")
   510  	}
   511  
   512  	if !a.Has(node(2)) || !a.Has(node(3)) || a.Has(node(4)) || a.Count() != 2 {
   513  		t.Error("Intersection of sets mutates non-destination set (argument 1)")
   514  	}
   515  
   516  	if b.Has(node(2)) || !b.Has(node(3)) || !b.Has(node(4)) || b.Count() != 2 {
   517  		t.Error("Intersection of sets mutates non-destination set (argument 1)")
   518  	}
   519  
   520  	for i, s := range []Nodes{a, b, c} {
   521  		for e, n := range s {
   522  			if e != n.ID() {
   523  				t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID())
   524  			}
   525  		}
   526  	}
   527  
   528  	c = IntersectionOfNodes(c, a)
   529  	want := Nodes{3: node(3)}
   530  	if !reflect.DeepEqual(c, want) {
   531  		t.Errorf("Intersection of sets with dst equal to a not equal: %v != %v", c, want)
   532  	}
   533  	c = IntersectionOfNodes(a, c)
   534  	if !reflect.DeepEqual(c, want) {
   535  		t.Errorf("Intersection of sets with dst equal to a not equal: %v != %v", c, want)
   536  	}
   537  }