Test that string representations of long-double roundtrip both for array casting and scalar coercion, see also gh-15608.
(strtype)
| 52 | @pytest.mark.skipif(string_to_longdouble_inaccurate, reason="Need strtold_l") |
| 53 | @pytest.mark.parametrize("strtype", (np.str_, np.bytes_, str, bytes)) |
| 54 | def 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 | |
| 78 | def test_bogus_string(): |