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