Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that DecimalNodes with mathematically equal values are equal #434

Merged
merged 1 commit into from
Apr 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ public boolean equals(Object o)
if (o == this) return true;
if (o == null) return false;
if (o instanceof DecimalNode) {
return ((DecimalNode) o)._value.equals(_value);
return ((DecimalNode) o)._value.compareTo(_value) == 0;
}
return false;
}

@Override
public int hashCode() { return _value.hashCode(); }
public int hashCode() { return Double.valueOf(doubleValue()).hashCode(); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,33 @@ public void testDecimalNode() throws Exception
assertTrue(DecimalNode.valueOf(BigDecimal.valueOf(Long.MIN_VALUE)).canConvertToLong());
}

public void testDecimalNodeEqualsHashCode()
{
/*
* We want DecimalNodes with equivalent _numeric_ values to be equal;
* this is not the case for BigDecimal where "1.0" and "1" are not
* equal!
*/
BigDecimal b1 = BigDecimal.ONE;
BigDecimal b2 = new BigDecimal("1.0");
BigDecimal b3 = new BigDecimal("0.01e2");
BigDecimal b4 = new BigDecimal("1000e-3");

DecimalNode node1 = new DecimalNode(b1);
DecimalNode node2 = new DecimalNode(b2);
DecimalNode node3 = new DecimalNode(b3);
DecimalNode node4 = new DecimalNode(b4);

assertEquals(node1.hashCode(), node2.hashCode());
assertEquals(node2.hashCode(), node3.hashCode());
assertEquals(node3.hashCode(), node4.hashCode());

assertEquals(node1, node2);
assertEquals(node2, node1);
assertEquals(node2, node3);
assertEquals(node3, node4);
}

public void testBigIntegerNode() throws Exception
{
BigIntegerNode n = BigIntegerNode.valueOf(BigInteger.ONE);
Expand Down