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

Method test_islice

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

Source from the content-addressed store, hash-verified

1167 self.assertRaises(TypeError, next, starmap(onearg, [(4,5)]))
1168
1169 def test_islice(self):
1170 for args in [ # islice(args) should agree with range(args)
1171 (10, 20, 3),
1172 (10, 3, 20),
1173 (10, 20),
1174 (10, 10),
1175 (10, 3),
1176 (20,)
1177 ]:
1178 self.assertEqual(list(islice(range(100), *args)),
1179 list(range(*args)))
1180
1181 for args, tgtargs in [ # Stop when seqn is exhausted
1182 ((10, 110, 3), ((10, 100, 3))),
1183 ((10, 110), ((10, 100))),
1184 ((110,), (100,))
1185 ]:
1186 self.assertEqual(list(islice(range(100), *args)),
1187 list(range(*tgtargs)))
1188
1189 # Test stop=None
1190 self.assertEqual(list(islice(range(10), None)), list(range(10)))
1191 self.assertEqual(list(islice(range(10), None, None)), list(range(10)))
1192 self.assertEqual(list(islice(range(10), None, None, None)), list(range(10)))
1193 self.assertEqual(list(islice(range(10), 2, None)), list(range(2, 10)))
1194 self.assertEqual(list(islice(range(10), 1, None, 2)), list(range(1, 10, 2)))
1195
1196 # Test number of items consumed SF #1171417
1197 it = iter(range(10))
1198 self.assertEqual(list(islice(it, 3)), list(range(3)))
1199 self.assertEqual(list(it), list(range(3, 10)))
1200
1201 it = iter(range(10))
1202 self.assertEqual(list(islice(it, 3, 3)), [])
1203 self.assertEqual(list(it), list(range(3, 10)))
1204
1205 # Test invalid arguments
1206 ra = range(10)
1207 self.assertRaises(TypeError, islice, ra)
1208 self.assertRaises(TypeError, islice, ra, 1, 2, 3, 4)
1209 self.assertRaises(ValueError, islice, ra, -5, 10, 1)
1210 self.assertRaises(ValueError, islice, ra, 1, -5, -1)
1211 self.assertRaises(ValueError, islice, ra, 1, 10, -1)
1212 self.assertRaises(ValueError, islice, ra, 1, 10, 0)
1213 self.assertRaises(ValueError, islice, ra, 'a')
1214 self.assertRaises(ValueError, islice, ra, 'a', 1)
1215 self.assertRaises(ValueError, islice, ra, 1, 'a')
1216 self.assertRaises(ValueError, islice, ra, 'a', 1, 1)
1217 self.assertRaises(ValueError, islice, ra, 1, 'a', 1)
1218 self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1)
1219
1220 # Issue #10323: Less islice in a predictable state
1221 c = count()
1222 self.assertEqual(list(islice(c, 1, 3, 50)), [1])
1223 self.assertEqual(next(c), 3)
1224
1225 # Issue #21321: check source iterator is not referenced
1226 # from islice() after the latter has been exhausted

Callers

nothing calls this directly

Calls 7

listClass · 0.85
countFunction · 0.85
assertIsNotNoneMethod · 0.80
assertIsNoneMethod · 0.80
IntLikeClass · 0.70
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected