forked from castleproject/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByRefLikeReferenceTestCase.cs
More file actions
151 lines (128 loc) · 4.41 KB
/
Copy pathByRefLikeReferenceTestCase.cs
File metadata and controls
151 lines (128 loc) · 4.41 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2004-2026 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#if FEATURE_BYREFLIKE
#nullable enable
#pragma warning disable CS8500
namespace Castle.DynamicProxy.Tests.ByRefLikeSupport
{
using System;
#if NET9_0_OR_GREATER
using System.Runtime.CompilerServices;
#endif
using NUnit.Framework;
/// <summary>
/// Tests for the substitute types used by DynamicProxy to implement byref-like parameter and return type support.
/// </summary>
[TestFixture]
public class ByRefLikeReferenceTestCase
{
#region `ByRefLikeReference`
[Test]
public unsafe void Ctor_throws_if_non_by_ref_like_type()
{
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
bool local = default;
_ = new ByRefLikeReference(typeof(bool), &local);
});
}
[Test]
public unsafe void Ctor_succeeds_if_by_ref_like_type()
{
ReadOnlySpan<char> local = default;
_ = new ByRefLikeReference(typeof(ReadOnlySpan<char>), &local);
}
[Test]
public unsafe void Invalidate_throws_if_address_mismatch()
{
ReadOnlySpan<char> local = default;
var reference = new ByRefLikeReference(typeof(ReadOnlySpan<char>), &local);
Assert.Throws<AccessViolationException>(() =>
{
ReadOnlySpan<char> otherLocal = default;
reference.Invalidate(&otherLocal);
});
}
[Test]
public unsafe void Invalidate_succeeds_if_address_match()
{
ReadOnlySpan<char> local = default;
var reference = new ByRefLikeReference(typeof(ReadOnlySpan<char>), &local);
reference.Invalidate(&local);
}
[Test]
public unsafe void GetPtr_throws_if_type_mismatch()
{
ReadOnlySpan<char> local = default;
var reference = new ByRefLikeReference(typeof(ReadOnlySpan<char>), &local);
Assert.Throws<AccessViolationException>(() => reference.GetPtr(typeof(bool)));
}
[Test]
public unsafe void GetPtr_returns_ctor_address_if_type_match()
{
ReadOnlySpan<char> local = default;
var reference = new ByRefLikeReference(typeof(ReadOnlySpan<char>), &local);
var ptr = reference.GetPtr(typeof(ReadOnlySpan<char>));
Assert.True(ptr == &local);
}
[Test]
public unsafe void GetPtr_throws_after_Invalidate()
{
ReadOnlySpan<char> local = default;
var reference = new ByRefLikeReference(typeof(ReadOnlySpan<char>), &local);
reference.Invalidate(&local);
Assert.Throws<AccessViolationException>(() => reference.GetPtr(typeof(ReadOnlySpan<char>)));
}
#endregion
#region `ReadOnlySpanReference<T>`
// We do not repeat the above tests for `ReadOnlySpanReference<T>`
// since it inherits the tested methods from `ByRefLikeReference`.
public unsafe void ReadOnlySpanReference_ctor_throws_if_type_mismatch()
{
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
ReadOnlySpan<bool> local = default;
_ = new ReadOnlySpanReference<char>(typeof(ReadOnlySpan<bool>), &local);
});
}
public unsafe void ReadOnlySpanReference_Value_returns_equal_span()
{
ReadOnlySpan<char> local = "foo".AsSpan();
var reference = new ReadOnlySpanReference<char>(typeof(ReadOnlySpan<char>), &local);
Assert.True(reference.Value == "foo".AsSpan());
}
#if NET9_0_OR_GREATER
[Test]
public unsafe void ReadOnlySpanReference_Value_returns_same_span()
{
ReadOnlySpan<char> local = "foo".AsSpan();
var reference = new ReadOnlySpanReference<char>(typeof(ReadOnlySpan<char>), &local);
Assert.True(Unsafe.AreSame(ref reference.Value, ref local));
}
#endif
[Test]
public unsafe void ReadOnlySpanReference_Value_can_update_original()
{
ReadOnlySpan<char> local = "foo".AsSpan();
var reference = new ReadOnlySpanReference<char>(typeof(ReadOnlySpan<char>), &local);
reference.Value = "bar".AsSpan();
Assert.True(local == "bar".AsSpan());
}
#endregion
// We do not test `ByRefLikeReference<TByRefLike>` and `SpanReference<T>`
// since these two types are practically identical to `ReadOnlySpanReference<T>`.
}
}
#pragma warning restore CS8500
#endif