Esempio n. 1
0
def test_while_str() -> None:
    assert_equal(
        str(ada.While(
            ada.Variable("X"),
            [ada.NullStatement()],
        )),
        multilinestr("""while X loop
                  null;
               end loop;"""),
    )
    assert_equal(
        str(
            ada.While(
                ada.And(
                    ada.Variable("X"),
                    ada.Variable("Y"),
                ),
                [ada.NullStatement()],
            )),
        multilinestr("""while
                  X
                  and Y
               loop
                  null;
               end loop;"""),
    )
Esempio n. 2
0
def test_slice_simplified() -> None:
    assert_equal(
        Slice(
            Variable("Buffer"), First("Buffer"), Add(Last("Buffer"), Add(Number(21), Number(21))),
        ).simplified(),
        Slice(Variable("Buffer"), First("Buffer"), Add(Last("Buffer"), Number(42))),
    )
Esempio n. 3
0
def test_range_size() -> None:
    assert_equal(
        RangeInteger("P.T", Number(0),
                     Sub(Pow(Number(2), Number(32)), Number(1)),
                     Number(32)).size,
        Number(32),
    )
Esempio n. 4
0
def test_term_simplified() -> None:
    assert_equal(
        Add(
            Mul(Number(1), Number(6)), Sub(Variable("X"), Number(10)), Add(Number(1), Number(3))
        ).simplified(),
        Variable("X"),
    )
Esempio n. 5
0
def test_attribute_expression_substituted() -> None:
    assert_equal(
        Val("X", Variable("Y")).substituted(
            lambda x: Number(42) if x == Val("X", Variable("Y")) else x
        ),
        Number(42),
    )
    assert_equal(
        -Val("X", Variable("Y")).substituted(
            lambda x: Number(42) if x == Val("X", Variable("Y")) else x
        ),
        Number(-42),
    )
    assert_equal(
        Val("X", Variable("Y")).substituted(lambda x: Call("Y") if x == Variable("Y") else x),
        Val("X", Call("Y")),
    )
    assert_equal(
        -Val("X", Variable("Y")).substituted(lambda x: Call("Y") if x == Variable("Y") else x),
        -Val("X", Call("Y")),
    )
    assert_equal(
        -Val("X", Variable("Y")).substituted(
            lambda x: Variable(f"P_{x}")
            if isinstance(x, Variable)
            else (Pos(x.prefix, x.expression) if isinstance(x, Val) else x)
        ),
        -Pos("P_X", Variable("P_Y")),
    )
Esempio n. 6
0
def test_message_successors() -> None:
    assert_equal(
        ETHERNET_FRAME.successors(INITIAL),
        (
            Field("Destination"),
            Field("Source"),
            Field("Type_Length_TPID"),
            Field("TPID"),
            Field("TCI"),
            Field("Type_Length"),
            Field("Payload"),
        ),
    )
    assert_equal(
        ETHERNET_FRAME.successors(Field("Source")),
        (
            Field("Type_Length_TPID"),
            Field("TPID"),
            Field("TCI"),
            Field("Type_Length"),
            Field("Payload"),
        ),
    )
    assert_equal(
        ETHERNET_FRAME.successors(Field("TPID")),
        (Field("TCI"), Field("Type_Length"), Field("Payload")),
    )
    assert_equal(ETHERNET_FRAME.successors(Field("Payload")), ())
    assert_equal(ETHERNET_FRAME.successors(FINAL), ())
Esempio n. 7
0
def test_ass_expr_findall() -> None:
    assert_equal(
        And(Equal(Variable("X"), Number(1)), Variable("Y"), Number(2)).findall(
            lambda x: isinstance(x, Number)
        ),
        [Number(1), Number(2)],
    )
Esempio n. 8
0
def test_quantified_expression_variables() -> None:
    assert_equal(
        ForAllOf(
            "A", Variable("List"), Add(Variable("X"), Add(Variable("Y"), Variable("Z")))
        ).variables(),
        [Variable("List"), Variable("X"), Variable("Y"), Variable("Z")],
    )
Esempio n. 9
0
def test_message_copy() -> None:
    message = Message(
        "P.M",
        [Link(INITIAL, Field("F")),
         Link(Field("F"), FINAL)],
        {Field("F"): MODULAR_INTEGER},
    )
    assert_equal(
        message.copy(identifier="A.B"),
        Message(
            "A.B",
            [Link(INITIAL, Field("F")),
             Link(Field("F"), FINAL)],
            {Field("F"): MODULAR_INTEGER},
        ),
    )
    assert_equal(
        message.copy(
            structure=[Link(INITIAL, Field("C")),
                       Link(Field("C"), FINAL)],
            types={Field("C"): RANGE_INTEGER},
        ),
        Message(
            "P.M",
            [Link(INITIAL, Field("C")),
             Link(Field("C"), FINAL)],
            {Field("C"): RANGE_INTEGER},
        ),
    )
Esempio n. 10
0
def test_merge_message_simple_derived() -> None:
    assert_equal(
        deepcopy(M_SMPL_REF_DERI).merged(),
        UnprovenDerivedMessage(
            "P.Smpl_Ref_Deri",
            M_SMPL_REF,
            [
                Link(INITIAL, Field("NR_F1"), length=Number(16)),
                Link(Field("NR_F3"), FINAL,
                     Equal(Variable("NR_F3"), Variable("P.ONE"))),
                Link(Field("NR_F4"), FINAL),
                Link(Field("NR_F1"), Field("NR_F2")),
                Link(
                    Field("NR_F2"),
                    Field("NR_F3"),
                    LessEqual(Variable("NR_F2"), Number(100)),
                    first=First("NR_F2"),
                ),
                Link(
                    Field("NR_F2"),
                    Field("NR_F4"),
                    GreaterEqual(Variable("NR_F2"), Number(200)),
                    first=First("NR_F2"),
                ),
            ],
            {
                Field("NR_F1"): Opaque(),
                Field("NR_F2"): deepcopy(MODULAR_INTEGER),
                Field("NR_F3"): deepcopy(ENUMERATION),
                Field("NR_F4"): deepcopy(RANGE_INTEGER),
            },
        ),
    )
Esempio n. 11
0
def test_prefixed_message() -> None:
    assert_equal(
        UnprovenMessage(
            "P.M",
            [
                Link(INITIAL, Field("F1")),
                Link(
                    Field("F1"),
                    Field("F2"),
                    LessEqual(Variable("F1"), Number(100)),
                    first=First("F1"),
                ),
                Link(
                    Field("F1"),
                    Field("F3"),
                    GreaterEqual(Variable("F1"), Number(200)),
                    first=First("F1"),
                ),
                Link(Field("F2"), FINAL),
                Link(Field("F3"), Field("F4"), length=Variable("F3")),
                Link(Field("F4"), FINAL),
            ],
            {
                Field("F1"): deepcopy(MODULAR_INTEGER),
                Field("F2"): deepcopy(MODULAR_INTEGER),
                Field("F3"): deepcopy(RANGE_INTEGER),
                Field("F4"): Opaque(),
            },
        ).prefixed("X_"),
        UnprovenMessage(
            "P.M",
            [
                Link(INITIAL, Field("X_F1")),
                Link(
                    Field("X_F1"),
                    Field("X_F2"),
                    LessEqual(Variable("X_F1"), Number(100)),
                    first=First("X_F1"),
                ),
                Link(
                    Field("X_F1"),
                    Field("X_F3"),
                    GreaterEqual(Variable("X_F1"), Number(200)),
                    first=First("X_F1"),
                ),
                Link(Field("X_F2"), FINAL),
                Link(Field("X_F3"), Field("X_F4"), length=Variable("X_F3")),
                Link(Field("X_F4"), FINAL),
            ],
            {
                Field("X_F1"): deepcopy(MODULAR_INTEGER),
                Field("X_F2"): deepcopy(MODULAR_INTEGER),
                Field("X_F3"): deepcopy(RANGE_INTEGER),
                Field("X_F4"): Opaque(),
            },
        ),
    )
Esempio n. 12
0
def test_substitution_relation_scalar(
    relation: Callable[[expr.Expr, expr.Expr], expr.Relation],
    expressions: Tuple[expr.Expr, expr.Expr],
    expected: Tuple[expr.Expr, expr.Expr],
) -> None:
    assert_equal(
        relation(*expressions).substituted(common.substitution(TLV_MESSAGE, public=True)),
        relation(*expected),
    )
Esempio n. 13
0
def test_case() -> None:
    assert_equal(
        Case(Variable("x"), [(Number(5), Number(1)), (Number(10), Number(2))]).z3expr(),
        z3.If(
            z3.Int("x") == z3.IntVal(5),
            z3.IntVal(1),
            z3.If(z3.Int("x") == z3.IntVal(10), z3.IntVal(2), z3.BoolVal(False)),
        ),
    )
Esempio n. 14
0
def test_merge_message_recursive() -> None:
    assert_equal(
        deepcopy(M_DBL_REF).merged(),
        UnprovenMessage(
            "P.Dbl_Ref",
            [
                Link(INITIAL, Field("SR_NR_F1"), length=Number(16)),
                Link(
                    Field("SR_NR_F3"),
                    Field("NR_F1"),
                    Equal(Variable("SR_NR_F3"), Variable("P.ONE")),
                    length=Number(16),
                ),
                Link(Field("SR_NR_F4"), Field("NR_F1"), length=Number(16)),
                Link(Field("NR_F3"), FINAL,
                     Equal(Variable("NR_F3"), Variable("P.ONE"))),
                Link(Field("NR_F4"), FINAL),
                Link(Field("SR_NR_F1"), Field("SR_NR_F2")),
                Link(
                    Field("SR_NR_F2"),
                    Field("SR_NR_F3"),
                    LessEqual(Variable("SR_NR_F2"), Number(100)),
                    first=First("SR_NR_F2"),
                ),
                Link(
                    Field("SR_NR_F2"),
                    Field("SR_NR_F4"),
                    GreaterEqual(Variable("SR_NR_F2"), Number(200)),
                    first=First("SR_NR_F2"),
                ),
                Link(Field("NR_F1"), Field("NR_F2")),
                Link(
                    Field("NR_F2"),
                    Field("NR_F3"),
                    LessEqual(Variable("NR_F2"), Number(100)),
                    first=First("NR_F2"),
                ),
                Link(
                    Field("NR_F2"),
                    Field("NR_F4"),
                    GreaterEqual(Variable("NR_F2"), Number(200)),
                    first=First("NR_F2"),
                ),
            ],
            {
                Field("SR_NR_F1"): Opaque(),
                Field("SR_NR_F2"): deepcopy(MODULAR_INTEGER),
                Field("SR_NR_F3"): deepcopy(ENUMERATION),
                Field("SR_NR_F4"): deepcopy(RANGE_INTEGER),
                Field("NR_F1"): Opaque(),
                Field("NR_F2"): deepcopy(MODULAR_INTEGER),
                Field("NR_F3"): deepcopy(ENUMERATION),
                Field("NR_F4"): deepcopy(RANGE_INTEGER),
            },
        ),
    )
Esempio n. 15
0
def test_if_expr_findall() -> None:
    assert_equal(
        If(
            [
                (Equal(Variable("X"), Number(42)), Number(21)),
                (Variable("Y"), Number(42)),
                (Number(42), Variable("Z")),
            ]
        ).findall(lambda x: isinstance(x, Number)),
        [Number(42), Number(21), Number(42), Number(42)],
    )
Esempio n. 16
0
def test_message_definite_fields() -> None:
    assert_equal(
        ETHERNET_FRAME.definite_fields,
        (
            Field("Destination"),
            Field("Source"),
            Field("Type_Length_TPID"),
            Field("Type_Length"),
            Field("Payload"),
        ),
    )
Esempio n. 17
0
def test_if_variables() -> None:
    assert_equal(
        If(
            [
                (Variable("X"), Number(21)),
                (Variable("Y"), Add(Number(21), Number(21))),
                (Add(Number(21), Number(21)), Variable("Z")),
            ]
        ).variables(),
        [Variable("X"), Variable("Y"), Variable("Z")],
    )
Esempio n. 18
0
def test_if_simplified() -> None:
    assert_equal(
        If(
            [
                (Variable("X"), Number(21)),
                (Variable("Y"), Add(Number(21), Number(21))),
                (Add(Number(21), Number(21)), Variable("Z")),
            ]
        ).simplified(),
        If([(Variable("X"), Number(21)), (Variable("Y"), Number(42)), (Number(42), Variable("Z"))]),
    )
    assert If([(TRUE, Variable("X"))]).simplified() == Variable("X")
Esempio n. 19
0
def test_case_variables() -> None:
    assert_equal(
        Case(
            Add(Number(21), Number(21)),
            [
                (Variable("X"), Number(21)),
                (Variable("Y"), Add(Number(21), Number(21))),
                (Add(Number(21), Number(21)), Variable("Z")),
            ],
        ).variables(),
        [Variable("X"), Variable("Y"), Variable("Z")],
    )
Esempio n. 20
0
def test_cat_packed_sequence(data, token_sizes, dim, device):
    inputs = [
        torch.randn((token_size, dim), device=device, requires_grad=True)
        for token_size in token_sizes
    ]
    packed_sequence = tgt.pack_sequence(inputs, enforce_sorted=False)

    actual_data, actual_token_sizes = rua.cat_sequence(inputs, device=device)
    expected_data, expected_token_sizes = rua.cat_packed_sequence(
        packed_sequence, device=device)

    assert_close(actual_data, expected_data)
    assert_equal(actual_token_sizes, expected_token_sizes)
    assert_grad_close(actual_data, expected_data, inputs=inputs)
Esempio n. 21
0
def test_expr_variables() -> None:
    assert_equal(
        Or(
            Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(42)))
        ).variables(),
        [Variable("Y"), Variable("X")],
    )
    assert_equal(
        Or(
            Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(42)))
        ).variables(),
        [Variable("Y"), Variable("X")],
    )
    assert_equal(
        Or(
            Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(42)))
        ).variables(),
        [Variable("Y"), Variable("X")],
    )
    assert_equal(
        Or(
            Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(1)))
        ).variables(),
        [Variable("Y"), Variable("X")],
    )
Esempio n. 22
0
def test_mathematical_expression_concatenation() -> None:
    assert_equal(
        grammar.mathematical_expression().parseString('(137) & "PNG" & (13, 10, 26, 10)')[0],
        Aggregate(
            Number(137),
            Number(80),
            Number(78),
            Number(71),
            Number(13),
            Number(10),
            Number(26),
            Number(10),
        ),
    )
Esempio n. 23
0
def test_message_field_condition() -> None:
    assert_equal(ETHERNET_FRAME.field_condition(INITIAL), TRUE)
    assert_equal(
        ETHERNET_FRAME.field_condition(Field("TPID")),
        Equal(Variable("Type_Length_TPID"), Number(33024, 16)),
    )
    assert_equal(
        ETHERNET_FRAME.field_condition(Field("Type_Length")),
        Or(
            NotEqual(Variable("Type_Length_TPID"), Number(33024, 16)),
            Equal(Variable("Type_Length_TPID"), Number(33024, 16)),
        ),
    )
    assert_equal(
        ETHERNET_FRAME.field_condition(Field("Payload")),
        Or(
            And(
                Or(
                    NotEqual(Variable("Type_Length_TPID"), Number(33024, 16)),
                    Equal(Variable("Type_Length_TPID"), Number(33024, 16)),
                ),
                LessEqual(Variable("Type_Length"), Number(1500)),
            ),
            And(
                Or(
                    NotEqual(Variable("Type_Length_TPID"), Number(33024, 16)),
                    Equal(Variable("Type_Length_TPID"), Number(33024, 16)),
                ),
                GreaterEqual(Variable("Type_Length"), Number(1536)),
            ),
        ),
    )
Esempio n. 24
0
def test_expr_str() -> None:
    assert_equal(
        str(
            And(
                If([(Variable("X"), Number(1)), (Variable("Y"), Number(2))], Number(3)),
                Variable("A"),
                Or(Variable("B"), Variable("C")),
                Variable("D"),
            )
        ),
        multilinestr(
            """(if
                   X
                then
                   1
                elsif
                   Y
                then
                   2
                else
                   3)
               and A
               and (B
                    or C)
               and D"""
        ),
    )
    assert_equal(
        str(
            ForAllOf(
                "X",
                Variable("Z"),
                If([(Variable("X"), Number(1)), (Variable("Y"), Number(2))], Number(3)),
            )
        ),
        multilinestr(
            """(for all X of Z =>
                   (if
                       X
                    then
                       1
                    elsif
                       Y
                    then
                       2
                    else
                       3))"""
        ),
    )
Esempio n. 25
0
def test_if() -> None:
    assert_equal(
        If(
            [
                (Greater(Variable("a"), Number(5)), Number(1)),
                (Greater(Variable("b"), Number(100)), Number(10)),
            ],
            Number(100),
        ).z3expr(),
        z3.If(
            z3.Int("a") > z3.IntVal(5),
            z3.IntVal(1),
            z3.If(z3.Int("b") > z3.IntVal(100), z3.IntVal(10), z3.IntVal(100)),
        ),
    )
Esempio n. 26
0
def test_cat_padded_sequence(data, token_sizes, dim, batch_first, device):
    inputs = [
        torch.randn((token_size, dim), device=device, requires_grad=True)
        for token_size in token_sizes
    ]
    padded_sequence = tgt.pad_sequence(inputs, batch_first=batch_first)
    token_sizes = torch.tensor(token_sizes, device=device)

    actual_data, actual_token_sizes = rua.cat_sequence(inputs, device=device)
    expected_data, expected_token_sizes = rua.cat_padded_sequence(
        padded_sequence, token_sizes, batch_first=batch_first, device=device)

    assert_close(actual_data, expected_data)
    assert_equal(actual_token_sizes, expected_token_sizes)
    assert_grad_close(actual_data, expected_data, inputs=inputs)
Esempio n. 27
0
def test_if_str() -> None:
    assert_equal(
        str(If([(Variable("X"), Number(1)), (Variable("Y"), Number(2))], Number(3))),
        multilinestr(
            """(if
                   X
                then
                   1
                elsif
                   Y
                then
                   2
                else
                   3)"""
        ),
    )
Esempio n. 28
0
def test_pad_packed_sequence(data, token_sizes, dim, batch_first, device):
    inputs = [
        torch.randn((token_size, dim), device=device, requires_grad=True)
        for token_size in token_sizes
    ]
    packed_sequence = tgt.pack_sequence(inputs, enforce_sorted=False)
    excepted_token_sizes = torch.tensor(token_sizes,
                                        device=torch.device('cpu'))

    excepted = tgt.pad_sequence(inputs, batch_first=batch_first)
    actual, actual_token_sizes = rua.pad_packed_sequence(
        packed_sequence, batch_first=batch_first)

    assert_close(actual, excepted)
    assert_grad_close(actual, excepted, inputs=inputs)
    assert_equal(actual_token_sizes, excepted_token_sizes)
Esempio n. 29
0
def test_message_definite_predecessors() -> None:
    assert_equal(
        ETHERNET_FRAME.definite_predecessors(FINAL),
        (
            Field("Destination"),
            Field("Source"),
            Field("Type_Length_TPID"),
            Field("Type_Length"),
            Field("Payload"),
        ),
    )
    assert_equal(
        ETHERNET_FRAME.definite_predecessors(Field("TCI")),
        (Field("Destination"), Field("Source"), Field("Type_Length_TPID"),
         Field("TPID")),
    )
Esempio n. 30
0
def test_case_str() -> None:
    assert_equal(
        str(
            ada.Case(
                ada.Variable("X"),
                [
                    (ada.Variable("Y"), ada.Number(1)),
                    (ada.Variable("Z"), ada.Number(1)),
                    (ada.Variable("others"), ada.Number(2)),
                ],
            )),
        multilinestr("""(case X is
                   when Y | Z =>
                      1,
                   when others =>
                      2)"""),
    )