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