Change Auto charset to mean UTF-8 off-Windows (#23664)
[platform/upstream/coreclr.git] / tests / src / Interop / PInvoke / ExactSpelling / ExactSpellingTest.cs
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 using System;
6 using System.Runtime.InteropServices;
7
8 class ExactSpellingTest
9 {
10     class Ansi
11     {
12         [DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = true)]
13         public static extern int Marshal_Int_InOut([In, Out] int intValue);
14
15         [DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = true)]
16         public static extern int MarshalPointer_Int_InOut([In, Out] ref int intValue);
17
18         [DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = false)]
19         public static extern int Marshal_Int_InOut2([In, Out] int intValue);
20
21         [DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = false)]
22         public static extern int MarshalPointer_Int_InOut2([In, Out] ref int intValue);
23     }
24
25     class Unicode
26     {
27         [DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = true)]
28         public static extern int Marshal_Int_InOut([In, Out] int intValue);
29
30         [DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = true)]
31         public static extern int MarshalPointer_Int_InOut([In, Out] ref int intValue);
32
33         [DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = false)]
34         public static extern int Marshal_Int_InOut2([In, Out] int intValue);
35
36         [DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = false)]
37         public static extern int MarshalPointer_Int_InOut2([In, Out] ref int intValue);
38     }
39
40     class Auto
41     {
42         [DllImport("ExactSpellingNative", CharSet = CharSet.Auto, ExactSpelling = false)]
43         public static extern int Marshal_Int_InOut2([In, Out] int intValue);
44     }
45
46     public static int Main(string[] args)
47     {
48         int failures = 0;
49         int intManaged = 1000;
50         int intNative = 2000;
51         int intReturn = 3000;
52         
53         Console.WriteLine("Method Unicode.Marshal_Int_InOut: ExactSpelling = true");
54         int int1 = intManaged;
55         int intRet1 = Unicode.Marshal_Int_InOut(int1);
56         failures += Verify(intReturn, intManaged, intRet1, int1);
57         
58         Console.WriteLine("Method Unicode.MarshalPointer_Int_InOut: ExactSpelling = true");
59         int int2 = intManaged;
60         int intRet2 = Unicode.MarshalPointer_Int_InOut(ref int2);
61         
62         failures += Verify(intReturn, intNative, intRet2, int2);
63
64         Console.WriteLine("Method Ansi.Marshal_Int_InOut: ExactSpelling = true");
65         int int3 = intManaged;
66         int intRet3 = Ansi.Marshal_Int_InOut(int3);
67         failures += Verify(intReturn, intManaged, intRet3, int3);
68
69         Console.WriteLine("Method Ansi.MarshalPointer_Int_InOut: ExactSpelling = true");
70         int int4 = intManaged;
71         int intRet4 = Ansi.MarshalPointer_Int_InOut(ref int4);
72         failures += Verify(intReturn, intNative, intRet4, int4);
73
74         int intReturnAnsi = 4000;
75         int intReturnUnicode = 5000;
76
77         Console.WriteLine("Method Unicode.Marshal_Int_InOut2: ExactSpelling = false");
78         int int5 = intManaged;
79         int intRet5 = Unicode.Marshal_Int_InOut2(int5);
80         failures += Verify(intReturnUnicode, intManaged, intRet5, int5);
81         
82         Console.WriteLine("Method Unicode.MarshalPointer_Int_InOut2: ExactSpelling = false");
83         int int6 = intManaged;
84         int intRet6 = Unicode.MarshalPointer_Int_InOut2(ref int6);
85         failures += Verify(intReturnUnicode, intNative, intRet6, int6);
86
87         Console.WriteLine("Method Ansi.Marshal_Int_InOut2: ExactSpelling = false");
88         int int7 = intManaged;
89         int intRet7 = Ansi.Marshal_Int_InOut2(int7);
90         failures += Verify(intReturnAnsi, intManaged, intRet7, int7);
91
92         Console.WriteLine("Method Ansi.MarshalPointer_Int_InOut2: ExactSpelling = false");
93         int int8 = intManaged;
94         int intRet8 = Ansi.MarshalPointer_Int_InOut2(ref int8);
95         failures += Verify(intReturnAnsi, intNative, intRet8, int8);
96
97         Console.WriteLine("Method Auto.Marshal_Int_InOut: ExactSpelling = false. Verify CharSet.Auto behavior per-platform.");
98         int int9 = intManaged;
99         int intRet9 = Auto.Marshal_Int_InOut2(int9);
100 #if PLATFORM_WINDOWS
101         failures += Verify(intReturnUnicode, intManaged, intRet9, int9);
102 #else
103         failures += Verify(intReturnAnsi, intManaged, intRet9, int9);
104 #endif
105         
106         return 100 + failures;
107     }
108
109     private static int Verify(int expectedReturnValue, int expectedParameterValue, int actualReturnValue, int actualParameterValue)
110     {
111         int failures = 0;
112         if (expectedReturnValue != actualReturnValue)
113         {
114             failures++;
115             Console.WriteLine($"The return value is wrong. Expected {expectedReturnValue}, got {actualReturnValue}");
116         }
117         if (expectedParameterValue != actualParameterValue)
118         {
119             failures++;
120             Console.WriteLine($"The parameter value is changed. Expected {expectedParameterValue}, got {actualParameterValue}");
121         }
122
123         return failures;
124     }
125 }