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