MCPcopy Index your code
hub / github.com/python/cpython / test_batched

Method test_batched

Lib/test/test_itertools.py:160–200  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 9

listClass · 0.85
chainFunction · 0.85
assertTrueMethod · 0.80
assertEqualMethod · 0.45
assertRaisesMethod · 0.45
subTestMethod · 0.45
joinMethod · 0.45
popMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected