11#r" TestLibrary.dll"
22
3- let x = TestType.Get( 100 )
4- if x= 100 then System.IO.File.WriteAllText( " test.ok" , " " )
3+ let mutable errors : string list = []
4+ let fail msg =
5+ printfn" FAILURE:%s " msg
6+ errors<- errors@ [ msg]
7+
8+ module Test1 =
9+ let x = TestType.Get( 100 )
10+ if x<> 100 then fail" Test 1 failed, expected 100"
11+
12+
13+
14+ module TestPack1 =
15+ open System.Runtime .InteropServices
16+ [<type: StructLayout( LayoutKind.Sequential) >]
17+ type LayoutType =
18+ struct
19+ val a : int
20+ end
21+ if ( sizeof< LayoutType>) <> 4 then fail" TestPack1, expected size 4"
22+
23+ module TestPack2 =
24+ open System.Runtime .InteropServices
25+ [<type: StructLayout( LayoutKind.Sequential, Pack= 2 ) >]
26+ type LayoutType =
27+ struct
28+ val mutable b : byte
29+ val mutable a : int
30+ end
31+ let sz = ( sizeof< LayoutType>)
32+ let mutable n = LayoutType()
33+ let a1 = NativeInterop.NativePtr.toNativeInt&& n.a- NativeInterop.NativePtr.toNativeInt&& n
34+ let b1 = NativeInterop.NativePtr.toNativeInt&& n.b- NativeInterop.NativePtr.toNativeInt&& n
35+ let got = ( sz, a1, b1)
36+ printfn" got%A " got
37+ let expected = ( 6 , 2 n, 0 n)
38+ if got<> expectedthen fail( sprintf" TestPack2: got%A , expected%A " got expected)
39+
40+ // From http://bytes.com/topic/c-sharp/answers/654343-what-does-structlayoutattribute-pack-do
41+ module TestPack3 =
42+ open System.Runtime .InteropServices
43+ [<type: StructLayout( LayoutKind.Sequential, Pack= 8 , Size= 128 ) >]
44+ type LayoutType =
45+ struct
46+ val mutable b1 : byte
47+ val mutable b2 : byte
48+ val mutable l1 : int64
49+ val mutable i1 : int
50+ end
51+ let sz = ( sizeof< LayoutType>)
52+ let mutable n = LayoutType()
53+ let b1off = NativeInterop.NativePtr.toNativeInt&& n.b1- NativeInterop.NativePtr.toNativeInt&& n
54+ let b2off = NativeInterop.NativePtr.toNativeInt&& n.b2- NativeInterop.NativePtr.toNativeInt&& n
55+ let l1off = NativeInterop.NativePtr.toNativeInt&& n.l1- NativeInterop.NativePtr.toNativeInt&& n
56+ let i1off = NativeInterop.NativePtr.toNativeInt&& n.i1- NativeInterop.NativePtr.toNativeInt&& n
57+ let got = ( sz, b1off, b2off, l1off, i1off)
58+ let expected = ( 128 , 0 n, 1 n, 8 n, 16 n)
59+ printfn" got%A " got
60+ if got<> expectedthen fail( sprintf" TestPack3: got%A , expected%A " got expected)
61+
62+
63+ // Test explicit layout
64+ module TestPack4 =
65+ open System.Runtime .InteropServices
66+ [<type: StructLayout( LayoutKind.Explicit, Pack= 8 , Size= 128 ) >]
67+ type LayoutType =
68+ struct
69+ [<FieldOffset( 0 ) >]
70+ val mutable b1 : byte
71+ [<FieldOffset( 4 ) >]
72+ val mutable b2 : byte
73+ [<FieldOffset( 8 ) >]
74+ val mutable l1 : int64
75+ [<FieldOffset( 12 ) >]
76+ val mutable i1 : int
77+ end
78+ let sz = ( sizeof< LayoutType>)
79+ let mutable n = LayoutType()
80+ let b1off = NativeInterop.NativePtr.toNativeInt&& n.b1- NativeInterop.NativePtr.toNativeInt&& n
81+ let b2off = NativeInterop.NativePtr.toNativeInt&& n.b2- NativeInterop.NativePtr.toNativeInt&& n
82+ let l1off = NativeInterop.NativePtr.toNativeInt&& n.l1- NativeInterop.NativePtr.toNativeInt&& n
83+ let i1off = NativeInterop.NativePtr.toNativeInt&& n.i1- NativeInterop.NativePtr.toNativeInt&& n
84+ let got = ( sz, b1off, b2off, l1off, i1off)
85+ let expected = ( 128 , 0 n, 4 n, 8 n, 12 n)
86+ printfn" got%A " got
87+ if got<> expectedthen fail( sprintf" TestPack4: got%A , expected%A " got expected)
88+
89+
90+ if errors.IsEmptythen
91+ System.IO.File.WriteAllText( " test.ok" , " " )
92+ else
93+ for errorin errorsdo
94+ printfn" ERROR:%s " error
95+
96+ if errors.IsEmptythen System.IO.File.WriteAllText( " test.ok" , " " )
97+ else
98+ for errorin errorsdo
99+ printfn" ERROR:%s " error
100+