Skip to content

Enable tests #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion conda-recipe/run_test.bat
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
call "%ONEAPI_ROOT%/compiler/latest/env/vars.bat"
REM IF %ERRORLEVEL% NEQ 0 exit 1
IF %ERRORLEVEL% NEQ 0 exit 1

@echo on

"%PYTHON%" -c "import dppl"
IF %ERRORLEVEL% NEQ 0 exit 1

"%PYTHON%" -c "import dppl.ocldrv"
IF %ERRORLEVEL% NEQ 0 exit 1

"%PYTHON%" -m unittest -v dppl.tests
IF %ERRORLEVEL% NEQ 0 exit 1
1 change: 1 addition & 0 deletions conda-recipe/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ source ${ONEAPI_ROOT}/compiler/latest/env/vars.sh || true

${PYTHON} -c "import dppl"
${PYTHON} -c "import dppl.ocldrv"
${PYTHON} -m unittest -v dppl.tests
1 change: 1 addition & 0 deletions dppl/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .test_dump_functions import *
57 changes: 3 additions & 54 deletions dppl/tests/test_dump_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,16 @@
# limitations under the License.
#******************************************************************************/

from contextlib import contextmanager
import ctypes
import dppl
import io
import os, sys
import tempfile
import unittest

import dppl
import dppl.ocldrv as drv


libc = ctypes.CDLL(None)
c_stdout = ctypes.c_void_p.in_dll(libc, 'stdout')

# Sourced from
# https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/
@contextmanager
def stdout_redirector(stream):
# The original fd stdout points to. Usually 1 on POSIX systems.
original_stdout_fd = sys.stdout.fileno()

def _redirect_stdout(to_fd):
"""Redirect stdout to the given file descriptor."""
# Flush the C-level buffer stdout
libc.fflush(c_stdout)
# Flush and close sys.stdout - also closes the file descriptor (fd)
sys.stdout.close()
# Make original_stdout_fd point to the same file as to_fd
os.dup2(to_fd, original_stdout_fd)
# Create a new sys.stdout that points to the redirected fd
sys.stdout = io.TextIOWrapper(os.fdopen(original_stdout_fd, 'wb'))

# Save a copy of the original stdout fd in saved_stdout_fd
saved_stdout_fd = os.dup(original_stdout_fd)
try:
# Create a temporary file and redirect stdout to it
tfile = tempfile.TemporaryFile(mode='w+b')
_redirect_stdout(tfile.fileno())
# Yield to caller, then redirect stdout back to the saved fd
yield
_redirect_stdout(saved_stdout_fd)
# Copy contents of temporary file to the given stream
tfile.flush()
tfile.seek(0, io.SEEK_SET)
if stream:
stream.write(tfile.read())
finally:
tfile.close()
os.close(saved_stdout_fd)


class TestDumpMethods(unittest.TestCase):

def test_dppl_dump_runtime(self):
with stdout_redirector(None):
self.assertEqual(dppl.runtime.dump(), 0)
self.assertEqual(dppl.runtime.dump(), 0)

def test_dppl_ocldrv_dump_runtime(self):
with stdout_redirector(None):
self.assertEqual(drv.runtime.dump(), 0)


suite = unittest.TestLoader().loadTestsFromTestCase(TestDumpMethods)
unittest.TextTestRunner(verbosity=2).run(suite)
self.assertEqual(drv.runtime.dump(), 0)