-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Program.cs
244 lines (209 loc) · 9.37 KB
/
Program.cs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace NetClient
{
using System;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using TestLibrary;
using Xunit;
using Server.Contract;
using Server.Contract.Servers;
class Program
{
static void Validate_Numeric_In_ReturnByRef()
{
var dispatchTesting = (DispatchTesting)new DispatchTestingClass();
byte b1 = 1;
byte b2 = b1;
short s1 = -1;
short s2 = s1;
ushort us1 = 1;
ushort us2 = us1;
int i1 = -1;
int i2 = i1;
uint ui1 = 1;
uint ui2 = ui1;
long l1 = -1;
long l2 = l1;
ulong ul1 = 1;
ulong ul2 = ul1;
Console.WriteLine($"Calling {nameof(DispatchTesting.DoubleNumeric_ReturnByRef)} ...");
dispatchTesting.DoubleNumeric_ReturnByRef (
b1, ref b2,
s1, ref s2,
us1, ref us2,
i1, ref i2,
ui1, ref ui2,
l1, ref l2,
ul1, ref ul2);
Console.WriteLine($"Call to {nameof(DispatchTesting.DoubleNumeric_ReturnByRef)} complete");
Assert.Equal(b1 * 2, b2);
Assert.Equal(s1 * 2, s2);
Assert.Equal(us1 * 2, us2);
Assert.Equal(i1 * 2, i2);
Assert.Equal(ui1 * 2, ui2);
Assert.Equal(l1 * 2, l2);
Assert.Equal(ul1 * 2, ul2);
}
static private bool EqualByBound(float expected, float actual)
{
float low = expected - 0.0001f;
float high = expected + 0.0001f;
float eps = Math.Abs(expected - actual);
return eps < float.Epsilon || (low < actual && actual < high);
}
static private bool EqualByBound(double expected, double actual)
{
double low = expected - 0.00001;
double high = expected + 0.00001;
double eps = Math.Abs(expected - actual);
return eps < double.Epsilon || (low < actual && actual < high);
}
static void Validate_Float_In_ReturnAndUpdateByRef()
{
var dispatchTesting = (DispatchTesting)new DispatchTestingClass();
float a = .1f;
float b = .2f;
float expected = a + b;
Console.WriteLine($"Calling {nameof(DispatchTesting.Add_Float_ReturnAndUpdateByRef)} ...");
float c = b;
float d = dispatchTesting.Add_Float_ReturnAndUpdateByRef (a, ref c);
Console.WriteLine($"Call to {nameof(DispatchTesting.Add_Float_ReturnAndUpdateByRef)} complete: {a} + {b} = {d}; {c} == {d}");
Assert.True(EqualByBound(expected, c));
Assert.True(EqualByBound(expected, d));
}
static void Validate_Double_In_ReturnAndUpdateByRef()
{
var dispatchTesting = (DispatchTesting)new DispatchTestingClass();
double a = .1;
double b = .2;
double expected = a + b;
Console.WriteLine($"Calling {nameof(DispatchTesting.Add_Double_ReturnAndUpdateByRef)} ...");
double c = b;
double d = dispatchTesting.Add_Double_ReturnAndUpdateByRef (a, ref c);
Console.WriteLine($"Call to {nameof(DispatchTesting.Add_Double_ReturnAndUpdateByRef)} complete: {a} + {b} = {d}; {c} == {d}");
Assert.True(EqualByBound(expected, c));
Assert.True(EqualByBound(expected, d));
}
static int GetErrorCodeFromHResult(int hresult)
{
// https://msdn.microsoft.com/en-us/library/cc231198.aspx
return hresult & 0xffff;
}
static void Validate_Exception()
{
var dispatchTesting = (DispatchTesting)new DispatchTestingClass();
int errorCode = 127;
string resultString = errorCode.ToString("x");
try
{
Console.WriteLine($"Calling {nameof(DispatchTesting.TriggerException)} with {nameof(IDispatchTesting_Exception.Disp)} {errorCode}...");
dispatchTesting.TriggerException(IDispatchTesting_Exception.Disp, errorCode);
Assert.True(false, "DISP exception not thrown properly");
}
catch (COMException e)
{
Assert.Equal(GetErrorCodeFromHResult(e.HResult), errorCode);
Assert.Equal(e.Message, resultString);
}
try
{
Console.WriteLine($"Calling {nameof(DispatchTesting.TriggerException)} with {nameof(IDispatchTesting_Exception.HResult)} {errorCode}...");
dispatchTesting.TriggerException(IDispatchTesting_Exception.HResult, errorCode);
Assert.True(false, "HRESULT exception not thrown properly");
}
catch (COMException e)
{
Assert.Equal(GetErrorCodeFromHResult(e.HResult), errorCode);
// Failing HRESULT exceptions contain CLR generated messages
}
// Calling methods through IDispatch::Invoke() (i.e., late-bound) doesn't
// propagate the HRESULT when marked with PreserveSig. It is always 0.
{
Console.WriteLine($"Calling {nameof(DispatchTesting.TriggerException)} (PreserveSig) with {nameof(IDispatchTesting_Exception.Int)} {errorCode}...");
var dispatchTesting2 = (IDispatchTestingPreserveSig1)dispatchTesting;
Assert.Equal(0, dispatchTesting2.TriggerException(IDispatchTesting_Exception.Int, errorCode));
}
{
// Validate the HRESULT as a value type construct works for IDispatch.
Console.WriteLine($"Calling {nameof(DispatchTesting.TriggerException)} (PreserveSig, ValueType) with {nameof(IDispatchTesting_Exception.Int)} {errorCode}...");
var dispatchTesting3 = (IDispatchTestingPreserveSig2)dispatchTesting;
Assert.Equal(0, dispatchTesting3.TriggerException(IDispatchTesting_Exception.Int, errorCode).Value);
}
}
static void Validate_StructNotSupported()
{
Console.WriteLine($"IDispatch with structs not supported...");
var dispatchTesting = (DispatchTesting)new DispatchTestingClass();
var input = new HFA_4() { x = 1f, y = 2f, z = 3f, w = 4f };
Assert.Throws<NotSupportedException>(() => dispatchTesting.DoubleHVAValues(ref input));
}
static void Validate_LCID_Marshaled()
{
var dispatchTesting = (DispatchTesting)new DispatchTestingClass();
CultureInfo oldCulture = CultureInfo.CurrentCulture;
CultureInfo newCulture = new CultureInfo("es-ES", false);
try
{
CultureInfo englishCulture = new CultureInfo("en-US", false);
CultureInfo.CurrentCulture = newCulture;
int lcid = dispatchTesting.PassThroughLCID();
Assert.Equal(englishCulture.LCID, lcid); // CLR->Dispatch LCID marshalling is explicitly hardcoded to en-US instead of passing the current culture.
}
finally
{
CultureInfo.CurrentCulture = oldCulture;
}
}
static void Validate_Enumerator()
{
var dispatchTesting = (DispatchTesting)new DispatchTestingClass();
var expected = System.Linq.Enumerable.Range(0, 10);
Console.WriteLine($"Calling {nameof(DispatchTesting.GetEnumerator)} ...");
var enumerator = dispatchTesting.GetEnumerator();
AssertExtensions.CollectionEqual(expected, GetEnumerable(enumerator));
enumerator.Reset();
AssertExtensions.CollectionEqual(expected, GetEnumerable(enumerator));
Console.WriteLine($"Calling {nameof(DispatchTesting.ExplicitGetEnumerator)} ...");
var enumeratorExplicit = dispatchTesting.ExplicitGetEnumerator();
AssertExtensions.CollectionEqual(expected, GetEnumerable(enumeratorExplicit));
enumeratorExplicit.Reset();
AssertExtensions.CollectionEqual(expected, GetEnumerable(enumeratorExplicit));
System.Collections.Generic.IEnumerable<int> GetEnumerable(System.Collections.IEnumerator e)
{
var list = new System.Collections.Generic.List<int>();
while (e.MoveNext())
{
list.Add((int)e.Current);
}
return list;
}
}
static int Main()
{
// RegFree COM is not supported on Windows Nano
if (Utilities.IsWindowsNanoServer)
{
return 100;
}
try
{
Validate_Numeric_In_ReturnByRef();
Validate_Float_In_ReturnAndUpdateByRef();
Validate_Double_In_ReturnAndUpdateByRef();
Validate_Exception();
Validate_StructNotSupported();
Validate_LCID_Marshaled();
Validate_Enumerator();
}
catch (Exception e)
{
Console.WriteLine($"Test Failure: {e}");
return 101;
}
return 100;
}
}
}