|
1 | 1 | // #Conformance #Interop #PInvoke #Structs |
2 | 2 |
|
| 3 | +#if TESTS_AS_APP |
| 4 | +module Core_csext |
| 5 | +#endif |
3 | 6 |
|
4 | 7 | #nowarn "9" |
5 | 8 | open System |
6 | 9 | open System.Runtime.InteropServices |
7 | | -open System.Windows.Forms |
8 | 10 | open System.Drawing |
9 | 11 |
|
| 12 | +let failures = ref [] |
10 | 13 |
|
11 | | -[<DllImport("cards.dll")>] |
12 | | -let cdtInit((width: IntPtr), (height: IntPtr)) : unit = () |
13 | | - |
14 | | -let pinned (obj: obj) f = |
15 | | - let gch = GCHandle.Alloc(obj,GCHandleType.Pinned) in |
16 | | - try f(gch.AddrOfPinnedObject()) |
17 | | - finally |
18 | | - gch.Free() |
19 | | - |
20 | | -//The following types from the System namespace are blittable types: |
21 | | -// |
22 | | -//System.Byte |
23 | | -//System.SByte |
24 | | -//System.Int16 |
25 | | -//System.UInt16 |
26 | | -//System.Int32 |
27 | | -//System.UInt32 |
28 | | -//System.Int64 |
29 | | -//System.IntPtr |
30 | | -//System.UIntPtr |
31 | | -//The following complex types are also blittable types: |
32 | | -//One-dimensional arrays of blittable types, such as an array of integers. |
33 | | -//Formatted value types that contain only blittable types (and classes if they are marshaled as formatted types). |
34 | | - |
35 | | -// |
36 | | -// assert ((typeof<'a>) == (typeof<int>) or |
37 | | -// (typeof<'a>) == (typeof<int64>) or |
38 | | -// etc. |
39 | | - |
40 | | -type PinBox<'a> = |
41 | | - { v : obj } |
42 | | - static member Create(x) = { v = box(x) } |
43 | | - member x.Value = (unbox x.v : 'a) |
44 | | - member x.Pin(f) = pinned(x.v) f |
45 | | - |
46 | | -let card_init () = |
47 | | - let width = PinBox<_>.Create(300) in |
48 | | - let height = PinBox<_>.Create(400) in |
49 | | - width.Pin (fun widthAddress -> |
50 | | - height.Pin (fun heightAddress -> |
51 | | - cdtInit (widthAddress, heightAddress))); |
52 | | - Printf.printf "width = %d\n" width.Value; |
53 | | - Printf.printf "height = %d\n" height.Value; |
54 | | - () |
55 | | - |
56 | | -do card_init() |
57 | | - |
58 | | -let asciiz (pBytes: nativeptr<sbyte>) = new System.String(pBytes) |
59 | | - |
60 | | -#nowarn "0044";; |
61 | | -#nowarn "0051";; |
62 | | - |
63 | | -open System |
64 | | -open System.Runtime.InteropServices |
65 | | -open Microsoft.FSharp.NativeInterop |
66 | | - |
67 | | -type voidptr = System.IntPtr |
68 | | - |
69 | | -//int (*derivs)(double, double [], double [], void *), |
70 | | -type DerivsFunction = delegate of double * double nativeptr * double nativeptr * voidptr -> int |
71 | | - |
72 | | -//int (*outputFn)(double, double*, void*) ); |
73 | | -type OutputFunction = delegate of double * double nativeptr * voidptr -> int |
74 | | - |
75 | | -[<DllImport("PopDyn.dll")>] |
76 | | -// Wrap the C function with the following signature: |
77 | | -// |
78 | | -extern int SolveODE2(double *ystart, int nvar, double x1, double x2, double eps, double h1, |
79 | | - double hmin, double hmax, int *nok, int *nbad, double dx, void *info, |
80 | | - DerivsFunction derivs, |
81 | | - OutputFunction outputFn); |
82 | | -module Array = |
83 | | - let inline pinObjUnscoped (obj: obj) = GCHandle.Alloc(obj,GCHandleType.Pinned) |
84 | | - |
85 | | - let inline pinObj (obj: obj) f = |
86 | | - let gch = pinObjUnscoped obj |
87 | | - try f gch |
88 | | - finally |
89 | | - gch.Free() |
90 | | - |
91 | | - [<NoDynamicInvocation>] |
92 | | - let inline pin (arr: 'T []) (f : nativeptr<'T> -> 'U) = |
93 | | - pinObj (box arr) (fun _ -> f (&&arr.[0])) |
94 | | - |
95 | | - |
96 | | -type NativeArray<'T when 'T : unmanaged>(ptr : nativeptr<'T>, len: int) = |
97 | | - member x.Ptr = ptr |
98 | | - [<NoDynamicInvocation>] |
99 | | - member inline x.Item |
100 | | - with get n = NativePtr.get x.Ptr n |
101 | | - and set n v = NativePtr.set x.Ptr n v |
102 | | - member x.Length = len |
103 | | -// Provide a nicer wrapper for use from F# code. This takes an F# array as input, |
104 | | -// and when the callbacks happen wraps up the returned native arrays in the |
105 | | -// F# NativeArray thin wrapper which lets you use nice syntax arr.[n] for getting and |
106 | | -// setting values of these arrays. |
107 | | -let solveODE ystart (x1,x2,eps,h1,hmin,hmax) (nok,nbad) dx derivs outputFn = |
108 | | - Array.pin ystart (fun ystartAddr -> |
109 | | - let nvar = Array.length ystart in |
110 | | - let mutable nok = nok in |
111 | | - let mutable nbad = nbad in |
112 | | - let info = 0n in |
113 | | - let derivsF = new DerivsFunction(fun x arr1 arr2 _ -> derivs x (new NativeArray<_>(arr1,nvar)) (new NativeArray<_>(arr2,nvar))) in |
114 | | - let outputFnF = new OutputFunction(fun x pY _ -> outputFn x) in |
115 | | - SolveODE2(ystartAddr,nvar,x1,x2,eps,h1,hmin,hmax,&&nok,&&nbad,dx,info,derivsF,outputFnF)) |
116 | | - |
117 | | -let example1() = |
118 | | - solveODE |
119 | | - // initial values |
120 | | - [| 1.0; 2.0 |] |
121 | | - // settings |
122 | | - (1.0,2.0,0.0001,1.0,1.0,1.0) |
123 | | - // nok,nbad |
124 | | - (10,20) |
125 | | - // dx |
126 | | - 0.05 |
127 | | - // Compute the derivatives. Note outp and inp are both NativeArrays, passed to us from C. |
128 | | - // So there is no bounds checking on these assignments - be careful! |
129 | | - // If it turns out that these arrays are of static known size then we can do better here. |
130 | | - (fun x inp outp -> |
131 | | - outp.[0] <- inp.[0] + 0.05; 1) |
132 | | - // output |
133 | | - (fun v -> printf "v = %G\n" v; 5) |
134 | | - |
135 | | - |
| 14 | +let report_failure (s : string) = |
| 15 | + stderr.Write" NO: " |
| 16 | + stderr.WriteLine s |
| 17 | + failures := !failures @ [s] |
136 | 18 |
|
137 | 19 | module GetSystemTimeTest = |
138 | 20 | open System |
@@ -241,3 +123,22 @@ module MemoryStatusTest2 = |
241 | 123 |
|
242 | 124 | main() |
243 | 125 |
|
| 126 | +(*--------------------*) |
| 127 | + |
| 128 | +#if TESTS_AS_APP |
| 129 | +let RUN() = !failures |
| 130 | +#else |
| 131 | +let aa = |
| 132 | + match !failures with |
| 133 | + | [] -> |
| 134 | + stdout.WriteLine "Test Passed" |
| 135 | + System.IO.File.WriteAllText("test.ok","ok") |
| 136 | + exit 0 |
| 137 | + | messages -> |
| 138 | + printfn "%A" messages |
| 139 | + stdout.WriteLine "Test Failed" |
| 140 | + exit 1 |
| 141 | +#endif |
| 142 | + |
| 143 | + |
| 144 | + |
0 commit comments