Beispiel #1
0
def test03_convert(param):
    p1, p2 = param
    s1 = Struct().append('val', p1[1])
    s2 = Struct().append('val', p2[1])
    s = StructConverter(s1, s2)
    values = list(range(10))
    if Struct.is_signed(p1[1]) and Struct.is_signed(p2[1]):
        values += list(range(-10, 0))
    max_range = int(min(Struct.range(p1[1])[1], Struct.range(p2[1])[1]))
    if max_range > 1024:
        values += list(range(1000, 1024))

    # Native -> Native
    check_conversion(s, '@' + p1[0] * len(values),
                        '@' + p2[0] * len(values), values)

    # LE -> BE
    s1 = Struct(byte_order=Struct.ByteOrder.BigEndian).append('val', p1[1])
    s2 = Struct(byte_order=Struct.ByteOrder.LittleEndian).append('val', p2[1])
    s = StructConverter(s1, s2)
    check_conversion(s, '>' + p1[0] * len(values),
                        '<' + p2[0] * len(values), values)

    # BE -> LE
    s1 = Struct(byte_order=Struct.ByteOrder.LittleEndian).append('val', p1[1])
    s2 = Struct(byte_order=Struct.ByteOrder.BigEndian).append('val', p2[1])
    s = StructConverter(s1, s2)
    check_conversion(s, '<' + p1[0] * len(values),
                        '>' + p2[0] * len(values), values)
Beispiel #2
0
def test02_passthrough(param):
    s = Struct().append('val', param[1])
    ss = StructConverter(s, s)
    values = list(range(10))
    if Struct.is_signed(param[1]):
        values += list(range(-10, 0))
    check_conversion(ss, '@' + param[0] * len(values),
                     '@' + param[0] * len(values), values)
Beispiel #3
0
def test08_roundtrip_normalization_int2int(param):
    if Struct.is_float(param[1]):
        return
    s1_type = Struct.Type.Int8 if Struct.is_signed(param[1]) else Struct.Type.UInt8
    s1_dtype = 'b' if Struct.is_signed(param[1]) else 'B'
    s1_range = Struct.range(s1_type)
    s2_range = Struct.range(param[1])
    s1 = Struct().append('val', s1_type, Struct.Flags.Normalized)
    s2 = Struct().append('val', param[1], Struct.Flags.Normalized)
    s = StructConverter(s1, s2)
    values_in = list(range(int(s1_range[0]), int(s1_range[1] + 1)))
    values_out = np.array(values_in, dtype=np.float64)
    values_out *= s2_range[1] / s1_range[1]
    values_out = np.rint(values_out)
    values_out = np.maximum(values_out, s2_range[0])
    values_out = np.minimum(values_out, s2_range[1])
    values_out = np.array(values_out, s2.dtype()['val'])
    check_conversion(s, '@' + (s1_dtype * len(values_in)),
                        '@' + (param[0] * len(values_in)),
                     values_in, values_out)
Beispiel #4
0
def test03_missing_field(param):
    s1 = Struct().append('val1', param[1]) \
                 .append('val3', param[1])
    s2 = Struct().append('val1', param[1]) \
                 .append('val2', param[1], Struct.Flags.Default, 123) \
                 .append('val3', param[1])
    s = StructConverter(s1, s2)

    values = list(range(10))
    if Struct.is_signed(param[1]):
        values += list(range(-10, 0))
    output = []
    for k in range(len(values) // 2):
        output += [values[k * 2], 123, values[k * 2 + 1]]

    check_conversion(s, '@' + param[0] * len(values),
                        '@' + param[0] * (len(values) + len(values) // 2),
                     values, output)