(self)
| 158 | list(accumulate([10, 20], 100)) |
| 159 | |
| 160 | def test_batched(self): |
| 161 | self.assertEqual(list(batched('ABCDEFG', 3)), |
| 162 | [('A', 'B', 'C'), ('D', 'E', 'F'), ('G',)]) |
| 163 | self.assertEqual(list(batched('ABCDEFG', 2)), |
| 164 | [('A', 'B'), ('C', 'D'), ('E', 'F'), ('G',)]) |
| 165 | self.assertEqual(list(batched('ABCDEFG', 1)), |
| 166 | [('A',), ('B',), ('C',), ('D',), ('E',), ('F',), ('G',)]) |
| 167 | self.assertEqual(list(batched('ABCDEF', 2, strict=True)), |
| 168 | [('A', 'B'), ('C', 'D'), ('E', 'F')]) |
| 169 | |
| 170 | with self.assertRaises(ValueError): # Incomplete batch when strict |
| 171 | list(batched('ABCDEFG', 3, strict=True)) |
| 172 | with self.assertRaises(TypeError): # Too few arguments |
| 173 | list(batched('ABCDEFG')) |
| 174 | with self.assertRaises(TypeError): |
| 175 | list(batched('ABCDEFG', 3, None)) # Too many arguments |
| 176 | with self.assertRaises(TypeError): |
| 177 | list(batched(None, 3)) # Non-iterable input |
| 178 | with self.assertRaises(TypeError): |
| 179 | list(batched('ABCDEFG', 'hello')) # n is a string |
| 180 | with self.assertRaises(ValueError): |
| 181 | list(batched('ABCDEFG', 0)) # n is zero |
| 182 | with self.assertRaises(ValueError): |
| 183 | list(batched('ABCDEFG', -1)) # n is negative |
| 184 | |
| 185 | data = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 186 | for n in range(1, 6): |
| 187 | for i in range(len(data)): |
| 188 | s = data[:i] |
| 189 | batches = list(batched(s, n)) |
| 190 | with self.subTest(s=s, n=n, batches=batches): |
| 191 | # Order is preserved and no data is lost |
| 192 | self.assertEqual(''.join(chain(*batches)), s) |
| 193 | # Each batch is an exact tuple |
| 194 | self.assertTrue(all(type(batch) is tuple for batch in batches)) |
| 195 | # All but the last batch is of size n |
| 196 | if batches: |
| 197 | last_batch = batches.pop() |
| 198 | self.assertTrue(all(len(batch) == n for batch in batches)) |
| 199 | self.assertTrue(len(last_batch) <= n) |
| 200 | batches.append(last_batch) |
| 201 | |
| 202 | def test_chain(self): |
| 203 |
nothing calls this directly
no test coverage detected