diff --git a/csharp/Platform.Collections.Tests/ArrayTests.cs b/csharp/Platform.Collections.Tests/ArrayTests.cs index 1b9e67fb..3a7b6256 100644 --- a/csharp/Platform.Collections.Tests/ArrayTests.cs +++ b/csharp/Platform.Collections.Tests/ArrayTests.cs @@ -20,5 +20,29 @@ public void GetElementTest() Assert.False(array.TryGetElement(10, out element)); Assert.Equal(0, element); } + + [Fact] + public void GetElementWithNegativeIndexTest() + { + var array = new int[] { 1, 2, 3 }; + + // Test GetElementOrDefault with negative index + Assert.Equal(0, array.GetElementOrDefault(-1)); + Assert.Equal(0, array.GetElementOrDefault(-10)); + + // Test TryGetElement with negative index + Assert.False(array.TryGetElement(-1, out int element)); + Assert.Equal(0, element); + Assert.False(array.TryGetElement(-10, out element)); + Assert.Equal(0, element); + + // Test long versions + Assert.Equal(0, array.GetElementOrDefault(-1L)); + Assert.Equal(0, array.GetElementOrDefault(-10L)); + Assert.False(array.TryGetElement(-1L, out element)); + Assert.Equal(0, element); + Assert.False(array.TryGetElement(-10L, out element)); + Assert.Equal(0, element); + } } } diff --git a/csharp/Platform.Collections/Arrays/GenericArrayExtensions.cs b/csharp/Platform.Collections/Arrays/GenericArrayExtensions.cs index 3f4966c7..2ff00e07 100644 --- a/csharp/Platform.Collections/Arrays/GenericArrayExtensions.cs +++ b/csharp/Platform.Collections/Arrays/GenericArrayExtensions.cs @@ -19,7 +19,7 @@ public static class GenericArrayExtensions /// Number type int to compare.Число типа int для сравнения. /// Array element or default value.Элемент массива или же значение по умолчанию. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T GetElementOrDefault(this T[] array, int index) => array != null && array.Length > index ? array[index] : default; + public static T GetElementOrDefault(this T[] array, int index) => array != null && index >= 0 && array.Length > index ? array[index] : default; /// /// Сhecks whether the array exists, if so, checks the array length using the index variable type long, and if the array length is greater than the index - return array[index], otherwise - default value. @@ -30,7 +30,7 @@ public static class GenericArrayExtensions /// Number type long to compare.Число типа long для сравнения. /// Array element or default value.Элемент массива или же значение по умолчанию. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T GetElementOrDefault(this T[] array, long index) => array != null && array.LongLength > index ? array[index] : default; + public static T GetElementOrDefault(this T[] array, long index) => array != null && index >= 0 && array.LongLength > index ? array[index] : default; /// /// Checks whether the array exist, if so, checks the array length using the index varible type int, and if the array length is greater than the index, set the element variable to array[index] and return . @@ -44,7 +44,7 @@ public static class GenericArrayExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool TryGetElement(this T[] array, int index, out T element) { - if (array != null && array.Length > index) + if (array != null && index >= 0 && array.Length > index) { element = array[index]; return true; @@ -68,7 +68,7 @@ public static bool TryGetElement(this T[] array, int index, out T element) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool TryGetElement(this T[] array, long index, out T element) { - if (array != null && array.LongLength > index) + if (array != null && index >= 0 && array.LongLength > index) { element = array[index]; return true; diff --git a/experiments/ExperimentApp.csproj b/experiments/ExperimentApp.csproj new file mode 100644 index 00000000..2d730307 --- /dev/null +++ b/experiments/ExperimentApp.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8 + latest + enable + + + + + + + \ No newline at end of file diff --git a/experiments/NegativeIndexExperiment.cs b/experiments/NegativeIndexExperiment.cs new file mode 100644 index 00000000..adce21f3 --- /dev/null +++ b/experiments/NegativeIndexExperiment.cs @@ -0,0 +1,49 @@ +using System; +using Platform.Collections.Arrays; + +// This experiment demonstrates that the fix works correctly +public class Program +{ + public static void Main() + { + var array = new int[] { 1, 2, 3 }; + + Console.WriteLine("Testing GetElementOrDefault with negative index:"); + + // Before fix: This would throw IndexOutOfRangeException + // After fix: Returns default value (0) + var result1 = array.GetElementOrDefault(-1); + Console.WriteLine($"GetElementOrDefault(-1) = {result1}"); // Should be 0 + + var result2 = array.GetElementOrDefault(-10); + Console.WriteLine($"GetElementOrDefault(-10) = {result2}"); // Should be 0 + + Console.WriteLine("\nTesting TryGetElement with negative index:"); + + // Before fix: This would throw IndexOutOfRangeException + // After fix: Returns false and sets element to default + bool success1 = array.TryGetElement(-1, out int element1); + Console.WriteLine($"TryGetElement(-1, out element) = {success1}, element = {element1}"); // Should be false, 0 + + bool success2 = array.TryGetElement(-10, out int element2); + Console.WriteLine($"TryGetElement(-10, out element) = {success2}, element = {element2}"); // Should be false, 0 + + Console.WriteLine("\nTesting with long versions:"); + + var result3 = array.GetElementOrDefault(-1L); + Console.WriteLine($"GetElementOrDefault(-1L) = {result3}"); // Should be 0 + + bool success3 = array.TryGetElement(-1L, out int element3); + Console.WriteLine($"TryGetElement(-1L, out element) = {success3}, element = {element3}"); // Should be false, 0 + + Console.WriteLine("\nTesting with valid indices (should still work):"); + + var result4 = array.GetElementOrDefault(1); + Console.WriteLine($"GetElementOrDefault(1) = {result4}"); // Should be 2 + + bool success4 = array.TryGetElement(1, out int element4); + Console.WriteLine($"TryGetElement(1, out element) = {success4}, element = {element4}"); // Should be true, 2 + + Console.WriteLine("\nAll tests completed without exceptions!"); + } +} \ No newline at end of file