예제 #1
0
        private void DefaultValue()
        {
            int[] expected = { Default1, Default2, Default3 };

            // Omit all arguments
            int[] ret = obj.DefaultValue();
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with all arguments omitted");

            // Specify some arguments
            expected[0] = one;
            ret         = obj.DefaultValue(one);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with some arguments specified");

            expected[1] = two;
            ret         = obj.DefaultValue(one, two);
            Assert.AreAllEqual(expected, ret);

            // Specify all arguments
            expected[1] = two;
            expected[2] = three;
            ret         = obj.DefaultValue(one, two, three);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with all arguments specified");

            // Named arguments
            expected[0] = Default1;
            expected[1] = Default2;
            ret         = obj.DefaultValue(third: three);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with named arguments");
        }
예제 #2
0
        private void Named()
        {
            int[] expected = { one, two, three };

            // Name all arguments
            int[] ret = obj.Required(first: one, second: two, third: three);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with all named arguments");

            // Name some arguments
            ret = obj.Required(one, two, third: three);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with some named arguments");

            ret = obj.Required(one, second: two, third: three);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with some named arguments");

            // Name in different order
            ret = obj.Required(third: three, first: one, second: two);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with out-of-order named arguments");

            ret = obj.Required(one, third: three, second: two);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with out-of-order named arguments");

            // Invalid name
            COMException e = Assert.Throws <COMException>(() => obj.Required(one, two, invalid: three));
            const int    DISP_E_UNKNOWNNAME = unchecked ((int)0x80020006);

            Assert.AreEqual(DISP_E_UNKNOWNNAME, e.HResult, "Unexpected HRESULT on COMException");
        }
예제 #3
0
    private static void TestUnicodeStringArray()
    {
        string unMappableUnicodeStr_back = "" + NormalChar2 + unmappableChar + NormalChar1;

        char[] unMappableCharArray_In    = new char[3];
        char[] unMappableCharArray_InOut = new char[3];
        char[] unMappableCharArray_Out   = new char[3];
        char[] mappableCharArray_In      = new char[3];
        char[] mappableCharArray_InOut   = new char[3];
        char[] mappableCharArray_Out     = new char[3];

        CharArrayInit(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out,
                      mappableCharArray_In, mappableCharArray_InOut, mappableCharArray_Out, UnmappableString, MappableString);
        Assert.IsTrue(PassUnicodeCharArrayTT(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out));
        Assert.AreAllEqual(unMappableUnicodeStr_back.ToCharArray(), unMappableCharArray_InOut);
        Assert.AreAllEqual(unMappableUnicodeStr_back.ToCharArray(), unMappableCharArray_Out);

        CharArrayInit(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out,
                      mappableCharArray_In, mappableCharArray_InOut, mappableCharArray_Out, UnmappableString, MappableString);
        Assert.IsTrue(PassUnicodeCharArrayFT(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out));
        Assert.AreAllEqual(unMappableUnicodeStr_back.ToCharArray(), unMappableCharArray_InOut);
        Assert.AreAllEqual(unMappableUnicodeStr_back.ToCharArray(), unMappableCharArray_Out);

        CharArrayInit(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out,
                      mappableCharArray_In, mappableCharArray_InOut, mappableCharArray_Out, UnmappableString, MappableString);
        Assert.IsTrue(PassUnicodeCharArrayFF(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out));
        Assert.AreAllEqual(unMappableUnicodeStr_back.ToCharArray(), unMappableCharArray_InOut);
        Assert.AreAllEqual(unMappableUnicodeStr_back.ToCharArray(), unMappableCharArray_Out);
    }
예제 #4
0
        static void Validate_Enumerator()
        {
            var dispatchTesting = (DispatchTesting) new DispatchTestingClass();
            var expected        = System.Linq.Enumerable.Range(0, 10);

            Console.WriteLine($"Calling {nameof(DispatchTesting.GetEnumerator)} ...");
            var enumerator = dispatchTesting.GetEnumerator();

            Assert.AreAllEqual(expected, GetEnumerable(enumerator));

            enumerator.Reset();
            Assert.AreAllEqual(expected, GetEnumerable(enumerator));

            Console.WriteLine($"Calling {nameof(DispatchTesting.ExplicitGetEnumerator)} ...");
            var enumeratorExplicit = dispatchTesting.ExplicitGetEnumerator();

            Assert.AreAllEqual(expected, GetEnumerable(enumeratorExplicit));

            enumeratorExplicit.Reset();
            Assert.AreAllEqual(expected, GetEnumerable(enumeratorExplicit));

            System.Collections.Generic.IEnumerable <int> GetEnumerable(System.Collections.IEnumerator e)
            {
                var list = new System.Collections.Generic.List <int>();

                while (e.MoveNext())
                {
                    list.Add((int)e.Current);
                }

                return(list);
            }
        }
예제 #5
0
        private void Array()
        {
            int len = 5;

            int[] array    = new int[len];
            int[] expected = new int[len];
            for (int i = 0; i < len; i++)
            {
                int val = rand.Next(int.MaxValue - 1);
                array[i]    = val;
                expected[i] = val + 1;
            }

            // Call method returning array
            Assert.AreAllEqual(expected, obj.Array_PlusOne_Ret(array));

            // Call method with array in/out
            int[] inout = new int[len];
            System.Array.Copy(array, inout, len);
            obj.Array_PlusOne_InOut(ref inout);
            Assert.AreAllEqual(expected, inout);

            // Call method returning array as variant
            Assert.AreAllEqual(expected, obj.ArrayVariant_PlusOne_Ret(array));

            // Call method with array as variant in/out
            inout = new int[len];
            System.Array.Copy(array, inout, len);
            obj.ArrayVariant_PlusOne_InOut(ref inout);
            Assert.AreAllEqual(expected, inout);
        }
예제 #6
0
        private void CustomCollection()
        {
            // Add to the collection
            Assert.AreEqual(0, obj.Count);
            string[] array = { "ONE", "TWO", "THREE" };
            foreach (string s in array)
            {
                obj.Add(s);
            }

            // Get item by index
            Assert.AreEqual(array[0], obj[0]);
            Assert.AreEqual(array[0], obj.Item(0));
            Assert.AreEqual(array[0], obj.Item[0]);
            Assert.AreEqual(array[1], obj[1]);
            Assert.AreEqual(array[1], obj.Item(1));
            Assert.AreEqual(array[2], obj[2]);
            Assert.AreEqual(array[2], obj.Item(2));
            Assert.AreEqual(array.Length, obj.Count);

            // Enumerate collection
            List <string> list = new List <string>();

            foreach (string str in obj)
            {
                list.Add(str);
            }
            Assert.AreAllEqual(array, list);

            array = new string[] { "NEW_ONE", "NEW_TWO", "NEW_THREE" };
            // Update items by index
            obj[0] = array[0];
            Assert.AreEqual(array[0], obj[0]);
            obj[1] = array[1];
            Assert.AreEqual(array[1], obj[1]);
            obj[2] = array[2];
            Assert.AreEqual(array[2], obj[2]);
            Assert.AreEqual(array.Length, obj.Count);

            list.Clear();
            foreach (string str in obj)
            {
                list.Add(str);
            }
            Assert.AreAllEqual(array, list);

            // Remove item
            obj.Remove(1);
            Assert.AreEqual(2, obj.Count);
            Assert.AreEqual(array[0], obj[0]);
            Assert.AreEqual(array[2], obj[1]);

            // Clear collection
            obj.Clear();
            Assert.AreEqual(0, obj.Count);
        }
예제 #7
0
 private static void TestCharArray()
 {
     Console.WriteLine("Scenario : Checking Marshal AsAny for char array ");
     char[] charArray       = new char[] { 'a', 'b', 'c' };
     char[] charArray_In    = new char[] { 'a', 'b', 'c' };
     char[] charArray_InOut = new char[] { 'a', 'b', 'c' };
     char[] charArray_Out   = new char[] { 'a', 'b', 'c' };
     char[] charArray_Back  = new char[] { 'd', 'e', 'f' };
     Assert.IsTrue(PassArrayChar(charArray, charArray_In, charArray_InOut, charArray_Out, new char[] { 'a', 'b', 'c' }, 3));
     Assert.AreAllEqual(charArray_Back, charArray_InOut);
     Assert.AreAllEqual(charArray_Back, charArray_Out);
 }
예제 #8
0
 private static void TestDoubleArray()
 {
     Console.WriteLine("Scenario : Checking Marshal AsAny for double array ");
     double[] doubleArray       = new double[] { 0.0, 1.1, 2.2 };
     double[] doubleArray_In    = new double[] { 0.0, 1.1, 2.2 };
     double[] doubleArray_InOut = new double[] { 0.0, 1.1, 2.2 };
     double[] doubleArray_Out   = new double[] { 0.0, 1.1, 2.2 };
     double[] doubleArray_Back  = new double[] { 10.0, 11.1, 12.2 };
     Assert.IsTrue(PassArrayDouble(doubleArray, doubleArray_In, doubleArray_InOut, doubleArray_Out, new double[] { 0.0, 1.1, 2.2 }, 3));
     Assert.AreAllEqual(doubleArray_Back, doubleArray_InOut);
     Assert.AreAllEqual(doubleArray_Back, doubleArray_Out);
 }
예제 #9
0
 private static void TestSByteArray()
 {
     Console.WriteLine("Scenario : Checking Marshal AsAny for sbyte array ");
     sbyte[] sbyteArray       = new sbyte[] { -1, 0, 1 };
     sbyte[] sbyteArray_In    = new sbyte[] { -1, 0, 1 };
     sbyte[] sbyteArray_InOut = new sbyte[] { -1, 0, 1 };
     sbyte[] sbyteArray_Out   = new sbyte[] { -1, 0, 1 };
     sbyte[] sbyteArray_Back  = new sbyte[] { 9, 10, 11 };
     Assert.IsTrue(PassArraySbyte(sbyteArray, sbyteArray_In, sbyteArray_InOut, sbyteArray_Out, new sbyte[] { -1, 0, 1 }, 3));
     Assert.AreAllEqual(sbyteArray_Back, sbyteArray_InOut);
     Assert.AreAllEqual(sbyteArray_Back, sbyteArray_Out);
 }
예제 #10
0
 private static void TestInt16Array()
 {
     Console.WriteLine("Scenario : Checking Marshal AsAny for short array ");
     short[] shortArray       = new short[] { -1, 0, 1 };
     short[] shortArray_In    = new short[] { -1, 0, 1 };
     short[] shortArray_InOut = new short[] { -1, 0, 1 };
     short[] shortArray_Out   = new short[] { -1, 0, 1 };
     short[] shortArray_Back  = new short[] { 9, 10, 11 };
     Assert.IsTrue(PassArrayShort(shortArray, shortArray_In, shortArray_InOut, shortArray_Out, new short[] { -1, 0, 1 }, 3));
     Assert.AreAllEqual(shortArray_Back, shortArray_InOut);
     Assert.AreAllEqual(shortArray_Back, shortArray_Out);
 }
예제 #11
0
 private static void TestByteArray()
 {
     Console.WriteLine("Scenario : Checking Marshal AsAny for byte array ");
     byte[] byteArray       = new byte[] { 0, 1, 2 };
     byte[] byteArray_In    = new byte[] { 0, 1, 2 };
     byte[] byteArray_InOut = new byte[] { 0, 1, 2 };
     byte[] byteArray_Out   = new byte[] { 0, 1, 2 };
     byte[] byteArray_Back  = new byte[] { 10, 11, 12 };
     Assert.IsTrue(PassArrayByte(byteArray, byteArray_In, byteArray_InOut, byteArray_Out, new byte[] { 0, 1, 2 }, 3));
     Assert.AreAllEqual(byteArray_Back, byteArray_InOut);
     Assert.AreAllEqual(byteArray_Back, byteArray_Out);
 }
예제 #12
0
 private static void TestInt32Array()
 {
     Console.WriteLine("Scenario : Checking Marshal AsAny for Int array ");
     int[] intArray       = new int[] { 0, 1, 2 };
     int[] intArray_In    = new int[] { 0, 1, 2 };
     int[] intArray_InOut = new int[] { 0, 1, 2 };
     int[] intArray_Out   = new int[] { 0, 1, 2 };
     int[] intArray_Back  = new int[] { 10, 11, 12 };
     Assert.IsTrue(PassArrayInt(intArray, intArray_In, intArray_InOut, intArray_Out, new int[] { 0, 1, 2 }, 3));
     Assert.AreAllEqual(intArray_Back, intArray_InOut);
     Assert.AreAllEqual(intArray_Back, intArray_Out);
 }
예제 #13
0
 private static void TestUInt16Array()
 {
     Console.WriteLine("Scenario : Checking Marshal AsAny for ushort array ");
     ushort[] ushortArray       = new ushort[] { 0, 1, 2 };
     ushort[] ushortArray_In    = new ushort[] { 0, 1, 2 };
     ushort[] ushortArray_InOut = new ushort[] { 0, 1, 2 };
     ushort[] ushortArray_Out   = new ushort[] { 0, 1, 2 };
     ushort[] ushortArray_Back  = new ushort[] { 10, 11, 12 };
     Assert.IsTrue(PassArrayUshort(ushortArray, ushortArray_In, ushortArray_InOut, ushortArray_Out, new ushort[] { 0, 1, 2 }, 3));
     Assert.AreAllEqual(ushortArray_Back, ushortArray_InOut);
     Assert.AreAllEqual(ushortArray_Back, ushortArray_Out);
 }
예제 #14
0
 private static void TestULongArray()
 {
     Console.WriteLine("Scenario : Checking Marshal AsAny for ulong array ");
     ulong[] ulongArray       = new ulong[] { 0, 1, 2 };
     ulong[] ulongArray_In    = new ulong[] { 0, 1, 2 };
     ulong[] ulongArray_InOut = new ulong[] { 0, 1, 2 };
     ulong[] ulongArray_Out   = new ulong[] { 0, 1, 2 };
     ulong[] ulongArray_Back  = new ulong[] { 10, 11, 12 };
     Assert.IsTrue(PassArrayUlong(ulongArray, ulongArray_In, ulongArray_InOut, ulongArray_Out, new ulong[] { 0, 1, 2 }, 3));
     Assert.AreAllEqual(ulongArray_Back, ulongArray_InOut);
     Assert.AreAllEqual(ulongArray_Back, ulongArray_Out);
 }
예제 #15
0
 private static void TestLongArray()
 {
     Console.WriteLine("Scenario : Checking Marshal AsAny for long array ");
     long[] longArray       = new long[] { 0, 1, 2 };
     long[] longArray_In    = new long[] { 0, 1, 2 };
     long[] longArray_InOut = new long[] { 0, 1, 2 };
     long[] longArray_Out   = new long[] { 0, 1, 2 };
     long[] longArray_Back  = new long[] { 10, 11, 12 };
     Assert.IsTrue(PassArrayLong(longArray, longArray_In, longArray_InOut, longArray_Out, new long[] { 0, 1, 2 }, 3));
     Assert.AreAllEqual(longArray_Back, longArray_InOut);
     Assert.AreAllEqual(longArray_Back, longArray_Out);
 }
예제 #16
0
 private static void TestBoolArray()
 {
     Console.WriteLine("Scenario : Checking Marshal AsAny for bool array ");
     bool[] boolArray       = new bool[] { true, false, false };
     bool[] boolArray_In    = new bool[] { true, false, false };
     bool[] boolArray_InOut = new bool[] { true, false, false };
     bool[] boolArray_Out   = new bool[] { true, false, false };
     bool[] boolArray_Back  = new bool[] { false, true, true };
     Assert.IsTrue(PassArrayBool(boolArray, boolArray_In, boolArray_InOut, boolArray_Out, new bool[] { true, false, false }, 3));
     Assert.AreAllEqual(boolArray_Back, boolArray_InOut);
     Assert.AreAllEqual(boolArray_Back, boolArray_Out);
 }
예제 #17
0
 private static void TestSingleArray()
 {
     Console.WriteLine("Scenario : Checking Marshal AsAny for float array ");
     float[] singleArray       = new float[] { 0, 1, 2 };
     float[] singleArray_In    = new float[] { 0, 1, 2 };
     float[] singleArray_InOut = new float[] { 0, 1, 2 };
     float[] singleArray_Out   = new float[] { 0, 1, 2 };
     float[] singleArray_Back  = new float[] { 10, 11, 12 };
     Assert.IsTrue(PassArraySingle(singleArray, singleArray_In, singleArray_InOut, singleArray_Out, new float[] { 0, 1, 2 }, 3));
     Assert.AreAllEqual(singleArray_Back, singleArray_InOut);
     Assert.AreAllEqual(singleArray_Back, singleArray_Out);
 }
예제 #18
0
 private static void TestIntPtrArray()
 {
     Console.WriteLine("Scenario : Checking Marshal AsAny for IntPtr array ");
     IntPtr[] intPtrArray       = new IntPtr[] { new IntPtr(0), new IntPtr(1), new IntPtr(2) };
     IntPtr[] intPtrArray_In    = new IntPtr[] { new IntPtr(0), new IntPtr(1), new IntPtr(2) };
     IntPtr[] intPtrArray_InOut = new IntPtr[] { new IntPtr(0), new IntPtr(1), new IntPtr(2) };
     IntPtr[] intPtrArray_Out   = new IntPtr[] { new IntPtr(0), new IntPtr(1), new IntPtr(2) };
     IntPtr[] intPtrArray_Back  = new IntPtr[] { new IntPtr(10), new IntPtr(11), new IntPtr(12) };
     IntPtr[] expected          = new IntPtr[] { new IntPtr(0), new IntPtr(1), new IntPtr(2) };
     Assert.IsTrue(PassArrayIntPtr(intPtrArray, intPtrArray_In, intPtrArray_InOut, intPtrArray_Out, expected, 3));
     Assert.AreAllEqual(intPtrArray_Back, intPtrArray_InOut);
     Assert.AreAllEqual(intPtrArray_Back, intPtrArray_Out);
 }
예제 #19
0
        private void VarArgs()
        {
            VarEnum[] ret = obj.VarArgs();
            Assert.AreEqual(0, ret.Length);

            // COM server returns the type of each variant
            ret = obj.VarArgs(false);
            Assert.AreEqual(1, ret.Length);
            Assert.AreAllEqual(new [] { VarEnum.VT_BOOL }, ret);

            VarEnum[] expected = { VarEnum.VT_BSTR, VarEnum.VT_R8, VarEnum.VT_DATE, VarEnum.VT_I4 };
            ret = obj.VarArgs("s", 10d, new DateTime(), 10);
            Assert.AreEqual(expected.Length, ret.Length);
            Assert.AreAllEqual(expected, ret);
        }
예제 #20
0
        private void Optional()
        {
            int[] expected = { MissingParamId, MissingParamId, MissingParamId };

            // Omit all arguments
            int[] ret = obj.Optional();
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with all arguments omitted");

            // Specify some arguments
            expected[0] = one;
            ret         = obj.Optional(one);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with some arguments specified");

            expected[1] = Default2;
            ret         = obj.Mixed(one);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with some arguments specified");

            expected[1] = two;
            ret         = obj.Optional(one, two);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with some arguments specified");

            ret = obj.Mixed(one, two);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with some arguments specified");

            // Specify all arguments
            expected[1] = two;
            expected[2] = three;
            ret         = obj.Optional(one, two, three);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with all arguments specified");

            ret = obj.Mixed(one, two, three);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with all arguments specified");

            // Named arguments
            expected[1] = MissingParamId;
            ret         = obj.Optional(first: one, third: three);
            Assert.AreAllEqual(expected, ret, "Unexpected result calling function with named arguments");
        }
예제 #21
0
    private static void TestAnsiStringArrayBestFitMapping()
    {
        string unMappableAnsiStr_back = "" + NormalChar2 + (char)0x003f + NormalChar1;
        string mappableAnsiStr_back   = "" + NormalChar2 + (char)0x0035 + NormalChar1;

        char[] unMappableCharArray_In    = new char[3];
        char[] unMappableCharArray_InOut = new char[3];
        char[] unMappableCharArray_Out   = new char[3];
        char[] mappableCharArray_In      = new char[3];
        char[] mappableCharArray_InOut   = new char[3];
        char[] mappableCharArray_Out     = new char[3];

        CharArrayInit(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out,
                      mappableCharArray_In, mappableCharArray_InOut, mappableCharArray_Out, UnmappableString, MappableString);
        Assert.Throws <ArgumentException>(() => PassAnsiCharArrayTT(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out, true));

        CharArrayInit(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out,
                      mappableCharArray_In, mappableCharArray_InOut, mappableCharArray_Out, UnmappableString, MappableString);
        Assert.IsTrue(PassAnsiCharArrayTT(mappableCharArray_In, mappableCharArray_InOut, mappableCharArray_Out, false));
        Assert.AreAllEqual(mappableAnsiStr_back.ToCharArray(), mappableCharArray_InOut);
        Assert.AreAllEqual(mappableAnsiStr_back.ToCharArray(), mappableCharArray_Out);

        CharArrayInit(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out,
                      mappableCharArray_In, mappableCharArray_InOut, mappableCharArray_Out, UnmappableString, MappableString);
        Assert.Throws <ArgumentException>(() => PassAnsiCharArrayFT(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out, true));

        CharArrayInit(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out,
                      mappableCharArray_In, mappableCharArray_InOut, mappableCharArray_Out, UnmappableString, MappableString);
        Assert.Throws <ArgumentException>(() => PassAnsiCharArrayFT(mappableCharArray_In, mappableCharArray_InOut, mappableCharArray_Out, false));

        CharArrayInit(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out,
                      mappableCharArray_In, mappableCharArray_InOut, mappableCharArray_Out, UnmappableString, MappableString);
        Assert.IsTrue(PassAnsiCharArrayFF(unMappableCharArray_In, unMappableCharArray_InOut, unMappableCharArray_Out, true));
        Assert.AreAllEqual(unMappableAnsiStr_back.ToCharArray(), unMappableCharArray_InOut);
        Assert.AreAllEqual(unMappableAnsiStr_back.ToCharArray(), unMappableCharArray_Out);
    }
예제 #22
0
 private static void TestNativeToManaged()
 {
     Assert.AreAllEqual(Enumerable.Range(1, 10), EnumeratorAsEnumerable(IEnumeratorNative.GetIntegerEnumerator(1, 10)));
     Assert.AreAllEqual(Enumerable.Range(1, 10), IEnumeratorNative.GetIntegerEnumeration(1, 10).OfType <int>());
 }
예제 #23
0
        private void CustomCollection()
        {
            // Add to the collection
            Assert.AreEqual(0, obj.Count);
            string[] array = { "ONE", "TWO", "THREE" };
            foreach (string s in array)
            {
                obj.Add(s);
            }

            // Get item by index
            Assert.AreEqual(array[0], obj[0]);
            Assert.AreEqual(array[0], obj.Item(0));
            Assert.AreEqual(array[0], obj.Item[0]);
            Assert.AreEqual(array[1], obj[1]);
            Assert.AreEqual(array[1], obj.Item(1));
            Assert.AreEqual(array[2], obj[2]);
            Assert.AreEqual(array[2], obj.Item(2));
            Assert.AreEqual(array.Length, obj.Count);

            // Enumerate collection
            List <string> list = new List <string>();

            // Get and use enumerator directly
            System.Collections.IEnumerator enumerator = obj.GetEnumerator();
            while (enumerator.MoveNext())
            {
                list.Add((string)enumerator.Current);
            }
            Assert.AreAllEqual(array, list);

            list.Clear();
            enumerator.Reset();
            while (enumerator.MoveNext())
            {
                list.Add((string)enumerator.Current);
            }
            Assert.AreAllEqual(array, list);

            // Iterate over object that handles DISPID_NEWENUM
            list.Clear();
            foreach (string str in obj)
            {
                list.Add(str);
            }
            Assert.AreAllEqual(array, list);

            array = new string[] { "NEW_ONE", "NEW_TWO", "NEW_THREE" };
            // Update items by index
            obj[0] = array[0];
            Assert.AreEqual(array[0], obj[0]);
            obj[1] = array[1];
            Assert.AreEqual(array[1], obj[1]);
            obj[2] = array[2];
            Assert.AreEqual(array[2], obj[2]);
            Assert.AreEqual(array.Length, obj.Count);

            list.Clear();
            foreach (string str in obj)
            {
                list.Add(str);
            }
            Assert.AreAllEqual(array, list);

            // Remove item
            obj.Remove(1);
            Assert.AreEqual(2, obj.Count);
            Assert.AreEqual(array[0], obj[0]);
            Assert.AreEqual(array[2], obj[1]);

            // Clear collection
            obj.Clear();
            Assert.AreEqual(0, obj.Count);
        }
예제 #24
0
    public static int Main()
    {
        try
        {
            var boolArray = new bool[] { true, false, true, false, false, true };
            SafeArrayNative.XorBoolArray(boolArray, out var xorResult);
            Assert.AreEqual(XorArray(boolArray), xorResult);

            var decimalArray = new decimal[] { 1.5M, 30.2M, 6432M, 12.5832M };
            SafeArrayNative.MeanDecimalArray(decimalArray, out var meanDecimalValue);
            Assert.AreEqual(decimalArray.Average(), meanDecimalValue);

            SafeArrayNative.SumCurrencyArray(decimalArray, out var sumCurrencyValue);
            Assert.AreEqual(decimalArray.Sum(), sumCurrencyValue);

            var strings         = new [] { "ABCDE", "12345", "Microsoft" };
            var reversedStrings = strings.Select(str => Reverse(str)).ToArray();

            var ansiTest = strings.ToArray();
            SafeArrayNative.ReverseStringsAnsi(ansiTest);
            Assert.AreAllEqual(reversedStrings, ansiTest);

            var unicodeTest = strings.ToArray();
            SafeArrayNative.ReverseStringsUnicode(unicodeTest);
            Assert.AreAllEqual(reversedStrings, unicodeTest);

            var bstrTest = strings.ToArray();
            SafeArrayNative.ReverseStringsBSTR(bstrTest);
            Assert.AreAllEqual(reversedStrings, bstrTest);

            var blittableRecords = new SafeArrayNative.BlittableRecord[]
            {
                new SafeArrayNative.BlittableRecord {
                    a = 1
                },
                new SafeArrayNative.BlittableRecord {
                    a = 5
                },
                new SafeArrayNative.BlittableRecord {
                    a = 7
                },
                new SafeArrayNative.BlittableRecord {
                    a = 3
                },
                new SafeArrayNative.BlittableRecord {
                    a = 9
                },
                new SafeArrayNative.BlittableRecord {
                    a = 15
                },
            };
            Assert.AreAllEqual(blittableRecords, SafeArrayNative.CreateSafeArrayOfRecords(blittableRecords));

            var nonBlittableRecords = boolArray.Select(b => new SafeArrayNative.NonBlittableRecord {
                b = b
            }).ToArray();
            Assert.AreAllEqual(nonBlittableRecords, SafeArrayNative.CreateSafeArrayOfRecords(nonBlittableRecords));

            var objects = new object[] { new object(), new object(), new object() };
            SafeArrayNative.VerifyIUnknownArray(objects);
            SafeArrayNative.VerifyIDispatchArray(objects);

            var variantInts = new object[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            SafeArrayNative.MeanVariantIntArray(variantInts, out var variantMean);
            Assert.AreEqual(variantInts.OfType <int>().Average(), variantMean);

            var dates = new DateTime[] { new DateTime(2008, 5, 1), new DateTime(2010, 1, 1) };
            SafeArrayNative.DistanceBetweenDates(dates, out var numDays);
            Assert.AreEqual((dates[1] - dates[0]).TotalDays, numDays);

            SafeArrayNative.XorBoolArrayInStruct(
                new SafeArrayNative.StructWithSafeArray
            {
                values = boolArray
            },
                out var structXor);

            Assert.AreEqual(XorArray(boolArray), structXor);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            return(101);
        }
        return(100);
    }
 public void Validate(params string[] expectedNames)
 {
     Assert.AreAllEqual(expectedNames, invocations, $"Unexpected invocations for {nameof(LoadUnmanagedDll)}.");
 }