Skip to content
Open
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
5 changes: 3 additions & 2 deletions nineml/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ def values(self):

@property
def key(self):
# TODO: Should put a hash on the end of this to make it unique
return str('_'.join(str(v) for v in self._values[:10]))
prefix = '_'.join(str(v) for v in self._values[:10])
values_str = str(list(self._values))
return '{}_{}'.format(prefix, hash(values_str))

def is_array(self):
return True
Expand Down
23 changes: 23 additions & 0 deletions test/unittests/exception_test/test_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@
nsmap={None: NINEML_NS})


class TestArrayValueKey(unittest.TestCase):

def test_key_unique_for_arrays_sharing_first_ten_values(self):
# Two arrays with identical first 10 values but different lengths
# must produce different keys.
shared = list(range(10))
a = ArrayValue(shared + [99.0])
b = ArrayValue(shared + [0.0])
self.assertNotEqual(a.key, b.key)

def test_key_stable_for_identical_arrays(self):
values = list(range(15))
a = ArrayValue(values)
b = ArrayValue(values)
self.assertEqual(a.key, b.key)

def test_key_unique_short_vs_long_array(self):
# A short array must not collide with a longer one sharing those values.
a = ArrayValue([1.0, 2.0, 3.0])
b = ArrayValue([1.0, 2.0, 3.0, 4.0])
self.assertNotEqual(a.key, b.key)


class TestArrayValueExceptions(unittest.TestCase):

def test___init___ninemlvalueerror(self):
Expand Down