summaryrefslogtreecommitdiff
path: root/tests/src
diff options
context:
space:
mode:
authorJeremy Koritzinsky <jkoritzinsky@gmail.com>2018-11-15 23:48:16 -0800
committerGitHub <noreply@github.com>2018-11-15 23:48:16 -0800
commit78aa753ded9031c21a4b2804aeeb36ba43cbafa6 (patch)
treeefd1e933fa34f731131a5dcdbee3d7e328782b9f /tests/src
parent54bda7f874c435df8620949363bf3cf53b51aaeb (diff)
downloadcoreclr-78aa753ded9031c21a4b2804aeeb36ba43cbafa6.tar.gz
coreclr-78aa753ded9031c21a4b2804aeeb36ba43cbafa6.tar.bz2
coreclr-78aa753ded9031c21a4b2804aeeb36ba43cbafa6.zip
Add simple tests for ArrayWithOffset marshaling. (#21013)
* Add simple tests for ArrayWithOffset marshaling. * PR Feedback. * Fix visibility * Fix DllImport library name
Diffstat (limited to 'tests/src')
-rw-r--r--tests/src/Interop/CMakeLists.txt1
-rw-r--r--tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetNative.cpp19
-rw-r--r--tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetTest.cs54
-rw-r--r--tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetTest.csproj33
-rw-r--r--tests/src/Interop/PInvoke/ArrayWithOffset/CMakeLists.txt17
-rw-r--r--tests/src/Interop/PInvoke/ArrayWithOffset/PInvokeDefs.cs19
6 files changed, 143 insertions, 0 deletions
diff --git a/tests/src/Interop/CMakeLists.txt b/tests/src/Interop/CMakeLists.txt
index 37c24df20e..901bd9e995 100644
--- a/tests/src/Interop/CMakeLists.txt
+++ b/tests/src/Interop/CMakeLists.txt
@@ -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
index 0000000000..44d6e83795
--- /dev/null
+++ b/tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetNative.cpp
@@ -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
index 0000000000..9f3d91ad9e
--- /dev/null
+++ b/tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetTest.cs
@@ -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
index 0000000000..f83106e248
--- /dev/null
+++ b/tests/src/Interop/PInvoke/ArrayWithOffset/ArrayWithOffsetTest.csproj
@@ -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
index 0000000000..5af6a506f1
--- /dev/null
+++ b/tests/src/Interop/PInvoke/ArrayWithOffset/CMakeLists.txt
@@ -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
index 0000000000..cefe6fc51a
--- /dev/null
+++ b/tests/src/Interop/PInvoke/ArrayWithOffset/PInvokeDefs.cs
@@ -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);
+}