def assert_array_collections_equal(correct, test, decimal=7):
"""Assert that two collections of numpy arrays have the same values.
Collections can be either a Sequence or a Mapping.
"""
if type(correct) != type(test):
raise ValueError('correct ({}) and test ({}) must have the same type.'.format(type(correct), type(test)))
assert_equal = lambda c, t: assert_array_almost_equal(c, t, decimal=decimal)
if isinstance(correct, Sequence):
assert len(correct) == len(test)
for c, t in izip(correct, test):
assert_equal(c, t)
elif isinstance(correct, Mapping):
# same keys
assert set(test.keys()) == set(correct.keys())
# same values
for key in test:
assert_equal(correct[key], test[key])
else:
raise TypeError('Inputs must be of type Mapping or Sequence, not {}.'.format(type(correct)))
评论列表
文章目录