Skip to content

Commit 7fee9c0

Browse files
authored
Fix Int64.operator == (#911)
Bug introduced in commit a4dc873, which is not released yet. Note: `operator ==` implementations in this package are not symmetric, but we consider that as intentional. See #910 for details. Fixes #910.
1 parent a4dc873 commit 7fee9c0

File tree

3 files changed

+63
-40
lines changed

3 files changed

+63
-40
lines changed

pkgs/fixnum/lib/src/int64_native.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,17 @@ class Int64 implements IntX {
270270
/// Returns whether this [Int64] has the same numeric value as the given
271271
/// object. The argument may be an [int] or an [IntX].
272272
@override
273-
bool operator ==(Object other) => _i == _promote(other);
273+
bool operator ==(Object other) {
274+
if (other is Int64) {
275+
return _i == other._i;
276+
} else if (other is int) {
277+
return _i == other;
278+
} else if (other is Int32) {
279+
return _i == other.toInt();
280+
} else {
281+
return false;
282+
}
283+
}
274284

275285
@override
276286
Int64 abs() => Int64(_i.abs());

pkgs/fixnum/test/int32_test.dart

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,25 @@ void main() {
214214
});
215215

216216
test('==', () {
217-
expect(Int32(17), isNot(equals(Int32(18))));
218-
expect(Int32(17), equals(Int32(17)));
219-
expect(Int32(17), isNot(equals(Int32(16))));
220-
expect(Int32(17), isNot(equals(Int64(18))));
221-
expect(Int32(17), equals(Int64(17)));
222-
expect(Int32(17), isNot(equals(Int64(16))));
223-
expect(Int32.MIN_VALUE, isNot(equals(Int32.MAX_VALUE)));
224-
expect(Int32(17), isNot(equals(18)));
225-
expect(Int32(17) == 17, isTrue); // ignore: unrelated_type_equality_checks
226-
expect(Int32(17), isNot(equals(16)));
227-
expect(Int32(17), isNot(equals(Object())));
228-
expect(Int32(17), isNot(equals(null)));
217+
// Note: do not use `equals` matcher below as it considers exceptions as
218+
// `false`. See issue #910.
219+
expect(Int32(17) == Int32(18), false);
220+
expect(Int32(17) == Int32(17), true);
221+
expect(Int32(17) == Int32(16), false);
222+
// ignore: unrelated_type_equality_checks
223+
expect(Int32(17) == Int64(18), false);
224+
// ignore: unrelated_type_equality_checks
225+
expect(Int32(17) == Int64(17), true);
226+
// ignore: unrelated_type_equality_checks
227+
expect(Int32(17) == Int64(16), false);
228+
expect(Int32.MIN_VALUE == Int32.MAX_VALUE, false);
229+
expect(Int32(17) == 18, false);
230+
// ignore: unrelated_type_equality_checks
231+
expect(Int32(17) == 17, true);
232+
expect(Int32(17) == 16, false);
233+
expect(Int32(17) == Object(), false);
234+
// ignore: unnecessary_null_comparison
235+
expect(Int32(17) == null, false);
229236
});
230237

231238
test('>=', () {

pkgs/fixnum/test/int64_test.dart

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -459,34 +459,40 @@ void main() {
459459
});
460460

461461
test('==', () {
462-
expect(Int64(0), equals(Int64(0)));
463-
expect(Int64(0), isNot(equals(Int64(1))));
464-
expect(Int64(0), equals(Int32(0)));
465-
expect(Int64(0), isNot(equals(Int32(1))));
466-
expect(Int64(0) == 0, isTrue); // ignore: unrelated_type_equality_checks
467-
expect(Int64(0), isNot(equals(1)));
468-
expect(Int64(10), isNot(equals(Int64(11))));
469-
expect(Int64(10), equals(Int64(10)));
470-
expect(Int64(10), isNot(equals(Int64(9))));
471-
expect(Int64(10), isNot(equals(Int32(11))));
472-
expect(Int64(10), equals(Int32(10)));
473-
expect(Int64(10), isNot(equals(Int32(9))));
474-
expect(Int64(10), isNot(equals(11)));
475-
expect(Int64(10) == 10, isTrue); // ignore: unrelated_type_equality_checks
476-
expect(Int64(10), isNot(equals(9)));
477-
expect(Int64(-10), equals(Int64(-10)));
462+
// Note: do not use `equals` matcher below as it considers exceptions as
463+
// `false`. See issue #910.
464+
expect(Int64(0) == Int64(0), true);
465+
expect(Int64(0) == Int64(1), false);
466+
// ignore: unrelated_type_equality_checks
467+
expect(Int64(0) == Int32(0), true);
468+
// ignore: unrelated_type_equality_checks
469+
expect(Int64(0) == Int32(1), false);
470+
expect(Int64(0) == 0, true);
471+
expect(Int64(0) == 1, false);
472+
expect(Int64(10) == Int64(11), false);
473+
expect(Int64(10) == Int64(10), true);
474+
expect(Int64(10) == Int64(9), false);
475+
// ignore: unrelated_type_equality_checks
476+
expect(Int64(10) == Int32(11), false);
477+
// ignore: unrelated_type_equality_checks
478+
expect(Int64(10) == Int32(10), true);
479+
// ignore: unrelated_type_equality_checks
480+
expect(Int64(10) == Int32(9), false);
481+
expect(Int64(10) == 11, false);
482+
expect(Int64(10) == 10, true);
483+
expect(Int64(10) == 9, false);
484+
expect(Int64(-10) == Int64(-10), true);
478485
expect(Int64(-10) != Int64(-10), false);
479-
expect(
480-
Int64(-10) == -10,
481-
isTrue,
482-
); // ignore: unrelated_type_equality_checks
483-
expect(Int64(-10), isNot(equals(-9)));
484-
expect(largePos, equals(largePos));
485-
expect(largePos, isNot(equals(largePosPlusOne)));
486-
expect(largePosPlusOne, isNot(equals(largePos)));
487-
expect(Int64.MIN_VALUE, isNot(equals(Int64.MAX_VALUE)));
488-
expect(Int64(17), isNot(equals(Object())));
489-
expect(Int64(17), isNot(equals(null)));
486+
// ignore: unrelated_type_equality_checks
487+
expect(Int64(-10) == -10, true);
488+
expect(Int64(-10) == -9, false);
489+
expect(largePos == largePos, true);
490+
expect(largePos == largePosPlusOne, false);
491+
expect(largePosPlusOne == largePos, false);
492+
expect(Int64.MIN_VALUE == Int64.MAX_VALUE, false);
493+
expect(Int64(17) == Object(), false);
494+
// ignore: unnecessary_null_comparison
495+
expect(Int64(17) == null, false);
490496
});
491497

492498
test('>=', () {

0 commit comments

Comments
 (0)