Skip to content

Commit 644a412

Browse files
committed
Add unit tests for ReadOnlySpan
1 parent a2835e9 commit 644a412

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

MetadataProcessor.Tests/TestNFApp/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public static void Main()
9292
Console.WriteLine("Span<> tests");
9393
_ = new TestingSpan();
9494

95+
// testing ReadOnlySpan<>
96+
Console.WriteLine("ReadOnlySpan<> tests");
97+
_ = new TestingReadOnlySpan();
98+
99+
//
95100
Console.WriteLine("Exiting TestNFApp");
96101
}
97102

MetadataProcessor.Tests/TestNFApp/TestNFApp.nfproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<RootNamespace>TestNFApp</RootNamespace>
1616
<AssemblyName>TestNFApp</AssemblyName>
1717
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
<LangVersion>13.0</LangVersion>
1819
</PropertyGroup>
1920
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2021
<ItemGroup>
@@ -36,6 +37,7 @@
3637
<Compile Include="OneClassOverAll.cs" />
3738
<Compile Include="Program.cs" />
3839
<Compile Include="Properties\AssemblyInfo.cs" />
40+
<Compile Include="TestingReadOnlySpan.cs" />
3941
<Compile Include="TestingSpan.cs" />
4042
<Compile Include="TestingDestructors.cs" />
4143
<Compile Include="TestingDelegates.cs" />
@@ -50,4 +52,4 @@
5052
<ProjectConfigurationsDeclaredAsItems />
5153
</ProjectCapabilities>
5254
</ProjectExtensions>
53-
</Project>
55+
</Project>
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using TestNFClassLibrary;
6+
7+
namespace TestNFApp
8+
{
9+
public class TestingReadOnlySpan
10+
{
11+
public TestingReadOnlySpan()
12+
{
13+
int length = 3;
14+
15+
// base type - ReadOnlySpan<int>
16+
Console.WriteLine("+++ ReadOnlySpan<int> tests");
17+
Console.WriteLine();
18+
19+
int[] numbersArray = new int[length];
20+
for (int i = 0; i < length; i++)
21+
{
22+
numbersArray[i] = i;
23+
}
24+
25+
ReadOnlySpan<int> numbers = new ReadOnlySpan<int>(numbersArray);
26+
27+
// Test indexer access (uses ref readonly modreq return type)
28+
for (int i = 0; i < numbers.Length; i++)
29+
{
30+
Console.WriteLine($">>numbers[{i}]: {numbers[i]}");
31+
}
32+
33+
// Test Length property
34+
Console.WriteLine($">>Length: {numbers.Length}");
35+
36+
// Test IsEmpty property
37+
Console.WriteLine($">>IsEmpty: {numbers.IsEmpty}");
38+
39+
// string type - ReadOnlySpan<string>
40+
Console.WriteLine("+++ ReadOnlySpan<string> tests");
41+
Console.WriteLine();
42+
43+
string[] stringsArray = new string[length];
44+
for (int i = 0; i < length; i++)
45+
{
46+
stringsArray[i] = $"string_{i * 10}";
47+
}
48+
49+
ReadOnlySpan<string> strings = new ReadOnlySpan<string>(stringsArray);
50+
51+
// Test indexer with reference types
52+
for (int i = 0; i < strings.Length; i++)
53+
{
54+
Console.WriteLine($">>strings[{i}]: '{strings[i]}'");
55+
}
56+
57+
// class type - ReadOnlySpan<ClassOnAnotherAssembly>
58+
Console.WriteLine("+++ ReadOnlySpan<ClassOnAnotherAssembly> tests");
59+
Console.WriteLine();
60+
61+
ClassOnAnotherAssembly[] classArray = new ClassOnAnotherAssembly[length];
62+
for (int i = 0; i < length; i++)
63+
{
64+
classArray[i] = new ClassOnAnotherAssembly(i * 100);
65+
}
66+
67+
ReadOnlySpan<ClassOnAnotherAssembly> spanOfClass = new ReadOnlySpan<ClassOnAnotherAssembly>(classArray);
68+
69+
// Test indexer with custom class (tests modreq handling)
70+
for (int i = 0; i < spanOfClass.Length; i++)
71+
{
72+
Console.WriteLine($">>spanOfClass[{i}].DummyProperty: '{spanOfClass[i].DummyProperty}'");
73+
}
74+
75+
// Test constructor with start and length
76+
Console.WriteLine("+++ ReadOnlySpan with start/length tests");
77+
Console.WriteLine();
78+
79+
ReadOnlySpan<int> subspan = new ReadOnlySpan<int>(numbersArray, 1, 2);
80+
for (int i = 0; i < subspan.Length; i++)
81+
{
82+
Console.WriteLine($">>subspan[{i}]: {subspan[i]}");
83+
}
84+
85+
// Test ToArray method
86+
Console.WriteLine("+++ ReadOnlySpan.ToArray() tests");
87+
Console.WriteLine();
88+
89+
int[] copiedArray = numbers.ToArray();
90+
for (int i = 0; i < copiedArray.Length; i++)
91+
{
92+
Console.WriteLine($">>copiedArray[{i}]: {copiedArray[i]}");
93+
}
94+
95+
// Test empty span
96+
Console.WriteLine("+++ Empty ReadOnlySpan tests");
97+
Console.WriteLine();
98+
99+
ReadOnlySpan<int> emptySpan = new ReadOnlySpan<int>(new int[0]);
100+
Console.WriteLine($">>emptySpan.IsEmpty: {emptySpan.IsEmpty}");
101+
Console.WriteLine($">>emptySpan.Length: {emptySpan.Length}");
102+
103+
// Test equality operators
104+
Console.WriteLine("+++ ReadOnlySpan equality tests");
105+
Console.WriteLine();
106+
107+
ReadOnlySpan<int> span1 = new ReadOnlySpan<int>(numbersArray);
108+
ReadOnlySpan<int> span2 = new ReadOnlySpan<int>(numbersArray);
109+
Console.WriteLine($">>span1 == span2: {span1 == span2}");
110+
Console.WriteLine($">>span1 != span2: {span1 != span2}");
111+
112+
// Test implicit conversion from array
113+
Console.WriteLine("+++ Implicit conversion tests");
114+
Console.WriteLine();
115+
116+
ReadOnlySpan<int> implicitSpan = numbersArray;
117+
Console.WriteLine($">>implicitSpan.Length: {implicitSpan.Length}");
118+
Console.WriteLine($">>implicitSpan[0]: {implicitSpan[0]}");
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)