Skip to content

Commit

Permalink
Merge pull request #6 from bugless/fix-hashcode
Browse files Browse the repository at this point in the history
Add hashCode implementation
  • Loading branch information
rafaelring authored Sep 27, 2021
2 parents 07a9f98 + 6cae56b commit c605e8f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 0.3.0
- Add missing hashCode implementation ~aka remove feature where all BigDecimals were different~

## 0.2.1
- Remove undue logs

Expand Down
3 changes: 3 additions & 0 deletions lib/src/big_decimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ class BigDecimal implements Comparable<BigDecimal> {
return _add(intVal, -other.intVal, scale, other.scale).intVal.sign;
}

@override
int get hashCode => 31 * intVal.hashCode + scale;

@override
String toString() {
if (scale == 0) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: big_decimal
version: 0.2.1
version: 0.3.0
description: >
A bugless implementation of BigDecimal in Dart based on Java's BigDecimal
Expand Down
16 changes: 16 additions & 0 deletions test/big_decimal_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,20 @@ void main() {
tabCase(['123.456e-8', '0.00000123456']),
]),
);

group('hashcode', () {
test('same for equal numbers', () {
expect('1.0'.dec.hashCode, '1.0'.dec.hashCode);
expect('-1.0'.dec.hashCode, '-1.0'.dec.hashCode);
expect('1.0000'.dec.hashCode, '1.0000'.dec.hashCode);
expect('0'.dec.hashCode, '0'.dec.hashCode);
});

test('different for different numbers', () {
expect(false, '1.0'.dec.hashCode == '1.00'.dec.hashCode);
expect(false, '-1.0'.dec.hashCode == '1.0'.dec.hashCode);
expect(false, '1.0000'.dec.hashCode == '2'.dec.hashCode);
expect(false, '0'.dec.hashCode == '0.00'.dec.hashCode);
});
});
}

0 comments on commit c605e8f

Please sign in to comment.