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

Fix BigInteger equals bug, and its tests. #903

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ybank
Copy link

@ybank ybank commented Aug 22, 2016

Dear GSON developers, this pull request is a fix to solve the existed big integer equals problem: when an integer is really large it can be regarded as equal to some small integer, because of the loss of precision when converting big integer to long. The commit also fixed and improved the test. Thanks!

@ybank ybank mentioned this pull request Aug 22, 2016
BigInteger smallBigInt = new BigInteger("10");
JsonPrimitive p1 = new JsonPrimitive(smallBigInt);
JsonPrimitive p2 = new JsonPrimitive(smallBigInt);
assertTrue("should be different objects: " + p1 + ", " + p2, p1 != p2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed because above p1 and p2 are assigned two new (therefore not-same) instances

new BigInteger("18446744073709551616").add(BigInteger.valueOf(5));
JsonPrimitive p3 = new JsonPrimitive(limitPlus5);
JsonPrimitive p4 = new JsonPrimitive(limitPlus5);
assertTrue("should be different objects: " + p3 + ", " + p4, p3 != p4);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed because above p1 and p2 are assigned two new (therefore not-same) instances

JsonPrimitive p2 = new JsonPrimitive(smallBigInt);
assertTrue("should be different objects: " + p1 + ", " + p2, p1 != p2);
assertTrue("p1 and p2 should have the same value", p1.equals(p2));
assertTrue("p1 and p2 should have the same hash code", p1.hashCode() == p2.hashCode());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use assertEquals(int, int) to get a more descriptive error message in case of mismatch

JsonPrimitive p4 = new JsonPrimitive(limitPlus5);
assertTrue("should be different objects: " + p3 + ", " + p4, p3 != p4);
assertTrue("p3 and p4 should have the same value", p3.equals(p4));
assertTrue("p3 and p4 should have the same hash code", p3.hashCode() == p4.hashCode());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use assertEquals(int, int) to get a more descriptive error message in case of mismatch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants