MCPcopy
hub / github.com/destel/rill / TestMapReduce

Function TestMapReduce

reduce_test.go:145–304  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

143}
144
145func TestMapReduce(t *testing.T) {
146 for _, nm := range []int{1, 4} {
147 for _, nr := range []int{1, 4} {
148 t.Run(th.Name("empty", nm, nr), func(t *testing.T) {
149 in := FromSlice([]int{}, nil)
150
151 out, err := MapReduce(in,
152 nm, func(x int) (string, int, error) {
153 s := fmt.Sprint(x)
154 return fmt.Sprintf("%d-digit", len(s)), x, nil
155 },
156 nr, func(x, y int) (int, error) {
157 return x + y, nil
158 })
159
160 th.ExpectNoError(t, err)
161 th.ExpectMap(t, out, map[string]int{})
162 th.ExpectDrainedChan(t, in)
163 })
164
165 t.Run(th.Name("no errors", nm, nr), func(t *testing.T) {
166 in := FromChan(th.FromRange(0, 1000), nil)
167
168 var cntMap, cntReduce atomic.Int64
169 out, err := MapReduce(in,
170 nm, func(x int) (string, int, error) {
171 cntMap.Add(1)
172 s := fmt.Sprint(x)
173 return fmt.Sprintf("%d-digit", len(s)), x, nil
174 },
175 nr, func(x, y int) (int, error) {
176 cntReduce.Add(1)
177 return x + y, nil
178 },
179 )
180
181 th.ExpectNoError(t, err)
182 th.ExpectMap(t, out, map[string]int{
183 "1-digit": (0 + 9) * 10 / 2,
184 "2-digit": (10 + 99) * 90 / 2,
185 "3-digit": (100 + 999) * 900 / 2,
186 })
187 th.ExpectValue(t, cntMap.Load(), 1000)
188 th.ExpectValue(t, cntReduce.Load(), 9+89+899)
189 th.ExpectDrainedChan(t, in)
190 })
191
192 t.Run(th.Name("error in input", nm, nr), func(t *testing.T) {
193 in := FromChan(th.FromRange(0, 1000), nil)
194 in = replaceWithError(in, 100, fmt.Errorf("err100"))
195
196 var cntMap, cntReduce atomic.Int64
197 _, err := MapReduce(in,
198 nm, func(x int) (string, int, error) {
199 cntMap.Add(1)
200 s := fmt.Sprint(x)
201 return fmt.Sprintf("%d-digit", len(s)), x, nil
202 },

Callers

nothing calls this directly

Calls 11

NameFunction · 0.92
ExpectNoErrorFunction · 0.92
ExpectMapFunction · 0.92
ExpectDrainedChanFunction · 0.92
FromRangeFunction · 0.92
ExpectValueFunction · 0.92
ExpectErrorFunction · 0.92
FromChanFunction · 0.85
replaceWithErrorFunction · 0.85
FromSliceFunction · 0.70
MapReduceFunction · 0.70

Tested by

no test coverage detected