Rename metadata to typelib in variable names, comments and apis.
[gnome.gobject-introspection] / tests / invoke / invoke.c
1 #include <stdlib.h>
2 #include <dlfcn.h>
3 #include <string.h>
4
5 #include <glib.h>
6 #include <girepository.h>
7
8 int
9 main (int argc, char *argv[])
10 {
11   const gchar *testfns = "./testfns.la";
12   GModule *handle;
13   GIRepository *rep;
14   GIBaseInfo *info;
15   GIFunctionInfo *function;
16   GArgument in_args[3];
17   GArgument out_args[3];
18   GArgument retval;
19   gint res;
20   gchar *blurb;
21   gint len;
22   GError *error = NULL;
23   const gchar *name;
24   
25   g_type_init ();
26
27   rep = g_irepository_get_default ();
28
29   g_print ("before dlopening %s: %d infos in the repository\n", 
30            testfns,
31            g_irepository_get_n_infos (rep, "test"));
32
33   if (argc == 1)
34     {
35       handle = g_module_open (testfns, 0);
36       if (!handle)
37         {
38           g_error ("module open failed: %s\n", g_module_error ());
39           return;
40         }
41     }
42   else
43     {
44       name = g_irepository_register_file (rep, "test", &error);
45       if (error)
46         {
47           g_error ("Unable to load typelib 'test': %s", error->message);
48           return;
49         }
50       g_print ("Loaded %s from test.gmeta\n", name);
51     }
52
53   g_print ("after dlopening %s: %d infos in the repository\n", 
54            testfns,
55            g_irepository_get_n_infos (rep, "test"));
56
57   /* test1 calculates x + 4, 
58    * taking x as an in parameter
59    * and returning the result 
60    */
61   info = g_irepository_find_by_name (rep, "test", "test1");  
62   g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
63   function = (GIFunctionInfo *)info;
64
65   retval.v_int = 0;
66   in_args[0].v_int = 4;
67   if (!g_function_info_invoke (function, in_args, 1, NULL, 0, &retval, &error))
68     g_print ("Invokation of %s failed: %s\n",
69              g_base_info_get_name (info),
70              error->message);
71
72   g_assert (retval.v_int == 8);
73   g_base_info_unref (info);
74   
75   /* test2 calculates x + 4, 
76    * taking x as an in parameter
77    * and storing the result in an out parameter
78    */
79   info = g_irepository_find_by_name (rep, "test", "test2");  
80   g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
81   function = (GIFunctionInfo *)info;
82
83   in_args[0].v_int = 5;
84   res = 0;
85   out_args[0].v_pointer = &res;
86   if (!g_function_info_invoke (function, in_args, 1, out_args, 1, &retval, &error))
87     g_print ("Invokation of %s failed: %s\n",
88              g_base_info_get_name (info),
89              error->message);
90
91   g_assert (res == 9);
92   g_base_info_unref (info);
93   
94   /* test3 calculates x + 4, 
95    * taking x as an inout parameter
96    * and storing the result in the same parameter
97    */
98   info = g_irepository_find_by_name (rep, "test", "test3");  
99   g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
100   function = (GIFunctionInfo *)info;
101
102   res = 6;
103   in_args[0].v_pointer = out_args[0].v_pointer = &res;
104   if (!g_function_info_invoke (function, in_args, 1, out_args, 1, &retval, &error))
105     g_print ("Invokation of %s failed: %s\n",
106              g_base_info_get_name (info),
107              error->message);
108
109   g_assert (res == 10);
110   g_base_info_unref (info);
111
112   /* test4 prints out a string
113    */
114   info = g_irepository_find_by_name (rep, "test", "test4");  
115   g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
116   function = (GIFunctionInfo *)info;
117
118   in_args[0].v_pointer = "hello world\n";
119   if (!g_function_info_invoke (function, in_args, 1, NULL, 0, NULL, &error))
120     g_print ("Invokation of %s failed: %s\n",
121              g_base_info_get_name (info),
122              error->message);
123
124   g_base_info_unref (info);
125
126   /* test5 returns a string and a length
127    */
128   info = g_irepository_find_by_name (rep, "test", "test5");  
129   g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
130   function = (GIFunctionInfo *)info;
131
132   blurb = NULL;
133   len = 0;
134   out_args[0].v_pointer = &blurb;
135   out_args[1].v_pointer = &len;
136   if (!g_function_info_invoke (function, NULL, 0, out_args, 2, NULL, &error))
137     g_print ("Invokation of %s failed: %s\n",
138              g_base_info_get_name (info),
139              error->message);
140   
141   g_assert (strcmp (blurb, "hey there") == 0);
142   g_assert (len == strlen (blurb));
143   g_base_info_unref (info);
144   
145
146   /* test GList*/
147   g_print ("Test 6");
148   info = g_irepository_find_by_name (rep, "test", "test6");
149   g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
150   function = (GIFunctionInfo *)info;
151
152   {
153     GList *list = NULL;
154     list = g_list_prepend (list, GINT_TO_POINTER(1));
155     list = g_list_prepend (list, GINT_TO_POINTER(2));
156     retval.v_int = 0;
157     in_args[0].v_pointer = out_args[0].v_pointer = list;
158     if (!g_function_info_invoke (function, in_args, 1, NULL, 0, &retval, &error))
159       g_print ("Invokation of %s failed: %s\n",
160                g_base_info_get_name (info),
161                error->message);
162
163     g_print("returned %d", retval.v_int);
164     g_assert (retval.v_int == 2);
165     g_list_free (list);
166   }
167
168   g_base_info_unref (info);
169   g_clear_error (&error);
170
171   /* test GList more, transfer ownership*/
172   g_print ("Test 7");
173   info = g_irepository_find_by_name (rep, "test", "test7");
174   g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
175   function = (GIFunctionInfo *)info;
176
177   {
178     GList *list = NULL;
179     list = g_list_prepend (list, g_strdup("there..."));
180     list = g_list_prepend (list, g_strdup("Hey "));
181     retval.v_pointer = NULL;
182     in_args[0].v_pointer = out_args[0].v_pointer = list;
183     if (!g_function_info_invoke (function, in_args, 1, NULL, 0, &retval, &error))
184       g_print ("Invokation of %s failed: %s\n",
185                g_base_info_get_name (info),
186                error->message);
187
188     g_print("returned %s", retval.v_pointer);
189     g_assert (strcmp(retval.v_pointer, "Hey there...")==0);
190     g_list_foreach (list, (GFunc) g_free, NULL);
191     g_list_free (list);
192 #if 0
193     g_assert (g_callable_info_get_caller_owns ((GICallableInfo *)function) ==
194               GI_TRANSFER_EVERYTHING);
195 #endif    
196     g_free (retval.v_pointer);
197   }
198
199   g_base_info_unref (info);
200   g_clear_error (&error);
201
202   /* test error handling */
203
204 #if 0
205   /* "broken" is in the typelib but not in the .so*/
206   info = g_irepository_find_by_name (rep, "test", "broken");  
207   g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
208   function = (GIFunctionInfo *)info;
209
210   if (!g_function_info_invoke (function, NULL, 0, NULL, 0, NULL, &error))
211     g_print ("Invokation of %s failed: %s\n",
212              g_base_info_get_name (info),
213              error->message);
214
215   g_base_info_unref (info);
216   g_clear_error (&error);
217 #endif
218   
219   /* too few in arguments */
220   info = g_irepository_find_by_name (rep, "test", "test2");  
221   g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
222   function = (GIFunctionInfo *)info;
223
224   if (!g_function_info_invoke (function, NULL, 0, NULL, 0, NULL, &error))
225     g_print ("Invokation of %s failed: %s\n",
226              g_base_info_get_name (info),
227              error->message);
228
229   g_clear_error (&error);
230
231   /* too few out arguments */
232   if (!g_function_info_invoke (function, in_args, 1, NULL, 0, NULL, &error))
233     g_print ("Invokation of %s failed: %s\n",
234              g_base_info_get_name (info),
235              error->message);
236
237   g_clear_error (&error);
238
239   /* too many in arguments */
240   if (!g_function_info_invoke (function, in_args, 2, out_args, 1, NULL, &error))
241     g_print ("Invokation of %s failed: %s\n",
242              g_base_info_get_name (info),
243              error->message);
244
245   g_clear_error (&error);
246
247   /* too many out arguments */
248   if (!g_function_info_invoke (function, in_args, 1, out_args, 2, NULL, &error))
249     g_print ("Invokation of %s failed: %s\n",
250              g_base_info_get_name (info),
251              error->message);
252
253   g_clear_error (&error);
254
255   return 0;
256 }