Add PInvoke/SizeParamIndex tests (#19348)
[platform/upstream/coreclr.git] / tests / src / Interop / PInvoke / SizeParamIndex / PInvoke / helper.h
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 // helper.h : Defines helper functions
6 #include <xplatform.h>
7 #include "platformdefines.h"
8
9
10 const LONG Array_Size = 10;
11 const LONG CArray_Size = 20;
12
13 //////////////////////////////////////////////////////////////////////////////
14 // Verify helper methods
15 //////////////////////////////////////////////////////////////////////////////
16 template<typename T> BOOL IsObjectEquals(T o1, T o2)
17 {
18     // T::operator== required.
19     return o1 == o2;
20 }
21
22 //Int32 helper
23 template<typename T>
24 T* InitArray(SIZE_T arrSize)
25 {
26     T* pExpectArr = (T*)CoreClrAlloc(sizeof(T)*arrSize);
27     for(SIZE_T i = 0;i<arrSize;++i)
28     {
29         pExpectArr[i] = (T)i;
30     }
31     return pExpectArr;
32 }
33
34 template<typename T>
35 T* InitExpectedArray(SIZE_T arrSize)
36 {
37     T* pExpectArr = (T*)CoreClrAlloc(sizeof(T)*arrSize);
38     for(SIZE_T i = 0;i<arrSize;++i)
39     {
40         pExpectArr[i] = arrSize - 1 - i;
41     }
42     return pExpectArr;
43 }
44
45 template<typename T>
46 BOOL EqualArray(T* actualArray, SIZE_T actualSize, T* expectedArray, SIZE_T expectedSize)
47 {
48     int failures = 0;
49
50     if(actualArray == NULL && expectedArray == NULL)
51     {
52         printf("Two arrays are equal. Both of them are NULL\n");
53         return TRUE;
54     }
55     else if(actualArray == NULL && expectedArray != NULL)
56     {
57         printf("Two arrays aren't equal. Array from Managed to Native is NULL,but the Compared is not NULL!\n");
58         return FALSE;
59     }
60     else if(actualArray != NULL && expectedArray == NULL)
61     {
62         printf("Two arrays aren't equal. Array from Managed to Native is not NULL,but the Compared is NULL!\n");
63         return FALSE;
64     }
65     else if(actualSize != expectedSize)
66     {
67         printf("Two arrays aren't equal. The arrays size are not equal. Expected:%d, Actual:%d!\n",(int)expectedSize,(int)actualSize);
68         return FALSE;
69     }
70     for(SIZE_T i = 0;i<actualSize;++i)
71     {
72         if(actualArray[i] != expectedArray[i])
73         {
74             printf("Two arrays aren't equal.The value of index of %d isn't equal!\n",(int)i);
75             printf("\tThe value in array from managed to native is %d\n",(int)actualArray[i]);
76             printf("\tThe value in expected array is %d\n",(int)expectedArray[i]);
77             failures++;
78         }
79     }
80     if(failures>0)
81         return FALSE;
82     return TRUE;
83 }
84
85 template<typename T>
86 BOOL CheckAndChangeArrayByRef(T ** ppActual, T* Actual_Array_Size, SIZE_T Expected_Array_Size, SIZE_T Return_Array_Size)
87 {
88     T* pExpectedArr = InitArray<T>(Expected_Array_Size);
89     if(!EqualArray(*ppActual, (SIZE_T)*Actual_Array_Size, pExpectedArr, Expected_Array_Size))
90     {
91         printf("ManagedtoNative Error in Method: %s!\n",__FUNCTION__);
92         return FALSE;
93     }
94
95     CoreClrFree(pExpectedArr);
96     CoreClrFree(*ppActual);
97     *ppActual = (T*)CoreClrAlloc(sizeof(T)*Return_Array_Size);
98
99     *Actual_Array_Size = ((T)Return_Array_Size);
100     for(SIZE_T i = 0; i < Return_Array_Size; ++i)
101     {
102         (*ppActual)[i] = (T)(Return_Array_Size - 1 - i);
103     }
104     return TRUE;
105 }
106
107 template<typename T>
108 BOOL CheckAndChangeArrayByOut(T ** ppActual, T* Actual_Array_Size, SIZE_T Array_Size)
109 {
110     *ppActual = (T*)CoreClrAlloc(sizeof(T)*Array_Size);
111     *Actual_Array_Size = ((T)Array_Size);
112
113     for(SIZE_T i = 0; i < Array_Size; ++i)
114     {
115         (*ppActual)[i] = (T)(Array_Size - 1 - i);
116     }
117     return TRUE;
118 }
119
120 //template<typename T>
121 //BOOL CheckReturnArray(T* pReturnArr, T Actual_Array_Size, T Expected_Array_Size)
122 //{
123 //    T* pExpectedArr = InitExpectedArray(Expected_Array_Size);
124 //
125 //    if(!EqualArray(pReturnArr, Actual_Array_Size, pExpectedArr, Expected_Array_Size))
126 //    {
127 //        printf("ManagedtoNative Error in Method: %s!\n",__FUNCTION__);
128 //        CoreClrFree(pExpectedArr);
129 //        return FALSE;
130 //    }
131 //    else
132 //    {
133 //        //printf("Managed to Native:Passed!\n");
134 //        CoreClrFree(pExpectedArr);
135 //        return TRUE;
136 //    }
137 //}
138
139 //BSTR helper
140 #ifdef _WIN32
141 template<> BOOL IsObjectEquals(BSTR o1, BSTR o2)
142 {
143     if ( o1 == NULL && o2 == NULL )
144         return TRUE;
145     else if ( o1 == NULL && o2 != NULL )
146         return FALSE;
147     else if ( o1 != NULL && o2 == NULL )
148         return FALSE;
149
150     UINT uLen1 = SysStringLen(o1);
151     UINT uLen2 = SysStringLen(o2);
152
153     if (uLen1 != uLen2 )
154         return FALSE;
155
156     return memcmp(o1, o2, uLen1) == 0;
157 }
158
159 BSTR ToBSTR(int i)
160 {
161     BSTR bstrRet = NULL;
162     VarBstrFromI4(i, 0, 0, &bstrRet);
163
164     return bstrRet;
165 }
166
167 BOOL CmpBSTR(BSTR bstr1, BSTR bstr2)
168 {
169     UINT uLen1 = SysStringLen(bstr1);
170     UINT uLen2 = SysStringLen(bstr2);
171
172     if (uLen1 != uLen2 )
173         return FALSE;
174     return memcmp(bstr1, bstr2, uLen1) == 0;
175 }
176
177 BSTR* InitArrayBSTR(LONG arrSize)
178 {
179     BSTR* pExpectArr = (BSTR*)CoreClrAlloc(sizeof(BSTR)*arrSize);
180     for(LONG i = 0;i<arrSize;++i)
181     {
182         pExpectArr[i] = ToBSTR(i);
183     }
184     return pExpectArr;
185 }
186
187 BOOL EqualArrayBSTR(BSTR* ArrBSTR, LONG arrSize1, BSTR* CArrBSTR, LONG arrSize2)
188 {
189     int failures = 0;
190
191     if(ArrBSTR == NULL && CArrBSTR == NULL)
192     {
193         printf("Two arrays are equal. Both of them NULL\n");
194         return TRUE;
195     }
196     else if(ArrBSTR == NULL && CArrBSTR != NULL)
197     {
198         printf("Two arrays aren't equal. Array from Managed to Native is NULL,but the Compared is not NULL!\n");
199         return FALSE;
200     }
201     else if(ArrBSTR != NULL && CArrBSTR == NULL)
202     {
203         printf("Two arrays aren't equal. Array from Managed to Native is not NULL,but the Compared is NULL!\n");
204         return FALSE;
205     }
206     else if(arrSize1 != arrSize2)
207     {
208         printf("Two arrays aren't equal. The arrays size are not equal!\n");
209         return FALSE;
210     }
211     for(int i = 0;i<arrSize1;++i)
212     {
213         if(!CmpBSTR(ArrBSTR[i],CArrBSTR[i]))
214         {
215             printf("Two arrays aren't equal.The value of index of %d isn't equal!\n",i);
216             printf("\tThe value in array from managed to native is %S\n",ArrBSTR[i]);
217             printf("\tThe value in expected array is %S\n",CArrBSTR[i]);
218             failures++;
219         }
220     }
221     if(failures>0)
222         return FALSE;
223     return TRUE;
224 }
225 #endif