(t *testing.T)
| 143 | } |
| 144 | |
| 145 | func 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 | }, |
nothing calls this directly
no test coverage detected