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