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 inconsistent equals and hashcode when annotations are cleared #573

Merged
merged 1 commit into from
Sep 18, 2023
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
10 changes: 5 additions & 5 deletions src/com/amazon/ion/impl/lite/IonValueLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -983,18 +983,18 @@ public final int findTypeAnnotation(String annotation)
protected int hashTypeAnnotations(final int original)
{
final SymbolToken[] tokens = _annotations == null ? SymbolToken.EMPTY_ARRAY : _annotations;
if (tokens.length == 0)
{
return original;
}

// Note: Because the tokens array doubles in size when it grows, tokens.length does not correctly convey
// the actual number of annotations on the value.
int count = 0;
while (count < tokens.length && tokens[count] != null) {
count++;
}

if (count == 0)
{
return original;
}

int result = valueHashSalt * original + count;

for (int i = 0; i < count; i++)
Expand Down
6 changes: 6 additions & 0 deletions test/com/amazon/ion/IonValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ public void testClearTypeAnnotations() {
IonValue v = system().singleValue("a::b::null");
v.clearTypeAnnotations();
assertAnnotations(v);

IonAssert.assertIonEquals(system().newNull(), v);
}

@Test
Expand Down Expand Up @@ -252,16 +254,20 @@ public void testRemoveTypeAnnotation()

v.removeTypeAnnotation(ann);
assertAnnotations(v);
IonAssert.assertIonEquals(system().newNull(), v);

v = system().singleValue("ann::ben::cam::null");
v.removeTypeAnnotation(ben);
assertAnnotations(v, ann, "cam");
IonAssert.assertIonEquals(system().singleValue("ann::cam::null"), v);
v.removeTypeAnnotation(ben);
assertAnnotations(v, ann, "cam");
v.removeTypeAnnotation("cam");
assertAnnotations(v, ann);
IonAssert.assertIonEquals(system().singleValue("ann::null"), v);
v.removeTypeAnnotation(ann);
assertAnnotations(v);
IonAssert.assertIonEquals(system().newNull(), v);
}

@Test
Expand Down
7 changes: 6 additions & 1 deletion test/com/amazon/ion/junit/IonAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,12 @@ private static void doAssertIonEquals(String path,
IonValue expected,
IonValue actual)
{
if (expected == actual) return;
if (expected == actual) {
if (expected != null) {
assertEquals("IonValues were equal, but have unequal hash codes.", expected.hashCode(), actual.hashCode());
}
return;
}

IonType expectedType = expected.getType();
assertSame(path + " type", expectedType, actual.getType());
Expand Down
Loading