Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions Assets/Tests/InputSystem/Utilities/ArrayHelperTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Globalization;
using NUnit.Framework;
using Unity.Collections;
Expand Down Expand Up @@ -118,6 +119,35 @@ public void Utilities_IndexOfReference__IsUsingReferenceEqualsAndConstrainedBySt
Assert.AreEqual(2, arr.IndexOfReference(arr[2], 1, 3));
}

[Test]
[Category("Utilities")]
public void Utilities_HaveDuplicateReferences_DetectsDuplicatesInFullRange()
{
var withDup = new object[] { new object(), new object(), new object() };
withDup[2] = withDup[0]; // duplicate at 0 and 2
Assert.That(withDup.HaveDuplicateReferences(0, 3), Is.True);

var noDup = new object[] { new object(), new object(), new object() };
Assert.That(noDup.HaveDuplicateReferences(0, 3), Is.False);

// Regression test for ISXB-1792: inner loop was "n < count - i" so later pairs were never checked
var dupAtEnd = new object[] { new object(), new object(), new object(), new object() };
dupAtEnd[3] = dupAtEnd[2]; // duplicate at 2 and 3
Assert.That(dupAtEnd.HaveDuplicateReferences(0, 4), Is.True);
}

[Test]
[Category("Utilities")]
public void Utilities_MergeWithComparer_UsesComparerToDeduplicate()
{
// Regression test for ISXB-1790: Merge(IEqualityComparer) was calling comparer.Equals(secondValue)
// instead of comparer.Equals(x, secondValue), so it compared against the comparer instance.
var first = new[] { "a", "b" };
var second = new[] { "A", "c" }; // "A" equals "a" with case-insensitive comparer
var merged = ArrayHelpers.Merge(first, second, StringComparer.OrdinalIgnoreCase);
Assert.That(merged, Is.EqualTo(new[] { "a", "b", "c" }));
}

[Test]
[Category("Utilities")]
public void Utilities_IndexOfPredicate__IsUsingPredicateForEqualityAndConstraintedByStartIndexAndCount()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static bool HaveDuplicateReferences<TFirst>(this TFirst[] first, int inde
for (var i = 0; i < count; ++i)
Comment thread
K-Tone marked this conversation as resolved.
Outdated
{
var element = first[i];
for (var n = i + 1; n < count - i; ++n)
for (var n = i + 1; n < count; ++n)
Comment thread
K-Tone marked this conversation as resolved.
Outdated
{
if (ReferenceEquals(element, first[n]))
return true;
Expand Down Expand Up @@ -552,7 +552,7 @@ public static TValue[] Merge<TValue>(TValue[] first, TValue[] second, IEqualityC
for (var i = 0; i < second.Length; ++i)
{
var secondValue = second[i];
if (!merged.Exists(x => comparer.Equals(secondValue)))
if (!merged.Exists(x => comparer.Equals(x, secondValue)))
Comment thread
K-Tone marked this conversation as resolved.
Outdated
{
merged.Add(secondValue);
}
Expand Down
Loading