|
5 | 5 |
|
6 | 6 | from warnings import warn, simplefilter
|
7 | 7 |
|
8 |
| -from ..testing import catch_warn_reset |
| 8 | +import numpy as np |
9 | 9 |
|
10 |
| -from nose.tools import assert_equal |
| 10 | +from ..testing import catch_warn_reset, assert_allclose_safely |
| 11 | + |
| 12 | +from nose.tools import assert_equal, assert_raises |
11 | 13 |
|
12 | 14 |
|
13 | 15 | def assert_warn_len_equal(mod, n_in_context):
|
@@ -46,3 +48,39 @@ def test_catch_warn_reset():
|
46 | 48 | simplefilter('ignore')
|
47 | 49 | warn('Another warning')
|
48 | 50 | assert_warn_len_equal(my_mod, 2)
|
| 51 | + |
| 52 | + |
| 53 | +def test_assert_allclose_safely(): |
| 54 | + # Test the safe version of allclose |
| 55 | + assert_allclose_safely([1, 1], [1, 1]) |
| 56 | + assert_allclose_safely(1, 1) |
| 57 | + assert_allclose_safely(1, [1, 1]) |
| 58 | + assert_allclose_safely([1, 1], 1 + 1e-6) |
| 59 | + assert_raises(AssertionError, assert_allclose_safely, [1, 1], 1 + 1e-4) |
| 60 | + # Broadcastable matrices |
| 61 | + a = np.ones((2, 3)) |
| 62 | + b = np.ones((3, 2, 3)) |
| 63 | + eps = np.finfo(np.float).eps |
| 64 | + a[0, 0] = 1 + eps |
| 65 | + assert_allclose_safely(a, b) |
| 66 | + a[0, 0] = 1 + 1.1e-5 |
| 67 | + assert_raises(AssertionError, assert_allclose_safely, a, b) |
| 68 | + # Nans in same place |
| 69 | + a[0, 0] = np.nan |
| 70 | + b[:, 0, 0] = np.nan |
| 71 | + assert_allclose_safely(a, b) |
| 72 | + # Never equal with nans present, if not matching nans |
| 73 | + assert_raises(AssertionError, |
| 74 | + assert_allclose_safely, a, b, |
| 75 | + match_nans=False) |
| 76 | + b[0, 0, 0] = 1 |
| 77 | + assert_raises(AssertionError, assert_allclose_safely, a, b) |
| 78 | + # Test allcloseness of inf, especially np.float128 infs |
| 79 | + for dtt in (np.float32, np.float64, np.float128): |
| 80 | + a = np.array([-np.inf, 1, np.inf], dtype=dtt) |
| 81 | + b = np.array([-np.inf, 1, np.inf], dtype=dtt) |
| 82 | + assert_allclose_safely(a, b) |
| 83 | + b[1] = 0 |
| 84 | + assert_raises(AssertionError, assert_allclose_safely, a, b) |
| 85 | + # Empty compares equal to empty |
| 86 | + assert_allclose_safely([], []) |
0 commit comments