github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/collection/mutable/ordered/set/test/set_test.go (about)

     1  package test
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/m4gshm/gollections/collection/mutable/ordered"
    10  	"github.com/m4gshm/gollections/collection/mutable/ordered/set"
    11  	"github.com/m4gshm/gollections/collection/mutable/vector"
    12  	"github.com/m4gshm/gollections/convert/ptr"
    13  
    14  	"github.com/m4gshm/gollections/loop"
    15  	"github.com/m4gshm/gollections/op"
    16  	"github.com/m4gshm/gollections/predicate/eq"
    17  	"github.com/m4gshm/gollections/slice"
    18  	"github.com/m4gshm/gollections/walk/group"
    19  )
    20  
    21  func Test_Set_From(t *testing.T) {
    22  	set := set.From(loop.Of(1, 1, 2, 2, 3, 4, 3, 2, 1))
    23  	assert.Equal(t, slice.Of(1, 2, 3, 4), set.Slice())
    24  }
    25  
    26  func Test_Set_Iterate(t *testing.T) {
    27  	set := set.Of(1, 1, 2, 4, 3, 1)
    28  	values := set.Slice()
    29  
    30  	assert.Equal(t, 4, len(values))
    31  
    32  	expected := slice.Of(1, 2, 4, 3)
    33  	assert.Equal(t, expected, values)
    34  
    35  	iterSlice := loop.Slice[int](set.Loop())
    36  	assert.Equal(t, expected, iterSlice)
    37  
    38  	loopSlice := loop.Slice(ptr.Of(set.Head()).Next)
    39  	assert.Equal(t, expected, loopSlice)
    40  
    41  	out := make([]int, 0)
    42  	next := set.Loop()
    43  	for v, ok := next(); ok; v, ok = next() {
    44  		out = append(out, v)
    45  	}
    46  	assert.Equal(t, expected, out)
    47  
    48  	out = make([]int, 0)
    49  	set.ForEach(func(v int) { out = append(out, v) })
    50  }
    51  
    52  func Test_Set_AddVerify(t *testing.T) {
    53  	set := set.NewCap[int](0)
    54  	added := set.AddNew(1, 2, 4, 3)
    55  	assert.Equal(t, added, true)
    56  	added = set.AddOneNew(1)
    57  	assert.Equal(t, added, false)
    58  
    59  	values := set.Slice()
    60  
    61  	assert.Equal(t, slice.Of(1, 2, 4, 3), values)
    62  }
    63  
    64  func Test_Set_AddAll(t *testing.T) {
    65  	set := set.NewCap[int](0)
    66  	set.AddAll(vector.Of(1, 2))
    67  	set.AddAll(vector.Of(4, 3))
    68  
    69  	values := set.Slice()
    70  
    71  	assert.Equal(t, slice.Of(1, 2, 4, 3), values)
    72  }
    73  
    74  func Test_Set_AddAllNew(t *testing.T) {
    75  	set := set.NewCap[int](0)
    76  	added := set.AddAllNew(vector.Of(1, 2))
    77  	assert.True(t, added)
    78  	//4, 3 are new
    79  	added = set.AddAllNew(vector.Of(1, 4, 3))
    80  	assert.True(t, added)
    81  	added = set.AddAllNew(vector.Of(2, 4, 3))
    82  	assert.False(t, added)
    83  
    84  	values := set.Slice()
    85  	assert.Equal(t, slice.Of(1, 2, 4, 3), values)
    86  }
    87  
    88  func Test_Set_Delete(t *testing.T) {
    89  	set := set.Of(1, 1, 2, 4, 3, 1)
    90  	values := set.Slice()
    91  
    92  	for _, v := range values {
    93  		set.Delete(v)
    94  	}
    95  
    96  	assert.Equal(t, 0, len(set.Slice()))
    97  }
    98  
    99  func Test_Set_DeleteByIterator(t *testing.T) {
   100  	set := set.Of(1, 1, 2, 4, 3, 1)
   101  	iterator := set.IterEdit()
   102  
   103  	i := 0
   104  	for _, ok := iterator.Next(); ok; _, ok = iterator.Next() {
   105  		i++
   106  		iterator.Delete()
   107  	}
   108  
   109  	assert.Equal(t, 4, i)
   110  	assert.Equal(t, 0, len(set.Slice()))
   111  }
   112  
   113  func Test_Set_FilterMapReduce(t *testing.T) {
   114  	s := set.Of(1, 1, 2, 4, 3, 1).Filter(func(i int) bool { return i%2 == 0 }).Convert(func(i int) int { return i * 2 }).Reduce(op.Sum[int])
   115  	assert.Equal(t, 12, s)
   116  }
   117  
   118  func Test_Set_Group(t *testing.T) {
   119  	groups := group.Of(set.Of(0, 1, 1, 2, 4, 3, 1, 6, 7), func(e int) bool { return e%2 == 0 })
   120  
   121  	assert.Equal(t, len(groups), 2)
   122  	assert.Equal(t, []int{1, 3, 7}, groups[false])
   123  	assert.Equal(t, []int{0, 2, 4, 6}, groups[true])
   124  }
   125  
   126  func Test_Set_Convert(t *testing.T) {
   127  	var (
   128  		ints     = set.Of(3, 3, 1, 1, 1, 5, 6, 8, 8, 0, -2, -2)
   129  		strings  = loop.Slice[string](loop.Filter(set.Convert(ints, strconv.Itoa), func(s string) bool { return len(s) == 1 }))
   130  		strings2 = set.Convert(ints, strconv.Itoa).Filter(func(s string) bool { return len(s) == 1 }).Slice()
   131  	)
   132  	assert.Equal(t, slice.Of("3", "1", "5", "6", "8", "0"), strings)
   133  	assert.Equal(t, strings, strings2)
   134  }
   135  
   136  func Test_Set_Flatt(t *testing.T) {
   137  	var (
   138  		ints        = set.Of(3, 3, 1, 1, 1, 5, 6, 8, 8, 0, -2, -2)
   139  		fints       = set.Flat(ints, func(i int) []int { return slice.Of(i) })
   140  		stringsPipe = loop.Filter(loop.Convert(fints, strconv.Itoa).Filter(func(s string) bool { return len(s) == 1 }), func(s string) bool { return len(s) == 1 })
   141  	)
   142  	assert.Equal(t, slice.Of("3", "1", "5", "6", "8", "0"), stringsPipe.Slice())
   143  }
   144  
   145  func Test_Set_DoubleConvert(t *testing.T) {
   146  	var (
   147  		ints               = set.Of(3, 1, 5, 6, 8, 0, -2)
   148  		stringsPipe        = set.Convert(ints, strconv.Itoa).Filter(func(s string) bool { return len(s) == 1 })
   149  		prefixedStrinsPipe = loop.Convert(stringsPipe, func(s string) string { return "_" + s })
   150  	)
   151  	assert.Equal(t, slice.Of("_3", "_1", "_5", "_6", "_8", "_0"), prefixedStrinsPipe.Slice())
   152  
   153  	//second call do nothing
   154  	var no []string
   155  	assert.Equal(t, no, stringsPipe.Slice())
   156  }
   157  
   158  func Test_Set_Nil(t *testing.T) {
   159  	var set *ordered.Set[int]
   160  	var nils []int
   161  	set.Add(1, 2, 3)
   162  	set.Add(nils...)
   163  	set.AddOne(4)
   164  	set.AddAll(set)
   165  
   166  	set.Delete(1, 2, 3)
   167  	set.Delete(nils...)
   168  	set.DeleteOne(4)
   169  
   170  	set.IsEmpty()
   171  	set.Len()
   172  
   173  	_ = set.For(nil)
   174  	set.ForEach(nil)
   175  
   176  	set.Slice()
   177  
   178  	head := set.Head()
   179  	_, ok := head.Next()
   180  	assert.False(t, ok)
   181  	head.Delete()
   182  }
   183  
   184  func Test_Set_Zero(t *testing.T) {
   185  	var mset ordered.Set[int]
   186  	var nils []int
   187  	mset.Add(1, 2, 3)
   188  	assert.False(t, mset.IsEmpty())
   189  	mset.Add(nils...)
   190  	mset.AddOne(4)
   191  	mset.AddAll(&mset)
   192  
   193  	assert.Equal(t, slice.Of(1, 2, 3, 4), mset.Slice())
   194  
   195  	mset.Delete(1, 2, 3)
   196  	mset.Delete(nils...)
   197  	mset.DeleteOne(4)
   198  
   199  	assert.True(t, mset.IsEmpty())
   200  	assert.Equal(t, 0, mset.Len())
   201  
   202  	mset.For(nil)
   203  	mset.ForEach(nil)
   204  
   205  	head := mset.Head()
   206  	_, ok := head.Next()
   207  	assert.False(t, ok)
   208  	head.Delete()
   209  }
   210  
   211  func Test_Set_new(t *testing.T) {
   212  	var mset = new(ordered.Set[int])
   213  	var nils []int
   214  	mset.Add(1, 2, 3)
   215  	assert.False(t, mset.IsEmpty())
   216  	mset.Add(nils...)
   217  	mset.AddOne(4)
   218  	mset.AddAll(mset)
   219  
   220  	s := mset.Slice()
   221  	assert.Equal(t, slice.Of(1, 2, 3, 4), s)
   222  
   223  	mset.Delete(1, 2, 3)
   224  	mset.Delete(nils...)
   225  	mset.DeleteOne(4)
   226  
   227  	assert.True(t, mset.IsEmpty())
   228  	assert.Equal(t, 0, mset.Len())
   229  
   230  	mset.For(nil)
   231  	mset.ForEach(nil)
   232  
   233  	head := mset.Head()
   234  	_, ok := head.Next()
   235  	assert.False(t, ok)
   236  	head.Delete()
   237  }
   238  
   239  func Test_Set_CopyByValue(t *testing.T) {
   240  	set := set.Of[int]()
   241  
   242  	set.Add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
   243  
   244  	s := copySet(*set)
   245  	set2 := &s
   246  
   247  	set2.Add(17, 18, 19, 20)
   248  
   249  	c := set.Contains(20)
   250  	assert.True(t, c)
   251  
   252  	heq := set.HasAny(eq.To(20))
   253  	assert.True(t, heq)
   254  }
   255  
   256  func copySet[T comparable](set ordered.Set[T]) ordered.Set[T] {
   257  	return set
   258  }
   259  
   260  func Test_Set_Sort(t *testing.T) {
   261  	ints := set.Of(3, 1, 5, 6, 8, 0, -2)
   262  	sorted := ints.Sort(op.Compare)
   263  
   264  	assert.Same(t, ints, sorted)
   265  
   266  	expected := ordered.NewSet(-2, 0, 1, 3, 5, 6, 8)
   267  	assert.Equal(t, expected, sorted)
   268  
   269  	ssorted := ints.StableSort(op.Compare)
   270  	assert.Equal(t, expected, ssorted)
   271  }