-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
ExactSpellingTest.cs
155 lines (127 loc) · 5.98 KB
/
ExactSpellingTest.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Runtime.InteropServices;
using Xunit;
class ExactSpellingTest
{
class Ansi
{
[DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern int Marshal_Int_InOut([In, Out] int intValue);
[DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern int MarshalPointer_Int_InOut([In, Out] ref int intValue);
[DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = false)]
public static extern int Marshal_Int_InOut2([In, Out] int intValue);
[DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = false)]
public static extern int MarshalPointer_Int_InOut2([In, Out] ref int intValue);
}
class Unicode
{
[DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = true)]
public static extern int Marshal_Int_InOut([In, Out] int intValue);
[DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = true)]
public static extern int MarshalPointer_Int_InOut([In, Out] ref int intValue);
[DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = false)]
public static extern int Marshal_Int_InOut2([In, Out] int intValue);
[DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = false)]
public static extern int MarshalPointer_Int_InOut2([In, Out] ref int intValue);
}
class Auto
{
[DllImport("ExactSpellingNative", CharSet = CharSet.Auto, ExactSpelling = false)]
public static extern int Marshal_Int_InOut2([In, Out] int intValue);
}
private static void ExactSpellingTrue()
{
int intManaged = 1000;
int intNative = 2000;
int intReturn = 3000;
Console.WriteLine("Method Unicode.Marshal_Int_InOut: ExactSpelling = true");
int int1 = intManaged;
int intRet1 = Unicode.Marshal_Int_InOut(int1);
Verify(intReturn, intManaged, intRet1, int1);
Console.WriteLine("Method Unicode.MarshalPointer_Int_InOut: ExactSpelling = true");
int int2 = intManaged;
int intRet2 = Unicode.MarshalPointer_Int_InOut(ref int2);
Verify(intReturn, intNative, intRet2, int2);
Console.WriteLine("Method Ansi.Marshal_Int_InOut: ExactSpelling = true");
int int3 = intManaged;
int intRet3 = Ansi.Marshal_Int_InOut(int3);
Verify(intReturn, intManaged, intRet3, int3);
Console.WriteLine("Method Ansi.MarshalPointer_Int_InOut: ExactSpelling = true");
int int4 = intManaged;
int intRet4 = Ansi.MarshalPointer_Int_InOut(ref int4);
Verify(intReturn, intNative, intRet4, int4);
}
private static void ExactSpellingFalse_Windows()
{
int intManaged = 1000;
int intNative = 2000;
int intReturnAnsi = 4000;
int intReturnUnicode = 5000;
Console.WriteLine("Method Unicode.Marshal_Int_InOut2: ExactSpelling = false");
int int5 = intManaged;
int intRet5 = Unicode.Marshal_Int_InOut2(int5);
Verify(intReturnUnicode, intManaged, intRet5, int5);
Console.WriteLine("Method Unicode.MarshalPointer_Int_InOut2: ExactSpelling = false");
int int6 = intManaged;
int intRet6 = Unicode.MarshalPointer_Int_InOut2(ref int6);
Verify(intReturnUnicode, intNative, intRet6, int6);
Console.WriteLine("Method Ansi.Marshal_Int_InOut2: ExactSpelling = false");
int int7 = intManaged;
int intRet7 = Ansi.Marshal_Int_InOut2(int7);
Verify(intReturnAnsi, intManaged, intRet7, int7);
Console.WriteLine("Method Ansi.MarshalPointer_Int_InOut2: ExactSpelling = false");
int int8 = intManaged;
int intRet8 = Ansi.MarshalPointer_Int_InOut2(ref int8);
Verify(intReturnAnsi, intNative, intRet8, int8);
Console.WriteLine("Method Auto.Marshal_Int_InOut: ExactSpelling = false. Verify CharSet.Auto behavior per-platform.");
int int9 = intManaged;
int intRet9 = Auto.Marshal_Int_InOut2(int9);
Verify(intReturnUnicode, intManaged, intRet9, int9);
}
private static void ExactSpellingFalse_NonWindows()
{
int intManaged = 1000;
int intNative = 2000;
Console.WriteLine("Method Unicode.Marshal_Int_InOut2: ExactSpelling = false");
int int5 = intManaged;
Assert.Throws<EntryPointNotFoundException>(() => Unicode.Marshal_Int_InOut2(int5));
Console.WriteLine("Method Unicode.MarshalPointer_Int_InOut2: ExactSpelling = false");
int int6 = intManaged;
Assert.Throws<EntryPointNotFoundException>(() => Unicode.MarshalPointer_Int_InOut2(ref int6));
Console.WriteLine("Method Ansi.Marshal_Int_InOut2: ExactSpelling = false");
int int7 = intManaged;
Assert.Throws<EntryPointNotFoundException>(() => Ansi.Marshal_Int_InOut2(int7));
Console.WriteLine("Method Ansi.MarshalPointer_Int_InOut2: ExactSpelling = false");
int int8 = intManaged;
Assert.Throws<EntryPointNotFoundException>(() => Ansi.MarshalPointer_Int_InOut2(ref int8));
}
public static int Main()
{
try
{
ExactSpellingTrue();
if (OperatingSystem.IsWindows())
{
ExactSpellingFalse_Windows();
}
else
{
ExactSpellingFalse_NonWindows();
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.ToString());
return 101;
}
return 100;
}
private static void Verify(int expectedReturnValue, int expectedParameterValue, int actualReturnValue, int actualParameterValue)
{
Assert.Equal(expectedReturnValue, actualReturnValue);
Assert.Equal(expectedParameterValue, actualParameterValue);
}
}