Skip to content

Commit f2eb52c

Browse files
committed
Added NullableExtensions tests
1 parent 23380dd commit f2eb52c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
// See the LICENSE file in the project root for more information.
4+
5+
#if NET5_0
6+
7+
using System.Numerics;
8+
using Microsoft.Toolkit.HighPerformance.Extensions;
9+
using Microsoft.VisualStudio.TestTools.UnitTesting;
10+
11+
namespace UnitTests.HighPerformance.Extensions
12+
{
13+
[TestClass]
14+
public class Test_NullableExtensions
15+
{
16+
[TestCategory("NullableExtensions")]
17+
[TestMethod]
18+
public void Test_NullableExtensions_DangerousGetReference()
19+
{
20+
static void Test<T>(T before, T after)
21+
where T : struct
22+
{
23+
T? nullable = before;
24+
ref T reference = ref nullable.DangerousGetValueOrDefaultReference();
25+
26+
Assert.AreEqual(nullable.Value, before);
27+
28+
reference = after;
29+
30+
Assert.AreEqual(nullable.Value, after);
31+
}
32+
33+
Test(0, 42);
34+
Test(1.3f, 3.14f);
35+
Test(0.555, 8.49);
36+
Test(Vector4.Zero, new Vector4(1, 5.55f, 2, 3.14f));
37+
Test(Matrix4x4.Identity, Matrix4x4.CreateOrthographic(35, 88.34f, 9.99f, 24.6f));
38+
}
39+
}
40+
}
41+
42+
#endif

UnitTests/UnitTests.HighPerformance.Shared/UnitTests.HighPerformance.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_BoolExtensions.cs" />
2929
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_HashCodeExtensions.cs" />
3030
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_IBufferWriterExtensions.cs" />
31+
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_NullableExtensions.cs" />
3132
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_StreamExtensions.cs" />
3233
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_SpanExtensions.cs" />
3334
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_ReadOnlySpanExtensions.Count.cs" />

0 commit comments

Comments
 (0)