示例#1
0
def test_structs_with_dynamic_arrays_bounded_by_the_same_member_parsing_typedef_sizer(
):
    content = """\
typedef i32 num_of_elements_t;
typedef num_of_elements_t sz_t;
struct ExtSized
{
    sz_t sz;
    u32 one<@sz>;
    u16 two<@sz>;
    i32 three<@sz>;
};
"""

    assert parse(content) == [
        model.Typedef('num_of_elements_t', 'i32'),
        model.Typedef('sz_t', 'num_of_elements_t',
                      model.Typedef('num_of_elements_t', 'i32', None)),
        model.Struct('ExtSized', [
            model.StructMember(
                'sz', 'sz_t',
                model.Typedef('sz_t', 'num_of_elements_t',
                              model.Typedef('num_of_elements_t', 'i32',
                                            None))),
            model.StructMember('one', 'u32', bound='sz'),
            model.StructMember('two', 'u16', bound='sz'),
            model.StructMember('three', 'i32', bound='sz')
        ])
    ]
示例#2
0
def make_typedef(elem):
    if "type" in elem.attrib:
        return model.Typedef(elem.get("name"), elem.get("type"))
    elif "primitiveType" in elem.attrib and elem.get(
            "name") not in primitive_types.values():
        return model.Typedef(elem.get("name"),
                             primitive_types[elem.get("primitiveType")])
示例#3
0
def test_typedefs_primitive_type_parsing():
    xml = """\
<x>
    <typedef name="a" primitiveType="8 bit integer unsigned"/>
    <typedef name="b" primitiveType="16 bit integer unsigned"/>
    <typedef name="c" primitiveType="32 bit integer unsigned"/>
    <typedef name="d" primitiveType="64 bit integer unsigned"/>
    <typedef name="e" primitiveType="8 bit integer signed"/>
    <typedef name="f" primitiveType="16 bit integer signed"/>
    <typedef name="g" primitiveType="32 bit integer signed"/>
    <typedef name="h" primitiveType="64 bit integer signed"/>
    <typedef name="i" primitiveType="32 bit float"/>
    <typedef name="j" primitiveType="64 bit float"/>
</x>
"""
    assert parse(xml) == [
        model.Typedef("a", "u8"),
        model.Typedef("b", "u16"),
        model.Typedef("c", "u32"),
        model.Typedef("d", "u64"),
        model.Typedef("e", "i8"),
        model.Typedef("f", "i16"),
        model.Typedef("g", "i32"),
        model.Typedef("h", "i64"),
        model.Typedef("i", "r32"),
        model.Typedef("j", "r64")
    ]
示例#4
0
def test_definitions_typedefs():
    nodes = [
        model.Typedef("a", "b"),
        model.Typedef("c", "u8"),
    ]

    assert generate_definitions(nodes) == """\
示例#5
0
def test_model_sort_enums():
    nodes = [model.Typedef("B", "A"),
             model.Typedef("C", "A"),
             model.Enum("A", [])]

    model.topological_sort(nodes)

    assert ["A", "B", "C"] == [node.name for node in nodes]
示例#6
0
def test_model_sort_typedefs():
    nodes = [model.Typedef("A", "X"),
             model.Typedef("C", "B"),
             model.Typedef("B", "A"),
             model.Typedef("E", "D"),
             model.Typedef("D", "C")]

    model.topological_sort(nodes)

    assert ["A", "B", "C", "D", "E"] == [node.name for node in nodes]
示例#7
0
def test_typedefs_parsing():
    content = """\
typedef u32 x;
typedef x y;
"""

    assert parse(content) == [
        model.Typedef("x", "u32"),
        model.Typedef("y", "x")
    ]
示例#8
0
def test_swap_struct_with_greedy_array():
    nodes = process([
        model.Typedef('X', 'u32'),
        model.Typedef('Y', 'u32'),
        model.Struct("X", [(model.StructMember("x", "u8")),
                           (model.StructMember("y", "Y", unlimited=True))]),
        model.Struct("Z", [(model.StructMember("z", "X"))])
    ])

    assert generate_swap(nodes) == """\
示例#9
0
def make_typedef(xml_elem):
    if "type" in xml_elem.attrib:
        return model.Typedef(xml_elem.get("name"),
                             xml_elem.get("type"),
                             docstring=get_docstr(xml_elem))

    elif "primitiveType" in xml_elem.attrib and xml_elem.get(
            "name") not in primitive_types.values():
        return model.Typedef(xml_elem.get("name"),
                             primitive_types[xml_elem.get("primitiveType")],
                             docstring=get_docstr(xml_elem))
示例#10
0
def test_model_sort_typedefs():
    nodes = [
        model.Typedef("A", "X"),
        model.Typedef("C", "B"),
        model.Typedef("B", "A"),
        model.Typedef("E", "D"),
        model.Typedef("D", "C"),
    ]

    model.topological_sort(nodes)

    assert [node.name for node in nodes] == ["A", "B", "C", "D", "E"]
    assert [dep for node in nodes
            for dep in node.dependencies()] == ["X", "A", "B", "C", "D"]
示例#11
0
def test_definitions_newlines():
    nodes = [
        model.Typedef("a", "b"),
        model.Typedef("c", "d"),
        model.Enum("E1", [model.EnumMember("E1_A", "0")]),
        model.Enum("E2", [model.EnumMember("E2_A", "0")]),
        model.Constant("CONST_A", "0"),
        model.Typedef("e", "f"),
        model.Constant("CONST_B", "0"),
        model.Constant("CONST_C", "0"),
        model.Struct("A", [model.StructMember("a", "u32")]),
        model.Struct("B", [model.StructMember("b", "u32")])
    ]

    assert generate_definitions(nodes) == """\
示例#12
0
def test_floats_parsing():
    content = """\
typedef float x;
typedef double y;
struct z { float a; double b; };
"""

    assert parse(content) == [
        model.Typedef('x', 'r32'),
        model.Typedef('y', 'r64'),
        model.Struct('z', [
            model.StructMember('a', 'r32'),
            model.StructMember('b', 'r64')
        ])
    ]
示例#13
0
def test_evaluate_kinds_with_typedefs():
    nodes = [
        model.Struct("Empty", []),
        model.Struct("Dynamic", [
            model.StructMember("a_len", "u8"),
            model.StructMember("a", "u8", bound="a_len")
        ]),
        model.Struct("Fixed", [model.StructMember("a", "u8", size="10")]),
        model.Struct("Limited", [
            model.StructMember("a_len", "u8"),
            model.StructMember("a", "u8", bound="a_len", size="10")
        ]),
        model.Struct("Greedy",
                     [model.StructMember("a", "byte", unlimited=True)]),
        model.Struct("DynamicWrapper", [model.StructMember("a", "Dynamic")]),
        model.Struct("GreedyWrapper", [model.StructMember("a", "Greedy")]),
        model.Struct("GreedyDynamic",
                     [model.StructMember("a", "Dynamic", unlimited=True)]),
        model.Typedef("TU8", "u8"),
        model.Typedef("TDynamic", "Dynamic"),
        model.Typedef("TGreedy", "Greedy"),
        model.Struct("TypedefedU8", [model.StructMember("a", "TU8")]),
        model.Struct("TypedefedDynamic",
                     [model.StructMember("a", "TDynamic")]),
        model.Struct("TypedefedGreedy", [model.StructMember("a", "TGreedy")]),
        model.Typedef("TTDynamic", "TDynamic"),
        model.Typedef("TTTDynamic", "TTDynamic"),
        model.Struct("DeeplyTypedefed",
                     [model.StructMember("a", "TTTDynamic")]),
    ]

    model.cross_reference(nodes)
    model.evaluate_kinds(nodes)

    assert [x.kind for x in nodes if isinstance(x, model.Struct)] == [
        model.Kind.FIXED,
        model.Kind.DYNAMIC,
        model.Kind.FIXED,
        model.Kind.FIXED,
        model.Kind.UNLIMITED,
        model.Kind.DYNAMIC,
        model.Kind.UNLIMITED,
        model.Kind.UNLIMITED,
        model.Kind.FIXED,
        model.Kind.DYNAMIC,
        model.Kind.UNLIMITED,
        model.Kind.DYNAMIC,
    ]
示例#14
0
def test_includes_rendering():
    common_include = model.Include("foo", [
        model.Constant("symbol_1", 1),
        model.Constant("number_12", 12),
    ])
    nodes = [
        common_include,
        model.Include("root/ni_knights", [
            model.Include("../root/rabbit", [
                common_include,
                model.Constant("pi", "3.14159"),
                model.Typedef("definition", "things", "r32", "docstring"),
            ]),
            model.Constant("symbol_2", 2),
        ]),
        model.Include("../root/baz_bar", []),
        model.Include(
            "many/numbers",
            [model.Constant("number_%s" % n, n) for n in reversed(range(20))]),
    ]

    ref = """\
from foo import number_12, symbol_1
from ni_knights import definition, pi, symbol_2
from numbers import (
    number_0, number_1, number_10, number_11, number_13, number_14, number_15,
    number_16, number_17, number_18, number_19, number_2, number_3, number_4,
    number_5, number_6, number_7, number_8, number_9
)
"""
    # call twice to check if 'duplication avoidance' machinery in _PythonTranslator.translate_include works ok
    assert serialize(nodes) == ref
    assert serialize(nodes) == ref
示例#15
0
def test_change_field_type_not_a_struct():
    nodes = [model.Typedef("MyStruct", "MyRealStruct")]
    patches = {'MyStruct': [patch.Action('type', ['field2', 'TheRealType'])]}

    with pytest.raises(Exception) as e:
        patch.patch(nodes, patches)
    assert "Can change field only in struct: MyStruct Action(action='type', params=['field2', 'TheRealType'])" == e.value.message
示例#16
0
def test_typedefs_rendering():
    nodes = [model.Typedef("a", "b")]

    ref = """\
a = b
"""
    assert serialize(nodes) == ref
示例#17
0
def test_typedefs_parsing():
    xml = """\
<x>
    <typedef name="TILoveTypedefs_ALot" type="MyType"/>
</x>
"""
    assert parse(xml) == [model.Typedef("TILoveTypedefs_ALot", "MyType")]
示例#18
0
def test_cross_reference_typedef():
    nodes = [
        model.Struct("A", [model.StructMember("a", "u8")]),
        model.Typedef("B", "A"),
        model.Struct(
            "C", [model.StructMember("a", "A"),
                  model.StructMember("b", "B")]),
        model.Typedef("D", "B")
    ]

    model.cross_reference(nodes)

    assert nodes[1].definition.name == "A"
    assert nodes[2].members[1].definition.definition.name == "A"
    assert nodes[3].definition.name == "B"
    assert nodes[3].definition.definition.name == "A"
示例#19
0
def test_make_field_static_array_not_a_struct():
    nodes = [model.Typedef("MyStruct", "MyRealStruct")]
    patches = {'MyStruct': [patch.Action('static', ['field2', '3'])]}

    with pytest.raises(Exception) as e:
        patch.patch(nodes, patches)
    assert "Can change field only in struct: MyStruct" in e.value.message
示例#20
0
def test_make_field_limited_array_not_a_struct():
    nodes = [model.Typedef("MyStruct", "MyRealStruct")]
    patches = {'MyStruct': [patch.Action('limited', ['field3', 'field2'])]}

    with pytest.raises(Exception) as e:
        patch.patch(nodes, patches)
    assert "Can change field only in struct: MyStruct" in str(e.value)
示例#21
0
def test_remove_field_not_a_struct():
    nodes = [model.Typedef("MyStruct", "MyRealStruct")]
    patches = {'MyStruct': [patch.Action('remove', ['field2'])]}

    with pytest.raises(Exception) as e:
        patch.patch(nodes, patches)
    assert 'Can remove field only in struct: MyStruct' in str(e.value)
示例#22
0
def test_swap_struct_with_optional_element():
    nodes = process([
        model.Typedef('Y', 'u32'),
        model.Struct("X", [(model.StructMember("x", "u32", optional=True)),
                           (model.StructMember("y", "Y", optional=True))])
    ])

    assert generate_swap(nodes[-1:]) == """\
示例#23
0
def test_model_sort_union():
    nodes = [model.Typedef("C", "B"),
             model.Union("B", [model.UnionMember("a", "A", "0"),
                               model.UnionMember("b", "A", "1")]),
             model.Struct("A", [model.StructMember("a", "X")])]

    model.topological_sort(nodes)

    assert ["A", "B", "C"] == [node.name for node in nodes]
示例#24
0
def test_insert_field_not_a_struct():
    nodes = [model.Typedef("MyStruct", "MyRealStruct")]
    patches = {
        'MyStruct': [patch.Action('insert', ['1', 'additional1', 'u8'])]
    }

    with pytest.raises(Exception) as e:
        patch.patch(nodes, patches)
    assert 'Can insert field only in struct: MyStruct' in str(e.value)
示例#25
0
def test_swap_struct_with_limited_array_of_fixed_elements():
    nodes = process([
        model.Typedef('Y', 'u32'),
        model.Struct(
            "X", [(model.StructMember("num_of_x", "u16")),
                  (model.StructMember("x", "Y", bound="num_of_x", size=3))])
    ])

    assert generate_swap(nodes[-1:]) == """\
示例#26
0
def test_swap_union():
    nodes = process([
        model.Typedef('C', 'u32'),
        model.Union("X", [(model.UnionMember("a", "u8", 1)),
                          (model.UnionMember("b", "u64", 2)),
                          (model.UnionMember("c", "C", 3))])
    ])

    assert generate_swap(nodes[-1:]) == """\
示例#27
0
def test_definitions_struct():
    nodes = process([
        model.Typedef('TTypeX', 'u32'),
        model.Struct("Struct", [(model.StructMember("a", "u8")),
                                (model.StructMember("b", "i64")),
                                (model.StructMember("c", "r32")),
                                (model.StructMember("d", "TTypeX"))])
    ])

    assert generate_definitions(nodes[-1:]) == """\
示例#28
0
def test_evaluate_sizes_typedef():
    nodes = process([
        model.Typedef('T1', 'u32'),
        model.Struct('X', [
            model.StructMember('x', 'T1'),
        ]),
        model.Typedef('T2', 'T1'),
        model.Struct('Y', [
            model.StructMember('x', 'T2'),
        ]),
    ])
    assert list(map(get_size_alignment_padding, get_members_and_node(nodes[1]))) == [
        (4, 4, 0),
        (4, 4)
    ]
    assert list(map(get_size_alignment_padding, get_members_and_node(nodes[3]))) == [
        (4, 4, 0),
        (4, 4)
    ]
示例#29
0
def test_definitions_struct_with_dynamic_array():
    nodes = process([
        model.Typedef('TNumberOfItems', 'u32'),
        model.Struct("Struct", [
            model.StructMember("tmpName", "TNumberOfItems"),
            model.StructMember("a", "u8", bound="tmpName")
        ])
    ])

    assert generate_definitions(nodes[-1:]) == """\
示例#30
0
def test_structs_with_dynamic_array_parsing():
    content = """\
typedef u32 x_t;
struct test
{
    x_t x<>;
    bytes y<>;
};
"""

    assert parse(content) == [
        model.Typedef('x_t', 'u32'),
        model.Struct('test', [
            model.StructMember('num_of_x', 'u32'),
            model.StructMember(
                'x', 'x_t', model.Typedef('x_t', 'u32'), bound='num_of_x'),
            model.StructMember('num_of_y', 'u32'),
            model.StructMember('y', 'byte', bound='num_of_y')
        ])
    ]