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