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

Method test_map

Lib/test/test_builtin.py:1348–1406  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1346 self.assertRaises(TypeError, len, NoLenMethod())
1347
1348 def test_map(self):
1349 self.assertEqual(
1350 list(map(lambda x: x*x, range(1,4))),
1351 [1, 4, 9]
1352 )
1353 try:
1354 from math import sqrt
1355 except ImportError:
1356 def sqrt(x):
1357 return pow(x, 0.5)
1358 self.assertEqual(
1359 list(map(lambda x: list(map(sqrt, x)), [[16, 4], [81, 9]])),
1360 [[4.0, 2.0], [9.0, 3.0]]
1361 )
1362 self.assertEqual(
1363 list(map(lambda x, y: x+y, [1,3,2], [9,1,4])),
1364 [10, 4, 6]
1365 )
1366
1367 def plus(*v):
1368 accu = 0
1369 for i in v: accu = accu + i
1370 return accu
1371 self.assertEqual(
1372 list(map(plus, [1, 3, 7])),
1373 [1, 3, 7]
1374 )
1375 self.assertEqual(
1376 list(map(plus, [1, 3, 7], [4, 9, 2])),
1377 [1+4, 3+9, 7+2]
1378 )
1379 self.assertEqual(
1380 list(map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])),
1381 [1+4+1, 3+9+1, 7+2+0]
1382 )
1383 self.assertEqual(
1384 list(map(int, Squares(10))),
1385 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
1386 )
1387 def Max(a, b):
1388 if a is None:
1389 return b
1390 if b is None:
1391 return a
1392 return max(a, b)
1393 self.assertEqual(
1394 list(map(Max, Squares(3), Squares(2))),
1395 [0, 1]
1396 )
1397 self.assertRaises(TypeError, map)
1398 self.assertRaises(TypeError, map, lambda x: x, 42)
1399 class BadSeq:
1400 def __iter__(self):
1401 raise ValueError
1402 yield None
1403 self.assertRaises(ValueError, list, map(lambda x: x, BadSeq()))
1404 def badfunc(x):
1405 raise RuntimeError

Callers

nothing calls this directly

Calls 5

listClass · 0.85
SquaresClass · 0.70
BadSeqClass · 0.70
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected