signature_trie: Avoid unnecessary wrapping and unwrapping of values

Previously, Leaf.values() and Leaf.append_values() would wrap the
Leaf's value inside a list before appending it to the list of values.
So, the values list was actually a list of lists of values. The
get_matching_rows method would then use chain.from_iterable() to
flatten that list of list of values into a list of values.

This change removes the initial wrapping in a list and so removes the
need to flatten them into a single list. It also adds a test for the
values() method. Prior to this change the expected value would have
been [[1], ["A"], [{}]].

Bug: 202154151
Test: atest --host analyze_bcpf_test signature_trie_test verify_overlaps_test
Change-Id: Ida78500c9ab4466de127b2c36501b3606d0f3fe5
This commit is contained in:
Paul Duffin
2022-04-01 15:04:23 +01:00
parent 3aae38d451
commit dbbb8374a2
2 changed files with 26 additions and 10 deletions

View File

@@ -150,6 +150,27 @@ class TestSignatureToElements(unittest.TestCase):
str(context.exception))
class TestValues(unittest.TestCase):
def test_add_then_get(self):
trie = signature_trie()
trie.add("La/b/C;->l()", 1)
trie.add("La/b/C$D;->m()", "A")
trie.add("La/b/C$D;->n()", {})
package_a_node = next(iter(trie.child_nodes()))
self.assertEqual("package", package_a_node.type)
self.assertEqual("a", package_a_node.selector)
package_b_node = next(iter(package_a_node.child_nodes()))
self.assertEqual("package", package_b_node.type)
self.assertEqual("a/b", package_b_node.selector)
class_c_node = next(iter(package_b_node.child_nodes()))
self.assertEqual("class", class_c_node.type)
self.assertEqual("a/b/C", class_c_node.selector)
self.assertEqual([1, "A", {}], class_c_node.values(lambda _: True))
class TestGetMatchingRows(unittest.TestCase):
extractInput = """
Ljava/lang/Character$UnicodeScript;->of(I)Ljava/lang/Character$UnicodeScript;