MCPcopy Create free account
hub / github.com/numpy/numpy / test_zero_width_string

Method test_zero_width_string

numpy/core/tests/test_multiarray.py:1444–1498  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1442 assert_raises(ValueError, testassign)
1443
1444 def test_zero_width_string(self):
1445 # Test for PR #6430 / issues #473, #4955, #2585
1446
1447 dt = np.dtype([('I', int), ('S', 'S0')])
1448
1449 x = np.zeros(4, dtype=dt)
1450
1451 assert_equal(x['S'], [b'', b'', b'', b''])
1452 assert_equal(x['S'].itemsize, 0)
1453
1454 x['S'] = ['a', 'b', 'c', 'd']
1455 assert_equal(x['S'], [b'', b'', b'', b''])
1456 assert_equal(x['I'], [0, 0, 0, 0])
1457
1458 # Variation on test case from #4955
1459 x['S'][x['I'] == 0] = 'hello'
1460 assert_equal(x['S'], [b'', b'', b'', b''])
1461 assert_equal(x['I'], [0, 0, 0, 0])
1462
1463 # Variation on test case from #2585
1464 x['S'] = 'A'
1465 assert_equal(x['S'], [b'', b'', b'', b''])
1466 assert_equal(x['I'], [0, 0, 0, 0])
1467
1468 # Allow zero-width dtypes in ndarray constructor
1469 y = np.ndarray(4, dtype=x['S'].dtype)
1470 assert_equal(y.itemsize, 0)
1471 assert_equal(x['S'], y)
1472
1473 # More tests for indexing an array with zero-width fields
1474 assert_equal(np.zeros(4, dtype=[('a', 'S0,S0'),
1475 ('b', 'u1')])['a'].itemsize, 0)
1476 assert_equal(np.empty(3, dtype='S0,S0').itemsize, 0)
1477 assert_equal(np.zeros(4, dtype='S0,u1')['f0'].itemsize, 0)
1478
1479 xx = x['S'].reshape((2, 2))
1480 assert_equal(xx.itemsize, 0)
1481 assert_equal(xx, [[b'', b''], [b'', b'']])
1482 # check for no uninitialized memory due to viewing S0 array
1483 assert_equal(xx[:].dtype, xx.dtype)
1484 assert_array_equal(eval(repr(xx), dict(array=np.array)), xx)
1485
1486 b = io.BytesIO()
1487 np.save(b, xx)
1488
1489 b.seek(0)
1490 yy = np.load(b)
1491 assert_equal(yy.itemsize, 0)
1492 assert_equal(xx, yy)
1493
1494 with temppath(suffix='.npy') as tmp:
1495 np.save(tmp, xx)
1496 yy = np.load(tmp)
1497 assert_equal(yy.itemsize, 0)
1498 assert_equal(xx, yy)
1499
1500 def test_base_attr(self):
1501 a = np.zeros(3, dtype='i4,f4')

Callers

nothing calls this directly

Calls 6

assert_equalFunction · 0.90
assert_array_equalFunction · 0.90
temppathFunction · 0.90
reshapeMethod · 0.80
seekMethod · 0.80
dtypeMethod · 0.45

Tested by

no test coverage detected