-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
base: main
Are you sure you want to change the base?
Conversation
BigInteger smallBigInt = new BigInteger("10"); | ||
JsonPrimitive p1 = new JsonPrimitive(smallBigInt); | ||
JsonPrimitive p2 = new JsonPrimitive(smallBigInt); | ||
assertTrue("should be different objects: " + p1 + ", " + p2, p1 != p2); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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
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!