Created
December 17, 2014 00:08
-
-
Save moto-timo/d9a5b6bd698e723b73d3 to your computer and use it in GitHub Desktop.
Sanity check C# unit tests for ironclad cpymarshaltest throwing an ERROR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using System.Collections.Generic; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using Ironclad.Structs; | |
namespace Ironclad.Tests | |
{ | |
[TestClass] | |
public class CPyMarshalTest32 | |
{ | |
[TestMethod] | |
public void WriteDoubleTest() | |
{ | |
var data = Marshal.AllocHGlobal(CPyMarshal.DoubleSize); | |
CPyMarshal.WriteDouble(data, 2.2e22); | |
var doubleStruct = (DoubleStruct)Marshal.PtrToStructure(data, typeof(DoubleStruct)); | |
Assert.AreEqual(doubleStruct.value, 2.2e22); | |
Marshal.FreeHGlobal(data); | |
} | |
[TestMethod] | |
public void WriteDoubleFieldTest() | |
{ | |
var data = Marshal.AllocHGlobal(Marshal.SizeOf(typeof (PyFloatObject))); | |
CPyMarshal.Zero(data, Marshal.SizeOf(typeof(PyFloatObject))); | |
CPyMarshal.WriteDoubleField(data, typeof(PyFloatObject), "ob_fval", 7.6e-5); | |
var dataStruct = (PyFloatObject) Marshal.PtrToStructure(data, typeof (PyFloatObject)); | |
Assert.AreEqual(dataStruct.ob_fval, 7.6e-5); | |
Marshal.FreeHGlobal(data); | |
} | |
[TestMethod] | |
public void WriteIntFieldTest() | |
{ | |
var data = Marshal.AllocHGlobal(Marshal.SizeOf(typeof (PyIntObject))); | |
CPyMarshal.Zero(data, Marshal.SizeOf(typeof (PyIntObject))); | |
var values = new List<Int32> {Int32.MaxValue, Int32.MinValue}; | |
foreach(var value in values) | |
{ | |
CPyMarshal.WriteIntField(data, typeof(PyIntObject), "ob_ival", value); | |
var dataStruct = (PyIntObject) Marshal.PtrToStructure(data, typeof (PyIntObject)); | |
Assert.AreEqual(dataStruct.ob_ival, value, "failed to write"); | |
} | |
Marshal.FreeHGlobal(data); | |
} | |
[TestMethod] | |
public void WritePtrFieldTest() | |
{ | |
var data = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PyObject))); | |
CPyMarshal.Zero(data, Marshal.SizeOf(typeof(PyObject))); | |
CPyMarshal.WritePtrField(data, typeof(PyObject), "ob_type", new IntPtr(12345)); | |
var dataStruct = (PyObject) Marshal.PtrToStructure(data, typeof (PyObject)); | |
Assert.AreEqual(dataStruct.ob_type, new IntPtr(12345), "failed to write"); | |
Marshal.FreeHGlobal(data); | |
} | |
[TestMethod] | |
public void WriteUIntFieldTest() | |
{ | |
var data = Marshal.AllocHGlobal(Marshal.SizeOf(typeof (PyTypeObject))); | |
CPyMarshal.Zero(data, Marshal.SizeOf(typeof(PyTypeObject))); | |
var values = new List<UInt32> {UInt32.MaxValue, UInt32.MinValue}; | |
foreach (var value in values) | |
{ | |
CPyMarshal.WriteUIntField(data, typeof(PyTypeObject), "tp_version_tag", value); | |
var dataStruct = (PyTypeObject) Marshal.PtrToStructure(data, typeof (PyTypeObject)); | |
Assert.AreEqual(dataStruct.tp_version_tag, value, "failed to write"); | |
} | |
Marshal.FreeHGlobal(data); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment