def test_as_root_as_single_column(): q = atlas_xaod_query_ast_visitor() node = ast.parse('1/1') value_obj = crep.cpp_value('i', gc_scope_top_level(), ctyp.terminal('int')) sequence = crep.cpp_sequence( value_obj, crep.cpp_value('i', gc_scope_top_level(), ctyp.terminal('int')), gc_scope_top_level()) node.rep = sequence # type: ignore as_root = q.get_as_ROOT(node) assert isinstance(as_root, rh.cpp_ttree_rep)
def test_as_root_rep_already_set(): q = atlas_xaod_query_ast_visitor() node = ast.parse('1/1') v = rh.cpp_ttree_rep('junk', 'dude', gc_scope_top_level()) node.rep = v # type: ignore assert v is q.get_as_ROOT(node)
def test_as_root_as_dict(): q = atlas_xaod_query_ast_visitor() node = ast.parse('1/1') dict_obj = crep.cpp_dict( { ast.Constant(value='hi'): crep.cpp_value('i', gc_scope_top_level(), ctyp.terminal('int')) }, gc_scope_top_level()) sequence = crep.cpp_sequence( dict_obj, # type: ignore crep.cpp_value('i', gc_scope_top_level(), ctyp.terminal('int')), gc_scope_top_level()) node.rep = sequence # type: ignore as_root = q.get_as_ROOT(node) assert isinstance(as_root, rh.cpp_ttree_rep)
def test_everything_starts_with_top_level_scope(): top = gc_scope_top_level() g = generated_code() s1 = statement.iftest("true") g.add_statement(s1) scope_1 = g.current_scope() assert scope_1.starts_with(top)
def test_nothing_else_starts_with_top_level_scope(): top = gc_scope_top_level() g = generated_code() s1 = statement.iftest("true") g.add_statement(s1) scope_1 = g.current_scope() assert not top.starts_with(scope_1)
def test_sequence_type(): tc = gc_scope_top_level() s_value = crep.cpp_value('0.0', tc, ctyp.terminal('int', False)) i_value = crep.cpp_value('1.0', tc, ctyp.terminal('object', False)) seq = crep.cpp_sequence(s_value, i_value, tc) assert seq.sequence_value().cpp_type().type == 'int'
def test_variable_type_update(): tc = gc_scope_top_level() expr = "a" ctype = ctyp.terminal('int', False) v = crep.cpp_variable(expr, tc, ctype) v.update_type(ctyp.terminal('float', False)) assert v.cpp_type().type == 'float'
def test_compare_string_var(): q = atlas_xaod_query_ast_visitor() node = ast.parse('e == "hi"').body[0].value # type: ignore node.left.rep = crep.cpp_value('e', gc_scope_top_level(), ctyp.terminal('string')) # type: ignore r = q.get_rep(node) assert isinstance(r, crep.cpp_value) assert r.cpp_type().type == 'bool' assert r.as_cpp() == '(e=="hi")'
def test_deref_simple_ptr(): tc = gc_scope_top_level() expr = "a" c_type = ctyp.terminal('int', 1) v = crep.cpp_variable(expr, tc, c_type) d = crep.dereference_var(v) assert d.cpp_type().type == 'int' assert d.cpp_type().p_depth == 0 assert d.as_cpp() == '*a'
def test_variable_type__with_initial_update(): tc = gc_scope_top_level() expr = "a" c_type = ctyp.terminal('int', False) c_init = crep.cpp_value('0.0', tc, ctyp.terminal('int', False)) v = crep.cpp_variable(expr, tc, c_type, c_init) v.update_type(ctyp.terminal('float', False)) assert v.cpp_type().type == 'float' iv = v.initial_value() assert iv is not None assert iv.cpp_type().type == 'float'
def test_deref_collection_ptr(): tc = gc_scope_top_level() c_type = ctyp.collection(ctyp.terminal(ctyp.parse_type('int')), ctyp.parse_type('vector<int>*')) c = crep.cpp_collection('my_var', tc, c_type) d = crep.dereference_var(c) assert isinstance(d, crep.cpp_collection) cpp_type = d.cpp_type() assert isinstance(cpp_type, ctyp.collection) assert str(cpp_type) == 'vector<int>' assert str(cpp_type.element_type) == 'int'
def test_subscript(): q = atlas_xaod_query_ast_visitor() our_a = ast.Name(id='a') our_a.rep = crep.cpp_collection('jets', gc_scope_top_level(), ctyp.collection( ctyp.terminal('int'))) # type: ignore node = ast_parse_with_replacement('a[10]', { 'a': our_a }).body[0].value # type: ignore as_root = q.get_rep(node) assert isinstance(as_root, crep.cpp_value) assert str(as_root.cpp_type()) == 'int' assert as_root.as_cpp() == 'jets.at(10)'
def test_top_level_starts_with_top_level(): top1 = gc_scope_top_level() top2 = gc_scope_top_level() assert top1.starts_with(top2)
def test_member_access_obj_depth_1(): cv = crep.cpp_value('f', gc_scope_top_level(), ctyp.terminal(ctyp.parse_type('obj'))) assert crep.base_type_member_access(cv, 2) == '(*f)->'
def test_member_access_obj_ptr(): cv = crep.cpp_value('f', gc_scope_top_level(), ctyp.terminal(ctyp.parse_type('obj*'))) assert crep.base_type_member_access(cv) == 'f->'