-
Notifications
You must be signed in to change notification settings - Fork 665
DYN-6732: Fix 'ReferenceEqualityComparer' hash collisions #16754
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
benglin
wants to merge
4
commits into
master
Choose a base branch
from
DYN-6732
base: master
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.
+153
−3
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
72a8ed4
Fix 'ReferenceEqualityComparer' hash collisions
benglin d3b441e
Updated comments as suggested by Copilot
benglin 9c1187d
Add 'ReferenceEqualityComparer' test cases
benglin 228c7a5
Potential fix for pull request finding 'Equality check on floating po…
benglin 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
135 changes: 135 additions & 0 deletions
135
test/Engine/ProtoTest/FFITests/ReferenceEqualityComparerTests.cs
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,135 @@ | ||
| using System.Collections.Generic; | ||
| using System.Runtime.CompilerServices; | ||
| using NUnit.Framework; | ||
| using ReferenceEqualityComparer = ProtoFFI.ReferenceEqualityComparer; | ||
|
|
||
| namespace ProtoFFITests | ||
| { | ||
| [TestFixture] | ||
| public class ReferenceEqualityComparerTests | ||
| { | ||
| /// <summary> | ||
| /// Test class that simulates geometry objects with value-based hash codes | ||
| /// (similar to Point objects with identical coordinates) | ||
| /// </summary> | ||
| private class TestPoint | ||
| { | ||
| public double X { get; set; } | ||
| public double Y { get; set; } | ||
| public double Z { get; set; } | ||
|
|
||
| public TestPoint(double x, double y, double z) | ||
| { | ||
| X = x; | ||
| Y = y; | ||
| Z = z; | ||
| } | ||
|
|
||
| // Value-based hash code (like Point.ComputeHashCode in LibG) | ||
| public override int GetHashCode() | ||
| { | ||
| int hash = 17; | ||
| hash = hash * 23 + X.GetHashCode(); | ||
| hash = hash * 23 + Y.GetHashCode(); | ||
| hash = hash * 23 + Z.GetHashCode(); | ||
| return hash; | ||
| } | ||
|
|
||
| public override bool Equals(object obj) | ||
| { | ||
| if (obj == null || GetType() != obj.GetType()) | ||
| return false; | ||
|
|
||
| var other = (TestPoint)obj; | ||
| return X == other.X && Y == other.Y && Z == other.Z; | ||
github-code-quality[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
github-code-quality[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
|
||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| [Category("UnitTests")] | ||
| public void ProducesDifferentHashCodesForDifferentInstances() | ||
| { | ||
| // Arrange: Create multiple objects with identical values but different instances | ||
| var point1 = new TestPoint(0, 0, 0); | ||
| var point2 = new TestPoint(0, 0, 0); | ||
| var point3 = new TestPoint(0, 0, 0); | ||
|
|
||
| var comparer = new ReferenceEqualityComparer(); | ||
|
|
||
| // Act: Get hash codes using ReferenceEqualityComparer | ||
| int hash1 = comparer.GetHashCode(point1); | ||
| int hash2 = comparer.GetHashCode(point2); | ||
| int hash3 = comparer.GetHashCode(point3); | ||
|
|
||
| // Assert: Different instances should produce different hash codes | ||
| // (even though they have identical values) | ||
| Assert.AreNotEqual(hash1, hash2, "Different instances should produce different hash codes"); | ||
| Assert.AreNotEqual(hash1, hash3, "Different instances should produce different hash codes"); | ||
| Assert.AreNotEqual(hash2, hash3, "Different instances should produce different hash codes"); | ||
| } | ||
|
|
||
| [Test] | ||
| [Category("UnitTests")] | ||
| public void UsesIdentityHashCode() | ||
| { | ||
| // Arrange | ||
| var point = new TestPoint(1, 2, 3); | ||
| var comparer = new ReferenceEqualityComparer(); | ||
|
|
||
| // Act | ||
| int comparerHash = comparer.GetHashCode(point); | ||
| int identityHash = RuntimeHelpers.GetHashCode(point); | ||
|
|
||
| // Assert: ReferenceEqualityComparer should use RuntimeHelpers.GetHashCode | ||
| Assert.AreEqual(identityHash, comparerHash, | ||
| "ReferenceEqualityComparer should use RuntimeHelpers.GetHashCode for identity-based hashing"); | ||
| } | ||
|
|
||
| [Test] | ||
| [Category("UnitTests")] | ||
| public void DictionaryLookupPerformance_NoCollisions() | ||
| { | ||
| // Arrange: Create dictionary using ReferenceEqualityComparer | ||
| var dictionary = new Dictionary<object, string>(new ReferenceEqualityComparer()); | ||
|
|
||
| // Create objects with identical values but different instances | ||
| const int count = 20; | ||
| var objects = new TestPoint[count]; | ||
| for (int i = 0; i < count; i++) | ||
| { | ||
| objects[i] = new TestPoint(0, 0, 0); // All have identical coordinates | ||
| dictionary[objects[i]] = $"Value_{i}"; | ||
| } | ||
|
|
||
| // Act & Assert: All lookups should succeed and be fast (O(1)) | ||
| for (int i = 0; i < count; i++) | ||
| { | ||
| Assert.IsTrue(dictionary.TryGetValue(objects[i], out string value), | ||
| $"Lookup should succeed for object at index {i}"); | ||
| Assert.AreEqual($"Value_{i}", value, | ||
| $"Retrieved value should match for object at index {i}"); | ||
| } | ||
|
|
||
| // Verify that objects with same values but different instances are treated as different | ||
| var newPoint = new TestPoint(0, 0, 0); | ||
| Assert.IsFalse(dictionary.ContainsKey(newPoint), | ||
| "New instance with same values should not be found (reference equality)"); | ||
| } | ||
|
|
||
| [Test] | ||
| [Category("UnitTests")] | ||
| public void ReferenceEqualitySemantics() | ||
| { | ||
| // Arrange | ||
| var point1 = new TestPoint(1, 2, 3); | ||
| var point2 = new TestPoint(1, 2, 3); // Same values, different instance | ||
| IEqualityComparer<object> comparer = new ReferenceEqualityComparer(); | ||
|
|
||
| // Act & Assert: Reference equality should be used, not value equality | ||
| Assert.IsFalse(comparer.Equals(point1, point2), | ||
| "Different instances should not be equal (reference equality)"); | ||
| Assert.IsTrue(comparer.Equals(point1, point1), | ||
| "Same instance should be equal to itself"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.