MCPcopy
hub / github.com/numpy/numpy / test_zero_width_string

Method test_zero_width_string

numpy/_core/tests/test_multiarray.py:1662–1716  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1660 assert_raises(ValueError, testassign)
1661
1662 def test_zero_width_string(self):
1663 # Test for PR #6430 / issues #473, #4955, #2585
1664
1665 dt = np.dtype([('I', int), ('S', 'S0')])
1666
1667 x = np.zeros(4, dtype=dt)
1668
1669 assert_equal(x['S'], [b'', b'', b'', b''])
1670 assert_equal(x['S'].itemsize, 0)
1671
1672 x['S'] = ['a', 'b', 'c', 'd']
1673 assert_equal(x['S'], [b'', b'', b'', b''])
1674 assert_equal(x['I'], [0, 0, 0, 0])
1675
1676 # Variation on test case from #4955
1677 x['S'][x['I'] == 0] = 'hello'
1678 assert_equal(x['S'], [b'', b'', b'', b''])
1679 assert_equal(x['I'], [0, 0, 0, 0])
1680
1681 # Variation on test case from #2585
1682 x['S'] = 'A'
1683 assert_equal(x['S'], [b'', b'', b'', b''])
1684 assert_equal(x['I'], [0, 0, 0, 0])
1685
1686 # Allow zero-width dtypes in ndarray constructor
1687 y = np.ndarray(4, dtype=x['S'].dtype)
1688 assert_equal(y.itemsize, 0)
1689 assert_equal(x['S'], y)
1690
1691 # More tests for indexing an array with zero-width fields
1692 assert_equal(np.zeros(4, dtype=[('a', 'S0,S0'),
1693 ('b', 'u1')])['a'].itemsize, 0)
1694 assert_equal(np.empty(3, dtype='S0,S0').itemsize, 0)
1695 assert_equal(np.zeros(4, dtype='S0,u1')['f0'].itemsize, 0)
1696
1697 xx = x['S'].reshape((2, 2))
1698 assert_equal(xx.itemsize, 0)
1699 assert_equal(xx, [[b'', b''], [b'', b'']])
1700 # check for no uninitialized memory due to viewing S0 array
1701 assert_equal(xx[:].dtype, xx.dtype)
1702 assert_array_equal(eval(repr(xx), {"np": np, "array": np.array}), xx)
1703
1704 b = io.BytesIO()
1705 np.save(b, xx)
1706
1707 b.seek(0)
1708 yy = np.load(b)
1709 assert_equal(yy.itemsize, 0)
1710 assert_equal(xx, yy)
1711
1712 with temppath(suffix='.npy') as tmp:
1713 np.save(tmp, xx)
1714 yy = np.load(tmp)
1715 assert_equal(yy.itemsize, 0)
1716 assert_equal(xx, yy)
1717
1718 def test_base_attr(self):
1719 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