github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/collection/immutable/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/immutable"
    10  	oset "github.com/m4gshm/gollections/collection/immutable/ordered/set"
    11  	"github.com/m4gshm/gollections/collection/immutable/set"
    12  	"github.com/m4gshm/gollections/convert/as"
    13  	"github.com/m4gshm/gollections/convert/ptr"
    14  
    15  	"github.com/m4gshm/gollections/loop"
    16  	"github.com/m4gshm/gollections/op"
    17  	"github.com/m4gshm/gollections/slice"
    18  	"github.com/m4gshm/gollections/slice/sort"
    19  	"github.com/m4gshm/gollections/walk/group"
    20  )
    21  
    22  func Test_Set_From(t *testing.T) {
    23  	set := set.From(loop.Of(1, 1, 2, 2, 3, 4, 3, 2, 1))
    24  	assert.Equal(t, slice.Of(1, 2, 3, 4), sort.Asc(set.Slice()))
    25  }
    26  
    27  func Test_Set_Iterate(t *testing.T) {
    28  	set := set.Of(1, 1, 2, 4, 3, 1)
    29  	values := sort.Asc(set.Slice())
    30  
    31  	assert.Equal(t, 4, len(values))
    32  
    33  	expected := slice.Of(1, 2, 3, 4)
    34  	assert.Equal(t, expected, values)
    35  
    36  	loopSlice := sort.Asc(loop.Slice(ptr.Of(set.Head()).Next))
    37  	assert.Equal(t, expected, loopSlice)
    38  
    39  	out := make(map[int]int, 0)
    40  	for it, v, ok := set.First(); ok; v, ok = it.Next() {
    41  		out[v] = v
    42  	}
    43  
    44  	assert.Equal(t, len(expected), len(out))
    45  	for k := range out {
    46  		assert.True(t, set.Contains(k))
    47  	}
    48  
    49  	out = make(map[int]int, 0)
    50  	set.ForEach(func(n int) { out[n] = n })
    51  
    52  	assert.Equal(t, len(expected), len(out))
    53  	for k := range out {
    54  		assert.True(t, set.Contains(k))
    55  	}
    56  }
    57  
    58  func Test_Set_Contains(t *testing.T) {
    59  	set := set.Of(1, 1, 2, 4, 3, 1)
    60  	assert.True(t, set.Contains(1))
    61  	assert.True(t, set.Contains(2))
    62  	assert.True(t, set.Contains(4))
    63  	assert.True(t, set.Contains(3))
    64  	assert.False(t, set.Contains(0))
    65  	assert.False(t, set.Contains(-1))
    66  }
    67  
    68  func Test_Set_FilterMapReduce(t *testing.T) {
    69  	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])
    70  	assert.Equal(t, 12, s)
    71  }
    72  
    73  func Test_Set_Group_By_Walker(t *testing.T) {
    74  	groups := group.Of(set.Of(0, 1, 1, 2, 4, 3, 1, 6, 7), func(e int) bool { return e%2 == 0 })
    75  
    76  	fg := sort.Asc(groups[false])
    77  	tg := sort.Asc(groups[true])
    78  	assert.Equal(t, len(groups), 2)
    79  	assert.Equal(t, []int{1, 3, 7}, fg)
    80  	assert.Equal(t, []int{0, 2, 4, 6}, tg)
    81  }
    82  
    83  func Test_Set_Group_By_Iterator(t *testing.T) {
    84  	groups := loop.Group(set.Of(0, 1, 1, 2, 4, 3, 1, 6, 7).Loop(), func(e int) bool { return e%2 == 0 }, as.Is[int])
    85  
    86  	assert.Equal(t, len(groups), 2)
    87  	fg := sort.Asc(groups[false])
    88  
    89  	tg := sort.Asc(groups[true])
    90  
    91  	assert.Equal(t, []int{1, 3, 7}, fg)
    92  	assert.Equal(t, []int{0, 2, 4, 6}, tg)
    93  }
    94  
    95  func Test_Set_Sort(t *testing.T) {
    96  	var (
    97  		elements = set.Of(3, 3, 1, 1, 1, 5, 6, 8, 8, 0, -2, -2)
    98  		sorted   = elements.Sort(func(e1, e2 int) int { return e1 - e2 })
    99  	)
   100  	assert.Equal(t, oset.Of(-2, 0, 1, 3, 5, 6, 8), sorted)
   101  }
   102  
   103  func Test_Set_SortStructByField(t *testing.T) {
   104  	var (
   105  		anonymous = &user{"Anonymous", 0}
   106  		cherlie   = &user{"Cherlie", 25}
   107  		alise     = &user{"Alise", 20}
   108  		bob       = &user{"Bob", 19}
   109  
   110  		elements     = set.Of(anonymous, cherlie, alise, bob)
   111  		sortedByName = set.Sort(elements, (*user).Name)
   112  		sortedByAge  = set.Sort(elements, (*user).Age)
   113  	)
   114  	assert.Equal(t, oset.Of(alise, anonymous, bob, cherlie), sortedByName)
   115  	assert.Equal(t, oset.Of(anonymous, bob, alise, cherlie), sortedByAge)
   116  }
   117  
   118  func Test_Set_Convert(t *testing.T) {
   119  	var (
   120  		ints     = set.Of(3, 3, 1, 1, 1, 5, 6, 8, 8, 0, -2, -2)
   121  		strings  = sort.Asc(loop.Slice(loop.Filter(set.Convert(ints, strconv.Itoa), func(s string) bool { return len(s) == 1 })))
   122  		strings2 = sort.Asc(set.Convert(ints, strconv.Itoa).Filter(func(s string) bool { return len(s) == 1 }).Slice())
   123  	)
   124  	assert.Equal(t, slice.Of("0", "1", "3", "5", "6", "8"), strings)
   125  	assert.Equal(t, strings, strings2)
   126  }
   127  
   128  func Test_Set_Flatt(t *testing.T) {
   129  
   130  	var (
   131  		ints        = set.Of(3, 3, 1, 1, 1, 5, 6, 8, 8, 0, -2, -2)
   132  		fints       = set.Flat(ints, func(i int) []int { return slice.Of(i) })
   133  		convFilt    = loop.Convert(fints, strconv.Itoa).Filter(func(s string) bool { return len(s) == 1 })
   134  		stringsPipe = loop.Filter(convFilt, func(s string) bool { return len(s) == 1 })
   135  	)
   136  	assert.Equal(t, slice.Of("0", "1", "3", "5", "6", "8"), sort.Asc(stringsPipe.Slice()))
   137  }
   138  
   139  func Test_Set_DoubleConvert(t *testing.T) {
   140  	var (
   141  		ints               = set.Of(3, 1, 5, 6, 8, 0, -2)
   142  		stringsPipe        = set.Convert(ints, strconv.Itoa).Filter(func(s string) bool { return len(s) == 1 })
   143  		prefixedStrinsPipe = loop.Convert(stringsPipe, func(s string) string { return "_" + s })
   144  	)
   145  	assert.Equal(t, slice.Of("_0", "_1", "_3", "_5", "_6", "_8"), sort.Asc(prefixedStrinsPipe.Slice()))
   146  
   147  	//second call do nothing
   148  	var no []string
   149  	assert.Equal(t, no, stringsPipe.Slice())
   150  }
   151  
   152  func Test_Set_Zero(t *testing.T) {
   153  	var set immutable.Set[int]
   154  
   155  	assert.False(t, set.Contains(1))
   156  
   157  	set.IsEmpty()
   158  	set.Len()
   159  
   160  	set.For(nil)
   161  	set.ForEach(nil)
   162  
   163  	set.Slice()
   164  
   165  	set.Convert(nil)
   166  	set.Filter(nil)
   167  
   168  	head := set.Head()
   169  	_, ok := head.Next()
   170  	assert.False(t, ok)
   171  
   172  	_, _, ok = set.First()
   173  	assert.False(t, ok)
   174  
   175  }
   176  
   177  type user struct {
   178  	name string
   179  	age  int
   180  }
   181  
   182  func (u *user) Name() string { return u.name }
   183  func (u *user) Age() int     { return u.age }