图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1614 lines
54 KiB

  1. import warnings
  2. import sys
  3. import os
  4. import itertools
  5. import pytest
  6. import weakref
  7. import numpy as np
  8. from numpy.testing import (
  9. assert_equal, assert_array_equal, assert_almost_equal,
  10. assert_array_almost_equal, assert_array_less, build_err_msg, raises,
  11. assert_raises, assert_warns, assert_no_warnings, assert_allclose,
  12. assert_approx_equal, assert_array_almost_equal_nulp, assert_array_max_ulp,
  13. clear_and_catch_warnings, suppress_warnings, assert_string_equal, assert_,
  14. tempdir, temppath, assert_no_gc_cycles, HAS_REFCOUNT
  15. )
  16. from numpy.core.overrides import ARRAY_FUNCTION_ENABLED
  17. class _GenericTest:
  18. def _test_equal(self, a, b):
  19. self._assert_func(a, b)
  20. def _test_not_equal(self, a, b):
  21. with assert_raises(AssertionError):
  22. self._assert_func(a, b)
  23. def test_array_rank1_eq(self):
  24. """Test two equal array of rank 1 are found equal."""
  25. a = np.array([1, 2])
  26. b = np.array([1, 2])
  27. self._test_equal(a, b)
  28. def test_array_rank1_noteq(self):
  29. """Test two different array of rank 1 are found not equal."""
  30. a = np.array([1, 2])
  31. b = np.array([2, 2])
  32. self._test_not_equal(a, b)
  33. def test_array_rank2_eq(self):
  34. """Test two equal array of rank 2 are found equal."""
  35. a = np.array([[1, 2], [3, 4]])
  36. b = np.array([[1, 2], [3, 4]])
  37. self._test_equal(a, b)
  38. def test_array_diffshape(self):
  39. """Test two arrays with different shapes are found not equal."""
  40. a = np.array([1, 2])
  41. b = np.array([[1, 2], [1, 2]])
  42. self._test_not_equal(a, b)
  43. def test_objarray(self):
  44. """Test object arrays."""
  45. a = np.array([1, 1], dtype=object)
  46. self._test_equal(a, 1)
  47. def test_array_likes(self):
  48. self._test_equal([1, 2, 3], (1, 2, 3))
  49. class TestArrayEqual(_GenericTest):
  50. def setup(self):
  51. self._assert_func = assert_array_equal
  52. def test_generic_rank1(self):
  53. """Test rank 1 array for all dtypes."""
  54. def foo(t):
  55. a = np.empty(2, t)
  56. a.fill(1)
  57. b = a.copy()
  58. c = a.copy()
  59. c.fill(0)
  60. self._test_equal(a, b)
  61. self._test_not_equal(c, b)
  62. # Test numeric types and object
  63. for t in '?bhilqpBHILQPfdgFDG':
  64. foo(t)
  65. # Test strings
  66. for t in ['S1', 'U1']:
  67. foo(t)
  68. def test_0_ndim_array(self):
  69. x = np.array(473963742225900817127911193656584771)
  70. y = np.array(18535119325151578301457182298393896)
  71. assert_raises(AssertionError, self._assert_func, x, y)
  72. y = x
  73. self._assert_func(x, y)
  74. x = np.array(43)
  75. y = np.array(10)
  76. assert_raises(AssertionError, self._assert_func, x, y)
  77. y = x
  78. self._assert_func(x, y)
  79. def test_generic_rank3(self):
  80. """Test rank 3 array for all dtypes."""
  81. def foo(t):
  82. a = np.empty((4, 2, 3), t)
  83. a.fill(1)
  84. b = a.copy()
  85. c = a.copy()
  86. c.fill(0)
  87. self._test_equal(a, b)
  88. self._test_not_equal(c, b)
  89. # Test numeric types and object
  90. for t in '?bhilqpBHILQPfdgFDG':
  91. foo(t)
  92. # Test strings
  93. for t in ['S1', 'U1']:
  94. foo(t)
  95. def test_nan_array(self):
  96. """Test arrays with nan values in them."""
  97. a = np.array([1, 2, np.nan])
  98. b = np.array([1, 2, np.nan])
  99. self._test_equal(a, b)
  100. c = np.array([1, 2, 3])
  101. self._test_not_equal(c, b)
  102. def test_string_arrays(self):
  103. """Test two arrays with different shapes are found not equal."""
  104. a = np.array(['floupi', 'floupa'])
  105. b = np.array(['floupi', 'floupa'])
  106. self._test_equal(a, b)
  107. c = np.array(['floupipi', 'floupa'])
  108. self._test_not_equal(c, b)
  109. def test_recarrays(self):
  110. """Test record arrays."""
  111. a = np.empty(2, [('floupi', float), ('floupa', float)])
  112. a['floupi'] = [1, 2]
  113. a['floupa'] = [1, 2]
  114. b = a.copy()
  115. self._test_equal(a, b)
  116. c = np.empty(2, [('floupipi', float), ('floupa', float)])
  117. c['floupipi'] = a['floupi'].copy()
  118. c['floupa'] = a['floupa'].copy()
  119. with suppress_warnings() as sup:
  120. l = sup.record(FutureWarning, message="elementwise == ")
  121. self._test_not_equal(c, b)
  122. assert_equal(len(l), 1)
  123. def test_masked_nan_inf(self):
  124. # Regression test for gh-11121
  125. a = np.ma.MaskedArray([3., 4., 6.5], mask=[False, True, False])
  126. b = np.array([3., np.nan, 6.5])
  127. self._test_equal(a, b)
  128. self._test_equal(b, a)
  129. a = np.ma.MaskedArray([3., 4., 6.5], mask=[True, False, False])
  130. b = np.array([np.inf, 4., 6.5])
  131. self._test_equal(a, b)
  132. self._test_equal(b, a)
  133. def test_subclass_that_overrides_eq(self):
  134. # While we cannot guarantee testing functions will always work for
  135. # subclasses, the tests should ideally rely only on subclasses having
  136. # comparison operators, not on them being able to store booleans
  137. # (which, e.g., astropy Quantity cannot usefully do). See gh-8452.
  138. class MyArray(np.ndarray):
  139. def __eq__(self, other):
  140. return bool(np.equal(self, other).all())
  141. def __ne__(self, other):
  142. return not self == other
  143. a = np.array([1., 2.]).view(MyArray)
  144. b = np.array([2., 3.]).view(MyArray)
  145. assert_(type(a == a), bool)
  146. assert_(a == a)
  147. assert_(a != b)
  148. self._test_equal(a, a)
  149. self._test_not_equal(a, b)
  150. self._test_not_equal(b, a)
  151. @pytest.mark.skipif(
  152. not ARRAY_FUNCTION_ENABLED, reason='requires __array_function__')
  153. def test_subclass_that_does_not_implement_npall(self):
  154. class MyArray(np.ndarray):
  155. def __array_function__(self, *args, **kwargs):
  156. return NotImplemented
  157. a = np.array([1., 2.]).view(MyArray)
  158. b = np.array([2., 3.]).view(MyArray)
  159. with assert_raises(TypeError):
  160. np.all(a)
  161. self._test_equal(a, a)
  162. self._test_not_equal(a, b)
  163. self._test_not_equal(b, a)
  164. class TestBuildErrorMessage:
  165. def test_build_err_msg_defaults(self):
  166. x = np.array([1.00001, 2.00002, 3.00003])
  167. y = np.array([1.00002, 2.00003, 3.00004])
  168. err_msg = 'There is a mismatch'
  169. a = build_err_msg([x, y], err_msg)
  170. b = ('\nItems are not equal: There is a mismatch\n ACTUAL: array(['
  171. '1.00001, 2.00002, 3.00003])\n DESIRED: array([1.00002, '
  172. '2.00003, 3.00004])')
  173. assert_equal(a, b)
  174. def test_build_err_msg_no_verbose(self):
  175. x = np.array([1.00001, 2.00002, 3.00003])
  176. y = np.array([1.00002, 2.00003, 3.00004])
  177. err_msg = 'There is a mismatch'
  178. a = build_err_msg([x, y], err_msg, verbose=False)
  179. b = '\nItems are not equal: There is a mismatch'
  180. assert_equal(a, b)
  181. def test_build_err_msg_custom_names(self):
  182. x = np.array([1.00001, 2.00002, 3.00003])
  183. y = np.array([1.00002, 2.00003, 3.00004])
  184. err_msg = 'There is a mismatch'
  185. a = build_err_msg([x, y], err_msg, names=('FOO', 'BAR'))
  186. b = ('\nItems are not equal: There is a mismatch\n FOO: array(['
  187. '1.00001, 2.00002, 3.00003])\n BAR: array([1.00002, 2.00003, '
  188. '3.00004])')
  189. assert_equal(a, b)
  190. def test_build_err_msg_custom_precision(self):
  191. x = np.array([1.000000001, 2.00002, 3.00003])
  192. y = np.array([1.000000002, 2.00003, 3.00004])
  193. err_msg = 'There is a mismatch'
  194. a = build_err_msg([x, y], err_msg, precision=10)
  195. b = ('\nItems are not equal: There is a mismatch\n ACTUAL: array(['
  196. '1.000000001, 2.00002 , 3.00003 ])\n DESIRED: array(['
  197. '1.000000002, 2.00003 , 3.00004 ])')
  198. assert_equal(a, b)
  199. class TestEqual(TestArrayEqual):
  200. def setup(self):
  201. self._assert_func = assert_equal
  202. def test_nan_items(self):
  203. self._assert_func(np.nan, np.nan)
  204. self._assert_func([np.nan], [np.nan])
  205. self._test_not_equal(np.nan, [np.nan])
  206. self._test_not_equal(np.nan, 1)
  207. def test_inf_items(self):
  208. self._assert_func(np.inf, np.inf)
  209. self._assert_func([np.inf], [np.inf])
  210. self._test_not_equal(np.inf, [np.inf])
  211. def test_datetime(self):
  212. self._test_equal(
  213. np.datetime64("2017-01-01", "s"),
  214. np.datetime64("2017-01-01", "s")
  215. )
  216. self._test_equal(
  217. np.datetime64("2017-01-01", "s"),
  218. np.datetime64("2017-01-01", "m")
  219. )
  220. # gh-10081
  221. self._test_not_equal(
  222. np.datetime64("2017-01-01", "s"),
  223. np.datetime64("2017-01-02", "s")
  224. )
  225. self._test_not_equal(
  226. np.datetime64("2017-01-01", "s"),
  227. np.datetime64("2017-01-02", "m")
  228. )
  229. def test_nat_items(self):
  230. # not a datetime
  231. nadt_no_unit = np.datetime64("NaT")
  232. nadt_s = np.datetime64("NaT", "s")
  233. nadt_d = np.datetime64("NaT", "ns")
  234. # not a timedelta
  235. natd_no_unit = np.timedelta64("NaT")
  236. natd_s = np.timedelta64("NaT", "s")
  237. natd_d = np.timedelta64("NaT", "ns")
  238. dts = [nadt_no_unit, nadt_s, nadt_d]
  239. tds = [natd_no_unit, natd_s, natd_d]
  240. for a, b in itertools.product(dts, dts):
  241. self._assert_func(a, b)
  242. self._assert_func([a], [b])
  243. self._test_not_equal([a], b)
  244. for a, b in itertools.product(tds, tds):
  245. self._assert_func(a, b)
  246. self._assert_func([a], [b])
  247. self._test_not_equal([a], b)
  248. for a, b in itertools.product(tds, dts):
  249. self._test_not_equal(a, b)
  250. self._test_not_equal(a, [b])
  251. self._test_not_equal([a], [b])
  252. self._test_not_equal([a], np.datetime64("2017-01-01", "s"))
  253. self._test_not_equal([b], np.datetime64("2017-01-01", "s"))
  254. self._test_not_equal([a], np.timedelta64(123, "s"))
  255. self._test_not_equal([b], np.timedelta64(123, "s"))
  256. def test_non_numeric(self):
  257. self._assert_func('ab', 'ab')
  258. self._test_not_equal('ab', 'abb')
  259. def test_complex_item(self):
  260. self._assert_func(complex(1, 2), complex(1, 2))
  261. self._assert_func(complex(1, np.nan), complex(1, np.nan))
  262. self._test_not_equal(complex(1, np.nan), complex(1, 2))
  263. self._test_not_equal(complex(np.nan, 1), complex(1, np.nan))
  264. self._test_not_equal(complex(np.nan, np.inf), complex(np.nan, 2))
  265. def test_negative_zero(self):
  266. self._test_not_equal(np.PZERO, np.NZERO)
  267. def test_complex(self):
  268. x = np.array([complex(1, 2), complex(1, np.nan)])
  269. y = np.array([complex(1, 2), complex(1, 2)])
  270. self._assert_func(x, x)
  271. self._test_not_equal(x, y)
  272. def test_object(self):
  273. #gh-12942
  274. import datetime
  275. a = np.array([datetime.datetime(2000, 1, 1),
  276. datetime.datetime(2000, 1, 2)])
  277. self._test_not_equal(a, a[::-1])
  278. class TestArrayAlmostEqual(_GenericTest):
  279. def setup(self):
  280. self._assert_func = assert_array_almost_equal
  281. def test_closeness(self):
  282. # Note that in the course of time we ended up with
  283. # `abs(x - y) < 1.5 * 10**(-decimal)`
  284. # instead of the previously documented
  285. # `abs(x - y) < 0.5 * 10**(-decimal)`
  286. # so this check serves to preserve the wrongness.
  287. # test scalars
  288. self._assert_func(1.499999, 0.0, decimal=0)
  289. assert_raises(AssertionError,
  290. lambda: self._assert_func(1.5, 0.0, decimal=0))
  291. # test arrays
  292. self._assert_func([1.499999], [0.0], decimal=0)
  293. assert_raises(AssertionError,
  294. lambda: self._assert_func([1.5], [0.0], decimal=0))
  295. def test_simple(self):
  296. x = np.array([1234.2222])
  297. y = np.array([1234.2223])
  298. self._assert_func(x, y, decimal=3)
  299. self._assert_func(x, y, decimal=4)
  300. assert_raises(AssertionError,
  301. lambda: self._assert_func(x, y, decimal=5))
  302. def test_nan(self):
  303. anan = np.array([np.nan])
  304. aone = np.array([1])
  305. ainf = np.array([np.inf])
  306. self._assert_func(anan, anan)
  307. assert_raises(AssertionError,
  308. lambda: self._assert_func(anan, aone))
  309. assert_raises(AssertionError,
  310. lambda: self._assert_func(anan, ainf))
  311. assert_raises(AssertionError,
  312. lambda: self._assert_func(ainf, anan))
  313. def test_inf(self):
  314. a = np.array([[1., 2.], [3., 4.]])
  315. b = a.copy()
  316. a[0, 0] = np.inf
  317. assert_raises(AssertionError,
  318. lambda: self._assert_func(a, b))
  319. b[0, 0] = -np.inf
  320. assert_raises(AssertionError,
  321. lambda: self._assert_func(a, b))
  322. def test_subclass(self):
  323. a = np.array([[1., 2.], [3., 4.]])
  324. b = np.ma.masked_array([[1., 2.], [0., 4.]],
  325. [[False, False], [True, False]])
  326. self._assert_func(a, b)
  327. self._assert_func(b, a)
  328. self._assert_func(b, b)
  329. # Test fully masked as well (see gh-11123).
  330. a = np.ma.MaskedArray(3.5, mask=True)
  331. b = np.array([3., 4., 6.5])
  332. self._test_equal(a, b)
  333. self._test_equal(b, a)
  334. a = np.ma.masked
  335. b = np.array([3., 4., 6.5])
  336. self._test_equal(a, b)
  337. self._test_equal(b, a)
  338. a = np.ma.MaskedArray([3., 4., 6.5], mask=[True, True, True])
  339. b = np.array([1., 2., 3.])
  340. self._test_equal(a, b)
  341. self._test_equal(b, a)
  342. a = np.ma.MaskedArray([3., 4., 6.5], mask=[True, True, True])
  343. b = np.array(1.)
  344. self._test_equal(a, b)
  345. self._test_equal(b, a)
  346. def test_subclass_that_cannot_be_bool(self):
  347. # While we cannot guarantee testing functions will always work for
  348. # subclasses, the tests should ideally rely only on subclasses having
  349. # comparison operators, not on them being able to store booleans
  350. # (which, e.g., astropy Quantity cannot usefully do). See gh-8452.
  351. class MyArray(np.ndarray):
  352. def __eq__(self, other):
  353. return super().__eq__(other).view(np.ndarray)
  354. def __lt__(self, other):
  355. return super().__lt__(other).view(np.ndarray)
  356. def all(self, *args, **kwargs):
  357. raise NotImplementedError
  358. a = np.array([1., 2.]).view(MyArray)
  359. self._assert_func(a, a)
  360. class TestAlmostEqual(_GenericTest):
  361. def setup(self):
  362. self._assert_func = assert_almost_equal
  363. def test_closeness(self):
  364. # Note that in the course of time we ended up with
  365. # `abs(x - y) < 1.5 * 10**(-decimal)`
  366. # instead of the previously documented
  367. # `abs(x - y) < 0.5 * 10**(-decimal)`
  368. # so this check serves to preserve the wrongness.
  369. # test scalars
  370. self._assert_func(1.499999, 0.0, decimal=0)
  371. assert_raises(AssertionError,
  372. lambda: self._assert_func(1.5, 0.0, decimal=0))
  373. # test arrays
  374. self._assert_func([1.499999], [0.0], decimal=0)
  375. assert_raises(AssertionError,
  376. lambda: self._assert_func([1.5], [0.0], decimal=0))
  377. def test_nan_item(self):
  378. self._assert_func(np.nan, np.nan)
  379. assert_raises(AssertionError,
  380. lambda: self._assert_func(np.nan, 1))
  381. assert_raises(AssertionError,
  382. lambda: self._assert_func(np.nan, np.inf))
  383. assert_raises(AssertionError,
  384. lambda: self._assert_func(np.inf, np.nan))
  385. def test_inf_item(self):
  386. self._assert_func(np.inf, np.inf)
  387. self._assert_func(-np.inf, -np.inf)
  388. assert_raises(AssertionError,
  389. lambda: self._assert_func(np.inf, 1))
  390. assert_raises(AssertionError,
  391. lambda: self._assert_func(-np.inf, np.inf))
  392. def test_simple_item(self):
  393. self._test_not_equal(1, 2)
  394. def test_complex_item(self):
  395. self._assert_func(complex(1, 2), complex(1, 2))
  396. self._assert_func(complex(1, np.nan), complex(1, np.nan))
  397. self._assert_func(complex(np.inf, np.nan), complex(np.inf, np.nan))
  398. self._test_not_equal(complex(1, np.nan), complex(1, 2))
  399. self._test_not_equal(complex(np.nan, 1), complex(1, np.nan))
  400. self._test_not_equal(complex(np.nan, np.inf), complex(np.nan, 2))
  401. def test_complex(self):
  402. x = np.array([complex(1, 2), complex(1, np.nan)])
  403. z = np.array([complex(1, 2), complex(np.nan, 1)])
  404. y = np.array([complex(1, 2), complex(1, 2)])
  405. self._assert_func(x, x)
  406. self._test_not_equal(x, y)
  407. self._test_not_equal(x, z)
  408. def test_error_message(self):
  409. """Check the message is formatted correctly for the decimal value.
  410. Also check the message when input includes inf or nan (gh12200)"""
  411. x = np.array([1.00000000001, 2.00000000002, 3.00003])
  412. y = np.array([1.00000000002, 2.00000000003, 3.00004])
  413. # Test with a different amount of decimal digits
  414. with pytest.raises(AssertionError) as exc_info:
  415. self._assert_func(x, y, decimal=12)
  416. msgs = str(exc_info.value).split('\n')
  417. assert_equal(msgs[3], 'Mismatched elements: 3 / 3 (100%)')
  418. assert_equal(msgs[4], 'Max absolute difference: 1.e-05')
  419. assert_equal(msgs[5], 'Max relative difference: 3.33328889e-06')
  420. assert_equal(
  421. msgs[6],
  422. ' x: array([1.00000000001, 2.00000000002, 3.00003 ])')
  423. assert_equal(
  424. msgs[7],
  425. ' y: array([1.00000000002, 2.00000000003, 3.00004 ])')
  426. # With the default value of decimal digits, only the 3rd element
  427. # differs. Note that we only check for the formatting of the arrays
  428. # themselves.
  429. with pytest.raises(AssertionError) as exc_info:
  430. self._assert_func(x, y)
  431. msgs = str(exc_info.value).split('\n')
  432. assert_equal(msgs[3], 'Mismatched elements: 1 / 3 (33.3%)')
  433. assert_equal(msgs[4], 'Max absolute difference: 1.e-05')
  434. assert_equal(msgs[5], 'Max relative difference: 3.33328889e-06')
  435. assert_equal(msgs[6], ' x: array([1. , 2. , 3.00003])')
  436. assert_equal(msgs[7], ' y: array([1. , 2. , 3.00004])')
  437. # Check the error message when input includes inf
  438. x = np.array([np.inf, 0])
  439. y = np.array([np.inf, 1])
  440. with pytest.raises(AssertionError) as exc_info:
  441. self._assert_func(x, y)
  442. msgs = str(exc_info.value).split('\n')
  443. assert_equal(msgs[3], 'Mismatched elements: 1 / 2 (50%)')
  444. assert_equal(msgs[4], 'Max absolute difference: 1.')
  445. assert_equal(msgs[5], 'Max relative difference: 1.')
  446. assert_equal(msgs[6], ' x: array([inf, 0.])')
  447. assert_equal(msgs[7], ' y: array([inf, 1.])')
  448. # Check the error message when dividing by zero
  449. x = np.array([1, 2])
  450. y = np.array([0, 0])
  451. with pytest.raises(AssertionError) as exc_info:
  452. self._assert_func(x, y)
  453. msgs = str(exc_info.value).split('\n')
  454. assert_equal(msgs[3], 'Mismatched elements: 2 / 2 (100%)')
  455. assert_equal(msgs[4], 'Max absolute difference: 2')
  456. assert_equal(msgs[5], 'Max relative difference: inf')
  457. def test_error_message_2(self):
  458. """Check the message is formatted correctly when either x or y is a scalar."""
  459. x = 2
  460. y = np.ones(20)
  461. with pytest.raises(AssertionError) as exc_info:
  462. self._assert_func(x, y)
  463. msgs = str(exc_info.value).split('\n')
  464. assert_equal(msgs[3], 'Mismatched elements: 20 / 20 (100%)')
  465. assert_equal(msgs[4], 'Max absolute difference: 1.')
  466. assert_equal(msgs[5], 'Max relative difference: 1.')
  467. y = 2
  468. x = np.ones(20)
  469. with pytest.raises(AssertionError) as exc_info:
  470. self._assert_func(x, y)
  471. msgs = str(exc_info.value).split('\n')
  472. assert_equal(msgs[3], 'Mismatched elements: 20 / 20 (100%)')
  473. assert_equal(msgs[4], 'Max absolute difference: 1.')
  474. assert_equal(msgs[5], 'Max relative difference: 0.5')
  475. def test_subclass_that_cannot_be_bool(self):
  476. # While we cannot guarantee testing functions will always work for
  477. # subclasses, the tests should ideally rely only on subclasses having
  478. # comparison operators, not on them being able to store booleans
  479. # (which, e.g., astropy Quantity cannot usefully do). See gh-8452.
  480. class MyArray(np.ndarray):
  481. def __eq__(self, other):
  482. return super().__eq__(other).view(np.ndarray)
  483. def __lt__(self, other):
  484. return super().__lt__(other).view(np.ndarray)
  485. def all(self, *args, **kwargs):
  486. raise NotImplementedError
  487. a = np.array([1., 2.]).view(MyArray)
  488. self._assert_func(a, a)
  489. class TestApproxEqual:
  490. def setup(self):
  491. self._assert_func = assert_approx_equal
  492. def test_simple_0d_arrays(self):
  493. x = np.array(1234.22)
  494. y = np.array(1234.23)
  495. self._assert_func(x, y, significant=5)
  496. self._assert_func(x, y, significant=6)
  497. assert_raises(AssertionError,
  498. lambda: self._assert_func(x, y, significant=7))
  499. def test_simple_items(self):
  500. x = 1234.22
  501. y = 1234.23
  502. self._assert_func(x, y, significant=4)
  503. self._assert_func(x, y, significant=5)
  504. self._assert_func(x, y, significant=6)
  505. assert_raises(AssertionError,
  506. lambda: self._assert_func(x, y, significant=7))
  507. def test_nan_array(self):
  508. anan = np.array(np.nan)
  509. aone = np.array(1)
  510. ainf = np.array(np.inf)
  511. self._assert_func(anan, anan)
  512. assert_raises(AssertionError, lambda: self._assert_func(anan, aone))
  513. assert_raises(AssertionError, lambda: self._assert_func(anan, ainf))
  514. assert_raises(AssertionError, lambda: self._assert_func(ainf, anan))
  515. def test_nan_items(self):
  516. anan = np.array(np.nan)
  517. aone = np.array(1)
  518. ainf = np.array(np.inf)
  519. self._assert_func(anan, anan)
  520. assert_raises(AssertionError, lambda: self._assert_func(anan, aone))
  521. assert_raises(AssertionError, lambda: self._assert_func(anan, ainf))
  522. assert_raises(AssertionError, lambda: self._assert_func(ainf, anan))
  523. class TestArrayAssertLess:
  524. def setup(self):
  525. self._assert_func = assert_array_less
  526. def test_simple_arrays(self):
  527. x = np.array([1.1, 2.2])
  528. y = np.array([1.2, 2.3])
  529. self._assert_func(x, y)
  530. assert_raises(AssertionError, lambda: self._assert_func(y, x))
  531. y = np.array([1.0, 2.3])
  532. assert_raises(AssertionError, lambda: self._assert_func(x, y))
  533. assert_raises(AssertionError, lambda: self._assert_func(y, x))
  534. def test_rank2(self):
  535. x = np.array([[1.1, 2.2], [3.3, 4.4]])
  536. y = np.array([[1.2, 2.3], [3.4, 4.5]])
  537. self._assert_func(x, y)
  538. assert_raises(AssertionError, lambda: self._assert_func(y, x))
  539. y = np.array([[1.0, 2.3], [3.4, 4.5]])
  540. assert_raises(AssertionError, lambda: self._assert_func(x, y))
  541. assert_raises(AssertionError, lambda: self._assert_func(y, x))
  542. def test_rank3(self):
  543. x = np.ones(shape=(2, 2, 2))
  544. y = np.ones(shape=(2, 2, 2))+1
  545. self._assert_func(x, y)
  546. assert_raises(AssertionError, lambda: self._assert_func(y, x))
  547. y[0, 0, 0] = 0
  548. assert_raises(AssertionError, lambda: self._assert_func(x, y))
  549. assert_raises(AssertionError, lambda: self._assert_func(y, x))
  550. def test_simple_items(self):
  551. x = 1.1
  552. y = 2.2
  553. self._assert_func(x, y)
  554. assert_raises(AssertionError, lambda: self._assert_func(y, x))
  555. y = np.array([2.2, 3.3])
  556. self._assert_func(x, y)
  557. assert_raises(AssertionError, lambda: self._assert_func(y, x))
  558. y = np.array([1.0, 3.3])
  559. assert_raises(AssertionError, lambda: self._assert_func(x, y))
  560. def test_nan_noncompare(self):
  561. anan = np.array(np.nan)
  562. aone = np.array(1)
  563. ainf = np.array(np.inf)
  564. self._assert_func(anan, anan)
  565. assert_raises(AssertionError, lambda: self._assert_func(aone, anan))
  566. assert_raises(AssertionError, lambda: self._assert_func(anan, aone))
  567. assert_raises(AssertionError, lambda: self._assert_func(anan, ainf))
  568. assert_raises(AssertionError, lambda: self._assert_func(ainf, anan))
  569. def test_nan_noncompare_array(self):
  570. x = np.array([1.1, 2.2, 3.3])
  571. anan = np.array(np.nan)
  572. assert_raises(AssertionError, lambda: self._assert_func(x, anan))
  573. assert_raises(AssertionError, lambda: self._assert_func(anan, x))
  574. x = np.array([1.1, 2.2, np.nan])
  575. assert_raises(AssertionError, lambda: self._assert_func(x, anan))
  576. assert_raises(AssertionError, lambda: self._assert_func(anan, x))
  577. y = np.array([1.0, 2.0, np.nan])
  578. self._assert_func(y, x)
  579. assert_raises(AssertionError, lambda: self._assert_func(x, y))
  580. def test_inf_compare(self):
  581. aone = np.array(1)
  582. ainf = np.array(np.inf)
  583. self._assert_func(aone, ainf)
  584. self._assert_func(-ainf, aone)
  585. self._assert_func(-ainf, ainf)
  586. assert_raises(AssertionError, lambda: self._assert_func(ainf, aone))
  587. assert_raises(AssertionError, lambda: self._assert_func(aone, -ainf))
  588. assert_raises(AssertionError, lambda: self._assert_func(ainf, ainf))
  589. assert_raises(AssertionError, lambda: self._assert_func(ainf, -ainf))
  590. assert_raises(AssertionError, lambda: self._assert_func(-ainf, -ainf))
  591. def test_inf_compare_array(self):
  592. x = np.array([1.1, 2.2, np.inf])
  593. ainf = np.array(np.inf)
  594. assert_raises(AssertionError, lambda: self._assert_func(x, ainf))
  595. assert_raises(AssertionError, lambda: self._assert_func(ainf, x))
  596. assert_raises(AssertionError, lambda: self._assert_func(x, -ainf))
  597. assert_raises(AssertionError, lambda: self._assert_func(-x, -ainf))
  598. assert_raises(AssertionError, lambda: self._assert_func(-ainf, -x))
  599. self._assert_func(-ainf, x)
  600. @pytest.mark.skip(reason="The raises decorator depends on Nose")
  601. class TestRaises:
  602. def setup(self):
  603. class MyException(Exception):
  604. pass
  605. self.e = MyException
  606. def raises_exception(self, e):
  607. raise e
  608. def does_not_raise_exception(self):
  609. pass
  610. def test_correct_catch(self):
  611. raises(self.e)(self.raises_exception)(self.e) # raises?
  612. def test_wrong_exception(self):
  613. try:
  614. raises(self.e)(self.raises_exception)(RuntimeError) # raises?
  615. except RuntimeError:
  616. return
  617. else:
  618. raise AssertionError("should have caught RuntimeError")
  619. def test_catch_no_raise(self):
  620. try:
  621. raises(self.e)(self.does_not_raise_exception)() # raises?
  622. except AssertionError:
  623. return
  624. else:
  625. raise AssertionError("should have raised an AssertionError")
  626. class TestWarns:
  627. def test_warn(self):
  628. def f():
  629. warnings.warn("yo")
  630. return 3
  631. before_filters = sys.modules['warnings'].filters[:]
  632. assert_equal(assert_warns(UserWarning, f), 3)
  633. after_filters = sys.modules['warnings'].filters
  634. assert_raises(AssertionError, assert_no_warnings, f)
  635. assert_equal(assert_no_warnings(lambda x: x, 1), 1)
  636. # Check that the warnings state is unchanged
  637. assert_equal(before_filters, after_filters,
  638. "assert_warns does not preserver warnings state")
  639. def test_context_manager(self):
  640. before_filters = sys.modules['warnings'].filters[:]
  641. with assert_warns(UserWarning):
  642. warnings.warn("yo")
  643. after_filters = sys.modules['warnings'].filters
  644. def no_warnings():
  645. with assert_no_warnings():
  646. warnings.warn("yo")
  647. assert_raises(AssertionError, no_warnings)
  648. assert_equal(before_filters, after_filters,
  649. "assert_warns does not preserver warnings state")
  650. def test_warn_wrong_warning(self):
  651. def f():
  652. warnings.warn("yo", DeprecationWarning)
  653. failed = False
  654. with warnings.catch_warnings():
  655. warnings.simplefilter("error", DeprecationWarning)
  656. try:
  657. # Should raise a DeprecationWarning
  658. assert_warns(UserWarning, f)
  659. failed = True
  660. except DeprecationWarning:
  661. pass
  662. if failed:
  663. raise AssertionError("wrong warning caught by assert_warn")
  664. class TestAssertAllclose:
  665. def test_simple(self):
  666. x = 1e-3
  667. y = 1e-9
  668. assert_allclose(x, y, atol=1)
  669. assert_raises(AssertionError, assert_allclose, x, y)
  670. a = np.array([x, y, x, y])
  671. b = np.array([x, y, x, x])
  672. assert_allclose(a, b, atol=1)
  673. assert_raises(AssertionError, assert_allclose, a, b)
  674. b[-1] = y * (1 + 1e-8)
  675. assert_allclose(a, b)
  676. assert_raises(AssertionError, assert_allclose, a, b, rtol=1e-9)
  677. assert_allclose(6, 10, rtol=0.5)
  678. assert_raises(AssertionError, assert_allclose, 10, 6, rtol=0.5)
  679. def test_min_int(self):
  680. a = np.array([np.iinfo(np.int_).min], dtype=np.int_)
  681. # Should not raise:
  682. assert_allclose(a, a)
  683. def test_report_fail_percentage(self):
  684. a = np.array([1, 1, 1, 1])
  685. b = np.array([1, 1, 1, 2])
  686. with pytest.raises(AssertionError) as exc_info:
  687. assert_allclose(a, b)
  688. msg = str(exc_info.value)
  689. assert_('Mismatched elements: 1 / 4 (25%)\n'
  690. 'Max absolute difference: 1\n'
  691. 'Max relative difference: 0.5' in msg)
  692. def test_equal_nan(self):
  693. a = np.array([np.nan])
  694. b = np.array([np.nan])
  695. # Should not raise:
  696. assert_allclose(a, b, equal_nan=True)
  697. def test_not_equal_nan(self):
  698. a = np.array([np.nan])
  699. b = np.array([np.nan])
  700. assert_raises(AssertionError, assert_allclose, a, b, equal_nan=False)
  701. def test_equal_nan_default(self):
  702. # Make sure equal_nan default behavior remains unchanged. (All
  703. # of these functions use assert_array_compare under the hood.)
  704. # None of these should raise.
  705. a = np.array([np.nan])
  706. b = np.array([np.nan])
  707. assert_array_equal(a, b)
  708. assert_array_almost_equal(a, b)
  709. assert_array_less(a, b)
  710. assert_allclose(a, b)
  711. def test_report_max_relative_error(self):
  712. a = np.array([0, 1])
  713. b = np.array([0, 2])
  714. with pytest.raises(AssertionError) as exc_info:
  715. assert_allclose(a, b)
  716. msg = str(exc_info.value)
  717. assert_('Max relative difference: 0.5' in msg)
  718. def test_timedelta(self):
  719. # see gh-18286
  720. a = np.array([[1, 2, 3, "NaT"]], dtype="m8[ns]")
  721. assert_allclose(a, a)
  722. class TestArrayAlmostEqualNulp:
  723. def test_float64_pass(self):
  724. # The number of units of least precision
  725. # In this case, use a few places above the lowest level (ie nulp=1)
  726. nulp = 5
  727. x = np.linspace(-20, 20, 50, dtype=np.float64)
  728. x = 10**x
  729. x = np.r_[-x, x]
  730. # Addition
  731. eps = np.finfo(x.dtype).eps
  732. y = x + x*eps*nulp/2.
  733. assert_array_almost_equal_nulp(x, y, nulp)
  734. # Subtraction
  735. epsneg = np.finfo(x.dtype).epsneg
  736. y = x - x*epsneg*nulp/2.
  737. assert_array_almost_equal_nulp(x, y, nulp)
  738. def test_float64_fail(self):
  739. nulp = 5
  740. x = np.linspace(-20, 20, 50, dtype=np.float64)
  741. x = 10**x
  742. x = np.r_[-x, x]
  743. eps = np.finfo(x.dtype).eps
  744. y = x + x*eps*nulp*2.
  745. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  746. x, y, nulp)
  747. epsneg = np.finfo(x.dtype).epsneg
  748. y = x - x*epsneg*nulp*2.
  749. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  750. x, y, nulp)
  751. def test_float64_ignore_nan(self):
  752. # Ignore ULP differences between various NAN's
  753. # Note that MIPS may reverse quiet and signaling nans
  754. # so we use the builtin version as a base.
  755. offset = np.uint64(0xffffffff)
  756. nan1_i64 = np.array(np.nan, dtype=np.float64).view(np.uint64)
  757. nan2_i64 = nan1_i64 ^ offset # nan payload on MIPS is all ones.
  758. nan1_f64 = nan1_i64.view(np.float64)
  759. nan2_f64 = nan2_i64.view(np.float64)
  760. assert_array_max_ulp(nan1_f64, nan2_f64, 0)
  761. def test_float32_pass(self):
  762. nulp = 5
  763. x = np.linspace(-20, 20, 50, dtype=np.float32)
  764. x = 10**x
  765. x = np.r_[-x, x]
  766. eps = np.finfo(x.dtype).eps
  767. y = x + x*eps*nulp/2.
  768. assert_array_almost_equal_nulp(x, y, nulp)
  769. epsneg = np.finfo(x.dtype).epsneg
  770. y = x - x*epsneg*nulp/2.
  771. assert_array_almost_equal_nulp(x, y, nulp)
  772. def test_float32_fail(self):
  773. nulp = 5
  774. x = np.linspace(-20, 20, 50, dtype=np.float32)
  775. x = 10**x
  776. x = np.r_[-x, x]
  777. eps = np.finfo(x.dtype).eps
  778. y = x + x*eps*nulp*2.
  779. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  780. x, y, nulp)
  781. epsneg = np.finfo(x.dtype).epsneg
  782. y = x - x*epsneg*nulp*2.
  783. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  784. x, y, nulp)
  785. def test_float32_ignore_nan(self):
  786. # Ignore ULP differences between various NAN's
  787. # Note that MIPS may reverse quiet and signaling nans
  788. # so we use the builtin version as a base.
  789. offset = np.uint32(0xffff)
  790. nan1_i32 = np.array(np.nan, dtype=np.float32).view(np.uint32)
  791. nan2_i32 = nan1_i32 ^ offset # nan payload on MIPS is all ones.
  792. nan1_f32 = nan1_i32.view(np.float32)
  793. nan2_f32 = nan2_i32.view(np.float32)
  794. assert_array_max_ulp(nan1_f32, nan2_f32, 0)
  795. def test_float16_pass(self):
  796. nulp = 5
  797. x = np.linspace(-4, 4, 10, dtype=np.float16)
  798. x = 10**x
  799. x = np.r_[-x, x]
  800. eps = np.finfo(x.dtype).eps
  801. y = x + x*eps*nulp/2.
  802. assert_array_almost_equal_nulp(x, y, nulp)
  803. epsneg = np.finfo(x.dtype).epsneg
  804. y = x - x*epsneg*nulp/2.
  805. assert_array_almost_equal_nulp(x, y, nulp)
  806. def test_float16_fail(self):
  807. nulp = 5
  808. x = np.linspace(-4, 4, 10, dtype=np.float16)
  809. x = 10**x
  810. x = np.r_[-x, x]
  811. eps = np.finfo(x.dtype).eps
  812. y = x + x*eps*nulp*2.
  813. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  814. x, y, nulp)
  815. epsneg = np.finfo(x.dtype).epsneg
  816. y = x - x*epsneg*nulp*2.
  817. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  818. x, y, nulp)
  819. def test_float16_ignore_nan(self):
  820. # Ignore ULP differences between various NAN's
  821. # Note that MIPS may reverse quiet and signaling nans
  822. # so we use the builtin version as a base.
  823. offset = np.uint16(0xff)
  824. nan1_i16 = np.array(np.nan, dtype=np.float16).view(np.uint16)
  825. nan2_i16 = nan1_i16 ^ offset # nan payload on MIPS is all ones.
  826. nan1_f16 = nan1_i16.view(np.float16)
  827. nan2_f16 = nan2_i16.view(np.float16)
  828. assert_array_max_ulp(nan1_f16, nan2_f16, 0)
  829. def test_complex128_pass(self):
  830. nulp = 5
  831. x = np.linspace(-20, 20, 50, dtype=np.float64)
  832. x = 10**x
  833. x = np.r_[-x, x]
  834. xi = x + x*1j
  835. eps = np.finfo(x.dtype).eps
  836. y = x + x*eps*nulp/2.
  837. assert_array_almost_equal_nulp(xi, x + y*1j, nulp)
  838. assert_array_almost_equal_nulp(xi, y + x*1j, nulp)
  839. # The test condition needs to be at least a factor of sqrt(2) smaller
  840. # because the real and imaginary parts both change
  841. y = x + x*eps*nulp/4.
  842. assert_array_almost_equal_nulp(xi, y + y*1j, nulp)
  843. epsneg = np.finfo(x.dtype).epsneg
  844. y = x - x*epsneg*nulp/2.
  845. assert_array_almost_equal_nulp(xi, x + y*1j, nulp)
  846. assert_array_almost_equal_nulp(xi, y + x*1j, nulp)
  847. y = x - x*epsneg*nulp/4.
  848. assert_array_almost_equal_nulp(xi, y + y*1j, nulp)
  849. def test_complex128_fail(self):
  850. nulp = 5
  851. x = np.linspace(-20, 20, 50, dtype=np.float64)
  852. x = 10**x
  853. x = np.r_[-x, x]
  854. xi = x + x*1j
  855. eps = np.finfo(x.dtype).eps
  856. y = x + x*eps*nulp*2.
  857. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  858. xi, x + y*1j, nulp)
  859. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  860. xi, y + x*1j, nulp)
  861. # The test condition needs to be at least a factor of sqrt(2) smaller
  862. # because the real and imaginary parts both change
  863. y = x + x*eps*nulp
  864. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  865. xi, y + y*1j, nulp)
  866. epsneg = np.finfo(x.dtype).epsneg
  867. y = x - x*epsneg*nulp*2.
  868. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  869. xi, x + y*1j, nulp)
  870. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  871. xi, y + x*1j, nulp)
  872. y = x - x*epsneg*nulp
  873. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  874. xi, y + y*1j, nulp)
  875. def test_complex64_pass(self):
  876. nulp = 5
  877. x = np.linspace(-20, 20, 50, dtype=np.float32)
  878. x = 10**x
  879. x = np.r_[-x, x]
  880. xi = x + x*1j
  881. eps = np.finfo(x.dtype).eps
  882. y = x + x*eps*nulp/2.
  883. assert_array_almost_equal_nulp(xi, x + y*1j, nulp)
  884. assert_array_almost_equal_nulp(xi, y + x*1j, nulp)
  885. y = x + x*eps*nulp/4.
  886. assert_array_almost_equal_nulp(xi, y + y*1j, nulp)
  887. epsneg = np.finfo(x.dtype).epsneg
  888. y = x - x*epsneg*nulp/2.
  889. assert_array_almost_equal_nulp(xi, x + y*1j, nulp)
  890. assert_array_almost_equal_nulp(xi, y + x*1j, nulp)
  891. y = x - x*epsneg*nulp/4.
  892. assert_array_almost_equal_nulp(xi, y + y*1j, nulp)
  893. def test_complex64_fail(self):
  894. nulp = 5
  895. x = np.linspace(-20, 20, 50, dtype=np.float32)
  896. x = 10**x
  897. x = np.r_[-x, x]
  898. xi = x + x*1j
  899. eps = np.finfo(x.dtype).eps
  900. y = x + x*eps*nulp*2.
  901. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  902. xi, x + y*1j, nulp)
  903. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  904. xi, y + x*1j, nulp)
  905. y = x + x*eps*nulp
  906. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  907. xi, y + y*1j, nulp)
  908. epsneg = np.finfo(x.dtype).epsneg
  909. y = x - x*epsneg*nulp*2.
  910. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  911. xi, x + y*1j, nulp)
  912. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  913. xi, y + x*1j, nulp)
  914. y = x - x*epsneg*nulp
  915. assert_raises(AssertionError, assert_array_almost_equal_nulp,
  916. xi, y + y*1j, nulp)
  917. class TestULP:
  918. def test_equal(self):
  919. x = np.random.randn(10)
  920. assert_array_max_ulp(x, x, maxulp=0)
  921. def test_single(self):
  922. # Generate 1 + small deviation, check that adding eps gives a few UNL
  923. x = np.ones(10).astype(np.float32)
  924. x += 0.01 * np.random.randn(10).astype(np.float32)
  925. eps = np.finfo(np.float32).eps
  926. assert_array_max_ulp(x, x+eps, maxulp=20)
  927. def test_double(self):
  928. # Generate 1 + small deviation, check that adding eps gives a few UNL
  929. x = np.ones(10).astype(np.float64)
  930. x += 0.01 * np.random.randn(10).astype(np.float64)
  931. eps = np.finfo(np.float64).eps
  932. assert_array_max_ulp(x, x+eps, maxulp=200)
  933. def test_inf(self):
  934. for dt in [np.float32, np.float64]:
  935. inf = np.array([np.inf]).astype(dt)
  936. big = np.array([np.finfo(dt).max])
  937. assert_array_max_ulp(inf, big, maxulp=200)
  938. def test_nan(self):
  939. # Test that nan is 'far' from small, tiny, inf, max and min
  940. for dt in [np.float32, np.float64]:
  941. if dt == np.float32:
  942. maxulp = 1e6
  943. else:
  944. maxulp = 1e12
  945. inf = np.array([np.inf]).astype(dt)
  946. nan = np.array([np.nan]).astype(dt)
  947. big = np.array([np.finfo(dt).max])
  948. tiny = np.array([np.finfo(dt).tiny])
  949. zero = np.array([np.PZERO]).astype(dt)
  950. nzero = np.array([np.NZERO]).astype(dt)
  951. assert_raises(AssertionError,
  952. lambda: assert_array_max_ulp(nan, inf,
  953. maxulp=maxulp))
  954. assert_raises(AssertionError,
  955. lambda: assert_array_max_ulp(nan, big,
  956. maxulp=maxulp))
  957. assert_raises(AssertionError,
  958. lambda: assert_array_max_ulp(nan, tiny,
  959. maxulp=maxulp))
  960. assert_raises(AssertionError,
  961. lambda: assert_array_max_ulp(nan, zero,
  962. maxulp=maxulp))
  963. assert_raises(AssertionError,
  964. lambda: assert_array_max_ulp(nan, nzero,
  965. maxulp=maxulp))
  966. class TestStringEqual:
  967. def test_simple(self):
  968. assert_string_equal("hello", "hello")
  969. assert_string_equal("hello\nmultiline", "hello\nmultiline")
  970. with pytest.raises(AssertionError) as exc_info:
  971. assert_string_equal("foo\nbar", "hello\nbar")
  972. msg = str(exc_info.value)
  973. assert_equal(msg, "Differences in strings:\n- foo\n+ hello")
  974. assert_raises(AssertionError,
  975. lambda: assert_string_equal("foo", "hello"))
  976. def test_regex(self):
  977. assert_string_equal("a+*b", "a+*b")
  978. assert_raises(AssertionError,
  979. lambda: assert_string_equal("aaa", "a+b"))
  980. def assert_warn_len_equal(mod, n_in_context, py34=None, py37=None):
  981. try:
  982. mod_warns = mod.__warningregistry__
  983. except AttributeError:
  984. # the lack of a __warningregistry__
  985. # attribute means that no warning has
  986. # occurred; this can be triggered in
  987. # a parallel test scenario, while in
  988. # a serial test scenario an initial
  989. # warning (and therefore the attribute)
  990. # are always created first
  991. mod_warns = {}
  992. num_warns = len(mod_warns)
  993. # Python 3.4 appears to clear any pre-existing warnings of the same type,
  994. # when raising warnings inside a catch_warnings block. So, there is a
  995. # warning generated by the tests within the context manager, but no
  996. # previous warnings.
  997. if 'version' in mod_warns:
  998. # Python 3 adds a 'version' entry to the registry,
  999. # do not count it.
  1000. num_warns -= 1
  1001. # Behavior of warnings is Python version dependent. Adjust the
  1002. # expected result to compensate. In particular, Python 3.7 does
  1003. # not make an entry for ignored warnings.
  1004. if sys.version_info[:2] >= (3, 7):
  1005. if py37 is not None:
  1006. n_in_context = py37
  1007. else:
  1008. if py34 is not None:
  1009. n_in_context = py34
  1010. assert_equal(num_warns, n_in_context)
  1011. def test_warn_len_equal_call_scenarios():
  1012. # assert_warn_len_equal is called under
  1013. # varying circumstances depending on serial
  1014. # vs. parallel test scenarios; this test
  1015. # simply aims to probe both code paths and
  1016. # check that no assertion is uncaught
  1017. # parallel scenario -- no warning issued yet
  1018. class mod:
  1019. pass
  1020. mod_inst = mod()
  1021. assert_warn_len_equal(mod=mod_inst,
  1022. n_in_context=0)
  1023. # serial test scenario -- the __warningregistry__
  1024. # attribute should be present
  1025. class mod:
  1026. def __init__(self):
  1027. self.__warningregistry__ = {'warning1':1,
  1028. 'warning2':2}
  1029. mod_inst = mod()
  1030. assert_warn_len_equal(mod=mod_inst,
  1031. n_in_context=2)
  1032. def _get_fresh_mod():
  1033. # Get this module, with warning registry empty
  1034. my_mod = sys.modules[__name__]
  1035. try:
  1036. my_mod.__warningregistry__.clear()
  1037. except AttributeError:
  1038. # will not have a __warningregistry__ unless warning has been
  1039. # raised in the module at some point
  1040. pass
  1041. return my_mod
  1042. def test_clear_and_catch_warnings():
  1043. # Initial state of module, no warnings
  1044. my_mod = _get_fresh_mod()
  1045. assert_equal(getattr(my_mod, '__warningregistry__', {}), {})
  1046. with clear_and_catch_warnings(modules=[my_mod]):
  1047. warnings.simplefilter('ignore')
  1048. warnings.warn('Some warning')
  1049. assert_equal(my_mod.__warningregistry__, {})
  1050. # Without specified modules, don't clear warnings during context
  1051. # Python 3.7 catch_warnings doesn't make an entry for 'ignore'.
  1052. with clear_and_catch_warnings():
  1053. warnings.simplefilter('ignore')
  1054. warnings.warn('Some warning')
  1055. assert_warn_len_equal(my_mod, 1, py37=0)
  1056. # Confirm that specifying module keeps old warning, does not add new
  1057. with clear_and_catch_warnings(modules=[my_mod]):
  1058. warnings.simplefilter('ignore')
  1059. warnings.warn('Another warning')
  1060. assert_warn_len_equal(my_mod, 1, py37=0)
  1061. # Another warning, no module spec does add to warnings dict, except on
  1062. # Python 3.4 (see comments in `assert_warn_len_equal`)
  1063. # Python 3.7 catch_warnings doesn't make an entry for 'ignore'.
  1064. with clear_and_catch_warnings():
  1065. warnings.simplefilter('ignore')
  1066. warnings.warn('Another warning')
  1067. assert_warn_len_equal(my_mod, 2, py34=1, py37=0)
  1068. def test_suppress_warnings_module():
  1069. # Initial state of module, no warnings
  1070. my_mod = _get_fresh_mod()
  1071. assert_equal(getattr(my_mod, '__warningregistry__', {}), {})
  1072. def warn_other_module():
  1073. # Apply along axis is implemented in python; stacklevel=2 means
  1074. # we end up inside its module, not ours.
  1075. def warn(arr):
  1076. warnings.warn("Some warning 2", stacklevel=2)
  1077. return arr
  1078. np.apply_along_axis(warn, 0, [0])
  1079. # Test module based warning suppression:
  1080. assert_warn_len_equal(my_mod, 0)
  1081. with suppress_warnings() as sup:
  1082. sup.record(UserWarning)
  1083. # suppress warning from other module (may have .pyc ending),
  1084. # if apply_along_axis is moved, had to be changed.
  1085. sup.filter(module=np.lib.shape_base)
  1086. warnings.warn("Some warning")
  1087. warn_other_module()
  1088. # Check that the suppression did test the file correctly (this module
  1089. # got filtered)
  1090. assert_equal(len(sup.log), 1)
  1091. assert_equal(sup.log[0].message.args[0], "Some warning")
  1092. assert_warn_len_equal(my_mod, 0, py37=0)
  1093. sup = suppress_warnings()
  1094. # Will have to be changed if apply_along_axis is moved:
  1095. sup.filter(module=my_mod)
  1096. with sup:
  1097. warnings.warn('Some warning')
  1098. assert_warn_len_equal(my_mod, 0)
  1099. # And test repeat works:
  1100. sup.filter(module=my_mod)
  1101. with sup:
  1102. warnings.warn('Some warning')
  1103. assert_warn_len_equal(my_mod, 0)
  1104. # Without specified modules, don't clear warnings during context
  1105. # Python 3.7 does not add ignored warnings.
  1106. with suppress_warnings():
  1107. warnings.simplefilter('ignore')
  1108. warnings.warn('Some warning')
  1109. assert_warn_len_equal(my_mod, 1, py37=0)
  1110. def test_suppress_warnings_type():
  1111. # Initial state of module, no warnings
  1112. my_mod = _get_fresh_mod()
  1113. assert_equal(getattr(my_mod, '__warningregistry__', {}), {})
  1114. # Test module based warning suppression:
  1115. with suppress_warnings() as sup:
  1116. sup.filter(UserWarning)
  1117. warnings.warn('Some warning')
  1118. assert_warn_len_equal(my_mod, 0)
  1119. sup = suppress_warnings()
  1120. sup.filter(UserWarning)
  1121. with sup:
  1122. warnings.warn('Some warning')
  1123. assert_warn_len_equal(my_mod, 0)
  1124. # And test repeat works:
  1125. sup.filter(module=my_mod)
  1126. with sup:
  1127. warnings.warn('Some warning')
  1128. assert_warn_len_equal(my_mod, 0)
  1129. # Without specified modules, don't clear warnings during context
  1130. # Python 3.7 does not add ignored warnings.
  1131. with suppress_warnings():
  1132. warnings.simplefilter('ignore')
  1133. warnings.warn('Some warning')
  1134. assert_warn_len_equal(my_mod, 1, py37=0)
  1135. def test_suppress_warnings_decorate_no_record():
  1136. sup = suppress_warnings()
  1137. sup.filter(UserWarning)
  1138. @sup
  1139. def warn(category):
  1140. warnings.warn('Some warning', category)
  1141. with warnings.catch_warnings(record=True) as w:
  1142. warnings.simplefilter("always")
  1143. warn(UserWarning) # should be supppressed
  1144. warn(RuntimeWarning)
  1145. assert_equal(len(w), 1)
  1146. def test_suppress_warnings_record():
  1147. sup = suppress_warnings()
  1148. log1 = sup.record()
  1149. with sup:
  1150. log2 = sup.record(message='Some other warning 2')
  1151. sup.filter(message='Some warning')
  1152. warnings.warn('Some warning')
  1153. warnings.warn('Some other warning')
  1154. warnings.warn('Some other warning 2')
  1155. assert_equal(len(sup.log), 2)
  1156. assert_equal(len(log1), 1)
  1157. assert_equal(len(log2),1)
  1158. assert_equal(log2[0].message.args[0], 'Some other warning 2')
  1159. # Do it again, with the same context to see if some warnings survived:
  1160. with sup:
  1161. log2 = sup.record(message='Some other warning 2')
  1162. sup.filter(message='Some warning')
  1163. warnings.warn('Some warning')
  1164. warnings.warn('Some other warning')
  1165. warnings.warn('Some other warning 2')
  1166. assert_equal(len(sup.log), 2)
  1167. assert_equal(len(log1), 1)
  1168. assert_equal(len(log2), 1)
  1169. assert_equal(log2[0].message.args[0], 'Some other warning 2')
  1170. # Test nested:
  1171. with suppress_warnings() as sup:
  1172. sup.record()
  1173. with suppress_warnings() as sup2:
  1174. sup2.record(message='Some warning')
  1175. warnings.warn('Some warning')
  1176. warnings.warn('Some other warning')
  1177. assert_equal(len(sup2.log), 1)
  1178. assert_equal(len(sup.log), 1)
  1179. def test_suppress_warnings_forwarding():
  1180. def warn_other_module():
  1181. # Apply along axis is implemented in python; stacklevel=2 means
  1182. # we end up inside its module, not ours.
  1183. def warn(arr):
  1184. warnings.warn("Some warning", stacklevel=2)
  1185. return arr
  1186. np.apply_along_axis(warn, 0, [0])
  1187. with suppress_warnings() as sup:
  1188. sup.record()
  1189. with suppress_warnings("always"):
  1190. for i in range(2):
  1191. warnings.warn("Some warning")
  1192. assert_equal(len(sup.log), 2)
  1193. with suppress_warnings() as sup:
  1194. sup.record()
  1195. with suppress_warnings("location"):
  1196. for i in range(2):
  1197. warnings.warn("Some warning")
  1198. warnings.warn("Some warning")
  1199. assert_equal(len(sup.log), 2)
  1200. with suppress_warnings() as sup:
  1201. sup.record()
  1202. with suppress_warnings("module"):
  1203. for i in range(2):
  1204. warnings.warn("Some warning")
  1205. warnings.warn("Some warning")
  1206. warn_other_module()
  1207. assert_equal(len(sup.log), 2)
  1208. with suppress_warnings() as sup:
  1209. sup.record()
  1210. with suppress_warnings("once"):
  1211. for i in range(2):
  1212. warnings.warn("Some warning")
  1213. warnings.warn("Some other warning")
  1214. warn_other_module()
  1215. assert_equal(len(sup.log), 2)
  1216. def test_tempdir():
  1217. with tempdir() as tdir:
  1218. fpath = os.path.join(tdir, 'tmp')
  1219. with open(fpath, 'w'):
  1220. pass
  1221. assert_(not os.path.isdir(tdir))
  1222. raised = False
  1223. try:
  1224. with tempdir() as tdir:
  1225. raise ValueError()
  1226. except ValueError:
  1227. raised = True
  1228. assert_(raised)
  1229. assert_(not os.path.isdir(tdir))
  1230. def test_temppath():
  1231. with temppath() as fpath:
  1232. with open(fpath, 'w'):
  1233. pass
  1234. assert_(not os.path.isfile(fpath))
  1235. raised = False
  1236. try:
  1237. with temppath() as fpath:
  1238. raise ValueError()
  1239. except ValueError:
  1240. raised = True
  1241. assert_(raised)
  1242. assert_(not os.path.isfile(fpath))
  1243. class my_cacw(clear_and_catch_warnings):
  1244. class_modules = (sys.modules[__name__],)
  1245. def test_clear_and_catch_warnings_inherit():
  1246. # Test can subclass and add default modules
  1247. my_mod = _get_fresh_mod()
  1248. with my_cacw():
  1249. warnings.simplefilter('ignore')
  1250. warnings.warn('Some warning')
  1251. assert_equal(my_mod.__warningregistry__, {})
  1252. @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
  1253. class TestAssertNoGcCycles:
  1254. """ Test assert_no_gc_cycles """
  1255. def test_passes(self):
  1256. def no_cycle():
  1257. b = []
  1258. b.append([])
  1259. return b
  1260. with assert_no_gc_cycles():
  1261. no_cycle()
  1262. assert_no_gc_cycles(no_cycle)
  1263. def test_asserts(self):
  1264. def make_cycle():
  1265. a = []
  1266. a.append(a)
  1267. a.append(a)
  1268. return a
  1269. with assert_raises(AssertionError):
  1270. with assert_no_gc_cycles():
  1271. make_cycle()
  1272. with assert_raises(AssertionError):
  1273. assert_no_gc_cycles(make_cycle)
  1274. @pytest.mark.slow
  1275. def test_fails(self):
  1276. """
  1277. Test that in cases where the garbage cannot be collected, we raise an
  1278. error, instead of hanging forever trying to clear it.
  1279. """
  1280. class ReferenceCycleInDel:
  1281. """
  1282. An object that not only contains a reference cycle, but creates new
  1283. cycles whenever it's garbage-collected and its __del__ runs
  1284. """
  1285. make_cycle = True
  1286. def __init__(self):
  1287. self.cycle = self
  1288. def __del__(self):
  1289. # break the current cycle so that `self` can be freed
  1290. self.cycle = None
  1291. if ReferenceCycleInDel.make_cycle:
  1292. # but create a new one so that the garbage collector has more
  1293. # work to do.
  1294. ReferenceCycleInDel()
  1295. try:
  1296. w = weakref.ref(ReferenceCycleInDel())
  1297. try:
  1298. with assert_raises(RuntimeError):
  1299. # this will be unable to get a baseline empty garbage
  1300. assert_no_gc_cycles(lambda: None)
  1301. except AssertionError:
  1302. # the above test is only necessary if the GC actually tried to free
  1303. # our object anyway, which python 2.7 does not.
  1304. if w() is not None:
  1305. pytest.skip("GC does not call __del__ on cyclic objects")
  1306. raise
  1307. finally:
  1308. # make sure that we stop creating reference cycles
  1309. ReferenceCycleInDel.make_cycle = False