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.
5 // helper.h : Defines helper functions
7 #include "platformdefines.h"
10 const LONG Array_Size = 10;
11 const LONG CArray_Size = 20;
13 //////////////////////////////////////////////////////////////////////////////
14 // Verify helper methods
15 //////////////////////////////////////////////////////////////////////////////
16 template<typename T> BOOL IsObjectEquals(T o1, T o2)
18 // T::operator== required.
24 T* InitArray(SIZE_T arrSize)
26 T* pExpectArr = (T*)CoreClrAlloc(sizeof(T)*arrSize);
27 for(SIZE_T i = 0;i<arrSize;++i)
35 T* InitExpectedArray(SIZE_T arrSize)
37 T* pExpectArr = (T*)CoreClrAlloc(sizeof(T)*arrSize);
38 for(SIZE_T i = 0;i<arrSize;++i)
40 pExpectArr[i] = arrSize - 1 - i;
46 BOOL EqualArray(T* actualArray, SIZE_T actualSize, T* expectedArray, SIZE_T expectedSize)
50 if(actualArray == NULL && expectedArray == NULL)
52 printf("Two arrays are equal. Both of them are NULL\n");
55 else if(actualArray == NULL && expectedArray != NULL)
57 printf("Two arrays aren't equal. Array from Managed to Native is NULL,but the Compared is not NULL!\n");
60 else if(actualArray != NULL && expectedArray == NULL)
62 printf("Two arrays aren't equal. Array from Managed to Native is not NULL,but the Compared is NULL!\n");
65 else if(actualSize != expectedSize)
67 printf("Two arrays aren't equal. The arrays size are not equal. Expected:%d, Actual:%d!\n",(int)expectedSize,(int)actualSize);
70 for(SIZE_T i = 0;i<actualSize;++i)
72 if(actualArray[i] != expectedArray[i])
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]);
86 BOOL CheckAndChangeArrayByRef(T ** ppActual, T* Actual_Array_Size, SIZE_T Expected_Array_Size, SIZE_T Return_Array_Size)
88 T* pExpectedArr = InitArray<T>(Expected_Array_Size);
89 if(!EqualArray(*ppActual, (SIZE_T)*Actual_Array_Size, pExpectedArr, Expected_Array_Size))
91 printf("ManagedtoNative Error in Method: %s!\n",__FUNCTION__);
95 CoreClrFree(pExpectedArr);
96 CoreClrFree(*ppActual);
97 *ppActual = (T*)CoreClrAlloc(sizeof(T)*Return_Array_Size);
99 *Actual_Array_Size = ((T)Return_Array_Size);
100 for(SIZE_T i = 0; i < Return_Array_Size; ++i)
102 (*ppActual)[i] = (T)(Return_Array_Size - 1 - i);
108 BOOL CheckAndChangeArrayByOut(T ** ppActual, T* Actual_Array_Size, SIZE_T Array_Size)
110 *ppActual = (T*)CoreClrAlloc(sizeof(T)*Array_Size);
111 *Actual_Array_Size = ((T)Array_Size);
113 for(SIZE_T i = 0; i < Array_Size; ++i)
115 (*ppActual)[i] = (T)(Array_Size - 1 - i);
120 //template<typename T>
121 //BOOL CheckReturnArray(T* pReturnArr, T Actual_Array_Size, T Expected_Array_Size)
123 // T* pExpectedArr = InitExpectedArray(Expected_Array_Size);
125 // if(!EqualArray(pReturnArr, Actual_Array_Size, pExpectedArr, Expected_Array_Size))
127 // printf("ManagedtoNative Error in Method: %s!\n",__FUNCTION__);
128 // CoreClrFree(pExpectedArr);
133 // //printf("Managed to Native:Passed!\n");
134 // CoreClrFree(pExpectedArr);
141 template<> BOOL IsObjectEquals(BSTR o1, BSTR o2)
143 if ( o1 == NULL && o2 == NULL )
145 else if ( o1 == NULL && o2 != NULL )
147 else if ( o1 != NULL && o2 == NULL )
150 UINT uLen1 = SysStringLen(o1);
151 UINT uLen2 = SysStringLen(o2);
156 return memcmp(o1, o2, uLen1) == 0;
162 VarBstrFromI4(i, 0, 0, &bstrRet);
167 BOOL CmpBSTR(BSTR bstr1, BSTR bstr2)
169 UINT uLen1 = SysStringLen(bstr1);
170 UINT uLen2 = SysStringLen(bstr2);
174 return memcmp(bstr1, bstr2, uLen1) == 0;
177 BSTR* InitArrayBSTR(LONG arrSize)
179 BSTR* pExpectArr = (BSTR*)CoreClrAlloc(sizeof(BSTR)*arrSize);
180 for(LONG i = 0;i<arrSize;++i)
182 pExpectArr[i] = ToBSTR(i);
187 BOOL EqualArrayBSTR(BSTR* ArrBSTR, LONG arrSize1, BSTR* CArrBSTR, LONG arrSize2)
191 if(ArrBSTR == NULL && CArrBSTR == NULL)
193 printf("Two arrays are equal. Both of them NULL\n");
196 else if(ArrBSTR == NULL && CArrBSTR != NULL)
198 printf("Two arrays aren't equal. Array from Managed to Native is NULL,but the Compared is not NULL!\n");
201 else if(ArrBSTR != NULL && CArrBSTR == NULL)
203 printf("Two arrays aren't equal. Array from Managed to Native is not NULL,but the Compared is NULL!\n");
206 else if(arrSize1 != arrSize2)
208 printf("Two arrays aren't equal. The arrays size are not equal!\n");
211 for(int i = 0;i<arrSize1;++i)
213 if(!CmpBSTR(ArrBSTR[i],CArrBSTR[i]))
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]);