Esempio n. 1
0
        public void Unicode()
        {
            DynLoaderBase defaultInstance = new EmptyLoader();
            DynLoaderBase customInstance  = new EncLoader();

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Assert.AreEqual(UnicodeConvention.Utf16, defaultInstance.UnicodeConvention);
                Assert.IsTrue(defaultInstance.UnicodeEncoding.Equals(Encoding.Unicode));

                Assert.AreEqual(UnicodeConvention.Ansi, customInstance.UnicodeConvention);
                Assert.IsTrue(customInstance.UnicodeEncoding.Equals(new UTF8Encoding(false)));
            }
            else
            {
                Assert.AreEqual(UnicodeConvention.Utf8, defaultInstance.UnicodeConvention);
                Assert.IsTrue(defaultInstance.UnicodeEncoding.Equals(new UTF8Encoding(false)));

                Assert.AreEqual(UnicodeConvention.Utf8, customInstance.UnicodeConvention);
                Assert.IsTrue(customInstance.UnicodeEncoding.Equals(new UTF8Encoding(false)));
            }
        }
Esempio n. 2
0
        public void StringPtr()
        {
            DynLoaderBase defaultInstance = new EmptyLoader();
            DynLoaderBase customInstance  = new EncLoader();

            const string str         = "Joveler.DynLoader";
            IntPtr       ansiUtf8Ptr = Marshal.StringToHGlobalAnsi(str); // ANSI on Windows, UTF8 on POSIX
            IntPtr       utf16Ptr    = Marshal.StringToHGlobalUni(str);

            try
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // PtrToStringAuto
                    string fromUniStr = defaultInstance.PtrToStringAuto(utf16Ptr);
                    Assert.IsTrue(str.Equals(fromUniStr, StringComparison.Ordinal));
                    string fromAnsiStr = customInstance.PtrToStringAuto(ansiUtf8Ptr);
                    Assert.IsTrue(str.Equals(fromAnsiStr, StringComparison.Ordinal));

                    // StringToHGlobalAuto
                    IntPtr fromUniPtr = defaultInstance.StringToHGlobalAuto(str);
                    try
                    {
                        fromUniStr = Marshal.PtrToStringUni(fromUniPtr);
                        Assert.IsTrue(str.Equals(fromUniStr, StringComparison.Ordinal));
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(fromUniPtr);
                    }
                    IntPtr fromAnsiPtr = customInstance.StringToHGlobalAuto(str);
                    try
                    {
                        fromAnsiStr = Marshal.PtrToStringAnsi(fromAnsiPtr);
                        Assert.IsTrue(str.Equals(fromAnsiStr, StringComparison.Ordinal));
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(fromAnsiPtr);
                    }

                    // StringToCoTaskMemAuto
                    fromUniPtr = defaultInstance.StringToCoTaskMemAuto(str);
                    try
                    {
                        fromUniStr = Marshal.PtrToStringUni(fromUniPtr);
                        Assert.IsTrue(str.Equals(fromUniStr, StringComparison.Ordinal));
                    }
                    finally
                    {
                        Marshal.FreeCoTaskMem(fromUniPtr);
                    }
                    fromAnsiPtr = customInstance.StringToCoTaskMemAuto(str);
                    try
                    {
                        fromAnsiStr = Marshal.PtrToStringAnsi(fromAnsiPtr);
                        Assert.IsTrue(str.Equals(fromAnsiStr, StringComparison.Ordinal));
                    }
                    finally
                    {
                        Marshal.FreeCoTaskMem(fromAnsiPtr);
                    }
                }
                else
                {
                    // PtrToStringAuto
                    string fromUtf8Str = defaultInstance.PtrToStringAuto(ansiUtf8Ptr);
                    Assert.IsTrue(str.Equals(fromUtf8Str, StringComparison.Ordinal));
                    fromUtf8Str = customInstance.PtrToStringAuto(ansiUtf8Ptr);
                    Assert.IsTrue(str.Equals(fromUtf8Str, StringComparison.Ordinal));

                    // StringToHGlobalAuto
                    IntPtr fromUtf8Ptr = defaultInstance.StringToHGlobalAuto(str);
                    try
                    {
                        fromUtf8Str = Marshal.PtrToStringAnsi(fromUtf8Ptr);
                        Assert.IsTrue(str.Equals(fromUtf8Str, StringComparison.Ordinal));
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(fromUtf8Ptr);
                    }
                    fromUtf8Ptr = customInstance.StringToHGlobalAuto(str);
                    try
                    {
                        fromUtf8Str = Marshal.PtrToStringAnsi(fromUtf8Ptr);
                        Assert.IsTrue(str.Equals(fromUtf8Str, StringComparison.Ordinal));
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(fromUtf8Ptr);
                    }

                    // StringToCoTaskMemAuto
                    fromUtf8Ptr = defaultInstance.StringToCoTaskMemAuto(str);
                    try
                    {
                        fromUtf8Str = Marshal.PtrToStringAnsi(fromUtf8Ptr);
                        Assert.IsTrue(str.Equals(fromUtf8Str, StringComparison.Ordinal));
                    }
                    finally
                    {
                        Marshal.FreeCoTaskMem(fromUtf8Ptr);
                    }
                    fromUtf8Ptr = customInstance.StringToCoTaskMemAuto(str);
                    try
                    {
                        fromUtf8Str = Marshal.PtrToStringAnsi(fromUtf8Ptr);
                        Assert.IsTrue(str.Equals(fromUtf8Str, StringComparison.Ordinal));
                    }
                    finally
                    {
                        Marshal.FreeCoTaskMem(fromUtf8Ptr);
                    }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(ansiUtf8Ptr);
                Marshal.FreeHGlobal(utf16Ptr);
            }
        }