Пример #1
0
    def test_recursive_type(self):
        variable_length_integer = Choice('variable length integer', [])
        variable_length_integer.children = [
            Sequence('final byte:', [
                Field(None, length=1, constraints=[Equals(0)]),
                Field('value:', length=7)],
                value=parse('${value:}')),
            Sequence('intermediate byte:', [
                Field(None, length=1, constraints=[Equals(1)]),
                Field('value:', length=7),
                Child('next:', variable_length_integer)],
                value=parse('(${value:} << 7) + ${next:}'))]

        params = ExpressionParameters([variable_length_integer])
        range = EntryValueType(variable_length_integer).range(params)
        self.assertEqual(None, range.min)
        self.assertEqual(None, range.max)
Пример #2
0
    def test_unknown_range(self):
        # In this case 'intermediate byte:' has an unknown range, while
        # 'final byte:' has a known range. We should attempt to solve
        # the smallest range first...
        variable_length_integer = Choice('variable length integer', [])
        variable_length_integer.children = [
            Sequence('final byte:', [
                Field(None, length=1, constraints=[Equals(0)]),
                Field('value:', length=7)],
                value=parse('${value:}')),
            Sequence('intermediate byte:', [
                Field(None, length=1, constraints=[Equals(1)]),
                Field('least significant:', length=7),
                Child('most significant:', variable_length_integer)],
                value=parse('(${most significant:} * 128) + ${least significant:}'),
            constraints=[Minimum(0)])]

        self.assertEqual({'${most significant:}':1, '${least significant:}':0},
                _solve(variable_length_integer, 1, 128))
        self.assertEqual({'${most significant:}':1, '${least significant:}':1},
                _solve(variable_length_integer, 1, 129))
Пример #3
0
    def test_recursive_common(self):
        # Test a recursive parser to decode xml style data

        embedded = Sequence('embedded', [])
        digit = Field('data', 8, constraints=[Minimum(ord('0')), Maximum(ord('9'))])
        item = Choice('item', [embedded, digit])
        embedded.children = [
                Field('', length=8, format=Field.TEXT, constraints=[Equals('<')]),
                item,
                Field('', length=8, format=Field.TEXT, constraints=[Equals('>')])]

        # Lets just test that we can decode things correctly...
        list(item.decode(Data('8')))
        list(item.decode(Data('<5>')))
        list(item.decode(Data('<<7>>')))
        self.assertRaises(bdec.DecodeError, list, item.decode(Data('a')))
        self.assertRaises(bdec.DecodeError, list, item.decode(Data('<5')))

        lookup = ResultParameters([item])
        self.assertEqual([Param('result', Param.OUT, EntryType(item))], lookup.get_params(item))
        self.assertEqual([Param('result', Param.OUT, EntryType(embedded))], lookup.get_params(embedded))