[python-dtcwt] 389/497: add testcase for switchable backends
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Tue Jul 21 18:06:31 UTC 2015
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to branch debian/sid
in repository python-dtcwt.
commit 1cee24c4da523cd75a79ff1ab6917c6e9d04a4d3
Author: Rich Wareham <rjw57 at cam.ac.uk>
Date: Mon Feb 10 14:50:39 2014 +0000
add testcase for switchable backends
Add test cases which define the expected behaviour for switchable
backends.
---
tests/testswitchbackends.py | 90 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 90 insertions(+)
diff --git a/tests/testswitchbackends.py b/tests/testswitchbackends.py
new file mode 100644
index 0000000..4e1841b
--- /dev/null
+++ b/tests/testswitchbackends.py
@@ -0,0 +1,90 @@
+import dtcwt
+import dtcwt.numpy as npbackend
+import dtcwt.opencl as clbackend
+
+from unittest import TestCase
+from nose.tools import raises, assert_raises
+from .util import skip_if_no_cl
+
+class TestSwitchBackends(TestCase):
+ def __init__(self, *args, **kwargs):
+ super(TestSwitchBackends, self).__init__(*args, **kwargs)
+ self._orig_stack = None
+
+ def setUp(self):
+ # Preserve _BACKEND_STACK in case test fails. This stops one test
+ # failure messing up other tests. This requires knowledge of how dtcw
+ # manages the backend stack which is not part of the public API.
+ #
+ # Perhaps we need to add __enter__() and __exit__() functions to a
+ # stack object.
+ self._orig_stack = list(dtcwt._BACKEND_STACK)
+
+ def tearDown(self):
+ dtcwt._BACKEND_STACK = self._orig_stack
+
+ def test_default_backend(self):
+ assert dtcwt.Transform2d is npbackend.Transform2d
+ assert dtcwt.Pyramid is npbackend.Pyramid
+ assert dtcwt.backend_name == 'numpy'
+
+ @skip_if_no_cl
+ def test_switch_to_opencl(self):
+ assert dtcwt.Transform2d is npbackend.Transform2d
+ assert dtcwt.Pyramid is npbackend.Pyramid
+ assert dtcwt.backend_name == 'numpy'
+ dtcwt.push_backend('opencl')
+ assert dtcwt.Transform2d is clbackend.Transform2d
+ assert dtcwt.Pyramid is clbackend.Pyramid
+ assert dtcwt.backend_name == 'opencl'
+ dtcwt.pop_backend()
+ assert dtcwt.Transform2d is npbackend.Transform2d
+ assert dtcwt.Pyramid is npbackend.Pyramid
+ assert dtcwt.backend_name == 'numpy'
+
+ def test_switch_to_numpy(self):
+ assert dtcwt.Transform2d is npbackend.Transform2d
+ assert dtcwt.Pyramid is npbackend.Pyramid
+ assert dtcwt.backend_name == 'numpy'
+ dtcwt.push_backend('numpy')
+ assert dtcwt.Transform2d is npbackend.Transform2d
+ assert dtcwt.Pyramid is npbackend.Pyramid
+ assert dtcwt.backend_name == 'numpy'
+ dtcwt.pop_backend()
+ assert dtcwt.Transform2d is npbackend.Transform2d
+ assert dtcwt.Pyramid is npbackend.Pyramid
+ assert dtcwt.backend_name == 'numpy'
+
+ @raises(ValueError)
+ def test_switch_to_invalid(self):
+ dtcwt.push_backend('does-not-exist')
+
+ @raises(IndexError)
+ def test_no_pop_default_backend(self):
+ dtcwt.pop_backend()
+
+def test_backend_with_guard():
+ """Test that manipulating the stack with preserve_backend_stack() will
+ return the stack to its pristine state even i pushes and pops are
+ imbalanced.
+
+ """
+ assert len(dtcwt._BACKEND_STACK) == 1
+ with dtcwt.preserve_backend_stack():
+ dtcwt.push_backend('numpy')
+ assert len(dtcwt._BACKEND_STACK) == 2
+ assert len(dtcwt._BACKEND_STACK) == 1
+
+def test_backend_with_guard_and_exception():
+ """Tests that even when preserving the backend stack, an exception is
+ correctly propagated.
+
+ """
+ assert len(dtcwt._BACKEND_STACK) == 1
+ def tst():
+ with dtcwt.preserve_backend_stack():
+ dtcwt.push_backend('numpy')
+ assert len(dtcwt._BACKEND_STACK) == 2
+ raise RuntimeError('test error')
+ assert_raises(RuntimeError, tst)
+ assert len(dtcwt._BACKEND_STACK) == 1
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-dtcwt.git
More information about the debian-science-commits
mailing list