Skip to content
Open
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
24 changes: 24 additions & 0 deletions csharp/Platform.Collections.Tests/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
8 changes: 4 additions & 4 deletions csharp/Platform.Collections/Arrays/GenericArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class GenericArrayExtensions
/// <param name="index"><para>Number type int to compare.</para><para>Число Ρ‚ΠΈΠΏΠ° int для сравнСния.</para></param>
/// <returns><para>Array element or default value.</para><para>Π­Π»Π΅ΠΌΠ΅Π½Ρ‚ массива ΠΈΠ»ΠΈ ΠΆΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ.</para></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T GetElementOrDefault<T>(this T[] array, int index) => array != null && array.Length > index ? array[index] : default;
public static T GetElementOrDefault<T>(this T[] array, int index) => array != null && index >= 0 && array.Length > index ? array[index] : default;

/// <summary>
/// <para>Π‘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.</para>
Expand All @@ -30,7 +30,7 @@ public static class GenericArrayExtensions
/// <param name="index"><para>Number type long to compare.</para><para>Число Ρ‚ΠΈΠΏΠ° long для сравнСния.</para></param>
/// <returns><para>Array element or default value.</para><para>Π­Π»Π΅ΠΌΠ΅Π½Ρ‚ массива ΠΈΠ»ΠΈ ΠΆΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ.</para></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T GetElementOrDefault<T>(this T[] array, long index) => array != null && array.LongLength > index ? array[index] : default;
public static T GetElementOrDefault<T>(this T[] array, long index) => array != null && index >= 0 && array.LongLength > index ? array[index] : default;

/// <summary>
/// <para>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 <see langword="true"/>.</para>
Expand All @@ -44,7 +44,7 @@ public static class GenericArrayExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGetElement<T>(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;
Expand All @@ -68,7 +68,7 @@ public static bool TryGetElement<T>(this T[] array, int index, out T element)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGetElement<T>(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;
Expand Down
14 changes: 14 additions & 0 deletions experiments/ExperimentApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../csharp/Platform.Collections/Platform.Collections.csproj" />
</ItemGroup>

</Project>
49 changes: 49 additions & 0 deletions experiments/NegativeIndexExperiment.cs
Original file line number Diff line number Diff line change
@@ -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!");
}
}
Loading