go-ml.dev/pkg/base@v0.0.0-20200610162856-60c38abac71b/tests/lazy2_test.go (about) 1 package tests 2 3 import ( 4 "fmt" 5 "go-ml.dev/pkg/base/fu/lazy" 6 "gotest.tools/assert" 7 "reflect" 8 "testing" 9 ) 10 11 type Color struct { 12 Color string 13 Index int 14 } 15 16 var colors = []Color{ 17 {"White", 0}, 18 {"Yellow", 1}, 19 {"Blue", 2}, 20 {"Red", 3}, 21 {"Green", 4}, 22 {"Black", 5}, 23 {"Brown", 6}, 24 {"Azure", 7}, 25 {"Ivory", 8}, 26 {"Teal", 9}, 27 {"Silver", 10}, 28 {"Purple", 11}, 29 {"Navy blue", 12}, 30 {"Pea green", 13}, 31 {"Gray", 14}, 32 {"Orange", 15}, 33 {"Maroon", 16}, 34 {"Charcoal", 17}, 35 {"Aquamarine", 18}, 36 {"Coral", 19}, 37 {"Fuchsia", 20}, 38 {"Wheat", 21}, 39 {"Lime", 22}, 40 {"Crimson", 23}, 41 {"Khaki", 24}, 42 {"Hot pink", 25}, 43 {"Magenta", 26}, 44 {"Olden", 27}, 45 {"Plum", 28}, 46 {"Olive", 29}, 47 {"Cyan", 30}, 48 } 49 50 func Test_NewFromChan(t *testing.T) { 51 c := make(chan Color) 52 go func() { 53 for _, x := range colors { 54 c <- x 55 } 56 close(c) 57 }() 58 rs := lazy.Chan(c).LuckyCollect().([]Color) 59 assert.DeepEqual(t, rs, colors) 60 } 61 62 func Test_Collect(t *testing.T) { 63 rs := lazy.List(colors).LuckyCollect().([]Color) 64 assert.DeepEqual(t, rs, colors) 65 } 66 67 func Test_ConqCollect(t *testing.T) { 68 z := lazy.List(colors) 69 rs := z.Parallel(8).LuckyCollect().([]Color) 70 assert.DeepEqual(t, rs, colors) 71 rs = z.Parallel().LuckyCollect().([]Color) 72 assert.DeepEqual(t, rs, colors) 73 } 74 75 func Test_Filter(t *testing.T) { 76 z := lazy.List(colors). 77 Filter(func(c Color) bool { return c.Index%2 == 0 }). 78 Parallel() 79 rs := z.LuckyCollect().([]Color) 80 for _, c := range rs { 81 assert.Assert(t, c.Index%2 == 0) 82 } 83 for _, c := range colors { 84 if c.Index%2 == 0 { 85 assert.Assert(t, rs[c.Index/2].Index == c.Index) 86 } 87 } 88 // again 89 rs = z.LuckyCollect().([]Color) 90 for _, c := range rs { 91 assert.Assert(t, c.Index%2 == 0) 92 } 93 for _, c := range colors { 94 if c.Index%2 == 0 { 95 assert.Assert(t, rs[c.Index/2].Index == c.Index) 96 } 97 } 98 } 99 100 func Test_Map1(t *testing.T) { 101 rs := lazy.List([]int{0, 1, 2, 3, 4}). 102 Map(func(r int) string { return fmt.Sprint(r) }). 103 Parallel(). 104 LuckyCollect().([]string) 105 assert.Assert(t, len(rs) == 5) 106 for i, r := range rs { 107 assert.Assert(t, r == fmt.Sprint(i)) 108 } 109 } 110 111 func Test_Map2(t *testing.T) { 112 rs := lazy.List(colors). 113 Map(func(r Color) string { return r.Color }). 114 Parallel(6). 115 LuckyCollect().([]string) 116 assert.Assert(t, len(rs) == len(colors)) 117 for i, r := range rs { 118 assert.Assert(t, r == colors[i].Color) 119 } 120 } 121 122 func Test_Map3(t *testing.T) { 123 type R struct{ c string } 124 z := lazy.List(colors) 125 rs := z.Map(func(r Color) R { return R{r.Color} }).LuckyCollect().([]R) 126 assert.Assert(t, len(rs) == len(colors)) 127 for i, r := range rs { 128 assert.Assert(t, r.c == colors[i].Color) 129 } 130 } 131 132 func Test_Sink(t *testing.T) { 133 type R struct{ c string } 134 rs := []R{} 135 z := lazy.List(colors) 136 z.Map(func(r Color) R { return R{r.Color} }).Drain(func(value reflect.Value) error { 137 if value.Kind() != reflect.Bool { 138 rs = append(rs, value.Interface().(R)) 139 } 140 return nil 141 }) 142 assert.Assert(t, len(rs) == len(colors)) 143 for i, r := range rs { 144 assert.Assert(t, r.c == colors[i].Color) 145 } 146 } 147 148 func Test_SinkClose(t *testing.T) { 149 type R struct{ c string } 150 closed := false 151 rs := []R{} 152 z := lazy.List(colors) 153 z.Map(func(r Color) R { return R{r.Color} }).Drain(func(value reflect.Value) error { 154 if closed { 155 panic("already closed") 156 } 157 if value.Kind() != reflect.Bool { 158 rs = append(rs, value.Interface().(R)) 159 } else { 160 closed = true 161 } 162 return nil 163 }) 164 assert.Assert(t, len(rs) == len(colors)) 165 for i, r := range rs { 166 assert.Assert(t, r.c == colors[i].Color) 167 } 168 assert.Assert(t, closed) 169 } 170 171 func Test_SinkErrClose(t *testing.T) { 172 type R struct{ c string } 173 closed := false 174 z := lazy.List(colors) 175 z.Map(func(r Color) R { return R{r.Color} }).Drain(func(value reflect.Value) error { 176 if closed { 177 panic("already closed") 178 } 179 if value.Kind() != reflect.Bool { 180 // nothing 181 } else { 182 closed = true 183 } 184 return fmt.Errorf("error") 185 }) 186 assert.Assert(t, closed) 187 } 188 189 func Test_SinkErrClose2(t *testing.T) { 190 type R struct{ c string } 191 closed1 := false 192 closed2 := false 193 z := lazy.Source(func() lazy.Stream { 194 return func(index uint64) (reflect.Value, error) { 195 if index == lazy.STOP { 196 closed1 = true 197 } 198 return reflect.Value{}, fmt.Errorf("error") 199 } 200 }) 201 z.Map(func(r Color) R { return R{r.Color} }).Drain(func(value reflect.Value) error { 202 if closed2 { 203 panic("already closed") 204 } 205 if value.Kind() != reflect.Bool { 206 // nothing 207 } else { 208 closed2 = true 209 } 210 return fmt.Errorf("error") 211 }) 212 assert.Assert(t, closed1) 213 assert.Assert(t, closed2) 214 }