From 7ea04094dc7bb83bffb18571f0382720067833fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Tue, 9 Dec 2025 13:35:14 +0000 Subject: [PATCH] Add unit tests for ReadOnlySpan --- MetadataProcessor.Tests/TestNFApp/Program.cs | 5 + .../TestNFApp/TestNFApp.nfproj | 4 +- .../TestNFApp/TestingReadOnlySpan.cs | 121 ++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 MetadataProcessor.Tests/TestNFApp/TestingReadOnlySpan.cs diff --git a/MetadataProcessor.Tests/TestNFApp/Program.cs b/MetadataProcessor.Tests/TestNFApp/Program.cs index 87de2a74..2e6784d5 100644 --- a/MetadataProcessor.Tests/TestNFApp/Program.cs +++ b/MetadataProcessor.Tests/TestNFApp/Program.cs @@ -92,6 +92,11 @@ public static void Main() Console.WriteLine("Span<> tests"); _ = new TestingSpan(); + // testing ReadOnlySpan<> + Console.WriteLine("ReadOnlySpan<> tests"); + _ = new TestingReadOnlySpan(); + + // Console.WriteLine("Exiting TestNFApp"); } diff --git a/MetadataProcessor.Tests/TestNFApp/TestNFApp.nfproj b/MetadataProcessor.Tests/TestNFApp/TestNFApp.nfproj index d130183e..35e252cb 100644 --- a/MetadataProcessor.Tests/TestNFApp/TestNFApp.nfproj +++ b/MetadataProcessor.Tests/TestNFApp/TestNFApp.nfproj @@ -15,6 +15,7 @@ TestNFApp TestNFApp v1.0 + 13.0 @@ -36,6 +37,7 @@ + @@ -50,4 +52,4 @@ - \ No newline at end of file + diff --git a/MetadataProcessor.Tests/TestNFApp/TestingReadOnlySpan.cs b/MetadataProcessor.Tests/TestNFApp/TestingReadOnlySpan.cs new file mode 100644 index 00000000..5ddcf369 --- /dev/null +++ b/MetadataProcessor.Tests/TestNFApp/TestingReadOnlySpan.cs @@ -0,0 +1,121 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using TestNFClassLibrary; + +namespace TestNFApp +{ + public class TestingReadOnlySpan + { + public TestingReadOnlySpan() + { + int length = 3; + + // base type - ReadOnlySpan + Console.WriteLine("+++ ReadOnlySpan tests"); + Console.WriteLine(); + + int[] numbersArray = new int[length]; + for (int i = 0; i < length; i++) + { + numbersArray[i] = i; + } + + ReadOnlySpan numbers = new ReadOnlySpan(numbersArray); + + // Test indexer access (uses ref readonly modreq return type) + for (int i = 0; i < numbers.Length; i++) + { + Console.WriteLine($">>numbers[{i}]: {numbers[i]}"); + } + + // Test Length property + Console.WriteLine($">>Length: {numbers.Length}"); + + // Test IsEmpty property + Console.WriteLine($">>IsEmpty: {numbers.IsEmpty}"); + + // string type - ReadOnlySpan + Console.WriteLine("+++ ReadOnlySpan tests"); + Console.WriteLine(); + + string[] stringsArray = new string[length]; + for (int i = 0; i < length; i++) + { + stringsArray[i] = $"string_{i * 10}"; + } + + ReadOnlySpan strings = new ReadOnlySpan(stringsArray); + + // Test indexer with reference types + for (int i = 0; i < strings.Length; i++) + { + Console.WriteLine($">>strings[{i}]: '{strings[i]}'"); + } + + // class type - ReadOnlySpan + Console.WriteLine("+++ ReadOnlySpan tests"); + Console.WriteLine(); + + ClassOnAnotherAssembly[] classArray = new ClassOnAnotherAssembly[length]; + for (int i = 0; i < length; i++) + { + classArray[i] = new ClassOnAnotherAssembly(i * 100); + } + + ReadOnlySpan spanOfClass = new ReadOnlySpan(classArray); + + // Test indexer with custom class (tests modreq handling) + for (int i = 0; i < spanOfClass.Length; i++) + { + Console.WriteLine($">>spanOfClass[{i}].DummyProperty: '{spanOfClass[i].DummyProperty}'"); + } + + // Test constructor with start and length + Console.WriteLine("+++ ReadOnlySpan with start/length tests"); + Console.WriteLine(); + + ReadOnlySpan subspan = new ReadOnlySpan(numbersArray, 1, 2); + for (int i = 0; i < subspan.Length; i++) + { + Console.WriteLine($">>subspan[{i}]: {subspan[i]}"); + } + + // Test ToArray method + Console.WriteLine("+++ ReadOnlySpan.ToArray() tests"); + Console.WriteLine(); + + int[] copiedArray = numbers.ToArray(); + for (int i = 0; i < copiedArray.Length; i++) + { + Console.WriteLine($">>copiedArray[{i}]: {copiedArray[i]}"); + } + + // Test empty span + Console.WriteLine("+++ Empty ReadOnlySpan tests"); + Console.WriteLine(); + + ReadOnlySpan emptySpan = new ReadOnlySpan(new int[0]); + Console.WriteLine($">>emptySpan.IsEmpty: {emptySpan.IsEmpty}"); + Console.WriteLine($">>emptySpan.Length: {emptySpan.Length}"); + + // Test equality operators + Console.WriteLine("+++ ReadOnlySpan equality tests"); + Console.WriteLine(); + + ReadOnlySpan span1 = new ReadOnlySpan(numbersArray); + ReadOnlySpan span2 = new ReadOnlySpan(numbersArray); + Console.WriteLine($">>span1 == span2: {span1 == span2}"); + Console.WriteLine($">>span1 != span2: {span1 != span2}"); + + // Test implicit conversion from array + Console.WriteLine("+++ Implicit conversion tests"); + Console.WriteLine(); + + ReadOnlySpan implicitSpan = numbersArray; + Console.WriteLine($">>implicitSpan.Length: {implicitSpan.Length}"); + Console.WriteLine($">>implicitSpan[0]: {implicitSpan[0]}"); + } + } +}