def test_build_while_body_given_body_if(self): as_tree = ast.parse( ms("""\ while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # 4th block """)) while_block = RawBasicBlock(1, 1, 'While') cfg_handler = Cfg() cfg_handler.as_tree = as_tree real_tail_list = cfg_handler.build_while_body(while_block) expected_block_list = build_blocks([1, 1, 'While'], [2, 2, 'If'], [3, 3, None], [4, 4, None], block_links={ '0': [1], '1': [2, 3], '2': [3], '3': [0] }) self.assertBasicBlockEqual(while_block, expected_block_list[0]) self.assertBasicBlockEqual(real_tail_list[0], expected_block_list[0])
def __init__(self): # 配置 self.m_cfg = Cfg() # 基础配置 self.m_cfg.load_cfg('basic.json') self.basic_info = self.m_cfg.get_basic_cfg() # 下行连续数据区 self.m_store_seq_down = Sequence(self.basic_info.sequence_max) # 下行离散数据区 self.m_store_dis_down = Discrete(self.basic_info.discrete_max) # 上行连续数据区 self.m_store_seq_up = Sequence(self.basic_info.sequence_max) # 上行离散数据区 self.m_store_dis_up = Discrete(self.basic_info.discrete_max) # 上行连续发布泵(关联下行连续数据区) self.m_pump_seq_up = PubSeq(self.m_store_seq_down) # 上行离散发布泵(关联下行离散数据区) self.m_pump_dis_up = PubDis(self.m_store_dis_down) # 下行连续发布泵(关联上行连续数据区) self.m_pump_seq_down = PubSeq(self.m_store_seq_up) # 下行离散发布泵(关联上行离散数据区) self.m_pump_dis_down = PubDis(self.m_store_dis_up) # 驱动字典 self.m_driver_dict = {} # 主题字典 self.m_topic_dict = {} # 主题数据字典 self.m_topic_data_dict = {}
def test_build_dominator_tree_given_1_lvl(self): as_tree = ast.parse( ms("""\ a = 3 # 1st if a > 3: # | a = E # 2nd else: # 3rd z = F # | y = F # Eth """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() expected_block_list = build_blocks([6, 6, None], [3, 3, None], [5, 5, None], [1, 2, None], block_links={ '3': [1, 2, 0], '2': [], '1': [], '0': [] }) self.assertBasicBlockListEqual(dom_tree.dominator_nodes, expected_block_list)
def assertCfgWithBasicBlocks(self, cfg_real, *args, block_links): cfg_expected = Cfg() block_list = build_blocks(*args, block_links=block_links) cfg_expected.block_list.extend(block_list) self.assertCfgEqual(cfg_real, cfg_expected)
def main(): # program start print("ParkSmart is starting up...") loop_count = 0 cfg = Cfg() camera = Camera(cfg.Camera) classifier = Classifier(cfg.Classifier) viewset = ViewSet(cfg.ViewSet, classifier) print("Now beginning to process the current ViewSet.") while True: print("Views have been processed {} times.".format(str(loop_count))) for view in viewset.views: img_pos = view.get_img_pos() print(img_pos) camera.move(img_pos[0], img_pos[1]) # NOTE: might need to cal a sleep to give camera time to move view_img = camera.capture() view.update(view_img) # TODO: print("The current state:") curr_views_dict = viewset.get_set_state() df = pd.DataFrame(curr_views_dict) df = df[['view_id', 'id', 'state', 'l_update']] df.to_csv('output.csv', index=False) print(df) loop_count += 1
def test_initial_info_given_3_simple_stmt_given_if(self): as_tree = ast.parse( ms("""\ a = 3 if c < 3: y = a + b x, y = a, b """)) cfg_real = Cfg(as_tree) cfg_real.gather_initial_info() expected_ue_var = ({'c'}, {'a', 'b'}) expected_var_kill = ({'a'}, {'y', 'x'}) self.assertUeVarKill(cfg_real.block_list, expected_ue_var, expected_var_kill) expected_globals_var = {'b', 'a', 'c'} self.assertSetEqual(cfg_real.globals_var, expected_globals_var) expected_block_set = { 'x': [cfg_real.block_list[1]], 'a': [cfg_real.block_list[0]], 'y': [cfg_real.block_list[1]] } self.assertDictEqual(cfg_real.block_set, expected_block_set)
def test_build_dominator_tree_given_2_lvl(self): as_tree = ast.parse( ms("""\ z = 2 # 0th block while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # Eth block c = 3 # Fth block """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() expected_block_list = build_blocks([5, 5, None], [4, 4, None], [3, 3, None], [6, 6, None], [2, 2, None], [1, 1, None], block_links={ '5': [4], '4': [2, 3], '2': [1, 0], '1': [], '0': [], '3': [] }) self.assertBasicBlockListEqual(dom_tree.dominator_nodes, expected_block_list) pass
def test_initial_info_given_3_simple_stmt_expect_ue_a_vk_a_y_x(self): as_tree = ast.parse( ms("""\ a = 3 y = a + b x, y = a, b """)) cfg_real = Cfg(as_tree) cfg_real.gather_initial_info() expected_ue_var = {'b'} expected_var_kill = {'a', 'y', 'x'} self.assertSetEqual(cfg_real.block_list[0].ue_var, expected_ue_var) self.assertSetEqual(cfg_real.block_list[0].var_kill, expected_var_kill) expected_globals_var = {'b'} self.assertSetEqual(cfg_real.globals_var, expected_globals_var) expected_block_set = { 'x': [cfg_real.block_list[0]], 'a': [cfg_real.block_list[0]], 'y': [cfg_real.block_list[0]] } self.assertDictEqual(cfg_real.block_set, expected_block_set)
def test_fill_df_given_while(self): as_tree = ast.parse( ms("""\ z = 2 # 0th block while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # Eth block c = 3 # Fth block """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() dom_tree.fill_df() self.assertDfEqual( dom_tree, [], [2, 2], [2, 2], [5, 5], [2, 2], [], )
def test_insert_phi_function_pruned_given_2_df(self): """ Note: '|' with no arrows means pointing down GIVEN: block B has 2 DF nodes, C and D. Definition in B will force phi function in C and D A --- / \ | B | | / \ | | | C | \ | D --- """ blocks, ast_string = th.build_blocks_arb(block_links={ 'A': ['B', 'C', 'D'], 'B': ['C', 'D'], 'C': [], 'D': [] }, code={ 'A': ms("""\ temp = 1 phi_var = 1 """), 'B': ms("""\ phi_var= 2 """), 'C': ms("""\ x = phi_var """), 'D': ms("""\ y = phi_var """) }) as_tree = ast.parse(ast_string) cfg_real = Cfg() cfg_real.block_list = blocks cfg_real.as_tree = as_tree cfg_real.root = cfg_real.block_list[0] cfg_real.fill_df() cfg_real.gather_initial_info() cfg_real.compute_live_out() cfg_real.ins_phi_function_pruned() expected_phi_list = { 'A': set(), 'B': set(), 'C': {'phi_var'}, 'D': {'phi_var'} } self.assertPhiListEqual(cfg_real.block_list, expected_phi_list)
def test_dominator_tree_given_13_blocks(self): """ |---------> R | / | \ | A <-- B C | | / \ | \ | D <-- E <- | ---- G | | | | F / \ | L | | | | J | \ | | \ | | | ----> H --| I <----| | \ / ----------------------> K ---/ """ blocks = self.build_blocks_arb( block_links={ 'A': ['B', 'C', 'H'], 'B': ['D'], 'C': ['B', 'D', 'F'], 'D': ['E'], 'E': ['G'], 'F': ['G'], 'G': ['F', 'M'], 'H': ['I', 'J'], 'I': ['L'], 'J': ['K', 'L'], 'K': ['L'], 'L': ['M'], 'M': ['A', 'L'] }) cfg_real = Cfg() cfg_real.block_list = blocks cfg_real.root = blocks[0] dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() expected_blocks = self.build_blocks_arb( block_links={ 'A': ['B', 'C', 'D', 'F', 'G', 'H', 'L', 'M'], 'B': [], 'C': [], 'D': ['E'], 'E': [], 'F': [], 'G': [], 'H': ['I', 'J'], 'I': [], 'J': ['K'], 'K': [], 'L': [], 'M': [] }) self.assertBasicBlockListEqual(dom_tree.dominator_nodes, expected_blocks)
def test_cfg_given_no_branch(self): as_tree = ast.parse( ms("""\ a = 3 a = 4 """)) cfg_real = Cfg(as_tree) self.assertCfgWithBasicBlocks(cfg_real, [1, 2, None], block_links={})
def assertCfgWithAst(self, cfg_real, *args): cfg_expected = Cfg() # a = 3 # a = 4 for ast_list in args: basic_block = RawBasicBlock() basic_block.ast_list.extend(ast_list) cfg_expected.block_list.append(basic_block) self.assertCfgEqual(cfg_real, cfg_expected)
def test_get_ast_node_given_2_assign(self): as_tree = ast.parse(ms("""\ a = 3 a = 4 """) ) cfg_real = Cfg() node = cfg_real.get_ast_node(as_tree, 2) self.assertEqual(node, as_tree.body[1])
def test_compute_liveout_given_5_blocks(self): blocks, ast_string = th.build_blocks_arb(block_links={ 'A': ['B'], 'B': ['C', 'D'], 'C': ['D'], 'D': ['E', 'B'], 'E': [] }, code={ 'A': ms("""\ i = 1 """), 'B': ms("""\ if i < 0: pass """), 'C': ms("""\ s = 0 """), 'D': ms("""\ s = s + i i = i + 1 if i < 0: pass """), 'E': ms("""\ if s < 3: pass """) }) as_tree = ast.parse(ast_string) cfg_real = Cfg() cfg_real.block_list = blocks cfg_real.as_tree = as_tree cfg_real.root = cfg_real.block_list[0] cfg_real.gather_initial_info() cfg_real.compute_live_out() expected_live_out = { 'A': {'s', 'i'}, 'B': {'s', 'i'}, 'C': {'s', 'i'}, 'D': {'s', 'i'}, 'E': set() } self.assertLiveOutEqual(cfg_real.block_list, expected_live_out)
def test_get_ast_node_given_if(self): as_tree = ast.parse(ms("""\ a = 3 if a < 3: z = 2 a = 4 """) ) cfg_real = Cfg() node = cfg_real.get_ast_node(as_tree, 3) self.assertEqual(node, as_tree.body[1].body[0])
def test_delete_node(self): as_tree = ast.parse(ms("""\ z = 2 # 0th block while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # 4th block c = 3 # 5th block """)) cfg_real = Cfg(as_tree) cfg_real.root = cfg_real.delete_node(cfg_real.root, RawBasicBlock(1, 1)) pass
def test_dominator_tree_given_complex_block(self): """ A | B <------| / \ | C F | | / \ | | G I | | \ / | | H | \ / | D-----------| | E """ blocks = self.build_blocks_arb( block_links={ 'A': ['B'], 'B': ['C', 'F'], 'C': ['D'], 'D': ['E', 'B'], 'E': [], 'F': ['G', 'I'], 'G': ['H'], 'H': ['D'], 'I': ['H'] }) cfg_real = Cfg() cfg_real.block_list = blocks cfg_real.root = blocks[0] dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() expected_blocks = self.build_blocks_arb( block_links={ 'A': ['B'], 'B': ['C', 'D', 'F'], 'C': [], 'D': ['E'], 'E': [], 'F': ['G', 'H', 'I'], 'G': [], 'H': [], 'I': [] }) self.assertBasicBlockListEqual(dom_tree.dominator_nodes, expected_blocks)
def test_get_simple_block_given_no_indent(self): as_tree = ast.parse(ms("""\ a = 3 a = 4 """) ) cfg_holder = Cfg() simple_block_list = [] for simple_block in cfg_holder.get_basic_block(as_tree.body): simple_block_list.append(simple_block) expected_block = RawBasicBlock(start_line=1, end_line=2) self.assertBasicBlockEqual(simple_block_list[0], expected_block)
def test_cfg_given_while_body_if(self): as_tree = ast.parse(ms("""\ z = 2 # 0th block while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # 4th block c = 3 # 5th block """)) cfg_real = Cfg(as_tree) self.assertCfgWithBasicBlocks(cfg_real, [1, 1, None], [2, 2, 'While'], [3, 3, 'If'], [4, 4, None], [5, 5, None], [6, 6, None], block_links={'0': [1], '1': [2, 5], '2': [3, 4], '3': [4], '4': [1], '5': []})
def test_get_ast_node_given_if_else(self): as_tree = ast.parse( ms("""\ a = 3 if a < 3: z = 2 else: y = 2 a = 4 """)) cfg_real = Cfg() node = get_ast_node(as_tree, 5) self.assertEqual(node, as_tree.body[1].orelse[0])
def test_cfg_given_if_else_without_link_tail(self): as_tree = ast.parse(ms("""\ a = 3 # 0 if a > 3: # 1 a = 4 # 2 else: # 3 z = 5 # 4 """) ) cfg_real = Cfg(as_tree) self.assertCfgWithBasicBlocks(cfg_real, [1, 2, 'If'], [3, 3, None], [5, 5, None], block_links={'0': [1, 2], '1': [], '2': []} )
def test_walk_given_recursive_block(self): as_tree = ast.parse(ms("""\ z = 2 # 0th block while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # 4th block c = 3 # 5th block """)) cfg_real = Cfg(as_tree) blocks_list = [] for blocks in cfg_real.walk_block(cfg_real.block_list[0]): blocks_list.append(blocks) pass
def test_cfg_given_if_else_with_link_tail(self): as_tree = ast.parse(ms("""\ a = 3 # 1st if a > 3: # | a = 4 # 2nd else: # 3rd z = 5 # | y = 5 # 4th """) ) cfg_real = Cfg(as_tree) self.assertCfgWithBasicBlocks(cfg_real, [1, 2, 'If'], [3, 3, None], [5, 5, None], [6, 6, None], block_links={'0': [1, 2], '1': [3], '2': [3], '3': []} )
def test_fill_dominate_given_if_else(self): as_tree = ast.parse( ms("""\ a = 3 # 1st if a > 3: # | a = E # 2nd else: # 3rd z = F # | y = F # Eth """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() expected_dominator = {'0': [1, 2, 3], '1': [], '2': [], '3': []} self.assertDominatorEqual(dom_tree, expected_dominator)
def test_cfg_given_if_elif_no_else(self): as_tree = ast.parse(ms("""\ a = 3 #--- 1st if a > 3: #--- | a = 4 #--- 2nd elif a < 1: #--- 3rd a = 5 #--- 4th c = 5 #--- 5th """) ) cfg_real = Cfg(as_tree) self.assertCfgWithBasicBlocks(cfg_real, [1, 2, 'If'], [3, 3, None], [4, 4, 'If'], [5, 5, None], [6, 6, None], block_links={'0': [1, 2], '1': [4], '2': [3, 4], '3': [4], '4': []} )
def test_build_while_body_given_only_while(self): as_tree = ast.parse(ms("""\ while a < 3: # 1st block z = 4 # 2nd block """)) while_block = RawBasicBlock(1, 1, 'While') cfg_handler = Cfg() cfg_handler.as_tree = as_tree real_tail_list = cfg_handler.build_while_body(while_block) expected_block_list = build_blocks([1, 1, 'While'], [2, 2, None], block_links={'0': [1], '1': [0]}) self.assertBasicBlockEqual(while_block, expected_block_list[0]) self.assertBasicBlockEqual(real_tail_list[0], expected_block_list[0])
def test_fill_df_given_if_else(self): as_tree = ast.parse( ms("""\ a = 3 # 1st if a > 3: # | a = E # 2nd else: # 3rd z = F # | y = F # Eth """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() dom_tree.fill_df() self.assertDfEqual(dom_tree, [], [6, 6], [6, 6], [])
def test_get_simple_block_given_if(self): as_tree = ast.parse(ms("""\ a = 3 if a < 3: b = 4 c = 5 """) ) cfg_holder = Cfg() simple_block_list = [] for basic_block in cfg_holder.get_basic_block(as_tree.body): simple_block_list.append(basic_block) expected_block_0 = RawBasicBlock(start_line=1, end_line=2, block_end_type='If') expected_block_1 = RawBasicBlock(start_line=4, end_line=4) self.assertBasicBlockEqual(simple_block_list[0], expected_block_0, block_index=0) self.assertBasicBlockEqual(simple_block_list[1], expected_block_1, block_index=1)
def test_walk_given_2_block(self): as_tree = ast.parse(ms("""\ z = 2 if z < 2: y = 3 """)) cfg_real = Cfg(as_tree) # cfg_real.walk_block(cfg_real.block_list[0]) blocks_list = [] for blocks in cfg_real.walk_block(cfg_real.block_list[0]): blocks_list.append(blocks) expected_block_list = build_blocks([3, 3, None], [1, 2, 'If'], block_links={'1': [0], '0':[]}) self.assertBasicBlockListEqual(blocks_list, expected_block_list)