Skip to content

Commit a1410fe

Browse files
authored
Fix tests for using pytest (#140)
1 parent 6317c44 commit a1410fe

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

numba_dppy/tests/test_controllable_fallback.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import numba
44
import numba_dppy
55
from numba_dppy.testing import unittest
6-
from numba.tests.support import captured_stderr
76
import dpctl
7+
import warnings
88

99

1010
@unittest.skipUnless(dpctl.has_gpu_queues(), "test only on GPU system")
@@ -24,7 +24,7 @@ def inner_call_fallback():
2424
return a
2525

2626
numba_dppy.compiler.DEBUG = 1
27-
with captured_stderr() as msg_fallback_true:
27+
with warnings.catch_warnings(record=True) as w:
2828
with dpctl.device_context("opencl:gpu") as gpu_queue:
2929
dppy = numba.njit(parallel=True)(inner_call_fallback)
3030
dppy_fallback_true = dppy()
@@ -33,9 +33,7 @@ def inner_call_fallback():
3333
numba_dppy.compiler.DEBUG = 0
3434

3535
np.testing.assert_array_equal(dppy_fallback_true, ref_result)
36-
self.assertTrue(
37-
"Failed to lower parfor on DPPY-device" in msg_fallback_true.getvalue()
38-
)
36+
self.assertIn("Failed to lower parfor on DPPY-device", str(w[-1].message))
3937

4038
@unittest.expectedFailure
4139
def test_dppy_fallback_false(self):
@@ -55,7 +53,7 @@ def inner_call_fallback():
5553
try:
5654
numba_dppy.compiler.DEBUG = 1
5755
numba_dppy.config.FALLBACK_ON_CPU = 0
58-
with captured_stderr() as msg_fallback_true:
56+
with warnings.catch_warnings(record=True) as w:
5957
with dpctl.device_context("opencl:gpu") as gpu_queue:
6058
dppy = numba.njit(parallel=True)(inner_call_fallback)
6159
dppy_fallback_false = dppy()
@@ -66,8 +64,8 @@ def inner_call_fallback():
6664
numba_dppy.compiler.DEBUG = 0
6765

6866
not np.testing.assert_array_equal(dppy_fallback_false, ref_result)
69-
not self.assertTrue(
70-
"Failed to lower parfor on DPPY-device" in msg_fallback_true.getvalue()
67+
self.assertNotIn(
68+
"Failed to lower parfor on DPPY-device", str(w[-1].message)
7169
)
7270

7371

numba_dppy/tests/test_dppy_fallback.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import numba
44
import unittest
5-
from numba.tests.support import captured_stderr
65
import dpctl
6+
import warnings
77

88

99
@unittest.skipUnless(dpctl.has_gpu_queues(), "test only on GPU system")
@@ -22,14 +22,16 @@ def inner_call_fallback():
2222

2323
return a
2424

25-
with captured_stderr() as msg, dpctl.device_context("opencl:gpu"):
25+
with warnings.catch_warnings(record=True) as w, dpctl.device_context(
26+
"opencl:gpu"
27+
):
2628
dppy = numba.njit(inner_call_fallback)
2729
dppy_result = dppy()
2830

2931
ref_result = inner_call_fallback()
3032

3133
np.testing.assert_array_equal(dppy_result, ref_result)
32-
self.assertTrue("Failed to lower parfor on DPPY-device" in msg.getvalue())
34+
self.assertIn("Failed to lower parfor on DPPY-device", str(w[-1].message))
3335

3436
def test_dppy_fallback_reductions(self):
3537
def reduction(a):
@@ -39,14 +41,16 @@ def reduction(a):
3941
return b
4042

4143
a = np.ones(10)
42-
with captured_stderr() as msg, dpctl.device_context("opencl:gpu"):
44+
with warnings.catch_warnings(record=True) as w, dpctl.device_context(
45+
"opencl:gpu"
46+
):
4347
dppy = numba.njit(reduction)
4448
dppy_result = dppy(a)
4549

4650
ref_result = reduction(a)
4751

4852
np.testing.assert_array_equal(dppy_result, ref_result)
49-
self.assertTrue("Failed to lower parfor on DPPY-device" in msg.getvalue())
53+
self.assertIn("Failed to lower parfor on DPPY-device", str(w[-1].message))
5054

5155

5256
if __name__ == "__main__":

numba_dppy/tests/test_math_functions.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def driver(a, jitfunc):
6161
return b
6262

6363

64-
def test_driver(input_arr, device_ty, jitfunc):
64+
def check_driver(input_arr, device_ty, jitfunc):
6565
out_actual = None
6666
if device_ty == "GPU":
6767
with dpctl.device_context("opencl:gpu") as gpu_queue:
@@ -79,65 +79,65 @@ def test_driver(input_arr, device_ty, jitfunc):
7979
@unittest.skipUnless(dpctl.has_cpu_queues(), "test only on CPU system")
8080
class TestDPPYMathFunctionsCPU(unittest.TestCase):
8181
def test_fabs_cpu(self):
82-
b_actual = test_driver(a, "CPU", dppy_fabs)
82+
b_actual = check_driver(a, "CPU", dppy_fabs)
8383
b_expected = np.fabs(a)
8484
self.assertTrue(np.all(b_actual == b_expected))
8585

8686
def test_sin_cpu(self):
87-
b_actual = test_driver(a, "CPU", dppy_sin)
87+
b_actual = check_driver(a, "CPU", dppy_sin)
8888
b_expected = np.sin(a)
8989
self.assertTrue(np.allclose(b_actual, b_expected))
9090

9191
def test_cos_cpu(self):
92-
b_actual = test_driver(a, "CPU", dppy_cos)
92+
b_actual = check_driver(a, "CPU", dppy_cos)
9393
b_expected = np.cos(a)
9494
self.assertTrue(np.allclose(b_actual, b_expected))
9595

9696
def test_exp_cpu(self):
97-
b_actual = test_driver(a, "CPU", dppy_exp)
97+
b_actual = check_driver(a, "CPU", dppy_exp)
9898
b_expected = np.exp(a)
9999
self.assertTrue(np.allclose(b_actual, b_expected))
100100

101101
def test_sqrt_cpu(self):
102-
b_actual = test_driver(a, "CPU", dppy_sqrt)
102+
b_actual = check_driver(a, "CPU", dppy_sqrt)
103103
b_expected = np.sqrt(a)
104104
self.assertTrue(np.allclose(b_actual, b_expected))
105105

106106
def test_log_cpu(self):
107-
b_actual = test_driver(a, "CPU", dppy_log)
107+
b_actual = check_driver(a, "CPU", dppy_log)
108108
b_expected = np.log(a)
109109
self.assertTrue(np.allclose(b_actual, b_expected))
110110

111111

112112
@unittest.skipUnless(dpctl.has_gpu_queues(), "test only on GPU system")
113113
class TestDPPYMathFunctionsGPU(unittest.TestCase):
114114
def test_fabs_gpu(self):
115-
b_actual = test_driver(a, "GPU", dppy_fabs)
115+
b_actual = check_driver(a, "GPU", dppy_fabs)
116116
b_expected = np.fabs(a)
117117
self.assertTrue(np.all(b_actual == b_expected))
118118

119119
def test_sin_gpu(self):
120-
b_actual = test_driver(a, "GPU", dppy_sin)
120+
b_actual = check_driver(a, "GPU", dppy_sin)
121121
b_expected = np.sin(a)
122122
self.assertTrue(np.allclose(b_actual, b_expected))
123123

124124
def test_cos_gpu(self):
125-
b_actual = test_driver(a, "GPU", dppy_cos)
125+
b_actual = check_driver(a, "GPU", dppy_cos)
126126
b_expected = np.cos(a)
127127
self.assertTrue(np.allclose(b_actual, b_expected))
128128

129129
def test_exp_gpu(self):
130-
b_actual = test_driver(a, "GPU", dppy_exp)
130+
b_actual = check_driver(a, "GPU", dppy_exp)
131131
b_expected = np.exp(a)
132132
self.assertTrue(np.allclose(b_actual, b_expected))
133133

134134
def test_sqrt_gpu(self):
135-
b_actual = test_driver(a, "GPU", dppy_sqrt)
135+
b_actual = check_driver(a, "GPU", dppy_sqrt)
136136
b_expected = np.sqrt(a)
137137
self.assertTrue(np.allclose(b_actual, b_expected))
138138

139139
def test_log_gpu(self):
140-
b_actual = test_driver(a, "GPU", dppy_log)
140+
b_actual = check_driver(a, "GPU", dppy_log)
141141
b_expected = np.log(a)
142142
self.assertTrue(np.allclose(b_actual, b_expected))
143143

0 commit comments

Comments
 (0)