Fix more warning-addition fallout
[platform/upstream/glib.git] / tests / module-test.c
1 /* module-test.c - test program for GMODULE
2  * Copyright (C) 1998 Tim Janik
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #undef G_DISABLE_ASSERT
28 #undef G_LOG_DOMAIN
29
30 #include <gmodule.h>
31 #include <string.h>
32
33 gchar* global_state;
34
35 G_MODULE_EXPORT void g_clash_func (void);
36
37 G_MODULE_EXPORT void
38 g_clash_func (void)
39 {
40   global_state = "global clash";
41 }
42
43 typedef void (*SimpleFunc) (void);
44 typedef void (*GModuleFunc) (GModule *);
45
46 static gchar **gplugin_a_state;
47 static gchar **gplugin_b_state;
48
49 static void
50 compare (const gchar *desc, const gchar *expected, const gchar *found)
51 {
52   if (!expected && !found)
53     return;
54   
55   if (expected && found && strcmp (expected, found) == 0)
56     return;
57     
58   g_error ("error: %s state should have been \"%s\", but is \"%s\"",
59            desc, expected ? expected : "NULL", found ? found : "NULL");
60 }
61
62 static void 
63 test_states (const gchar *global, const gchar *gplugin_a, 
64              const gchar *gplugin_b)
65 {       
66   compare ("global", global, global_state);
67   compare ("Plugin A", gplugin_a, *gplugin_a_state);
68   compare ("Plugin B", gplugin_b, *gplugin_b_state);
69   
70   global_state = *gplugin_a_state = *gplugin_b_state = NULL;
71 }
72  
73 static SimpleFunc plugin_clash_func = NULL;
74
75 int
76 main (int   arg,
77       char *argv[])
78 {
79   GModule *module_self, *module_a, *module_b;
80   gchar *dir;
81   gchar *plugin_a, *plugin_b;
82   SimpleFunc f_a, f_b, f_self;
83   GModuleFunc gmod_f;
84
85   if (!g_module_supported ())
86     g_error ("dynamic modules not supported");
87
88   dir = g_get_current_dir ();
89
90   plugin_a = g_strconcat (dir, G_DIR_SEPARATOR_S "libmoduletestplugin_a", 
91                           NULL);
92   plugin_b = g_strconcat (dir, G_DIR_SEPARATOR_S "libmoduletestplugin_b", 
93                           NULL);
94
95   g_free (dir);
96
97   /* module handles */
98   
99   module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
100   if (!module_self)
101     g_error ("error: %s", g_module_error ());
102
103   if (!g_module_symbol (module_self, "g_module_close", (gpointer *) &f_self))
104     g_error ("error: %s", g_module_error ());
105
106   module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
107   if (!module_a)
108     g_error ("error: %s", g_module_error ());
109
110   module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
111   if (!module_b)
112     g_error ("error: %s", g_module_error ());
113
114   /* get plugin state vars */
115
116   if (!g_module_symbol (module_a, "gplugin_a_state", 
117                         (gpointer *) &gplugin_a_state))
118     g_error ("error: %s", g_module_error ());
119   
120   if (!g_module_symbol (module_b, "gplugin_b_state", 
121                         (gpointer *) &gplugin_b_state))
122     g_error ("error: %s", g_module_error ());
123   test_states (NULL, NULL, "check-init");
124   
125   /* get plugin specific symbols and call them
126    */
127   if (!g_module_symbol (module_a, "gplugin_a_func", (gpointer *) &f_a))
128     g_error ("error: %s", g_module_error ());
129   test_states (NULL, NULL, NULL);
130  
131   if (!g_module_symbol (module_b, "gplugin_b_func", (gpointer *) &f_b))
132     g_error ("error: %s", g_module_error ());
133   test_states (NULL, NULL, NULL);
134  
135   f_a ();
136   test_states (NULL, "Hello world", NULL);
137   
138   f_b ();
139   test_states (NULL, NULL, "Hello world");
140   
141   /* get and call globally clashing functions
142    */
143  
144   if (!g_module_symbol (module_self, "g_clash_func", (gpointer *) &f_self))
145     g_error ("error: %s", g_module_error ());
146   test_states (NULL, NULL, NULL);
147
148   if (!g_module_symbol (module_a, "g_clash_func", (gpointer *) &f_a))
149     g_error ("error: %s", g_module_error ());
150   test_states (NULL, NULL, NULL);
151  
152   if (!g_module_symbol (module_b, "g_clash_func", (gpointer *) &f_b))
153     g_error ("error: %s", g_module_error ());
154   test_states (NULL, NULL, NULL);
155  
156   f_self ();
157   test_states ("global clash", NULL, NULL);
158   
159   f_a ();
160   test_states (NULL, "global clash", NULL);
161
162   f_b ();
163   test_states (NULL, NULL, "global clash");
164
165   /* get and call clashing plugin functions  */
166
167   if (!g_module_symbol (module_a, "gplugin_clash_func", (gpointer *) &f_a))
168     g_error ("error: %s", g_module_error ());
169   test_states (NULL, NULL, NULL);
170
171   if (!g_module_symbol (module_b, "gplugin_clash_func", (gpointer *) &f_b))
172     g_error ("error: %s", g_module_error ());
173   test_states (NULL, NULL, NULL);
174
175   plugin_clash_func = f_a;
176   plugin_clash_func ();
177   test_states (NULL, "plugin clash", NULL);
178
179   plugin_clash_func = f_b;
180   plugin_clash_func ();
181   test_states (NULL, NULL, "plugin clash");
182
183   /* call gmodule function from A  */
184
185   if (!g_module_symbol (module_a, "gplugin_a_module_func", (gpointer *) &gmod_f))
186     g_error ("error: %s", g_module_error ());
187   test_states (NULL, NULL, NULL);
188
189   gmod_f (module_b);
190   test_states (NULL, NULL, "BOOH");
191  
192   gmod_f (module_a);
193   test_states (NULL, "BOOH", NULL);
194
195   /* unload plugins  */
196
197   if (!g_module_close (module_a))
198     g_error ("error: %s", g_module_error ());
199
200   if (!g_module_close (module_b))
201     g_error ("error: %s", g_module_error ());
202  
203   return 0;
204 }