Add PInvoke/SizeParamIndex tests (#19348)
[platform/upstream/coreclr.git] / tests / src / Interop / PInvoke / SizeParamIndex / PInvoke / helper.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
8 public class Helper
9 {
10
11     #region General method
12
13     public static T[] InitArray<T>(int arrSize)
14     {
15         T[] array = new T[arrSize];
16         for (int i = 0; i < array.Length; i++)
17         {
18             array[i] = (T)Convert.ChangeType(i, typeof(T));
19         }
20         return array;
21     }
22
23     public static bool EqualArray<T>(T[] actualArray, int actualSize, T[] expectedArray, int expectedSize)
24     {
25         int failures = 0;
26         if (actualArray == null && expectedArray == null)
27         {
28             Console.WriteLine("\tTwo array are equal.Both of them null");
29             return true;
30         }
31         else if (actualArray == null && expectedArray != null)
32         {
33             Console.WriteLine("\tTwo array are not equal.The sourcArr is null,but the expectedArray is not null");
34             return false;
35         }
36         else if (actualArray != null && expectedArray == null)
37         {
38             Console.WriteLine("\tTwo array are not equal.The sourcArr is not null but the expectedArray is null");
39             return false;
40         }
41         else if (!actualSize.Equals(expectedSize))
42         {
43             Console.WriteLine("\tTwo array are not equal.The sizes are not equal:Expected:{0},Actaul:{1}", expectedSize, actualSize);
44             return false;
45         }
46         for (int i = 0; i < expectedSize; ++i)
47         {
48             if (!actualArray[i].Equals(expectedArray[i]))
49             {
50                 Console.WriteLine("\tTwo array are not equal.The values of index {0} are not equal!", i);
51                 Console.WriteLine("\t\tThe actualArray is {0},the expectedArray is {1}", actualArray[i].ToString(), expectedArray[i].ToString());
52                 failures++;
53             }
54         }
55         if (failures > 0)
56             return false;
57         return true;
58     }
59
60     public static T[] GetExpChangeArray<T>(int cSize)
61     {
62         T[] array = new T[cSize];
63
64         for (int i = array.Length - 1; i >= 0; --i)
65             array[i] = (T)Convert.ChangeType(array.Length - 1 - i, typeof(T));
66
67         return array;
68     }
69
70     public static bool CheckAndChangeArray<T>(ref T[] arrArg, ref T arrSize, int actualArrSize, int expectedArrSize)
71     {
72         T[] actualArr = InitArray<T>(actualArrSize);
73         if (!EqualArray<T>(arrArg, actualArrSize, actualArr, actualArrSize))
74         {
75             return false;
76         }
77
78         arrSize = (T)Convert.ChangeType(expectedArrSize, typeof(T));
79         arrArg = GetExpChangeArray<T>(expectedArrSize);
80         return true;
81     }
82
83     #endregion
84
85 }