Add PInvoke/SizeParamIndex tests (#19348)
[platform/upstream/coreclr.git] / tests / src / Interop / PInvoke / SizeParamIndex / PInvoke / PassingByRef / PassingByRefTest.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 using TestLibrary;
8
9 /// <summary>
10 ///  Pass LPArray Size by ref keyword using SizeParamIndex Attributes
11 /// </summary>
12
13 public class ClientMarshalArrayAsSizeParamIndexByRefTest
14 {
15
16     #region ByRef
17
18     [DllImport("PInvokePassingByRefNative")]
19     private static extern bool MarshalCStyleArrayByte_AsByRef_AsSizeParamIndex(
20         ref byte arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref byte[] arrByte);
21
22     [DllImport("PInvokePassingByRefNative")]
23     private static extern bool MarshalCStyleArraySbyte_AsByRef_AsSizeParamIndex(
24         ref sbyte arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref sbyte[] arrSbyte);
25
26     [DllImport("PInvokePassingByRefNative")]
27     private static extern bool MarshalCStyleArrayShort_AsByRef_AsSizeParamIndex(
28         ref short arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref short[] arrShort);
29
30     [DllImport("PInvokePassingByRefNative")]
31     private static extern bool MarshalCStyleArrayShortReturnNegative_AsByRef_AsSizeParamIndex(
32         ref short arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref short[] arrShort);
33
34     [DllImport("PInvokePassingByRefNative")]
35     private static extern bool MarshalCStyleArrayUshort_AsByRef_AsSizeParamIndex(
36         [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] ref ushort[] arrUshort, ref ushort arrSize);
37
38     [DllImport("PInvokePassingByRefNative")]
39     private static extern bool MarshalCStyleArrayInt_AsByRef_AsSizeParamIndex(
40         ref Int32 arrSize, Int32 unused, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref Int32[] arrInt32);
41
42     [DllImport("PInvokePassingByRefNative")]
43     private static extern bool MarshalCStyleArrayUInt_AsByRef_AsSizeParamIndex(
44          [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] ref UInt32[] arrUInt32, UInt32 unused, ref UInt32 arrSize);
45
46     [DllImport("PInvokePassingByRefNative")]
47     private static extern bool MarshalCStyleArrayLong_AsByRef_AsSizeParamIndex(
48         ref long arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref long[] arrLong);
49
50     [DllImport("PInvokePassingByRefNative")]
51     private static extern bool MarshalCStyleArrayUlong_AsByRef_AsSizeParamIndex(
52         ref ulong arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref ulong[] arrUlong);
53
54     [DllImport("PInvokePassingByRefNative")]
55     private static extern bool MarshalCStyleArrayString_AsByRef_AsSizeParamIndex(
56         ref int arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0, ArraySubType = UnmanagedType.BStr)] ref string[] arrStr,
57         [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0, ArraySubType = UnmanagedType.LPStr)] ref string[] arrStr2);
58
59     #endregion
60
61     static void SizeParamTypeIsByte()
62     {
63         string strDescription = "Scenario(byte==>BYTE):Array_Size(M->N)=1,Array_Size(N->M)= byte.MinValue";
64         Console.WriteLine();
65         Console.WriteLine(strDescription + " Starts!");
66
67         byte byte_Array_Size = 1;
68         byte[] arrByte = Helper.InitArray<byte>(byte_Array_Size);
69         Assert.IsTrue(MarshalCStyleArrayByte_AsByRef_AsSizeParamIndex(ref byte_Array_Size, ref arrByte));
70
71         //Construct Expected array
72         int expected_ByteArray_Size = Byte.MinValue;
73         byte[] expectedArrByte = Helper.GetExpChangeArray<byte>(expected_ByteArray_Size);
74         Assert.IsTrue(Helper.EqualArray<byte>(arrByte, (int)byte_Array_Size, expectedArrByte, (int)expectedArrByte.Length));
75
76         Console.WriteLine(strDescription + " Ends!");
77     }
78
79     static void SizeParamTypeIsSByte()
80     {
81         string strDescription = "Scenario(sbyte==>CHAR): Array_Size(M->N) = 10, Array_Size(N->M) = sbyte.Max";
82         Console.WriteLine();
83         Console.WriteLine(strDescription + " Starts!");
84
85         sbyte sbyte_Array_Size = (sbyte)10;
86         sbyte[] arrSbyte = Helper.InitArray<sbyte>(sbyte_Array_Size);
87
88         Assert.IsTrue(MarshalCStyleArraySbyte_AsByRef_AsSizeParamIndex(ref sbyte_Array_Size, ref arrSbyte));
89
90         //Construct Expected
91         sbyte[] expectedArrSbyte = Helper.GetExpChangeArray<sbyte>(sbyte.MaxValue);
92         Assert.IsTrue(Helper.EqualArray<sbyte>(arrSbyte, (int)sbyte_Array_Size, expectedArrSbyte, (int)sbyte.MaxValue));
93
94         Console.WriteLine(strDescription + " Ends!");
95     }
96
97     static void SizeParamTypeIsShort1()
98     {
99         string strDescription = "Scenario(short==>SHORT)1: Array_Size(M->N) = -1, Array_Size(N->M) = 20";
100         Console.WriteLine();
101         Console.WriteLine(strDescription + " Starts!");
102
103         short short_Array_Size = (short)-1;
104         short[] arrShort = Helper.InitArray<short>(10);
105         int expected_ByteArraySize = 20;
106
107         Assert.IsTrue(MarshalCStyleArrayShort_AsByRef_AsSizeParamIndex(ref short_Array_Size, ref arrShort));
108
109         //Construct Expected
110         short[] expectedArrShort = Helper.GetExpChangeArray<short>(expected_ByteArraySize);
111         Assert.IsTrue(Helper.EqualArray<short>(arrShort, (int)short_Array_Size, expectedArrShort, expectedArrShort.Length));
112
113         Console.WriteLine(strDescription + " Ends!");
114     }
115
116     static void SizeParamTypeIsShort2()
117     {
118         string strDescription = "Scenario(short==>SHORT)2: Array_Size(M->N) = 10, Array_Size(N->M) = -1";
119         Console.WriteLine();
120         Console.WriteLine(strDescription + " Starts!");
121
122         short short_Array_Size = (short)10;
123         short[] arrShort = Helper.InitArray<short>(10);
124         Assert.Throws<OverflowException>(() => MarshalCStyleArrayShortReturnNegative_AsByRef_AsSizeParamIndex(ref short_Array_Size, ref arrShort));
125         Console.WriteLine(strDescription + " Ends!");
126     }
127
128     static void SizeParamTypeIsUShort()
129     {
130         string strDescription = "Scenario(ushort==>USHORT): Array_Size(M->N) = 0, Array_Size(N->M) = ushort.MaxValue";
131         Console.WriteLine();
132         Console.WriteLine(strDescription + " Starts!");
133
134         ushort ushort_Array_Size = 20;
135         ushort[] arrUshort = Helper.InitArray<ushort>(ushort_Array_Size);
136
137         int expected_UshortArraySize = ushort.MaxValue;
138         Assert.IsTrue(MarshalCStyleArrayUshort_AsByRef_AsSizeParamIndex(ref arrUshort, ref ushort_Array_Size));
139
140         //Construct Expected
141         ushort[] expectedArrShort = Helper.GetExpChangeArray<ushort>(expected_UshortArraySize);
142         Assert.IsTrue(Helper.EqualArray<ushort>(arrUshort, (int)ushort_Array_Size, expectedArrShort, expectedArrShort.Length));
143
144         Console.WriteLine(strDescription + " Ends!");
145     }
146
147     static void SizeParamTypeIsInt32()
148     {
149         string strDescription = "Scenario(Int32==>LONG):Array_Size(M->N)=10, Array_Size(N->M)=1";
150         Console.WriteLine();
151         Console.WriteLine(strDescription + " Starts!");
152
153         Int32 Int32_Array_Size = (Int32)10;
154         Int32[] arrInt32 = Helper.InitArray<Int32>(Int32_Array_Size);
155
156         Assert.IsTrue(MarshalCStyleArrayInt_AsByRef_AsSizeParamIndex(ref Int32_Array_Size, Int32.MaxValue, ref arrInt32));
157
158         //Construct Expected
159         int expected_UshortArraySize = 1;
160         Int32[] expectedArrInt32 = Helper.GetExpChangeArray<Int32>(expected_UshortArraySize);
161         Assert.IsTrue(Helper.EqualArray<Int32>(arrInt32, Int32_Array_Size, expectedArrInt32, expectedArrInt32.Length));
162
163         Console.WriteLine(strDescription + " Ends!");
164     }
165
166     static void SizeParamTypeIsUInt32()
167     {
168         string strDescription = "Scenario(UInt32==>ULONG):Array_Size(M->N)=1234,Array_Size(N->M)=4321";
169         Console.WriteLine();
170         Console.WriteLine(strDescription + " Starts!");
171
172         UInt32 UInt32_Array_Size = (UInt32)1234;
173         UInt32[] arrUInt32 = Helper.InitArray<UInt32>((Int32)UInt32_Array_Size);
174         Assert.IsTrue(MarshalCStyleArrayUInt_AsByRef_AsSizeParamIndex(ref arrUInt32, 1234, ref UInt32_Array_Size));
175
176         //Construct Expected
177         int expected_UInt32ArraySize = 4321;
178         UInt32[] expectedArrUInt32 = Helper.GetExpChangeArray<UInt32>(expected_UInt32ArraySize);
179         Assert.IsTrue(Helper.EqualArray<UInt32>(arrUInt32, (Int32)UInt32_Array_Size, expectedArrUInt32, (Int32)expectedArrUInt32.Length));
180
181         Console.WriteLine(strDescription + " Ends!");
182     }
183
184     static void SizeParamTypeIsLong()
185     {
186         string strDescription = "Scenario(long==>LONGLONG):Array_Size(M->N)=10,Array_Size(N->M)=20";
187         Console.WriteLine();
188         Console.WriteLine(strDescription + " Starts!");
189
190         long long_Array_Size = (long)10;
191         long[] arrLong = Helper.InitArray<long>((Int32)long_Array_Size);
192         Assert.IsTrue(MarshalCStyleArrayLong_AsByRef_AsSizeParamIndex(ref long_Array_Size, ref arrLong));
193
194         //Construct Expected Array
195         int expected_LongArraySize = 20;
196         long[] expectedArrLong = Helper.GetExpChangeArray<long>(expected_LongArraySize);
197         Assert.IsTrue(Helper.EqualArray<long>(arrLong, (Int32)long_Array_Size, expectedArrLong, (Int32)expectedArrLong.Length));
198
199         Console.WriteLine(strDescription + " Ends!");
200     }
201
202     static void SizeParamTypeIsULong()
203     {
204         string strDescription = "Scenario(ulong==>ULONGLONG):Array_Size(M->N)=0, Array_Size(N->M)=0";
205         Console.WriteLine();
206         Console.WriteLine(strDescription + " Starts!");
207
208         ulong ulong_Array_Size = (ulong)0;
209         ulong[] arrUlong = Helper.InitArray<ulong>((Int32)ulong_Array_Size);
210
211         Assert.IsTrue(MarshalCStyleArrayUlong_AsByRef_AsSizeParamIndex(ref ulong_Array_Size, ref arrUlong));
212
213         //Construct Expected
214         int expected_ULongArraySize = 0;
215         ulong[] expectedArrUlong = Helper.GetExpChangeArray<ulong>(expected_ULongArraySize);
216         Assert.IsTrue(Helper.EqualArray<ulong>(arrUlong, (Int32)ulong_Array_Size, expectedArrUlong, (Int32)expectedArrUlong.Length));
217
218         Console.WriteLine(strDescription + " Ends!");
219     }
220
221     static void SizeParamTypeIsString()
222     {
223         string strDescription = "Scenario(String==>BSTR):Array_Size(M->N)= 20, Array_Size(N->M)=10";
224         Console.WriteLine();
225         Console.WriteLine(strDescription + " Starts!");
226
227         int array_Size = 20;
228         String[] arrString = Helper.InitArray<String>(array_Size);
229         String[] arrString2 = Helper.InitArray<String>(array_Size);
230
231         Assert.IsTrue(MarshalCStyleArrayString_AsByRef_AsSizeParamIndex(ref array_Size, ref arrString, ref arrString2));
232
233         //Construct Expected
234         int expected_StringArraySize = 10;
235         String[] expArrString = Helper.GetExpChangeArray<String>(expected_StringArraySize);
236         Assert.IsTrue(Helper.EqualArray<String>(arrString, array_Size, expArrString, expArrString.Length));
237
238         Console.WriteLine(strDescription + " Ends!");
239     }
240
241     static int Main()
242     {
243         try{
244             SizeParamTypeIsByte();
245             SizeParamTypeIsSByte();
246             SizeParamTypeIsShort1();
247             SizeParamTypeIsShort2();
248             SizeParamTypeIsUShort();
249             SizeParamTypeIsInt32();
250             SizeParamTypeIsUInt32();
251             SizeParamTypeIsLong();
252             SizeParamTypeIsULong();
253             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
254             {
255                 SizeParamTypeIsString();
256             }
257             return 100;
258         }
259         catch (Exception e)
260         {
261             Console.WriteLine($"Test Failure: {e}");
262             return 101;
263         }
264     }
265 }