github.com/enetx/g@v1.0.80/tests/set_test.go (about)

     1  package g_test
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/enetx/g"
     9  )
    10  
    11  func TestSetOf(t *testing.T) {
    12  	// Test empty values
    13  	emptySet := g.SetOf[int]()
    14  	if emptySet.Len() != 0 {
    15  		t.Errorf("Expected empty set size to be 0, got %d", emptySet.Len())
    16  	}
    17  
    18  	// Test single value
    19  	singleSet := g.SetOf(42)
    20  	if singleSet.Len() != 1 {
    21  		t.Errorf("Expected single set size to be 1, got %d", singleSet.Len())
    22  	}
    23  	if !singleSet.Contains(42) {
    24  		t.Errorf("Expected single set to contain value 42")
    25  	}
    26  
    27  	// Test multiple values
    28  	multiSet := g.SetOf(1, 2, 3, 4, 5)
    29  	expectedValues := []int{1, 2, 3, 4, 5}
    30  	for _, v := range expectedValues {
    31  		if !multiSet.Contains(v) {
    32  			t.Errorf("Expected multi set to contain value %d", v)
    33  		}
    34  	}
    35  
    36  	// Test duplicate values
    37  	duplicateSet := g.SetOf(1, 1, 2, 2, 3, 3)
    38  	if duplicateSet.Len() != 3 {
    39  		t.Errorf("Expected duplicate set size to be 3, got %d", duplicateSet.Len())
    40  	}
    41  }
    42  
    43  func TestSetDifference(t *testing.T) {
    44  	set1 := g.SetOf(1, 2, 3, 4)
    45  	set2 := g.SetOf(3, 4, 5, 6)
    46  	set5 := g.SetOf(1, 2)
    47  	set6 := g.SetOf(2, 3, 4)
    48  
    49  	set3 := set1.Difference(set2).Collect()
    50  	set4 := set2.Difference(set1).Collect()
    51  	set7 := set5.Difference(set6).Collect()
    52  	set8 := set6.Difference(set5).Collect()
    53  
    54  	if set3.Len() != 2 || set3.Ne(g.SetOf(1, 2)) {
    55  		t.Errorf("Unexpected result: %v", set3)
    56  	}
    57  
    58  	if set4.Len() != 2 || set4.Ne(g.SetOf(5, 6)) {
    59  		t.Errorf("Unexpected result: %v", set4)
    60  	}
    61  
    62  	if set7.Len() != 1 || set7.Ne(g.SetOf(1)) {
    63  		t.Errorf("Unexpected result: %v", set7)
    64  	}
    65  
    66  	if set8.Len() != 2 || set8.Ne(g.SetOf(3, 4)) {
    67  		t.Errorf("Unexpected result: %v", set8)
    68  	}
    69  }
    70  
    71  func TestSetSymmetricDifference(t *testing.T) {
    72  	set1 := g.NewSet[int](10)
    73  	set2 := set1.Clone()
    74  	result := set1.SymmetricDifference(set2).Collect()
    75  
    76  	if !result.Empty() {
    77  		t.Errorf("SymmetricDifference between equal sets should be empty, got %v", result)
    78  	}
    79  
    80  	set1 = g.SetOf(0, 1, 2, 3, 4)
    81  	set2 = g.SetOf(5, 6, 7, 8, 9)
    82  	result = set1.SymmetricDifference(set2).Collect()
    83  	expected := set1.Union(set2).Collect()
    84  
    85  	if !result.Eq(expected) {
    86  		t.Errorf(
    87  			"SymmetricDifference between disjoint sets should be their union, expected %v but got %v",
    88  			expected,
    89  			result,
    90  		)
    91  	}
    92  
    93  	set1 = g.SetOf(0, 1, 2, 3, 4, 5)
    94  	set2 = g.SetOf(4, 5, 6, 7, 8)
    95  	result = set1.SymmetricDifference(set2).Collect()
    96  	expected2 := g.SetOf(0, 1, 2, 3, 6, 7, 8)
    97  
    98  	if !result.Eq(expected2) {
    99  		t.Errorf(
   100  			"SymmetricDifference between sets with common elements should be correct, expected %v but got %v",
   101  			expected,
   102  			result,
   103  		)
   104  	}
   105  }
   106  
   107  func TestSetSubset(t *testing.T) {
   108  	tests := []struct {
   109  		name  string
   110  		s     g.Set[int]
   111  		other g.Set[int]
   112  		want  bool
   113  	}{
   114  		{
   115  			name:  "test_subset_1",
   116  			s:     g.SetOf(1, 2, 3),
   117  			other: g.SetOf(1, 2, 3, 4, 5),
   118  			want:  true,
   119  		},
   120  		{
   121  			name:  "test_subset_2",
   122  			s:     g.SetOf(1, 2, 3, 4),
   123  			other: g.SetOf(1, 2, 3),
   124  			want:  false,
   125  		},
   126  		{
   127  			name:  "test_subset_3",
   128  			s:     g.SetOf(5, 4, 3, 2, 1),
   129  			other: g.SetOf(1, 2, 3, 4, 5),
   130  			want:  true,
   131  		},
   132  	}
   133  
   134  	for _, tt := range tests {
   135  		t.Run(tt.name, func(t *testing.T) {
   136  			if got := tt.s.Subset(tt.other); got != tt.want {
   137  				t.Errorf("Subset() = %v, want %v", got, tt.want)
   138  			}
   139  		})
   140  	}
   141  }
   142  
   143  func TestSetSuperset(t *testing.T) {
   144  	tests := []struct {
   145  		name  string
   146  		s     g.Set[int]
   147  		other g.Set[int]
   148  		want  bool
   149  	}{
   150  		{
   151  			name:  "test_superset_1",
   152  			s:     g.SetOf(1, 2, 3, 4, 5),
   153  			other: g.SetOf(1, 2, 3),
   154  			want:  true,
   155  		},
   156  		{
   157  			name:  "test_superset_2",
   158  			s:     g.SetOf(1, 2, 3),
   159  			other: g.SetOf(1, 2, 3, 4),
   160  			want:  false,
   161  		},
   162  		{
   163  			name:  "test_superset_3",
   164  			s:     g.SetOf(1, 2, 3, 4, 5),
   165  			other: g.SetOf(5, 4, 3, 2, 1),
   166  			want:  true,
   167  		},
   168  	}
   169  
   170  	for _, tt := range tests {
   171  		t.Run(tt.name, func(t *testing.T) {
   172  			if got := tt.s.Superset(tt.other); got != tt.want {
   173  				t.Errorf("Superset() = %v, want %v", got, tt.want)
   174  			}
   175  		})
   176  	}
   177  }
   178  
   179  func TestSetEq(t *testing.T) {
   180  	tests := []struct {
   181  		name  string
   182  		s     g.Set[int]
   183  		other g.Set[int]
   184  		want  bool
   185  	}{
   186  		{
   187  			name:  "test_eq_1",
   188  			s:     g.SetOf(1, 2, 3),
   189  			other: g.SetOf(1, 2, 3),
   190  			want:  true,
   191  		},
   192  		{
   193  			name:  "test_eq_2",
   194  			s:     g.SetOf(1, 2, 3),
   195  			other: g.SetOf(1, 2, 4),
   196  			want:  false,
   197  		},
   198  		{
   199  			name:  "test_eq_3",
   200  			s:     g.SetOf(1, 2, 3),
   201  			other: g.SetOf(3, 2, 1),
   202  			want:  true,
   203  		},
   204  		{
   205  			name:  "test_eq_4",
   206  			s:     g.SetOf(1, 2, 3, 4),
   207  			other: g.SetOf(1, 2, 3),
   208  			want:  false,
   209  		},
   210  	}
   211  
   212  	for _, tt := range tests {
   213  		t.Run(tt.name, func(t *testing.T) {
   214  			if got := tt.s.Eq(tt.other); got != tt.want {
   215  				t.Errorf("Eq() = %v, want %v", got, tt.want)
   216  			}
   217  		})
   218  	}
   219  }
   220  
   221  func TestSetRemove(t *testing.T) {
   222  	// Test case 1: Remove a single value
   223  	set := g.SetOf(1, 2, 3)
   224  	set.Remove(2)
   225  	if _, ok := set[2]; ok {
   226  		t.Errorf("Set should not contain value 2 after removal")
   227  	}
   228  
   229  	// Test case 2: Remove multiple values
   230  	set2 := g.SetOf("a", "b", "c")
   231  	set2.Remove("a", "c")
   232  	if _, ok := set2["a"]; ok {
   233  		t.Errorf("Set should not contain value 'a' after removal")
   234  	}
   235  	if _, ok := set2["c"]; ok {
   236  		t.Errorf("Set should not contain value 'c' after removal")
   237  	}
   238  
   239  	// Test case 3: Remove non-existent value
   240  	set3 := g.SetOf(1.1, 2.2)
   241  	set3.Remove(3.3)
   242  	if len(set3) != 2 {
   243  		t.Errorf("Set should not be modified when removing non-existent value")
   244  	}
   245  }
   246  
   247  func TestSetContainsAny(t *testing.T) {
   248  	// Test case 1: Set contains some elements from another set
   249  	set1 := g.SetOf(1, 2, 3)
   250  	set2 := g.SetOf(2, 4, 6)
   251  	if !set1.ContainsAny(set2) {
   252  		t.Errorf("Expected Set to contain at least one element from the other Set")
   253  	}
   254  
   255  	// Test case 2: Set doesn't contain any elements from another set
   256  	set3 := g.SetOf("a", "b")
   257  	set4 := g.SetOf("c", "d", "e")
   258  	if set3.ContainsAny(set4) {
   259  		t.Errorf("Expected Set not to contain any elements from the other Set")
   260  	}
   261  
   262  	// Test case 3: Empty sets
   263  	set5 := g.Set[float64]{}
   264  	set6 := g.Set[float64]{}
   265  	if set5.ContainsAny(set6) {
   266  		t.Errorf("Expected empty sets not to contain any elements from each other")
   267  	}
   268  }
   269  
   270  func TestSetContainsAll(t *testing.T) {
   271  	// Test case 1: Set contains all elements from another set
   272  	set1 := g.SetOf(1, 2, 3)
   273  	set2 := g.SetOf(2, 3)
   274  	if !set1.ContainsAll(set2) {
   275  		t.Errorf("Expected Set to contain all elements from the other Set")
   276  	}
   277  
   278  	// Test case 2: Set doesn't contain all elements from another set
   279  	set3 := g.SetOf("a", "b", "c")
   280  	set4 := g.SetOf("b", "d")
   281  	if set3.ContainsAll(set4) {
   282  		t.Errorf("Expected Set not to contain all elements from the other Set")
   283  	}
   284  
   285  	// Test case 3: Empty sets
   286  	set5 := g.SetOf[float64]()
   287  	set6 := g.SetOf(1.1, 2.2)
   288  	if set5.ContainsAll(set6) {
   289  		t.Errorf("Expected empty Set not to contain all elements from another non-empty Set")
   290  	}
   291  }
   292  
   293  func TestSetToSlice(t *testing.T) {
   294  	// Test case 1: Set with elements
   295  	set1 := g.SetOf(1, 2, 3)
   296  	expected1 := g.Slice[int]{1, 2, 3}
   297  	slice1 := set1.ToSlice()
   298  	if len(slice1) != len(expected1) {
   299  		t.Errorf("Expected length of slice to be %d, got %d", len(expected1), len(slice1))
   300  	}
   301  
   302  	// Test case 2: Empty Set
   303  	set2 := g.NewSet[string]()
   304  	expected2 := g.Slice[string]{}
   305  	slice2 := set2.ToSlice()
   306  	if len(slice2) != len(expected2) {
   307  		t.Errorf("Expected length of slice to be %d, got %d", len(expected2), len(slice2))
   308  	}
   309  }
   310  
   311  func TestSetString(t *testing.T) {
   312  	// Test case 1: Set with elements
   313  	set1 := g.NewSet[int]()
   314  	set1.Add(1)
   315  	expected1 := "Set{1}"
   316  	if str := set1.String(); str != expected1 {
   317  		t.Errorf("Expected string representation to be %s, got %s", expected1, str)
   318  	}
   319  
   320  	// Test case 2: Empty Set
   321  	set2 := g.NewSet[int]()
   322  	expected2 := "Set{}"
   323  	if str := set2.String(); str != expected2 {
   324  		t.Errorf("Expected string representation to be %s, got %s", expected2, str)
   325  	}
   326  }
   327  
   328  func TestSetClear(t *testing.T) {
   329  	// Test case 1: Set with elements
   330  	set1 := g.SetOf(1, 2, 3)
   331  	set1.Clear()
   332  	if len(set1) != 0 {
   333  		t.Errorf("Expected Set to be empty after calling Clear()")
   334  	}
   335  
   336  	// Test case 2: Empty Set
   337  	set2 := g.NewSet[int]()
   338  	set2.Clear()
   339  	if len(set2) != 0 {
   340  		t.Errorf("Expected Set to remain empty after calling Clear() on an empty Set")
   341  	}
   342  }
   343  
   344  func TestSetIntersection(t *testing.T) {
   345  	// Test case 1: Set with elements
   346  	set1 := g.SetOf(1, 2, 3, 4, 5)
   347  	set2 := g.SetOf(4, 5, 6, 7, 8)
   348  	expected := g.SetOf(4, 5)
   349  	intersection := set1.Intersection(set2).Collect()
   350  	if len(intersection) != len(expected) {
   351  		t.Errorf("Expected intersection to have length %d, got %d", len(expected), len(intersection))
   352  	}
   353  	for k := range intersection {
   354  		if _, exists := expected[k]; !exists {
   355  			t.Errorf("Unexpected element in intersection: %d", k)
   356  		}
   357  	}
   358  
   359  	// Test case 2: Empty Set
   360  	set3 := g.SetOf("a", "b")
   361  	set4 := g.NewSet[string]()
   362  	intersection2 := set3.Intersection(set4).Collect()
   363  	if len(intersection2) != 0 {
   364  		t.Errorf("Expected intersection of an empty set to be empty")
   365  	}
   366  
   367  	// Test case 3: Both sets empty
   368  	set5 := g.NewSet[int]()
   369  	set6 := g.NewSet[int]()
   370  	intersection = set5.Intersection(set6).Collect()
   371  	if len(intersection) != 0 {
   372  		t.Errorf("Expected intersection of two empty sets to be empty")
   373  	}
   374  }
   375  
   376  func TestSetUnion(t *testing.T) {
   377  	// Test case 1: Set with elements
   378  	set1 := g.SetOf(1, 2, 3)
   379  	set2 := g.SetOf(3, 4, 5)
   380  	expected := g.SetOf(1, 2, 3, 4, 5)
   381  	union := set1.Union(set2).Collect()
   382  	if len(union) != len(expected) {
   383  		t.Errorf("Expected union to have length %d, got %d", len(expected), len(union))
   384  	}
   385  	for k := range union {
   386  		if _, exists := expected[k]; !exists {
   387  			t.Errorf("Unexpected element in union: %d", k)
   388  		}
   389  	}
   390  
   391  	// Test case 2: Empty Set
   392  	set3 := g.SetOf("a", "b")
   393  	set4 := g.NewSet[string]()
   394  	union2 := set3.Union(set4).Collect()
   395  	if len(union2) != len(set3) {
   396  		t.Errorf("Expected union to be equal to the non-empty set")
   397  	}
   398  
   399  	// Test case 3: Both sets empty
   400  	set5 := g.NewSet[int]()
   401  	set6 := g.NewSet[int]()
   402  	union = set5.Union(set6).Collect()
   403  	if len(union) != 0 {
   404  		t.Errorf("Expected union of two empty sets to be empty")
   405  	}
   406  }
   407  
   408  func TestSetMap(t *testing.T) {
   409  	// Test case 1: Set with elements
   410  	set1 := g.SetOf(1, 2, 3)
   411  	expected := g.SetOf("1", "2", "3")
   412  	setMap := g.SetMap(set1, func(val int) string { return fmt.Sprintf("%d", val) })
   413  	if len(setMap) != len(expected) {
   414  		t.Errorf("Expected SetMap to have length %d, got %d", len(expected), len(setMap))
   415  	}
   416  	for k := range setMap {
   417  		if _, exists := expected[k]; !exists {
   418  			t.Errorf("Unexpected element in SetMap: %s", k)
   419  		}
   420  	}
   421  
   422  	// Test case 2: Empty Set
   423  	set2 := g.NewSet[int]()
   424  	setMap = g.SetMap(set2, func(val int) string { return fmt.Sprintf("%d", val) })
   425  	if len(setMap) != 0 {
   426  		t.Errorf("Expected SetMap of an empty set to be empty")
   427  	}
   428  }
   429  
   430  func TestSetIterCount(t *testing.T) {
   431  	// Test case 1: Counting elements in the set
   432  	seq := g.SetOf(1, 2, 3)
   433  	count := seq.Iter().Count()
   434  	if count != 3 {
   435  		t.Errorf("Expected count to be 3, got %d", count)
   436  	}
   437  
   438  	// Test case 2: Counting elements in an empty set
   439  	emptySeq := g.NewSet[int]()
   440  	emptyCount := emptySeq.Iter().Count()
   441  	if emptyCount != 0 {
   442  		t.Errorf("Expected count to be 0 for an empty set, got %d", emptyCount)
   443  	}
   444  }
   445  
   446  func TestSetIterRange(t *testing.T) {
   447  	// Test case 1: Stop iteration when function returns false
   448  	seq := g.SetOf(1, 2, 3, 4)
   449  	var result []int
   450  	seq.Iter().Range(func(v int) bool {
   451  		if v == 3 {
   452  			result = append(result, v)
   453  			return false
   454  		}
   455  		return true
   456  	})
   457  
   458  	expected := []int{3}
   459  	if len(result) != len(expected) {
   460  		t.Errorf("Expected %d elements, got %d", len(expected), len(result))
   461  	}
   462  
   463  	// Test case 2: Iterate over the entire set
   464  	emptySeq := g.NewSet[string]()
   465  	emptyResult := make([]string, 0)
   466  
   467  	emptySeq.Iter().Range(func(v string) bool {
   468  		emptyResult = append(emptyResult, v)
   469  		return true
   470  	})
   471  
   472  	if len(emptyResult) != 0 {
   473  		t.Errorf("Expected no elements in an empty set, got %d elements", len(emptyResult))
   474  	}
   475  }
   476  
   477  func TestSetIterFilter(t *testing.T) {
   478  	// Test case 1: Filter even numbers
   479  	seq := g.SetOf(1, 2, 3, 4, 5)
   480  
   481  	even := seq.Iter().Filter(func(v int) bool {
   482  		return v%2 == 0
   483  	}).Collect()
   484  
   485  	expected := g.SetOf(2, 4)
   486  	if len(even) != len(expected) {
   487  		t.Errorf("Expected %d elements, got %d", len(expected), len(even))
   488  	}
   489  	for k := range even {
   490  		if _, ok := expected[k]; !ok {
   491  			t.Errorf("Unexpected element %v in the result", k)
   492  		}
   493  	}
   494  
   495  	// Test case 2: Filter odd numbers
   496  	odd := seq.Iter().Filter(func(v int) bool {
   497  		return v%2 != 0
   498  	}).Collect()
   499  
   500  	oddExpected := g.SetOf(1, 3, 5)
   501  	if len(odd) != len(oddExpected) {
   502  		t.Errorf("Expected %d elements, got %d", len(oddExpected), len(odd))
   503  	}
   504  	for k := range odd {
   505  		if _, ok := oddExpected[k]; !ok {
   506  			t.Errorf("Unexpected element %v in the result", k)
   507  		}
   508  	}
   509  
   510  	// Test case 3: Filter all elements
   511  	all := seq.Iter().Filter(func(v int) bool {
   512  		return true
   513  	}).Collect()
   514  
   515  	if len(all) != len(seq) {
   516  		t.Errorf("Expected %d elements, got %d", len(seq), len(all))
   517  	}
   518  	for k := range all {
   519  		if _, ok := seq[k]; !ok {
   520  			t.Errorf("Unexpected element %v in the result", k)
   521  		}
   522  	}
   523  }
   524  
   525  func TestSetIterExclude(t *testing.T) {
   526  	// Test case 1: Exclude even numbers
   527  	seq := g.SetOf(1, 2, 3, 4, 5)
   528  	notEven := seq.Iter().Exclude(func(v int) bool {
   529  		return v%2 == 0
   530  	}).Collect()
   531  
   532  	expected := g.SetOf(1, 3, 5)
   533  	if len(notEven) != len(expected) {
   534  		t.Errorf("Expected %d elements, got %d", len(expected), len(notEven))
   535  	}
   536  	for k := range notEven {
   537  		if _, ok := expected[k]; !ok {
   538  			t.Errorf("Unexpected element %v in the result", k)
   539  		}
   540  	}
   541  
   542  	// Test case 2: Exclude odd numbers
   543  	notOdd := seq.Iter().Exclude(func(v int) bool {
   544  		return v%2 != 0
   545  	}).Collect()
   546  
   547  	notOddExpected := g.SetOf(2, 4)
   548  	if len(notOdd) != len(notOddExpected) {
   549  		t.Errorf("Expected %d elements, got %d", len(notOddExpected), len(notOdd))
   550  	}
   551  	for k := range notOdd {
   552  		if _, ok := notOddExpected[k]; !ok {
   553  			t.Errorf("Unexpected element %v in the result", k)
   554  		}
   555  	}
   556  
   557  	// Test case 3: Exclude all elements
   558  	empty := seq.Iter().Exclude(func(v int) bool {
   559  		return true
   560  	}).Collect()
   561  
   562  	if len(empty) != 0 {
   563  		t.Errorf("Expected 0 elements, got %d", len(empty))
   564  	}
   565  }
   566  
   567  func TestSetIterMap(t *testing.T) {
   568  	// Test case 1: Double each element
   569  	set := g.SetOf(1, 2, 3)
   570  	doubled := set.Iter().Map(func(val int) int {
   571  		return val * 2
   572  	}).Collect()
   573  
   574  	expected := g.SetOf(2, 4, 6)
   575  	if !reflect.DeepEqual(doubled, expected) {
   576  		t.Errorf("Expected set after doubling elements to be %v, got %v", expected, doubled)
   577  	}
   578  
   579  	// Test case 2: Square each element
   580  	set2 := g.SetOf(1, 2, 3)
   581  	squared := set2.Iter().Map(func(val int) int {
   582  		return val * val
   583  	}).Collect()
   584  
   585  	expected2 := g.SetOf(1, 4, 9)
   586  	if !reflect.DeepEqual(squared, expected2) {
   587  		t.Errorf("Expected set after squaring elements to be %v, got %v", expected2, squared)
   588  	}
   589  }
   590  
   591  func TestSetIterInspect(t *testing.T) {
   592  	// Define a set to iterate over
   593  	s := g.SetOf(1, 2, 3)
   594  
   595  	// Define a slice to store the inspected elements
   596  	inspectedElements := g.NewSet[int]()
   597  
   598  	// Create a new iterator with Inspect and collect the elements
   599  	s.Iter().Inspect(func(v int) {
   600  		inspectedElements.Add(v)
   601  	}).Collect()
   602  
   603  	if !inspectedElements.Eq(s) {
   604  		t.Errorf("Expected inspected elements to be equal to the set, got %v", inspectedElements)
   605  	}
   606  }