-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathTest_NullableRef{T}.cs
More file actions
79 lines (61 loc) · 2.14 KB
/
Test_NullableRef{T}.cs
File metadata and controls
79 lines (61 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#if NET8_0_OR_GREATER
using System;
using System.Runtime.CompilerServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CommunityToolkit.HighPerformance.UnitTests;
[TestClass]
public class Test_NullableRefOfT
{
[TestMethod]
public void Test_NullableRefOfT_CreateNullableRefOfT_Ok()
{
int value = 1;
NullableRef<int> reference = new(ref value);
Assert.IsTrue(reference.HasValue);
Assert.IsTrue(Unsafe.AreSame(ref value, ref reference.Value));
reference.Value++;
Assert.AreEqual(value, 2);
}
[TestMethod]
public void Test_NullableRefOfT_CreateNullableRefOfT_Null()
{
Assert.IsFalse(default(NullableRef<int>).HasValue);
Assert.IsFalse(NullableRef<int>.Null.HasValue);
Assert.IsFalse(default(NullableRef<string>).HasValue);
Assert.IsFalse(NullableRef<string>.Null.HasValue);
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void Test_NullableRefOfT_CreateNullableRefOfT_Null_Exception()
{
NullableRef<int> reference = default;
_ = reference.Value;
}
[TestMethod]
public void Test_NullableRefOfT_CreateNullableRefOfT_ImplicitRefCast()
{
int value = 42;
Ref<int> reference = new(ref value);
NullableRef<int> nullableRef = reference;
Assert.IsTrue(nullableRef.HasValue);
Assert.IsTrue(Unsafe.AreSame(ref reference.Value, ref nullableRef.Value));
}
[TestMethod]
public void Test_NullableRefOfT_CreateNullableRefOfT_ExplicitCastOfT()
{
int value = 42;
NullableRef<int> reference = new(ref value);
Assert.AreEqual(value, (int)reference);
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void Test_NullableRefOfT_CreateNullableRefOfT_ExplicitCastOfT_Exception()
{
NullableRef<int> invalid = default;
_ = (int)invalid;
}
}
#endif