github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/collection/immutable/vector/test/vector_test.go (about) 1 package vector 2 3 import ( 4 "strconv" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 9 "github.com/m4gshm/gollections/collection/immutable" 10 "github.com/m4gshm/gollections/collection/immutable/vector" 11 12 "github.com/m4gshm/gollections/loop" 13 "github.com/m4gshm/gollections/op" 14 "github.com/m4gshm/gollections/slice" 15 ) 16 17 func Test_Vector_From(t *testing.T) { 18 set := vector.From(loop.Of(1, 1, 2, 2, 3, 4, 3, 2, 1)) 19 assert.Equal(t, slice.Of(1, 1, 2, 2, 3, 4, 3, 2, 1), set.Slice()) 20 } 21 22 func Test_VectorIterate(t *testing.T) { 23 expected := slice.Of(1, 2, 3, 4) 24 v := vector.Of(1, 2, 3, 4) 25 result := make([]int, v.Len()) 26 i := 0 27 for it := v.Head(); it.HasNext(); { 28 result[i] = it.GetNext() 29 i++ 30 } 31 assert.Equal(t, expected, result) 32 } 33 34 func Test_VectorIterate2(t *testing.T) { 35 expected := slice.Of(1, 2, 3, 4) 36 v := vector.Of(1, 2, 3, 4) 37 result := make([]int, v.Len()) 38 i := 0 39 for it, v, ok := v.First(); ok; v, ok = it.Next() { 40 result[i] = v 41 i++ 42 } 43 assert.Equal(t, expected, result) 44 } 45 46 func Test_VectorIterate3(t *testing.T) { 47 expected := slice.Of(1, 2, 3, 4) 48 v := vector.Of(1, 2, 3, 4) 49 result := make([]int, v.Len()) 50 i := 0 51 it := v.Head() 52 for v, ok := it.Next(); ok; v, ok = it.Next() { 53 result[i] = v 54 i++ 55 } 56 assert.Equal(t, expected, result) 57 } 58 59 func Test_VectorReverseIteration(t *testing.T) { 60 expected := slice.Of(4, 3, 2, 1) 61 v := vector.Of(1, 2, 3, 4) 62 result := make([]int, v.Len()) 63 i := 0 64 for it := v.Tail(); it.HasPrev(); { 65 result[i] = it.GetPrev() 66 i++ 67 } 68 assert.Equal(t, expected, result) 69 } 70 71 func Test_VectorReverseIteration2(t *testing.T) { 72 expected := slice.Of(4, 3, 2, 1) 73 v := vector.Of(1, 2, 3, 4) 74 result := make([]int, v.Len()) 75 i := 0 76 for it, v, ok := v.Last(); ok; v, ok = it.Prev() { 77 result[i] = v 78 i++ 79 } 80 assert.Equal(t, expected, result) 81 } 82 83 func Test_Vector_Sort(t *testing.T) { 84 var ( 85 elements = vector.Of(3, 1, 5, 6, 8, 0, -2) 86 sorted = elements.Sort(op.Compare) 87 ) 88 assert.Equal(t, vector.Of(-2, 0, 1, 3, 5, 6, 8), sorted) 89 } 90 91 func Test_Vector_SortStructByField(t *testing.T) { 92 var ( 93 anonymous = &user{"Anonymous", 0} 94 cherlie = &user{"Cherlie", 25} 95 alise = &user{"Alise", 20} 96 bob = &user{"Bob", 19} 97 98 elements = vector.Of(anonymous, cherlie, alise, bob) 99 sortedByName = vector.Sort(elements, (*user).Name) 100 sortedByAge = vector.Sort(elements, (*user).Age) 101 ) 102 assert.Equal(t, vector.Of(alise, anonymous, bob, cherlie), sortedByName) 103 assert.Equal(t, vector.Of(anonymous, bob, alise, cherlie), sortedByAge) 104 } 105 106 func Test_Vector_Convert(t *testing.T) { 107 var ( 108 ints = vector.Of(3, 1, 5, 6, 8, 0, -2) 109 strings = loop.Slice(loop.Filter(vector.Convert(ints, strconv.Itoa), func(s string) bool { return len(s) == 1 })) 110 strings2 = vector.Convert(ints, strconv.Itoa).Filter(func(s string) bool { return len(s) == 1 }).Slice() 111 ) 112 assert.Equal(t, slice.Of("3", "1", "5", "6", "8", "0"), strings) 113 assert.Equal(t, strings, strings2) 114 } 115 116 func Test_Vector_Flatt(t *testing.T) { 117 var ( 118 deepInts = vector.Of(vector.Of(3, 1), vector.Of(5, 6, 8, 0, -2)) 119 ints = vector.Flat(deepInts, immutable.Vector[int].Slice) 120 c = loop.Convert(ints, strconv.Itoa) 121 stringsPipe = loop.Filter(c.Filter(func(s string) bool { return len(s) == 1 }), func(s string) bool { return len(s) == 1 }) 122 ) 123 assert.Equal(t, slice.Of("3", "1", "5", "6", "8", "0"), stringsPipe.Slice()) 124 } 125 126 func Test_Vector_DoubleConvert(t *testing.T) { 127 var ( 128 ints = vector.Of(3, 1, 5, 6, 8, 0, -2) 129 stringsPipe = vector.Convert(ints, strconv.Itoa).Filter(func(s string) bool { return len(s) == 1 }) 130 prefixedStrinsPipe = loop.Convert(stringsPipe, func(s string) string { return "_" + s }) 131 ) 132 assert.Equal(t, slice.Of("_3", "_1", "_5", "_6", "_8", "_0"), prefixedStrinsPipe.Slice()) 133 134 //second call do nothing 135 var no []string 136 assert.Equal(t, no, stringsPipe.Slice()) 137 } 138 139 func Test_Vector_Zero(t *testing.T) { 140 var vec immutable.Vector[int] 141 142 vec.IsEmpty() 143 vec.Len() 144 145 vec.For(nil) 146 vec.ForEach(nil) 147 vec.Track(nil) 148 vec.TrackEach(nil) 149 150 vec.Slice() 151 152 head := vec.Head() 153 assert.False(t, head.HasNext()) 154 assert.False(t, head.HasPrev()) 155 156 _, ok := head.Get() 157 assert.False(t, ok) 158 _, ok = head.Next() 159 assert.False(t, ok) 160 head.Size() 161 162 tail := vec.Tail() 163 assert.False(t, tail.HasNext()) 164 assert.False(t, tail.HasPrev()) 165 166 _, ok = tail.Get() 167 assert.False(t, ok) 168 _, ok = tail.Next() 169 assert.False(t, ok) 170 tail.Size() 171 } 172 173 type user struct { 174 name string 175 age int 176 } 177 178 func (u *user) Name() string { return u.name } 179 func (u *user) Age() int { return u.age }