Skip to content

Commit eb544d4

Browse files
committed
fix largest unsigned int roundtrip
1 parent 151d2c4 commit eb544d4

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

pytests/test_dag_cbor.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,21 @@ def test_recursion_limit_exceed_on_nested_maps() -> None:
137137
assert 'in DAG-CBOR decoding' in str(exc_info.value)
138138

139139

140-
def test_dag_cbor_decode_largest_unsigned_int() -> None:
141-
result = libipld.decode_dag_cbor(bytes.fromhex('1bffffffffffffffff'))
142-
assert result == 2**64 - 1
140+
def test_dag_cbor_decode_largest_unsigned_int_roundtrip() -> None:
141+
data = bytes.fromhex('1bffffffffffffffff')
143142

143+
decoded_result = libipld.decode_dag_cbor(data)
144+
assert decoded_result == 2**64 - 1
144145

145-
def test_dag_cbor_decode_smallest_negative_int() -> None:
146-
result = libipld.decode_dag_cbor(bytes.fromhex('3bffffffffffffffff'))
147-
assert result == -(2**64)
146+
encoded_result = libipld.encode_dag_cbor(decoded_result)
147+
assert encoded_result == data
148+
149+
150+
def test_dag_cbor_decode_smallest_negative_int_roundtrip() -> None:
151+
data = bytes.fromhex('3bffffffffffffffff')
152+
153+
decoded_result = libipld.decode_dag_cbor(data)
154+
assert decoded_result == -(2**64)
155+
156+
encoded_result = libipld.encode_dag_cbor(decoded_result)
157+
assert encoded_result == data

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ fn encode_dag_cbor_from_pyobject<'py, W: Write>(
231231

232232
Ok(())
233233
} else if obj.is_instance_of::<PyInt>() {
234-
let i: i64 = obj.extract()?;
234+
let i: i128 = obj.extract()?;
235235

236236
if i.is_negative() {
237237
encode::write_u64(w, MajorKind::NegativeInt, -(i + 1) as u64)?

0 commit comments

Comments
 (0)