diff --git a/csharp/Platform.Data/LinkAddress.cs b/csharp/Platform.Data/LinkAddress.cs index 91c9d81..1657116 100644 --- a/csharp/Platform.Data/LinkAddress.cs +++ b/csharp/Platform.Data/LinkAddress.cs @@ -42,14 +42,11 @@ public TLinkAddress this[int index] [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (index == 0) - { - return Index; - } - else + if (index != 0) { throw new IndexOutOfRangeException(); } + return Index; } [MethodImpl(MethodImplOptions.AggressiveInlining)] set => throw new NotSupportedException(); diff --git a/csharp/Platform.Data/Point.cs b/csharp/Platform.Data/Point.cs index b1ed34b..7183b87 100644 --- a/csharp/Platform.Data/Point.cs +++ b/csharp/Platform.Data/Point.cs @@ -57,14 +57,11 @@ public TLinkAddress this[int index] [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (index < Size) - { - return Index; - } - else + if (index < 0 || index >= Size) { throw new IndexOutOfRangeException(); } + return Index; } [MethodImpl(MethodImplOptions.AggressiveInlining)] set => throw new NotSupportedException(); diff --git a/examples/experiments/IndexerTest.cs b/examples/experiments/IndexerTest.cs new file mode 100644 index 0000000..4bcf2d3 --- /dev/null +++ b/examples/experiments/IndexerTest.cs @@ -0,0 +1,101 @@ +using System; +using Platform.Data; + +namespace Platform.Data.Tests +{ + public class IndexerTest + { + public static void Main() + { + Console.WriteLine("Testing Point indexer..."); + var point = new Point(42UL, 3); + + // Test valid indices + try + { + for (int i = 0; i < 3; i++) + { + Console.WriteLine($"point[{i}] = {point[i]}"); + } + } + catch (Exception ex) + { + Console.WriteLine($"Unexpected error with valid indices: {ex.Message}"); + } + + // Test negative index (should throw) + try + { + var result = point[-1]; + Console.WriteLine($"ERROR: Negative index should have thrown exception, but got: {result}"); + } + catch (IndexOutOfRangeException) + { + Console.WriteLine("✓ Negative index correctly throws IndexOutOfRangeException"); + } + catch (Exception ex) + { + Console.WriteLine($"ERROR: Unexpected exception type: {ex.GetType().Name}: {ex.Message}"); + } + + // Test out-of-bounds index (should throw) + try + { + var result = point[3]; + Console.WriteLine($"ERROR: Out-of-bounds index should have thrown exception, but got: {result}"); + } + catch (IndexOutOfRangeException) + { + Console.WriteLine("✓ Out-of-bounds index correctly throws IndexOutOfRangeException"); + } + catch (Exception ex) + { + Console.WriteLine($"ERROR: Unexpected exception type: {ex.GetType().Name}: {ex.Message}"); + } + + Console.WriteLine(); + Console.WriteLine("Testing LinkAddress indexer..."); + var linkAddress = new LinkAddress(123UL); + + // Test valid index (0) + try + { + Console.WriteLine($"linkAddress[0] = {linkAddress[0]}"); + } + catch (Exception ex) + { + Console.WriteLine($"Unexpected error with valid index 0: {ex.Message}"); + } + + // Test negative index (should throw) + try + { + var result = linkAddress[-1]; + Console.WriteLine($"ERROR: Negative index should have thrown exception, but got: {result}"); + } + catch (IndexOutOfRangeException) + { + Console.WriteLine("✓ Negative index correctly throws IndexOutOfRangeException"); + } + catch (Exception ex) + { + Console.WriteLine($"ERROR: Unexpected exception type: {ex.GetType().Name}: {ex.Message}"); + } + + // Test positive index other than 0 (should throw) + try + { + var result = linkAddress[1]; + Console.WriteLine($"ERROR: Index 1 should have thrown exception, but got: {result}"); + } + catch (IndexOutOfRangeException) + { + Console.WriteLine("✓ Index 1 correctly throws IndexOutOfRangeException"); + } + catch (Exception ex) + { + Console.WriteLine($"ERROR: Unexpected exception type: {ex.GetType().Name}: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/examples/experiments/IndexerTest.csproj b/examples/experiments/IndexerTest.csproj new file mode 100644 index 0000000..6cd6395 --- /dev/null +++ b/examples/experiments/IndexerTest.csproj @@ -0,0 +1,11 @@ + + + Exe + net8 + enable + + + + + + \ No newline at end of file