|
1 | 1 | import numba
|
2 | 2 | import numpy as np
|
3 | 3 | import pytest
|
4 |
| -from numba import njit |
5 | 4 |
|
6 | 5 | from stumpy import fastmath
|
7 | 6 |
|
|
10 | 9 |
|
11 | 10 |
|
12 | 11 | def test_set():
|
13 |
| - # Test the _set and _reset function in fastmath.py |
14 | 12 | # The test is done by changing the value of fastmath flag for
|
15 | 13 | # the fastmath._add_assoc function, taken from the following link:
|
16 | 14 | # https://numba.pydata.org/numba-doc/dev/user/performance-tips.html#fastmath
|
17 |
| - py_func = fastmath._add_assoc.py_func |
18 | 15 |
|
19 |
| - x = 0.0 |
20 |
| - y = np.inf |
21 |
| - fastmath_flags = [False, {"reassoc", "nsz"}, {"reassoc"}, {"nsz"}] |
22 |
| - for flag in fastmath_flags: |
23 |
| - ref = njit(fastmath=flag)(py_func)(x, y) |
| 16 | + # case1: flag=False |
| 17 | + fastmath._set("fastmath", "_add_assoc", flag=False) |
| 18 | + out = fastmath._add_assoc(0, np.inf) |
| 19 | + assert np.isnan(out) |
24 | 20 |
|
25 |
| - fastmath._set("fastmath", "_add_assoc", flag) |
26 |
| - comp = fastmath._add_assoc(x, y) |
| 21 | + # case2: flag={'reassoc', 'nsz'} |
| 22 | + fastmath._set("fastmath", "_add_assoc", flag={"reassoc", "nsz"}) |
| 23 | + out = fastmath._add_assoc(0, np.inf) |
| 24 | + assert out == 0.0 |
27 | 25 |
|
28 |
| - if np.isnan(ref) and np.isnan(comp): |
29 |
| - assert True |
30 |
| - else: |
31 |
| - assert ref == comp |
| 26 | + # case3: flag={'reassoc'} |
| 27 | + fastmath._set("fastmath", "_add_assoc", flag={"reassoc"}) |
| 28 | + out = fastmath._add_assoc(0, np.inf) |
| 29 | + assert np.isnan(out) |
| 30 | + |
| 31 | + # case4: flag={'nsz'} |
| 32 | + fastmath._set("fastmath", "_add_assoc", flag={"nsz"}) |
| 33 | + out = fastmath._add_assoc(0, np.inf) |
| 34 | + assert np.isnan(out) |
32 | 35 |
|
33 | 36 |
|
34 | 37 | def test_reset():
|
35 |
| - # Test the _set and _reset function in fastmath.py |
36 | 38 | # The test is done by changing the value of fastmath flag for
|
37 | 39 | # the fastmath._add_assoc function, taken from the following link:
|
38 | 40 | # https://numba.pydata.org/numba-doc/dev/user/performance-tips.html#fastmath
|
| 41 | + # and then reset it to the default value, i.e. `True` |
39 | 42 | fastmath._set("fastmath", "_add_assoc", False)
|
40 | 43 | fastmath._reset("fastmath", "_add_assoc")
|
41 | 44 | assert fastmath._add_assoc(0.0, np.inf) == 0.0
|
0 commit comments