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

Function test_array_and_stringlike_roundtrip

numpy/core/tests/test_longdouble.py:54–75  ·  view source on GitHub ↗

Test that string representations of long-double roundtrip both for array casting and scalar coercion, see also gh-15608.

(strtype)

Source from the content-addressed store, hash-verified

52@pytest.mark.skipif(string_to_longdouble_inaccurate, reason="Need strtold_l")
53@pytest.mark.parametrize("strtype", (np.str_, np.bytes_, str, bytes))
54def test_array_and_stringlike_roundtrip(strtype):
55 """
56 Test that string representations of long-double roundtrip both
57 for array casting and scalar coercion, see also gh-15608.
58 """
59 o = 1 + LD_INFO.eps
60
61 if strtype in (np.bytes_, bytes):
62 o_str = strtype(repr(o).encode("ascii"))
63 else:
64 o_str = strtype(repr(o))
65
66 # Test that `o` is correctly coerced from the string-like
67 assert o == np.longdouble(o_str)
68
69 # Test that arrays also roundtrip correctly:
70 o_strarr = np.asarray([o] * 3, dtype=strtype)
71 assert (o == o_strarr.astype(np.longdouble)).all()
72
73 # And array coercion and casting to string give the same as scalar repr:
74 assert (o_strarr == o_str).all()
75 assert (np.asarray([o] * 3).astype(strtype) == o_str).all()
76
77
78def test_bogus_string():

Callers

nothing calls this directly

Calls 3

encodeMethod · 0.80
astypeMethod · 0.80
allMethod · 0.45

Tested by

no test coverage detected