From 3425fa3024fed964176b6bff3e7c7eca32c80176 Mon Sep 17 00:00:00 2001 From: JanLJL Date: Thu, 2 Apr 2020 08:57:26 +0200 Subject: [PATCH] added tests --- osaca/db_interface.py | 9 ++++----- tests/test_db_interface.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/osaca/db_interface.py b/osaca/db_interface.py index 7ac70c5..974e497 100755 --- a/osaca/db_interface.py +++ b/osaca/db_interface.py @@ -328,9 +328,8 @@ def _scrape_from_felixcloutier(mnemonic): return (suspicious, ' '.join(operands)) -def _get_src_dst_from_table(table): +def _get_src_dst_from_table(table, num_operands=2): """Prettify bs4 table object to string for user""" - NUM_OPERANDS = 2 # Parse table header = [''.join(x.string.lower().split()) for x in table.find('tr').findAll('td')] data = table.findAll('tr')[1:] @@ -342,10 +341,10 @@ def _get_src_dst_from_table(table): data_dict[i][header[j]] = col.string # Get only the instruction forms with 2 operands num_ops = [_get_number_of_operands(row) for _, row in data_dict.items()] - if NUM_OPERANDS in num_ops: - row = data_dict[num_ops.index(NUM_OPERANDS)] + if num_operands in num_ops: + row = data_dict[num_ops.index(num_operands)] reads_writes = [] - for i in range(1, NUM_OPERANDS + 1): + for i in range(1, num_operands + 1): m = re.search(r'(\([^\(\)]+\))', row['operand{}'.format(i)]) if not m: # no parentheses (probably immediate operand), assume READ diff --git a/tests/test_db_interface.py b/tests/test_db_interface.py index c5d138b..89fb225 100755 --- a/tests/test_db_interface.py +++ b/tests/test_db_interface.py @@ -124,6 +124,18 @@ class TestDBInterface(unittest.TestCase): with self.assertRaises(AssertionError): dbi.import_benchmark_output('csx', 'ibench', 'invalid_file') + def test_online_scraping(self): + # addpd -- suspicious instruction, normal URL + instr_1 = ['addpd', (True, '(r) (r,w)')] + self.assertEqual(dbi._scrape_from_felixcloutier(instr_1[0]), instr_1[1]) + # movpd -- not suspicious, + instr_2 = ['movapd', (False, '(r) (w)')] + self.assertEqual(dbi._scrape_from_felixcloutier(instr_2[0]), instr_2[1]) + # vfmadd132pd -- only in combined view with 213/231. + # No 2-operand version, therefore, empty string + instr_3 = ['vfmadd132pd', (True, '')] + self.assertEqual(dbi._scrape_from_felixcloutier(instr_3[0]), instr_3[1]) + ################## # Helper functions ##################