Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_unsigned_saturated_33bit(self):
dtype = parser.PrimitiveType(
parser.PrimitiveType.KIND_UNSIGNED_INT,
33,
parser.PrimitiveType.CAST_MODE_SATURATED)
value = transport.cast(0, dtype)
self.assertEqual(value, 0)
value = transport.cast(8589934591, dtype)
self.assertEqual(value, 8589934591)
value = transport.cast(8589934592, dtype)
self.assertEqual(value, 8589934591)
value = transport.cast(-1, dtype)
self.assertEqual(value, 0)
def test_unsigned_saturated_64bit(self):
dtype = parser.PrimitiveType(
parser.PrimitiveType.KIND_UNSIGNED_INT,
64,
parser.PrimitiveType.CAST_MODE_SATURATED)
value = transport.cast(0, dtype)
self.assertEqual(value, 0)
value = transport.cast(18446744073709551615, dtype)
self.assertEqual(value, 18446744073709551615)
value = transport.cast(-1, dtype)
self.assertEqual(value, 0)
value = transport.cast(18446744073709551617, dtype)
self.assertEqual(value, 18446744073709551615)
value = transport.cast(-18446744073709551615, dtype)
self.assertEqual(value, 0)
def test_truncated_33bit(self):
dtype = parser.PrimitiveType(
parser.PrimitiveType.KIND_UNSIGNED_INT,
33,
parser.PrimitiveType.CAST_MODE_TRUNCATED)
value = transport.cast(0, dtype)
self.assertEqual(value, 0)
value = transport.cast(8589934591, dtype)
self.assertEqual(value, 8589934591)
value = transport.cast(8589934592, dtype)
self.assertEqual(value, 0)
value = transport.cast(-1, dtype)
self.assertEqual(value, 8589934591)
def test_truncated_1bit(self):
dtype = parser.PrimitiveType(
parser.PrimitiveType.KIND_UNSIGNED_INT,
1,
parser.PrimitiveType.CAST_MODE_TRUNCATED)
value = transport.cast(0, dtype)
self.assertEqual(value, 0)
value = transport.cast(1, dtype)
self.assertEqual(value, 1)
value = transport.cast(2, dtype)
self.assertEqual(value, 0)
value = transport.cast(-1, dtype)
self.assertEqual(value, 1)
value = transport.cast(5, dtype)
self.assertEqual(value, 1)
value = transport.cast(-10, dtype)
self.assertEqual(value, 0)
def make_float(bitlen):
return parser.PrimitiveType(parser.PrimitiveType.KIND_FLOAT, bitlen,
parser.PrimitiveType.CAST_MODE_TRUNCATED)
parser.ArrayType.MODE_DYNAMIC,
5
),
"c"
)
]
def custom_type_factory(*args, **kwargs):
return transport.CompoundValue(custom_type, *args, **kwargs)
custom_type._instantiate = custom_type_factory
self.a1_type = parser.ArrayType(
parser.PrimitiveType(
parser.PrimitiveType.KIND_SIGNED_INT,
8,
parser.PrimitiveType.CAST_MODE_TRUNCATED
),
parser.ArrayType.MODE_STATIC,
4
)
self.a2_type = parser.ArrayType(
parser.PrimitiveType(
parser.PrimitiveType.KIND_FLOAT,
16,
parser.PrimitiveType.CAST_MODE_SATURATED
),
parser.ArrayType.MODE_STATIC,
2
)
self.a3_type = parser.ArrayType(
custom_type,
parser.ArrayType.MODE_STATIC,
(0,0),
""
)
self.custom_type.union = True
self.custom_type.fields = [
parser.Field(
parser.PrimitiveType(
parser.PrimitiveType.KIND_FLOAT,
16,
parser.PrimitiveType.CAST_MODE_SATURATED
),
"a"
),
parser.Field(
parser.ArrayType(
parser.PrimitiveType(
parser.PrimitiveType.KIND_UNSIGNED_INT,
8,
parser.PrimitiveType.CAST_MODE_SATURATED
),
parser.ArrayType.MODE_STATIC,
2
),
"b"
)
]
def custom_type_factory(*args, **kwargs):
return transport.CompoundValue(self.custom_type, *args,
**kwargs)
self.custom_type._instantiate = custom_type_factory
8,
parser.PrimitiveType.CAST_MODE_TRUNCATED
),
"a"
),
parser.Field(
parser.PrimitiveType(
parser.PrimitiveType.KIND_FLOAT,
16,
parser.PrimitiveType.CAST_MODE_SATURATED
),
"b"
),
parser.Field(
parser.ArrayType(
parser.PrimitiveType(
parser.PrimitiveType.KIND_UNSIGNED_INT,
1,
parser.PrimitiveType.CAST_MODE_SATURATED
),
parser.ArrayType.MODE_DYNAMIC,
5
),
"c"
)
]
def custom_type_factory(*args, **kwargs):
return transport.CompoundValue(custom_type, *args, **kwargs)
custom_type._instantiate = custom_type_factory
self.a1_type = parser.ArrayType(
def _extract_fields(self, x):
if isinstance(x, (int, float, bool, str, bytes)):
return x
if isinstance(x, uavcan.transport.Void) or isinstance(x.type, uavcan.dsdl.parser.VoidType): # HACK
return None
if isinstance(x.type, uavcan.dsdl.parser.PrimitiveType):
return x.value
if isinstance(x.type, uavcan.dsdl.parser.CompoundType):
return {name: self._extract_fields(value) for name, value in x.fields.items()}
if isinstance(x.type, uavcan.dsdl.parser.ArrayType):
val = [self._extract_fields(y) for y in x]
try:
val = bytes(val) # String handling heuristic
except Exception:
pass
return val
raise Exception('Invalid field type: %s' % x.type)