Skip to content

Commit

Permalink
List ordering doesn't matter.
Browse files Browse the repository at this point in the history
  • Loading branch information
cecille committed Aug 25, 2023
1 parent 2162669 commit dacf280
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 7 deletions.
18 changes: 16 additions & 2 deletions src/python_testing/TC_DeviceBasicComposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import base64
import copy
import functools
import json
import logging
import pathlib
Expand Down Expand Up @@ -248,7 +249,7 @@ def create_device_type_lists(roots: list[int], endpoint_dict: dict[int, Any]) ->
for root in roots:
tree_device_types = defaultdict(set)
eps = get_all_children(root, endpoint_dict)
eps.append(root)
eps.add(root)
for ep in eps:
for d in endpoint_dict[ep][Clusters.Descriptor][Clusters.Descriptor.Attributes.DeviceTypeList]:
tree_device_types[d.deviceType].add(ep)
Expand All @@ -257,6 +258,18 @@ def create_device_type_lists(roots: list[int], endpoint_dict: dict[int, Any]) ->
return device_types


def cmp_tag_list(a: Clusters.Descriptor.Structs.SemanticTagStruct, b: Clusters.Descriptor.Structs.SemanticTagStruct):
if a.mfgCode != b.mfgCode:
return -1 if a.mfgCode < b.mfgCode else 1
if a.namespaceID != b.namespaceID:
return -1 if a.namespaceID < b.namespaceID else 1
if a.tag != b.tag:
return -1 if a.tag < b.tag else 1
if a.label != b.label:
return -1 if a.label < b.label else 1
return 0


def find_tag_list_problems(roots: list[int], device_types: dict[int, dict[int, set[int]]], endpoint_dict: dict[int, Any]) -> dict[int, TagProblem]:
"""Checks for non-spec compliant tag lists"""
tag_problems = {}
Expand All @@ -278,7 +291,8 @@ def find_tag_list_problems(roots: list[int], device_types: dict[int, dict[int, s
continue
if Clusters.Descriptor.Attributes.TagList not in endpoint_dict[other][Clusters.Descriptor]:
continue
if endpoint_dict[endpoint][Clusters.Descriptor][Clusters.Descriptor.Attributes.TagList] == endpoint_dict[other][Clusters.Descriptor][Clusters.Descriptor.Attributes.TagList]:

if sorted(endpoint_dict[endpoint][Clusters.Descriptor][Clusters.Descriptor.Attributes.TagList], key=functools.cmp_to_key(cmp_tag_list)) == sorted(endpoint_dict[other][Clusters.Descriptor][Clusters.Descriptor.Attributes.TagList], key=functools.cmp_to_key(cmp_tag_list)):
duplicate_tags.add(other)
if len(duplicate_tags) != 0:
duplicate_tags.add(endpoint)
Expand Down
86 changes: 81 additions & 5 deletions src/python_testing/TestMatterTestingSupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def test_tag_list_problems(self):
asserts.assert_equal(set(problems.keys()), expected_problems, "Incorrect set of tag problems")
for root in roots:
eps = get_all_children(root, endpoints)
eps.append(root)
eps.add(root)
for ep in eps:
expected_problem = TagProblem(root=root, missing_attribute=True,
missing_feature=True, duplicates=set(eps), same_tag=set())
Expand All @@ -363,7 +363,7 @@ def test_tag_list_problems(self):
problems = find_tag_list_problems(roots, device_types, endpoints)
for root in roots:
eps = get_all_children(root, endpoints)
eps.append(root)
eps.add(root)
for ep in eps:
expected_problem = TagProblem(root=root, missing_attribute=True,
missing_feature=False, duplicates=set(eps), same_tag=set())
Expand All @@ -375,7 +375,7 @@ def test_tag_list_problems(self):
problems = find_tag_list_problems(roots, device_types, endpoints)
for root in roots:
eps = get_all_children(root, endpoints)
eps.append(root)
eps.add(root)
for ep in eps:
expected_problem = TagProblem(root=root, missing_attribute=True,
missing_feature=False, duplicates=set(eps), same_tag=set())
Expand All @@ -388,7 +388,7 @@ def test_tag_list_problems(self):
problems = find_tag_list_problems(roots, device_types, endpoints)
for root in roots:
eps = get_all_children(root, endpoints)
eps.append(root)
eps.add(root)
for ep in eps:
expected_problem = TagProblem(root=root, missing_attribute=False,
missing_feature=False, duplicates=set(eps), same_tag=set(eps))
Expand All @@ -407,7 +407,7 @@ def test_tag_list_problems(self):
problems = find_tag_list_problems(roots, device_types, endpoints)
for root in roots:
eps = get_all_children(root, endpoints)
eps.append(root)
eps.add(root)
for ep in eps:
expected_problem = TagProblem(root=root, missing_attribute=False,
missing_feature=True, duplicates=set(eps))
Expand Down Expand Up @@ -451,6 +451,82 @@ def test_tag_list_problems(self):
problems = find_tag_list_problems(roots, device_types, new_endpoints)
asserts.assert_equal(len(problems), 0, "Unexpected problems found in list")

# Create a simple tree where ONE of the tags in the set matches, but not the other - should be no problems
# 1 (dt 1) - 2 (dt 2) - tag 2,3
# - 3 (dt 2) - tag 2,4
desc_dt2_tag23 = {Clusters.Descriptor.Attributes.FeatureMap: 1,
Clusters.Descriptor.Attributes.PartsList: [],
Clusters.Descriptor.Attributes.DeviceTypeList: [Clusters.Descriptor.Structs.DeviceTypeStruct(2, 1)],
Clusters.Descriptor.Attributes.TagList: [Clusters.Descriptor.Structs.SemanticTagStruct(
tag=2), Clusters.Descriptor.Structs.SemanticTagStruct(tag=3)]
}
desc_dt2_tag24 = {Clusters.Descriptor.Attributes.FeatureMap: 1,
Clusters.Descriptor.Attributes.PartsList: [],
Clusters.Descriptor.Attributes.DeviceTypeList: [Clusters.Descriptor.Structs.DeviceTypeStruct(2, 1)],
Clusters.Descriptor.Attributes.TagList: [Clusters.Descriptor.Structs.SemanticTagStruct(
tag=2), Clusters.Descriptor.Structs.SemanticTagStruct(tag=4)]
}
simple = {}
simple[1] = {Clusters.Descriptor: desc_ep1}
simple[2] = {Clusters.Descriptor: desc_dt2_tag23}
simple[3] = {Clusters.Descriptor: desc_dt2_tag24}

_, tree = separate_endpoint_types(simple)
roots = find_tree_roots(tree, simple)
device_types = create_device_type_lists(roots, simple)

problems = find_tag_list_problems(roots, device_types, simple)
asserts.assert_equal(len(problems), 0, "Unexpected problems found in list")

# now both match, but the ordering is different - this SHOULD be a problem
desc_dt2_tag32 = {Clusters.Descriptor.Attributes.FeatureMap: 1,
Clusters.Descriptor.Attributes.PartsList: [],
Clusters.Descriptor.Attributes.DeviceTypeList: [Clusters.Descriptor.Structs.DeviceTypeStruct(2, 1)],
Clusters.Descriptor.Attributes.TagList: [Clusters.Descriptor.Structs.SemanticTagStruct(
tag=3), Clusters.Descriptor.Structs.SemanticTagStruct(tag=2)]
}
simple[3] = {Clusters.Descriptor: desc_dt2_tag32}

problems = find_tag_list_problems(roots, device_types, simple)
# expect this problem reported on both 2 and 3 endpoints
expected_problem = TagProblem(root=1, missing_attribute=False, missing_feature=False, duplicates={2, 3}, same_tag={2, 3})
asserts.assert_true(2 in problems.keys(), "Missing problem report for ep2")
asserts.assert_true(3 in problems.keys(), "Missing problem report for ep3")
asserts.assert_equal(problems[2], expected_problem, "Problem report for simple EP2 is not as expected")
asserts.assert_equal(problems[3], expected_problem, "Problem report for simple EP3 is not as expected")

# Let's check that we're correctly checking all the pieces of the tag
# Different mfgcode
simple[2][Clusters.Descriptor][Clusters.Descriptor.Attributes.TagList] = [
Clusters.Descriptor.Structs.SemanticTagStruct(mfgCode=1)]
simple[3][Clusters.Descriptor][Clusters.Descriptor.Attributes.TagList] = [Clusters.Descriptor.Structs.SemanticTagStruct()]
problems = find_tag_list_problems(roots, device_types, simple)
asserts.assert_equal(len(problems), 0, "Unexpected problems found in list")

simple[3][Clusters.Descriptor][Clusters.Descriptor.Attributes.TagList] = [
Clusters.Descriptor.Structs.SemanticTagStruct(mfgCode=2)]
problems = find_tag_list_problems(roots, device_types, simple)
asserts.assert_equal(len(problems), 0, "Unexpected problems found in list")

# Different namespace ids
simple[2][Clusters.Descriptor][Clusters.Descriptor.Attributes.TagList] = [
Clusters.Descriptor.Structs.SemanticTagStruct(namespaceID=1)]
simple[3][Clusters.Descriptor][Clusters.Descriptor.Attributes.TagList] = [Clusters.Descriptor.Structs.SemanticTagStruct()]
problems = find_tag_list_problems(roots, device_types, simple)
asserts.assert_equal(len(problems), 0, "Unexpected problems found in list")

# Different labels
simple[2][Clusters.Descriptor][Clusters.Descriptor.Attributes.TagList] = [
Clusters.Descriptor.Structs.SemanticTagStruct(label="test")]
simple[3][Clusters.Descriptor][Clusters.Descriptor.Attributes.TagList] = [Clusters.Descriptor.Structs.SemanticTagStruct()]
problems = find_tag_list_problems(roots, device_types, simple)
asserts.assert_equal(len(problems), 0, "Unexpected problems found in list")

simple[3][Clusters.Descriptor][Clusters.Descriptor.Attributes.TagList] = [
Clusters.Descriptor.Structs.SemanticTagStruct(label="test1")]
problems = find_tag_list_problems(roots, device_types, simple)
asserts.assert_equal(len(problems), 0, "Unexpected problems found in list")


if __name__ == "__main__":
default_matter_test_main()

0 comments on commit dacf280

Please sign in to comment.