Skip to content

Commit

Permalink
Fixed negative small int CBOR de-serializing (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexDochioiu authored Jul 10, 2024
1 parent 957d42f commit fbf4176
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/src/utils/arg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class _ArgInt implements Arg {
final int value;

@override
_ArgInt operator ~() => _ArgInt((~value).toSigned(32));
_ArgInt operator ~() => _ArgInt((~value).toSigned(33));

@override
final bool isIndefiniteLength = false;
Expand Down
52 changes: 52 additions & 0 deletions test/int_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Package : Cbor
* Author : Alex Dochioiu <[email protected]>
* Date : 19/06/2024
* Copyright : Alex Dochioiu
*/

import 'package:cbor/cbor.dart';
import 'package:test/test.dart';

void main() {
group("Int Numbers Test", () {
final numbersToTest = [
BigInt.parse("0"),
BigInt.parse("1"),
BigInt.parse("2656159029"),
BigInt.parse("4294967295"),
BigInt.parse("4294967296"),
BigInt.parse("4294967299"),
BigInt.parse("5294967590"),
BigInt.parse("91234567890"),
BigInt.parse("912345678901234567890"),
];

group("positive numbers", () {
for (final number in numbersToTest) {
test(number.toString(), () {
final cborInt = CborInt(number);
final serialized = cborEncode(cborInt);
final deserialized = cborDecode(serialized);
expect(cborInt.toBigInt(), number);
expect(deserialized, isA<CborInt>());
expect((deserialized as CborInt).toBigInt(), number);
});
}
});

group("negative numbers", () {
for (final positiveNumber in numbersToTest) {
final number = BigInt.zero - positiveNumber;
test(number.toString(), () {
final cborInt = CborInt(number);
final serialized = cborEncode(cborInt);
final deserialized = cborDecode(serialized);
expect(cborInt.toBigInt(), number);
expect(deserialized, isA<CborInt>());
expect((deserialized as CborInt).toBigInt(), number);
});
}
});
});
}

0 comments on commit fbf4176

Please sign in to comment.