-
Notifications
You must be signed in to change notification settings - Fork 408
fix: correct record equals/toString/hashCode for reference and array components (#628) #631
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
Open
Darshan-dev57
wants to merge
2
commits into
javapathfinder:call-site-generation
Choose a base branch
from
Darshan-dev57:fix-628-record-reference-components
base: call-site-generation
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
src/tests/java17/records/RecordReferenceComponentTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| package java17.records; | ||
|
|
||
| import gov.nasa.jpf.util.test.TestJPF; | ||
| import org.junit.Test; | ||
| import static org.junit.Assert.*; | ||
|
|
||
| /** | ||
| * Regression tests for GitHub issue #628: | ||
| * Record equals/toString/hashCode with reference and array components. | ||
| */ | ||
| public class RecordReferenceComponentTest extends TestJPF { | ||
|
|
||
| record ArrayRecord(int[] data) {} | ||
| record StringRecord(String name, int age) {} | ||
| record BoxedRecord(Integer val) {} | ||
| record NullableRecord(String s, Integer i) {} | ||
| record NestedRecord(StringRecord inner, int x) {} | ||
| record LongArrayRecord(long[][] data) {} | ||
|
|
||
| static class IdentityClass { | ||
| int value; | ||
| IdentityClass(int v) { this.value = v; } | ||
| } | ||
| record IdentityRecord(IdentityClass obj) {} | ||
|
|
||
| @Test | ||
| public void testArraySameReference() { | ||
| if (verifyNoPropertyViolation()) { | ||
| int[] arr = {1, 2, 3}; | ||
| ArrayRecord r1 = new ArrayRecord(arr); | ||
| ArrayRecord r2 = new ArrayRecord(arr); | ||
| assertTrue(r1.equals(r2)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testArrayDifferentReference() { | ||
| if (verifyNoPropertyViolation()) { | ||
| ArrayRecord r1 = new ArrayRecord(new int[]{1, 2, 3}); | ||
| ArrayRecord r2 = new ArrayRecord(new int[]{1, 2, 3}); | ||
| assertFalse(r1.equals(r2)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testArrayToStringFormat() { | ||
| if (verifyNoPropertyViolation()) { | ||
| ArrayRecord r = new ArrayRecord(new int[]{1, 2, 3}); | ||
| String s = r.toString(); | ||
| assertTrue("should contain [I@", s.contains("[I@")); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testStringToString() { | ||
| if (verifyNoPropertyViolation()) { | ||
| StringRecord p = new StringRecord("alice", 30); | ||
| assertEquals("StringRecord[name=alice, age=30]", p.toString()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testStringEquals() { | ||
| if (verifyNoPropertyViolation()) { | ||
| StringRecord p1 = new StringRecord("alice", 30); | ||
| StringRecord p2 = new StringRecord("alice", 30); | ||
| assertTrue(p1.equals(p2)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testBoxedIntegerEquals() { | ||
| if (verifyNoPropertyViolation()) { | ||
| BoxedRecord r1 = new BoxedRecord(Integer.valueOf(1000)); | ||
| BoxedRecord r2 = new BoxedRecord(Integer.valueOf(1000)); | ||
| assertTrue(r1.equals(r2)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testBoxedIntegerToString() { | ||
| if (verifyNoPropertyViolation()) { | ||
| BoxedRecord r = new BoxedRecord(42); | ||
| assertEquals("BoxedRecord[val=42]", r.toString()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testIdentityClassEquality() { | ||
| if (verifyNoPropertyViolation()) { | ||
| IdentityClass a = new IdentityClass(99); | ||
| IdentityClass b = new IdentityClass(99); | ||
| IdentityRecord r1 = new IdentityRecord(a); | ||
| IdentityRecord r2 = new IdentityRecord(b); | ||
| assertFalse(r1.equals(r2)); | ||
| assertTrue(new IdentityRecord(a).equals(new IdentityRecord(a))); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testNullComponents() { | ||
| if (verifyNoPropertyViolation()) { | ||
| NullableRecord r1 = new NullableRecord(null, null); | ||
| NullableRecord r2 = new NullableRecord(null, null); | ||
| assertTrue(r1.equals(r2)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testNullToString() { | ||
| if (verifyNoPropertyViolation()) { | ||
| NullableRecord r = new NullableRecord(null, null); | ||
| assertEquals("NullableRecord[s=null, i=null]", r.toString()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testNestedRecordEquals() { | ||
| if (verifyNoPropertyViolation()) { | ||
| NestedRecord r1 = new NestedRecord(new StringRecord("alice", 30), 1); | ||
| NestedRecord r2 = new NestedRecord(new StringRecord("alice", 30), 1); | ||
| assertTrue(r1.equals(r2)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testNestedRecordToString() { | ||
| if (verifyNoPropertyViolation()) { | ||
| NestedRecord r = new NestedRecord(new StringRecord("alice", 30), 1); | ||
| assertEquals("NestedRecord[inner=StringRecord[name=alice, age=30], x=1]", r.toString()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testHashCodeConsistencyString() { | ||
| if (verifyNoPropertyViolation()) { | ||
| StringRecord r1 = new StringRecord("alice", 30); | ||
| StringRecord r2 = new StringRecord("alice", 30); | ||
| assertEquals(r1.hashCode(), r2.hashCode()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testHashCodeConsistencyBoxed() { | ||
| if (verifyNoPropertyViolation()) { | ||
| BoxedRecord r1 = new BoxedRecord(Integer.valueOf(1000)); | ||
| BoxedRecord r2 = new BoxedRecord(Integer.valueOf(1000)); | ||
| assertEquals(r1.hashCode(), r2.hashCode()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testHashCodeConsistencyArray() { | ||
| if (verifyNoPropertyViolation()) { | ||
| int[] arr = {1, 2, 3}; | ||
| ArrayRecord r1 = new ArrayRecord(arr); | ||
| ArrayRecord r2 = new ArrayRecord(arr); | ||
| assertEquals(r1.hashCode(), r2.hashCode()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testHashCodeDifferentLengthArrays() { | ||
| if (verifyNoPropertyViolation()) { | ||
| int[] arr2a = {1, 2}; | ||
| int[] arr2b = {1, 2}; | ||
| int[] arr3 = {1, 2, 3}; | ||
|
|
||
| ArrayRecord r2a = new ArrayRecord(arr2a); | ||
| ArrayRecord r2b = new ArrayRecord(arr2b); | ||
| ArrayRecord r3 = new ArrayRecord(arr3); | ||
|
|
||
| assertEquals(r2a.hashCode(), r2b.hashCode()); | ||
| assertTrue(r2a.hashCode() != r3.hashCode()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testHashCodeHigherDimensionalArray() { | ||
| if (verifyNoPropertyViolation()) { | ||
| long[][] arr1 = {{1L, 2L}, {3L, 4L}}; | ||
| long[][] arr2 = {{1L, 2L}, {3L, 4L}}; | ||
|
|
||
| LongArrayRecord r1 = new LongArrayRecord(arr1); | ||
| LongArrayRecord r2 = new LongArrayRecord(arr2); | ||
|
|
||
| assertEquals(r1.hashCode(), r2.hashCode()); | ||
|
|
||
| LongArrayRecord r3 = new LongArrayRecord(arr1); | ||
| assertEquals(r1.hashCode(), r3.hashCode()); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.