Skip to content

Commit b0a4193

Browse files
committed
Add a spec for BigDecimal#hash.
1 parent 293ccb2 commit b0a4193

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require_relative '../../spec_helper'
2+
require 'bigdecimal'
3+
4+
describe "BidDecimal#hash" do
5+
describe "two BigDecimal objects with the same value" do
6+
it "should have the same hash for ordinary values" do
7+
BigDecimal('1.2920').hash.should == BigDecimal('1.2920').hash
8+
end
9+
10+
it "should have the same hash for infinite values" do
11+
BigDecimal("+Infinity").hash.should == BigDecimal("+Infinity").hash
12+
BigDecimal("-Infinity").hash.should == BigDecimal("-Infinity").hash
13+
end
14+
15+
it "should have the same hash for NaNs" do
16+
BigDecimal("NaN").hash.should == BigDecimal("NaN").hash
17+
end
18+
19+
it "should have the same hash for zero values" do
20+
BigDecimal("+0").hash.should == BigDecimal("+0").hash
21+
BigDecimal("-0").hash.should == BigDecimal("-0").hash
22+
end
23+
end
24+
25+
describe "two BigDecimal objects with numerically equal values" do
26+
it "should have the same hash value" do
27+
BigDecimal("1.2920").hash.should == BigDecimal("1.2920000").hash
28+
end
29+
end
30+
end

src/main/java/org/truffleruby/stdlib/bigdecimal/BigDecimalNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ public Object hashNormal(DynamicObject value) {
15661566
public Object hashSpecial(DynamicObject value) {
15671567
final BigDecimalType type = Layouts.BIG_DECIMAL.getType(value);
15681568

1569-
return getContext().getHashing(this).hash(CLASS_SALT, System.identityHashCode(type));
1569+
return getContext().getHashing(this).hash(CLASS_SALT, type.hashCode());
15701570
}
15711571

15721572
}

0 commit comments

Comments
 (0)