miranda/Hyphen/Plugins/UnitTests/UnmanagedStructHandleTest.cs
2013-06-25 22:53:41 +00:00

179 lines
5.9 KiB
C#

// The following code was generated by Microsoft Visual Studio 2005.
// The test owner should check each test for validity.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
using Virtuoso.Miranda.Plugins.Native;
using System.Runtime.InteropServices;
namespace Virtuoso.Miranda.Plugins.UnitTests
{
/// <summary>
///This is a test class for Virtuoso.Miranda.Plugins.Native.UnmanagedStructHandle&lt;T&gt; and is intended
///to contain all Virtuoso.Miranda.Plugins.Native.UnmanagedStructHandle&lt;T&gt; Unit Tests
///</summary>
[TestClass()]
public class UnmanagedStructHandleTest
{
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the class
//
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//Use ClassCleanup to run code after all tests in a class have run
//
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Use TestCleanup to run code after each test has run
//
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion
private const int TestDataValue = 100;
private int TestData;
private UnmanagedStructHandle<int> TestHandle;
[TestInitialize]
public void TestInitialize()
{
this.TestData = TestDataValue;
this.TestHandle = new UnmanagedStructHandle<int>(ref this.TestData);
}
[TestCleanup]
public void TestCleanup()
{
this.TestHandle.Free();
}
/// <summary>
///A test for Free ()
///</summary>
[TestMethod()]
public void FreeTest()
{
this.TestHandle.Free();
Assert.AreEqual<IntPtr>(IntPtr.Zero, this.TestHandle.IntPtr, "Free method did not zeroed the memory handle.");
this.TestHandle = UnmanagedStructHandle<int>.Empty;
this.TestHandle.Free();
IntPtr ptr = Marshal.AllocHGlobal(1);
Marshal.StructureToPtr(0xac, ptr, false);
this.TestHandle = new UnmanagedStructHandle<int>(ref this.TestData, ptr);
this.TestHandle.Free();
Assert.AreNotEqual<byte>(0xac, Marshal.ReadByte(ptr), "Free did not released the single-pressure.");
ptr = Marshal.AllocHGlobal(1);
Marshal.StructureToPtr(0xac, ptr, false);
this.TestHandle = new UnmanagedStructHandle<int>(ref this.TestData, new IntPtr[] { ptr });
this.TestHandle.Free();
Assert.AreNotEqual<byte>(0xac, Marshal.ReadByte(ptr), "Free did not released the pressure.");
}
/// <summary>
///A test for implicit operator (UnmanagedStructHandle&lt;T&gt;)
///</summary>
[TestMethod()]
public void ConversionTest()
{
IntPtr intPtr = this.TestHandle;
Assert.AreEqual<IntPtr>(this.TestHandle.IntPtr, intPtr, "Implicit conversion to IntPtr failed.");
}
/// <summary>
///A test for implicit operator (UnmanagedStructHandle&lt;T&gt;)
///</summary>
[TestMethod()]
public void ConversionTest1()
{
UIntPtr uintPtr = this.TestHandle;
Assert.AreEqual<UIntPtr>(new UIntPtr((ulong)this.TestHandle.IntPtr.ToInt64()), uintPtr, "Implicit conversion to UIntPtr failed.");
}
[TestMethod]
public void IntPtrTest()
{
this.TestHandle.Free();
Assert.AreEqual<IntPtr>(IntPtr.Zero, this.TestHandle.IntPtr);
}
/// <summary>
///A test for MarshalBack (out T)
///</summary>
[TestMethod()]
public void MarshalBackTest()
{
// Value type test
int newInt = -100;
Marshal.StructureToPtr(newInt, this.TestHandle.IntPtr, false);
int currentInt;
this.TestHandle.MarshalBack(out currentInt);
Assert.AreEqual<int>(newInt, currentInt, "MarshalBack method did not marshalled data back correctly.");
}
[TestMethod]
public void CtorTest()
{
int i = 100;
this.TestHandle = new UnmanagedStructHandle<int>(ref i, MarshalKind.Copy);
Marshal.WriteInt32(this.TestHandle, -100);
this.TestHandle.MarshalBack(out i);
Assert.AreEqual<int>(-100, i, "Copy method does not work properly.");
this.TestHandle.Free();
i = 100;
this.TestHandle = new UnmanagedStructHandle<int>(ref i, MarshalKind.PinBlittable);
Marshal.WriteInt32(this.TestHandle, -100);
this.TestHandle.MarshalBack(out i);
Assert.AreEqual<int>(-100, i, "PinBlittable method does not work properly.");
this.TestHandle.Free();
}
}
}