|
| 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