Add simple tests for ArrayWithOffset marshaling. (#21013)
authorJeremy Koritzinsky <jkoritzinsky@gmail.com>
Fri, 16 Nov 2018 07:48:16 +0000 (23:48 -0800)
committerGitHub <noreply@github.com>
Fri, 16 Nov 2018 07:48:16 +0000 (23:48 -0800)
* Add simple tests for ArrayWithOffset marshaling.

* PR Feedback.

* Fix visibility

* Fix DllImport library name

tests/src/Interop/CMakeLists.txt
tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetNative.cpp [new file with mode: 0644]
tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetTest.cs [new file with mode: 0644]
tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetTest.csproj [new file with mode: 0644]
tests/src/Interop/PInvoke/ArrayWithOffset/CMakeLists.txt [new file with mode: 0644]
tests/src/Interop/PInvoke/ArrayWithOffset/PInvokeDefs.cs [new file with mode: 0644]

index 37c24df..901bd9e 100644 (file)
@@ -12,6 +12,7 @@ list(APPEND LINK_LIBRARIES_ADDITIONAL platformdefines)
 SET(CLR_INTEROP_TEST_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
 
 include_directories(common)
+add_subdirectory(PInvoke/ArrayWithOffset)
 add_subdirectory(PInvoke/DllImportPath)
 add_subdirectory(PInvoke/BestFitMapping/Char)
 add_subdirectory(PInvoke/BestFitMapping/LPStr)
diff --git a/tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetNative.cpp b/tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetNative.cpp
new file mode 100644 (file)
index 0000000..44d6e83
--- /dev/null
@@ -0,0 +1,19 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+#include <xplatform.h>
+
+extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Marshal_InOut(int expected[], int actual[], int numElements, int newValue[])
+{
+    bool correctPassedIn = memcmp(expected, actual, numElements * sizeof(int)) == 0;
+
+    memcpy(actual, newValue, numElements * sizeof(int));
+
+    return correctPassedIn ? TRUE : FALSE;
+}
+
+extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Marshal_Invalid(void* invalid)
+{
+    return FALSE;
+}
diff --git a/tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetTest.cs b/tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetTest.cs
new file mode 100644 (file)
index 0000000..9f3d91a
--- /dev/null
@@ -0,0 +1,54 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Text;
+using System.Runtime.InteropServices;
+using TestLibrary;
+
+unsafe class ArrayWithOffsetTest
+{
+    public static int Main()
+    {
+        try
+        {
+            Span<int> expected = new int[] {1, 2, 3, 4, 5, 6};
+            Span<int> newValue = new int[] {7, 8, 9, 10, 11, 12};
+
+            for (int i = 0; i < expected.Length; i++)
+            {
+                int[] array = new int[] {1, 2, 3, 4, 5, 6};
+                ArrayWithOffset offset = new ArrayWithOffset(array, i * 4); // The offset parameter in ArrayWithOffset is a byte-offset, not an element offset.
+
+                fixed (int* expectedSubArray = expected.Slice(i))
+                fixed (int* newValueSubArray = newValue.Slice(i))
+                {
+                    Assert.IsTrue(ArrayWithOffsetNative.Marshal_InOut(expectedSubArray, offset, expected.Length - i, newValueSubArray), $"Native call failed with element offset {i}.");
+                }
+
+                for (int j = 0; j < i; j++)
+                {
+                    Assert.AreEqual(expected[j], array[j]);
+                }
+
+                for (int j = i; j < array.Length; j++)
+                {
+                    Assert.AreEqual(newValue[j], array[j]);
+                }
+            }
+
+            ArrayWithOffset arrayWithOffset = new ArrayWithOffset(new int[]{ 1 }, 0);
+
+            Assert.Throws<MarshalDirectiveException>(() => ArrayWithOffsetNative.Marshal_Invalid(arrayWithOffset));
+            Assert.Throws<MarshalDirectiveException>(() => ArrayWithOffsetNative.Marshal_Invalid(ref arrayWithOffset));
+        }
+        catch (Exception e)
+        {
+            Console.WriteLine(e);
+            return 101;
+        }
+
+        return 100;
+    }
+}
diff --git a/tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetTest.csproj b/tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetTest.csproj
new file mode 100644 (file)
index 0000000..f83106e
--- /dev/null
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <Import Project="../../Interop.settings.targets" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>ArrayWithOffsetTest</AssemblyName>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{F1E66554-8C8E-4141-85CF-D0CD6A0CD0B0}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+    <DefineConstants>$(DefineConstants);STATIC</DefineConstants>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <LangVersion>latest</LangVersion>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"></PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"></PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="*.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="CMakeLists.txt" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
diff --git a/tests/src/Interop/PInvoke/ArrayWithOffset/CMakeLists.txt b/tests/src/Interop/PInvoke/ArrayWithOffset/CMakeLists.txt
new file mode 100644 (file)
index 0000000..5af6a50
--- /dev/null
@@ -0,0 +1,17 @@
+cmake_minimum_required (VERSION 2.6) 
+project (ArrayWithOffsetNative) 
+include ("${CLR_INTEROP_TEST_ROOT}/Interop.cmake") 
+include_directories(${INC_PLATFORM_DIR}) 
+set(SOURCES 
+    ArrayWithOffsetNative.cpp 
+) 
+if(WIN32)
+    list(APPEND LINK_LIBRARIES_ADDITIONAL
+        OleAut32.lib
+    )
+endif(WIN32)
+# add the executable 
+add_library (ArrayWithOffsetNative SHARED ${SOURCES}) 
+target_link_libraries(ArrayWithOffsetNative ${LINK_LIBRARIES_ADDITIONAL}) 
+# add the install targets 
+install (TARGETS ArrayWithOffsetNative DESTINATION bin) 
diff --git a/tests/src/Interop/PInvoke/ArrayWithOffset/PInvokeDefs.cs b/tests/src/Interop/PInvoke/ArrayWithOffset/PInvokeDefs.cs
new file mode 100644 (file)
index 0000000..cefe6fc
--- /dev/null
@@ -0,0 +1,19 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Text;
+using System.Runtime.InteropServices;
+
+unsafe class ArrayWithOffsetNative
+{
+    [DllImport(nameof(ArrayWithOffsetNative))]
+    public static extern bool Marshal_InOut(int* expected, [In, Out] ArrayWithOffset actual, int numElements, int* newValue);
+
+    [DllImport(nameof(ArrayWithOffsetNative))]
+    public static extern bool Marshal_Invalid(ArrayWithOffset invalidArray);
+
+    [DllImport(nameof(ArrayWithOffsetNative))]
+    public static extern bool Marshal_Invalid(ref ArrayWithOffset array);
+}