def assert_set_equality(test_case, expected, actual):
"""Asserts that two lists are equal without order.
Given two lists, treat them as sets and test equality. This function only
requires an __eq__ method to be defined on the objects, and not __hash__
which set comparison requires. This function removes the burden of defining
a __hash__ method just for testing.
This function calls into tf.test.TestCase.assert* methods and behaves
like a test assert. The function returns if `expected` and `actual`
contain the same objects regardless of ordering.
Note, this is an O(n^2) operation and is not suitable for large lists.
Args:
test_case: A tf.test.TestCase instance from a test.
expected: A list of objects.
actual: A list of objects.
"""
actual_found = np.zeros(len(actual), dtype=bool)
for expected_obj in expected:
found = False
for i, actual_obj in enumerate(actual):
if expected_obj == actual_obj:
actual_found[i] = True
found = True
break
if not found:
test_case.fail('Expected %s not found in actual collection' %
expected_obj)
if not np.all(actual_found):
test_case.fail('Actual objects %s not found in expected collection' %
np.array(actual)[np.invert(actual_found)])
评论列表
文章目录