// 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 { /// ///This is a test class for Virtuoso.Miranda.Plugins.Native.UnmanagedStructHandle<T> and is intended ///to contain all Virtuoso.Miranda.Plugins.Native.UnmanagedStructHandle<T> Unit Tests /// [TestClass()] public class UnmanagedStructHandleTest { private TestContext testContextInstance; /// ///Gets or sets the test context which provides ///information about and functionality for the current test run. /// 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 TestHandle; [TestInitialize] public void TestInitialize() { this.TestData = TestDataValue; this.TestHandle = new UnmanagedStructHandle(ref this.TestData); } [TestCleanup] public void TestCleanup() { this.TestHandle.Free(); } /// ///A test for Free () /// [TestMethod()] public void FreeTest() { this.TestHandle.Free(); Assert.AreEqual(IntPtr.Zero, this.TestHandle.IntPtr, "Free method did not zeroed the memory handle."); this.TestHandle = UnmanagedStructHandle.Empty; this.TestHandle.Free(); IntPtr ptr = Marshal.AllocHGlobal(1); Marshal.StructureToPtr(0xac, ptr, false); this.TestHandle = new UnmanagedStructHandle(ref this.TestData, ptr); this.TestHandle.Free(); Assert.AreNotEqual(0xac, Marshal.ReadByte(ptr), "Free did not released the single-pressure."); ptr = Marshal.AllocHGlobal(1); Marshal.StructureToPtr(0xac, ptr, false); this.TestHandle = new UnmanagedStructHandle(ref this.TestData, new IntPtr[] { ptr }); this.TestHandle.Free(); Assert.AreNotEqual(0xac, Marshal.ReadByte(ptr), "Free did not released the pressure."); } /// ///A test for implicit operator (UnmanagedStructHandle<T>) /// [TestMethod()] public void ConversionTest() { IntPtr intPtr = this.TestHandle; Assert.AreEqual(this.TestHandle.IntPtr, intPtr, "Implicit conversion to IntPtr failed."); } /// ///A test for implicit operator (UnmanagedStructHandle<T>) /// [TestMethod()] public void ConversionTest1() { UIntPtr uintPtr = this.TestHandle; Assert.AreEqual(new UIntPtr((ulong)this.TestHandle.IntPtr.ToInt64()), uintPtr, "Implicit conversion to UIntPtr failed."); } [TestMethod] public void IntPtrTest() { this.TestHandle.Free(); Assert.AreEqual(IntPtr.Zero, this.TestHandle.IntPtr); } /// ///A test for MarshalBack (out T) /// [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(newInt, currentInt, "MarshalBack method did not marshalled data back correctly."); } [TestMethod] public void CtorTest() { int i = 100; this.TestHandle = new UnmanagedStructHandle(ref i, MarshalKind.Copy); Marshal.WriteInt32(this.TestHandle, -100); this.TestHandle.MarshalBack(out i); Assert.AreEqual(-100, i, "Copy method does not work properly."); this.TestHandle.Free(); i = 100; this.TestHandle = new UnmanagedStructHandle(ref i, MarshalKind.PinBlittable); Marshal.WriteInt32(this.TestHandle, -100); this.TestHandle.MarshalBack(out i); Assert.AreEqual(-100, i, "PinBlittable method does not work properly."); this.TestHandle.Free(); } } }