copying the latest Sys.Web.Services from trunk.
[mono.git] / mono / tests / marshal9.cs
1 //
2 // marshal9.cs: tests for custom marshalling
3 //
4
5 using System;
6 using System.Runtime.InteropServices;
7
8 public class Marshal1 : ICustomMarshaler
9 {
10         int param;
11
12         public static int cleanup_managed_count = 0;
13
14         public static int cleanup_native_count = 0;
15
16         public Marshal1 (int param) {
17                 this.param = param;
18         }
19
20         public static ICustomMarshaler GetInstance (string s) {
21                 int param = Int32.Parse (s);
22                 return new Marshal1 (param);
23         }
24
25         public void CleanUpManagedData (object managedObj)
26         {
27                 //Console.WriteLine ("CleanUpManagedData called");
28                 cleanup_managed_count ++;
29         }
30
31         public void CleanUpNativeData (IntPtr pNativeData)
32         {
33                 //Console.WriteLine("CleanUpNativeData:" + pNativeData);
34                 /* Might be allocated in libtest.c using g_new0 so dont free it */
35                 int alloc_type = Marshal.ReadInt32 (pNativeData);
36                 if (alloc_type == 1)
37                         Marshal.FreeHGlobal (pNativeData);
38                 cleanup_native_count ++;
39         }
40
41         // I really do not understand the purpose of this method
42         // or went it would be called. In fact, Rotor never seems
43         // to call it.
44         public int GetNativeDataSize ()
45         {
46                 //Console.WriteLine("GetNativeDataSize() called");
47                 return 4;
48         }
49
50         public IntPtr MarshalManagedToNative (object managedObj)
51         {
52                 int number;
53                 IntPtr ptr;
54
55                 number = Convert.ToInt32 (managedObj);
56                 ptr = Marshal.AllocHGlobal (8);
57                 Marshal.WriteInt32 (ptr, 1);  /* Allocated by AllocHGlobal */
58                 Marshal.WriteInt32(new IntPtr (ptr.ToInt64 () + 4), number);
59
60                 //Console.WriteLine ("ToNative: " + ptr);
61                 return ptr;
62         }
63
64         public object MarshalNativeToManaged (IntPtr pNativeData)
65         {
66                 //Console.WriteLine ("ToManaged: " + pNativeData);
67                 return param + Marshal.ReadInt32 (new IntPtr (pNativeData.ToInt64 () + 4));
68         }
69 }
70
71 public class Tests
72 {
73         public static int Main (string[] args) {
74                 return TestDriver.RunTests (typeof (Tests));
75         }
76
77         [DllImport ("libtest")]
78         [return : MarshalAs(UnmanagedType.CustomMarshaler,MarshalTypeRef = typeof
79                                                 (Marshal1), MarshalCookie = "5")]
80         private static extern object mono_test_marshal_pass_return_custom (int i,  
81                                                                                                                                         [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object number, int j);
82
83         public static int test_0_pass_return () {
84
85                 Marshal1.cleanup_managed_count = 0;
86                 Marshal1.cleanup_native_count = 0;
87
88                 int res = (int)mono_test_marshal_pass_return_custom (5, 10, 5);
89
90                 if (Marshal1.cleanup_managed_count != 0)
91                         return 1;
92                 if (Marshal1.cleanup_native_count != 2)
93                         return 2;
94
95                 return res == 15 ? 0 : 3;
96         }
97
98         [return : MarshalAs(UnmanagedType.CustomMarshaler,MarshalTypeRef = typeof
99 (Marshal1), MarshalCookie = "5")] public delegate object pass_return_int_delegate ([MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object o);
100
101         [DllImport ("libtest")]
102         private static extern int mono_test_marshal_pass_return_custom_in_delegate (pass_return_int_delegate del);
103
104         private static object pass_return_int (object i) {
105                 return (int)i;
106         }
107
108         public static int test_0_pass_return_delegate () {
109
110                 Marshal1.cleanup_managed_count = 0;
111                 Marshal1.cleanup_native_count = 0;
112
113                 int res = mono_test_marshal_pass_return_custom_in_delegate (new pass_return_int_delegate (pass_return_int));
114
115                 if (Marshal1.cleanup_managed_count != 2)
116                         return 1;
117                 if (Marshal1.cleanup_native_count != 0)
118                         return 2;
119
120                 return res == 15 ? 0 : 3;
121         }
122 }