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