diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2022-01-11 15:46:21 -0600 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2022-01-14 20:07:07 -0600 |
commit | e1f7ad16518f95b6c5b560a03375b4329c8136ff (patch) | |
tree | b3b7319c474a99997f82a7a7eb175f7b906ff106 /numpy/lib/tests/test_io.py | |
parent | 3ca9f5a2a252e020a44a355f4fc8114d91ea3423 (diff) | |
download | numpy-e1f7ad16518f95b6c5b560a03375b4329c8136ff.tar.gz |
BUG: Fix complex parser and add tests for whitespace and failure paths
Diffstat (limited to 'numpy/lib/tests/test_io.py')
-rw-r--r-- | numpy/lib/tests/test_io.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index b4ca5b74b..5ba852e3d 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -3263,3 +3263,30 @@ def test_loadtxt_warn_on_skipped_data(skiprows): txt = TextIO(data) with pytest.warns(UserWarning, match="input contained no data"): np.loadtxt(txt, skiprows=skiprows) + + +@pytest.mark.parametrize("dtype", + np.typecodes["AllInteger"] + "efdFD" + "?") +def test_loadtxt_unicode_whitespace_stripping(dtype): + # Test that all numeric types (and bool) strip whitespace correctly + # \u202F is a narrow no-break space, `\n` is just a whitespace if quoted. + # Currently, skip float128 as it did not always support this and has no + # "custom" parsing: + txt = StringIO(' 3 ,"\u202F2\n"') + res = np.loadtxt(txt, dtype=dtype, delimiter=",", quotechar='"') + assert_array_equal(res, np.array([3, 2]).astype(dtype)) + +@pytest.mark.parametrize("dtype", "FD") +def test_loadtxt_unicode_whitespace_stripping_complex(dtype): + # Complex has a few extra cases since it has two components and parentheses + line = " 1 , 2+3j , ( 4+5j ), ( 6+-7j ) , 8j , ( 9j ) \n" + data = [line, line.replace(" ", "\u202F")] + res = np.loadtxt(data, dtype=dtype, delimiter=',') + assert_array_equal(res, np.array([[1, 2+3j, 4+5j, 6-7j, 8j, 9j]] * 2)) + +@pytest.mark.parametrize("dtype", "FD") +@pytest.mark.parametrize("field", + ["1 +2j", "1+ 2j", "1+2 j", "1+-+3", "(1j", "(1", "(1+2j", "1+2j)"]) +def test_loadtxt_bad_complex(dtype, field): + with pytest.raises(ValueError): + np.loadtxt([field + "\n"], dtype=dtype, delimiter=",") |