Skip to content

Commit 7f0887f

Browse files
committed
factor patching tests into separate file
1 parent 5cd2258 commit 7f0887f

6 files changed

Lines changed: 103 additions & 14 deletions

File tree

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ extend-ignore =
1212
D102,
1313
# missing docstring in public function:
1414
D103,
15+
# missing docstring in public package:
16+
D104,
1517
# missing docstring in __init__:
1618
D107,
1719
# 1 blank line required between summary line and description:

.github/workflows/build-with-clang.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ jobs:
7777
# mkl_umath cannot be installed in editable mode, we need
7878
# to change directory before importing it and running tests
7979
cd ..
80-
python -m pytest -sv --pyargs mkl_umath/mkl_umath/tests
80+
python -m pytest -sv --pyargs mkl_umath

.github/workflows/build_pip.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ jobs:
5858
# mkl_umath cannot be installed in editable mode, we need
5959
# to change directory before importing it and running tests
6060
cd ..
61-
python -m pytest -v mkl_umath/mkl_umath/tests
61+
python -m pytest -v --pyargs mkl_umath

mkl_umath/tests/__init__.py

Whitespace-only changes.

mkl_umath/tests/test_basic.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import numpy as np
2727
import pytest
2828

29-
import mkl_umath
3029
import mkl_umath._ufuncs as mu # pylint: disable=no-name-in-module
3130

3231
np.random.seed(42)
@@ -190,14 +189,3 @@ def test_reduce_complex(func, dtype):
190189
assert np.allclose(
191190
mkl_res, np_res
192191
), f"Results for '{func}[reduce]' do not match"
193-
194-
195-
def test_patch():
196-
mkl_umath.restore_numpy_umath()
197-
assert not mkl_umath.is_patched()
198-
199-
mkl_umath.patch_numpy_umath() # Enable mkl_umath in Numpy
200-
assert mkl_umath.is_patched()
201-
202-
mkl_umath.restore_numpy_umath() # Disable mkl_umath in Numpy
203-
assert not mkl_umath.is_patched()

mkl_umath/tests/test_patching.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Copyright (c) 2019, Intel Corporation
2+
#
3+
# Redistribution and use in source and binary forms, with or without
4+
# modification, are permitted provided that the following conditions are met:
5+
#
6+
# * Redistributions of source code must retain the above copyright notice,
7+
# this list of conditions and the following disclaimer.
8+
# * Redistributions in binary form must reproduce the above copyright
9+
# notice, this list of conditions and the following disclaimer in the
10+
# documentation and/or other materials provided with the distribution.
11+
# * Neither the name of Intel Corporation nor the names of its contributors
12+
# may be used to endorse or promote products derived from this software
13+
# without specific prior written permission.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
19+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
26+
import contextlib
27+
import sys
28+
from dataclasses import dataclass
29+
from io import StringIO
30+
31+
import mkl_umath
32+
33+
34+
@dataclass
35+
class CapturedOutput:
36+
stdout: str
37+
38+
39+
@contextlib.contextmanager
40+
def capture_output():
41+
old_stdout = sys.stdout
42+
capturer = StringIO()
43+
sys.stdout = capturer
44+
output = CapturedOutput(stdout="")
45+
yield output
46+
sys.stdout = old_stdout
47+
output.stdout = capturer.getvalue()
48+
49+
50+
def test_patch_basic():
51+
mkl_umath.restore_numpy_umath()
52+
assert not mkl_umath.is_patched()
53+
54+
mkl_umath.patch_numpy_umath() # Enable mkl_umath in Numpy
55+
assert mkl_umath.is_patched()
56+
57+
mkl_umath.restore_numpy_umath() # Disable mkl_umath in Numpy
58+
assert not mkl_umath.is_patched()
59+
60+
61+
def test_patch_redundant_patching():
62+
assert not mkl_umath.is_patched()
63+
64+
mkl_umath.patch_numpy_umath()
65+
mkl_umath.patch_numpy_umath()
66+
67+
assert mkl_umath.is_patched()
68+
69+
mkl_umath.restore_numpy_umath()
70+
assert mkl_umath.is_patched()
71+
72+
mkl_umath.restore_numpy_umath()
73+
assert not mkl_umath.is_patched()
74+
75+
76+
def test_patch_reentrant():
77+
assert not mkl_umath.is_patched()
78+
79+
with mkl_umath.mkl_umath():
80+
assert mkl_umath.is_patched()
81+
82+
with mkl_umath.mkl_umath():
83+
assert mkl_umath.is_patched()
84+
85+
assert mkl_umath.is_patched()
86+
87+
assert not mkl_umath.is_patched()
88+
89+
90+
def test_patch_verbose():
91+
assert not mkl_umath.is_patched()
92+
93+
with capture_output() as output:
94+
mkl_umath.patch_numpy_umath(verbose=True)
95+
assert output.stdout
96+
assert mkl_umath.is_patched()
97+
98+
mkl_umath.restore_numpy_umath()
99+
assert not mkl_umath.is_patched()

0 commit comments

Comments
 (0)