/
StructMarshalTests.cs
89 lines (73 loc) · 3.18 KB
/
StructMarshalTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2018. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using System;
using System.Runtime.InteropServices;
using NUnit.Framework;
using LeapInternal;
namespace Leap.LeapCSharp.Tests {
[StructLayout(LayoutKind.Sequential)]
struct TestMarshaledStruct {
public int id;
public TestMarshaledStruct(int t) { id = t; }
}
[TestFixture()]
public class StructMarshalTests {
public const int ARRAY_SIZE = 5;
public const int ARRAY_TEST_INDEX = 3;
public const int TEST_ID = 23;
private int _size;
private IntPtr _ptr;
private TestMarshaledStruct _testStruct;
[SetUp]
public void Setup() {
_size = Marshal.SizeOf(typeof(TestMarshaledStruct));
//For each test, allocate a chunk of memory large enough for [ARRAY_SIZE] structs
_ptr = Marshal.AllocHGlobal(_size * ARRAY_SIZE);
_testStruct = new TestMarshaledStruct(TEST_ID);
}
[TearDown]
public void Teardown() {
_size = 0;
Marshal.FreeHGlobal(_ptr);
_ptr = IntPtr.Zero;
_testStruct = new TestMarshaledStruct();
}
[Test]
public void SizeTest() {
int reportedSize = StructMarshal<TestMarshaledStruct>.Size;
Assert.That(_size, Is.EqualTo(reportedSize), "Size must match Marshal.SizeOf.");
}
[Test]
public void PtrToStructTest() {
Marshal.StructureToPtr(_testStruct, _ptr, false);
TestMarshaledStruct output;
StructMarshal<TestMarshaledStruct>.PtrToStruct(_ptr, out output);
Assert.That(_testStruct.id, Is.EqualTo(output.id), "Input must match output.");
}
[Test]
public void ArrayElementToStructTest() {
Marshal.StructureToPtr(_testStruct, (IntPtr)((long)_ptr + _size * ARRAY_TEST_INDEX), false);
TestMarshaledStruct output;
StructMarshal<TestMarshaledStruct>.ArrayElementToStruct(_ptr, ARRAY_TEST_INDEX, out output);
Assert.That(_testStruct.id, Is.EqualTo(output.id), "Input must match output.");
}
[Test]
public void CopyIntoDestination() {
StructMarshal<TestMarshaledStruct>.CopyIntoDestination(_ptr, ref _testStruct);
var output = (TestMarshaledStruct)Marshal.PtrToStructure(_ptr, typeof(TestMarshaledStruct));
Assert.That(_testStruct.id, Is.EqualTo(output.id), "Input must match output.");
}
[Test]
public void CopyIntoArray() {
StructMarshal<TestMarshaledStruct>.CopyIntoArray(_ptr, ref _testStruct, ARRAY_TEST_INDEX);
var output = (TestMarshaledStruct)Marshal.PtrToStructure((IntPtr)((long)_ptr + _size * ARRAY_TEST_INDEX), typeof(TestMarshaledStruct));
Assert.That(_testStruct.id, Is.EqualTo(output.id), "Input must match output.");
}
}
}