diff options
author | Ralf Gommers <ralf.gommers@googlemail.com> | 2012-03-04 16:51:21 +0100 |
---|---|---|
committer | Ralf Gommers <ralf.gommers@googlemail.com> | 2012-03-07 19:09:32 +0100 |
commit | aae5b0a2b7e683792d56bb5c6994dca1e4b9b4b6 (patch) | |
tree | 07b7306a03d4ef4cb48530fd287e44b489f7899f /numpy/lib/tests | |
parent | 8670ca3b83991faf90d57363b551fdc90a196826 (diff) | |
download | numpy-aae5b0a2b7e683792d56bb5c6994dca1e4b9b4b6.tar.gz |
TST: fix string comparison test failures on Windows for Python 2.5.
This is caused by the inconsistent floating point handling of Python itself.
On Windows with 2.5:
>>> "%s" % 1e-6
'1e-006'
With 2.6:
>>> "%s" % 1e-6
'1e-06'
Reviewed as PR-225.
Diffstat (limited to 'numpy/lib/tests')
-rw-r--r-- | numpy/lib/tests/test_io.py | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 41a95de10..c62e586b5 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -287,7 +287,7 @@ class TestSaveTxt(TestCase): np.savetxt(c, a, fmt=' %+.3e') c.seek(0) lines = c.readlines() - assert_equal(lines, asbytes_nested([ + _assert_floatstr_lines_equal(lines, asbytes_nested([ ' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n', ' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n'])) # One format for each real and imaginary part @@ -295,7 +295,7 @@ class TestSaveTxt(TestCase): np.savetxt(c, a, fmt=' %+.3e' * 2 * ncols) c.seek(0) lines = c.readlines() - assert_equal(lines, asbytes_nested([ + _assert_floatstr_lines_equal(lines, asbytes_nested([ ' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n', ' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n'])) # One format for each complex number @@ -303,11 +303,30 @@ class TestSaveTxt(TestCase): np.savetxt(c, a, fmt=['(%.3e%+.3ej)'] * ncols) c.seek(0) lines = c.readlines() - assert_equal(lines, asbytes_nested([ + _assert_floatstr_lines_equal(lines, asbytes_nested([ '(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n', '(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n'])) +def _assert_floatstr_lines_equal(actual_lines, expected_lines): + """A string comparison function that also works on Windows + Python 2.5. + + This is necessary because Python 2.5 on Windows inserts an extra 0 in + the exponent of the string representation of floating point numbers. + + Only used in TestSaveTxt.test_complex_arrays, no attempt made to make this + more generic. + + Once Python 2.5 compatibility is dropped, simply use `assert_equal` instead + of this function. + """ + for actual, expected in zip(actual_lines, expected_lines): + if not actual == expected: + expected_win25 = expected.replace("e+00", "e+000") + if not actual == expected_win25: + msg = build_err_msg([actual, desired], verbose=True) + raise AssertionError(msg) + class TestLoadTxt(TestCase): def test_record(self): |