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_convert_zero_bytes():
int32 = Int32(bytes([0x00, 0x00, 0x00, 0x00]))
assert int32.string() == "0"
assert int32.bytes() == bytes([0x00, 0x00, 0x00, 0x00])
def test_convert_zero_string():
int32 = Int32("0")
assert int32.string() == "0"
assert int32.bytes() == bytes([0x00, 0x00, 0x00, 0x00])
def test_too_many_bytes():
int32 = Int32(bytes([0x00, 0x00, 0x00, 0x01, 0x00]))
assert int32.size() == 4
assert int32.string() is None
assert int32.bytes() is None
#assert int32.size() == 4
def test_convert_min_string():
int32 = Int32("-2147483648")
assert int32.string() == "-2147483648"
assert int32.bytes() == bytes([0x80, 0x00, 0x00, 0x00])
def test_convert_max_bytes():
int32 = Int32(bytes([0x7F, 0xFF, 0xFF, 0xFF]))
assert int32.string() == "2147483647"
assert int32.bytes() == bytes([0x7F, 0xFF, 0xFF, 0xFF])
def test_none():
int32 = Int32()
assert int32.size() == 4
assert int32.string() is None
assert int32.bytes() is None
def test_convert_positive_bytes():
int32 = Int32(bytes([0x00, 0x00, 0x00, 0x01]))
assert int32.string() == "1"
assert int32.bytes() == bytes([0x00, 0x00, 0x00, 0x01])
def test_convert_positive_string():
int32 = Int32("1")
assert int32.string() == "1"
assert int32.bytes() == bytes([0x00, 0x00, 0x00, 0x01])
def test_convert_min_bytes():
int32 = Int32(bytes([0x80, 0x00, 0x00, 0x00])) #TODO: get binary value for this
assert int32.string() == "-2147483648"
assert int32.bytes() == bytes([0x80, 0x00, 0x00, 0x00])
def to_bytes(self, data) -> bytes:
if not data:
return None
if isinstance(data, bytes):
if len(data) == 4:
return data
elif isinstance(data, str):
return struct.pack(">i", int(data))
else:
raise SpadeTypeException("Can't convert {}.".format(type(data)))
return None
typemanager.add_type(Int32)