[python-hdf5storage] 88/152: Added custom assert functions for the tests into their own module.
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:37 UTC 2016
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to annotated tag 0.1
in repository python-hdf5storage.
commit bf3702e57b4c69b17360610a9cbfb5a9f3a311ee
Author: Freja Nordsiek <fnordsie at gmail.com>
Date: Sun Feb 2 22:55:26 2014 -0500
Added custom assert functions for the tests into their own module.
---
tests/asserts.py | 36 ++++++++++++++++++++++++++++++
tests/test_write_readback.py | 53 +++++++++-----------------------------------
2 files changed, 46 insertions(+), 43 deletions(-)
diff --git a/tests/asserts.py b/tests/asserts.py
new file mode 100644
index 0000000..f04122d
--- /dev/null
+++ b/tests/asserts.py
@@ -0,0 +1,36 @@
+import numpy as np
+import numpy.testing as npt
+
+def assert_equal(a, b):
+ # Compares a and b for equality. If they are not numpy types (aren't
+ # or don't inherit from np.generic or np.ndarray), then it is a
+ # matter of just comparing them. Otherwise, their dtypes and shapes
+ # have to be compared. Then, if they are not an object array,
+ # numpy.testing.assert_equal will compare them elementwise. For
+ # object arrays, each element must be iterated over to be compared.
+ assert type(a) == type(b)
+ if not isinstance(b, (np.generic, np.ndarray)):
+ assert a == b
+ else:
+ assert a.dtype == b.dtype
+ assert a.shape == b.shape
+ if b.dtype.name != 'object':
+ npt.assert_equal(a, b)
+ else:
+ for index, x in np.ndenumerate(a):
+ assert_equal(a[index], b[index])
+
+
+def assert_equal_python_collection(a, b, tp):
+ # Compares two python collections that are supposed to be the
+ # specified type tp. First, they have to be that type. If the type
+ # is a set type, then a simple comparison is all that is
+ # needed. Otherwise, an elementwise comparison needs to be done.
+ assert type(a) == tp
+ assert type(b) == tp
+ assert len(a) == len(b)
+ if type(b) in (set, frozenset):
+ assert a == b
+ else:
+ for index in range(0, len(a)):
+ assert_equal(a[index], b[index])
diff --git a/tests/test_write_readback.py b/tests/test_write_readback.py
index 82c6748..a0a4438 100644
--- a/tests/test_write_readback.py
+++ b/tests/test_write_readback.py
@@ -1,5 +1,3 @@
-#!/usr/bin/env python3
-
import sys
import os
import os.path
@@ -15,6 +13,8 @@ import numpy.random
import hdf5storage
+from asserts import assert_equal, assert_equal_python_collection
+
random.seed()
@@ -161,38 +161,10 @@ class TestPythonMatlabFormat(object):
return out
def assert_equal(self, a, b):
- # Compares a and b for equality. If they are not numpy types
- # (aren't or don't inherit from np.generic or np.ndarray), then
- # it is a matter of just comparing them. Otherwise, their dtypes
- # and shapes have to be compared. Then, if they are not an
- # object array, numpy.testing.assert_equal will compare them
- # elementwise. For object arrays, each element must be iterated
- # over to be compared.
- assert type(a) == type(b)
- if not isinstance(b, (np.generic, np.ndarray)):
- assert a == b
- else:
- assert a.dtype == b.dtype
- assert a.shape == b.shape
- if b.dtype.name != 'object':
- npt.assert_equal(a, b)
- else:
- for index, x in np.ndenumerate(a):
- self.assert_equal(a[index], b[index])
+ assert_equal(a, b)
def assert_equal_python_collection(self, a, b, tp):
- # Compares two python collections that are supposed to be the
- # specified type tp. First, they have to be that type. If the
- # type is a set type, then a simple comparison is all that is
- # needed. Otherwise, an elementwise comparison needs to be done.
- assert type(a) == tp
- assert type(b) == tp
- assert len(a) == len(b)
- if type(b) in (set, frozenset):
- assert a == b
- else:
- for index in range(0, len(a)):
- self.assert_equal(a[index], b[index])
+ assert_equal_python_collection(a, b, tp)
def check_numpy_scalar(self, dtype):
# Makes a random numpy scalar of the given type, writes it and
@@ -483,25 +455,20 @@ class TestMatlabFormat(TestNoneFormat):
assert a.shape == (1, 0)
elif isinstance(b, (bytes, str, bytearray)):
if len(b) == 0:
- TestPythonMatlabFormat.assert_equal(self, a, \
- np.zeros(shape=(1, 0), dtype='U'))
+ assert_equal(a, np.zeros(shape=(1, 0), dtype='U'))
elif isinstance(b, (bytes, bytearray)):
- TestPythonMatlabFormat.assert_equal(self, a, \
- np.atleast_2d(np.str_(b.decode())))
+ assert_equal(a, np.atleast_2d(np.str_(b.decode())))
else:
- TestPythonMatlabFormat.assert_equal(self, a, \
- np.atleast_2d(np.str_(b)))
+ assert_equal(a, np.atleast_2d(np.str_(b)))
else:
- TestPythonMatlabFormat.assert_equal(self, a, \
- np.atleast_2d(np.array(b)))
+ assert_equal(a, np.atleast_2d(np.array(b)))
else:
if b.dtype.name != 'object':
if b.dtype.char in ('U', 'S'):
if len(b) == 0 and (b.shape == tuple() \
or b.shape == (0, )):
- TestPythonMatlabFormat.assert_equal(self, a, \
- np.zeros(shape=(1, 0), \
- dtype='U'))
+ assert_equal(a, np.zeros(shape=(1, 0),
+ dtype='U'))
elif b.dtype.char == 'U':
c = np.atleast_1d(b)
c = np.atleast_2d(c.view(np.dtype('U' \
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-hdf5storage.git
More information about the debian-science-commits
mailing list