Skip to content

Commit

Permalink
Fix the matches method of TraceParameterMap
Browse files Browse the repository at this point in the history
Also added some tests for it
  • Loading branch information
TomHKeysight committed Apr 26, 2022
1 parent 466c926 commit 46d81e0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
22 changes: 22 additions & 0 deletions tests/test_parametermap.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,28 @@ def test_add_parameter(self):
with self.assertRaises(TypeError):
param_map.add_parameter('OUT', [])

def test_matches(self):
param_map = TraceParameterMap()
param_map.add_parameter('IN', TestTraceParameterMap.CAFEBABE)
param_map.add_parameter('TITLE', 'Hello, world!')
param_map.add_parameter('中文', '你好,世界')

param_defs = TestTraceParameterDefinitionMap.create_parameterdefinitionmap()
self.assertTrue(param_map.matches(param_defs))

wrong_param_defs = TraceParameterDefinitionMap()
wrong_param_defs['TITLE'] = TraceParameterDefinition(ParameterType.STRING, 13, 0)
wrong_param_defs['IN'] = TraceParameterDefinition(ParameterType.BYTE, 16, 13)
wrong_param_defs['中文'] = TraceParameterDefinition(ParameterType.STRING, 15, 29)
self.assertFalse(param_map.matches(wrong_param_defs))

param_map.add_parameter('HAS_KEY', False)
self.assertFalse(param_map.matches(param_defs))

del(param_map['HAS_KEY'])
param_defs['HAS_KEY'] = TraceParameterDefinition(ParameterType.BOOL, 1, 44)
self.assertFalse(param_map.matches(param_defs))

def test_add_standard_parameter(self):
param_map1 = TraceParameterMap()
param_map1.add_standard_parameter(StandardTraceParameters.INPUT,
Expand Down
15 changes: 12 additions & 3 deletions trsfile/parametermap.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,20 @@ def matches(self, definitions: TraceParameterDefinitionMap) -> bool:
:return: A boolean that is true if the trace parameter definitions match the metadata of the trace
parameter map"""
match = True
for key, definition in definitions.items():
if key not in self:
offset = 0
matched_keys = []
for key, value in self.items():
if key not in definitions:
match = False
else:
match = len(self) == definition.length and ParameterType.from_class(type(self)) == definition.param_type
definition = definitions[key]
matched_keys.append(key)
# Confirm the length, type and offset are correct
match = len(value) == definition.length
match &= ParameterType.from_class(type(value)) == definition.param_type
match &= definition.offset == offset
offset += len(value)
if not match:
break
match &= matched_keys == list(definitions.keys())
return match

0 comments on commit 46d81e0

Please sign in to comment.