Add PInvoke/Primitives/Int tests (dotnet/coreclr#19304)
authorZeng Jiang <v-jiazen@microsoft.com>
Thu, 18 Oct 2018 17:19:12 +0000 (01:19 +0800)
committerJeremy Koritzinsky <jkoritzinsky@gmail.com>
Thu, 18 Oct 2018 17:19:12 +0000 (10:19 -0700)
* Add PInvoke/Primitives/Int tests

* Clean up Int tests to pass xplat.

Commit migrated from https://github.com/dotnet/coreclr/commit/577aafcdc7122649cb4c74c617aef597d194ac05

src/coreclr/tests/src/Interop/CMakeLists.txt
src/coreclr/tests/src/Interop/PInvoke/Primitives/Int/CMakeLists.txt [new file with mode: 0644]
src/coreclr/tests/src/Interop/PInvoke/Primitives/Int/PInvokeIntNative.cpp [new file with mode: 0644]
src/coreclr/tests/src/Interop/PInvoke/Primitives/Int/PInvokeIntTest.cs [new file with mode: 0644]
src/coreclr/tests/src/Interop/PInvoke/Primitives/Int/PInvokeIntTest.csproj [new file with mode: 0644]

index 7b509d6..934ab7a 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/Primitives/Int)
 add_subdirectory(NativeCallable)
 add_subdirectory(PrimitiveMarshalling/Bool)
 add_subdirectory(PrimitiveMarshalling/UIntPtr)
diff --git a/src/coreclr/tests/src/Interop/PInvoke/Primitives/Int/CMakeLists.txt b/src/coreclr/tests/src/Interop/PInvoke/Primitives/Int/CMakeLists.txt
new file mode 100644 (file)
index 0000000..ba99147
--- /dev/null
@@ -0,0 +1,8 @@
+cmake_minimum_required (VERSION 2.6) 
+project (PInvokeIntNative) 
+include ("${CLR_INTEROP_TEST_ROOT}/Interop.cmake") 
+include_directories(${INC_PLATFORM_DIR}) 
+set(SOURCES PInvokeIntNative.cpp) 
+add_library (PInvokeIntNative SHARED ${SOURCES}) 
+# add the install targets 
+install (TARGETS PInvokeIntNative DESTINATION bin) 
diff --git a/src/coreclr/tests/src/Interop/PInvoke/Primitives/Int/PInvokeIntNative.cpp b/src/coreclr/tests/src/Interop/PInvoke/Primitives/Int/PInvokeIntNative.cpp
new file mode 100644 (file)
index 0000000..b18456c
--- /dev/null
@@ -0,0 +1,115 @@
+// 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 <stdio.h>
+#include <xplatform.h>
+
+int intManaged = 1000;
+int intNative = 2000;
+int intReturn = 3000;
+int intErrReturn = 4000;
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE Marshal_In(/*[in]*/int intValue)
+{
+    //Check the input
+    if(intValue != intManaged)
+    {
+        printf("Error in Function Marshal_In(Native Client)\n");
+
+        //Expected
+        printf("Expected:%u\n", intManaged);
+
+        //Actual
+        printf("Actual:%u\n",intValue);
+
+        //Return the error value instead if verification failed
+        return intErrReturn;
+    }
+
+    return intReturn;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE Marshal_InOut(/*[In,Out]*/int intValue)
+{
+    //Check the input
+    if(intValue != intManaged)
+    {
+        printf("Error in Function Marshal_InOut(Native Client)\n");
+
+        //Expected
+        printf("Expected:%u\n", intManaged);
+
+        //Actual
+        printf("Actual:%u\n",intValue);
+
+        //Return the error value instead if verification failed
+        return intErrReturn;
+    }
+
+    //In-Place Change
+    intValue = intNative;
+
+    //Return
+    return intReturn;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE Marshal_Out(/*[Out]*/int intValue)
+{
+    intValue = intNative;
+
+    //Return
+    return intReturn;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE MarshalPointer_In(/*[in]*/int *pintValue)
+{
+    //Check the input
+    if(*pintValue != intManaged)
+    {
+        printf("Error in Function MarshalPointer_In(Native Client)\n");
+
+        //Expected
+        printf("Expected:%u\n", intManaged);
+
+        //Actual
+        printf("Actual:%u\n",*pintValue);
+
+        //Return the error value instead if verification failed
+        return intErrReturn;
+    }
+    
+    return intReturn;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE MarshalPointer_InOut(/*[in,out]*/int *pintValue)
+{
+    //Check the input
+    if(*pintValue != intManaged)
+    {
+        printf("Error in Function MarshalPointer_InOut(Native Client)\n");
+
+        //Expected
+        printf("Expected:%u\n", intManaged);
+
+        //Actual
+        printf("Actual:%u\n",*pintValue);
+
+        //Return the error value instead if verification failed
+        return intErrReturn;
+    }
+
+    //In-Place Change
+    *pintValue = intNative;
+
+    //Return
+    return intReturn;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE MarshalPointer_Out(/*[out]*/ int *pintValue)
+{
+    *pintValue = intNative;
+
+    //Return
+    return intReturn;
+}
diff --git a/src/coreclr/tests/src/Interop/PInvoke/Primitives/Int/PInvokeIntTest.cs b/src/coreclr/tests/src/Interop/PInvoke/Primitives/Int/PInvokeIntTest.cs
new file mode 100644 (file)
index 0000000..3a87a78
--- /dev/null
@@ -0,0 +1,105 @@
+// 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.Runtime.InteropServices;
+using System;
+
+class ClientPInvokeIntNativeTest
+{
+    [DllImport("PInvokeIntNative")]
+    private static extern int Marshal_In([In]int intValue);
+
+    [DllImport("PInvokeIntNative")]
+    private static extern int Marshal_InOut([In, Out]int intValue);
+
+    [DllImport("PInvokeIntNative")]
+    private static extern int Marshal_Out([Out]int intValue);
+
+    [DllImport("PInvokeIntNative")]
+    private static extern int MarshalPointer_In([In]ref int pintValue);
+
+    [DllImport("PInvokeIntNative")]
+    private static extern int MarshalPointer_InOut(ref int pintValue);
+
+    [DllImport("PInvokeIntNative")]
+    private static extern int MarshalPointer_Out(out int pintValue);
+
+
+    public static int Main(string[] args)
+    {
+        int failures = 0;
+        int intManaged = (int)1000;
+        int intNative = (int)2000;
+        int intReturn = (int)3000;
+
+        int int1 = intManaged;
+        if(intReturn != Marshal_In(int1))
+        {
+            failures++;
+            Console.WriteLine("In return value is wrong");
+        }
+
+        int int2 = intManaged;
+        if (intReturn != Marshal_InOut(int2))
+        {
+            failures++;
+            Console.WriteLine("InOut return value is wrong");
+        }
+        if (intManaged != int2)
+        {
+            failures++;
+            Console.WriteLine("InOut byval parameter value changed.");
+        }
+
+        int int3 = intManaged;
+        if (intReturn != Marshal_InOut(int3))
+        {
+            failures++;
+            Console.WriteLine("Out return value is wrong");
+        }
+        if (intManaged != int3)
+        {
+            failures++;
+            Console.WriteLine("Out byval parameter value changed.");
+        }
+
+        int int4 = intManaged;
+        if (intReturn != MarshalPointer_In(ref int4))
+        {
+            failures++;
+            Console.WriteLine("RefIn return value is wrong");
+        }
+        if (intManaged != int4)
+        {
+            failures++;
+            Console.WriteLine("In byref parameter value changed.");
+        }
+
+        int int5 = intManaged;
+        if (intReturn != MarshalPointer_InOut(ref int5))
+        {
+            failures++;
+            Console.WriteLine("RefInOut return value is wrong");
+        }
+        if (intNative != int5)
+        {
+            failures++;
+            Console.WriteLine("InOut byref value is wrong.");
+        }
+
+        int int6 = intManaged;
+        if (intReturn != MarshalPointer_Out(out int6))
+        {
+            failures++;
+            Console.WriteLine("RefInOut return value is wrong");
+        }
+        if (intNative != int6)
+        {
+            failures++;
+            Console.WriteLine("RefInOut byref value is wrong.");
+        }
+
+        return 100 + failures;
+    }
+}
diff --git a/src/coreclr/tests/src/Interop/PInvoke/Primitives/Int/PInvokeIntTest.csproj b/src/coreclr/tests/src/Interop/PInvoke/Primitives/Int/PInvokeIntTest.csproj
new file mode 100644 (file)
index 0000000..ba9fb9d
--- /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" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>PInvokeIntTest</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>
+  </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="PInvokeIntTest.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="CMakeLists.txt" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>