4b195d3f13737eee93edbd2562d3933d23329533
[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 "config.h"
31
32 #include <gmodule.h>
33 #include <string.h>
34
35 gchar* global_state;
36
37 _GLIB_EXTERN void g_clash_func (void);
38
39 _GLIB_EXTERN void
40 g_clash_func (void)
41 {
42   global_state = "global clash";
43 }
44
45 typedef void (*SimpleFunc) (void);
46 typedef void (*GModuleFunc) (GModule *);
47
48 static gchar **gplugin_a_state;
49 static gchar **gplugin_b_state;
50
51 static void
52 compare (const gchar *desc, const gchar *expected, const gchar *found)
53 {
54   if (!expected && !found)
55     return;
56   
57   if (expected && found && strcmp (expected, found) == 0)
58     return;
59     
60   g_error ("error: %s state should have been \"%s\", but is \"%s\"",
61            desc, expected ? expected : "NULL", found ? found : "NULL");
62 }
63
64 static void 
65 test_states (const gchar *global, const gchar *gplugin_a, 
66              const gchar *gplugin_b)
67 {       
68   compare ("global", global, global_state);
69   compare ("Plugin A", gplugin_a, *gplugin_a_state);
70   compare ("Plugin B", gplugin_b, *gplugin_b_state);
71   
72   global_state = *gplugin_a_state = *gplugin_b_state = NULL;
73 }
74  
75 static SimpleFunc plugin_clash_func = NULL;
76
77 int
78 main (int   arg,
79       char *argv[])
80 {
81   GModule *module_self, *module_a, *module_b;
82   gchar *dir;
83   gchar *plugin_a, *plugin_b;
84   SimpleFunc f_a, f_b, f_self;
85   GModuleFunc gmod_f;
86
87   if (!g_module_supported ())
88     g_error ("dynamic modules not supported");
89
90   dir = g_get_current_dir ();
91
92   plugin_a = g_strconcat (dir, G_DIR_SEPARATOR_S "libmoduletestplugin_a", 
93                           NULL);
94   plugin_b = g_strconcat (dir, G_DIR_SEPARATOR_S "libmoduletestplugin_b", 
95                           NULL);
96
97   g_free (dir);
98
99   /* module handles */
100   
101   module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
102   if (!module_self)
103     g_error ("error: %s", g_module_error ());
104
105   if (!g_module_symbol (module_self, "g_module_close", (gpointer *) &f_self))
106     g_error ("error: %s", g_module_error ());
107
108   module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
109   if (!module_a)
110     g_error ("error: %s", g_module_error ());
111
112   module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
113   if (!module_b)
114     g_error ("error: %s", g_module_error ());
115
116   /* get plugin state vars */
117
118   if (!g_module_symbol (module_a, "gplugin_a_state", 
119                         (gpointer *) &gplugin_a_state))
120     g_error ("error: %s", g_module_error ());
121   
122   if (!g_module_symbol (module_b, "gplugin_b_state", 
123                         (gpointer *) &gplugin_b_state))
124     g_error ("error: %s", g_module_error ());
125   test_states (NULL, NULL, "check-init");
126   
127   /* get plugin specific symbols and call them
128    */
129   if (!g_module_symbol (module_a, "gplugin_a_func", (gpointer *) &f_a))
130     g_error ("error: %s", g_module_error ());
131   test_states (NULL, NULL, NULL);
132  
133   if (!g_module_symbol (module_b, "gplugin_b_func", (gpointer *) &f_b))
134     g_error ("error: %s", g_module_error ());
135   test_states (NULL, NULL, NULL);
136  
137   f_a ();
138   test_states (NULL, "Hello world", NULL);
139   
140   f_b ();
141   test_states (NULL, NULL, "Hello world");
142   
143   /* get and call globally clashing functions
144    */
145  
146   if (!g_module_symbol (module_self, "g_clash_func", (gpointer *) &f_self))
147     g_error ("error: %s", g_module_error ());
148   test_states (NULL, NULL, NULL);
149
150   if (!g_module_symbol (module_a, "g_clash_func", (gpointer *) &f_a))
151     g_error ("error: %s", g_module_error ());
152   test_states (NULL, NULL, NULL);
153  
154   if (!g_module_symbol (module_b, "g_clash_func", (gpointer *) &f_b))
155     g_error ("error: %s", g_module_error ());
156   test_states (NULL, NULL, NULL);
157  
158   f_self ();
159   test_states ("global clash", NULL, NULL);
160   
161   f_a ();
162   test_states (NULL, "global clash", NULL);
163
164   f_b ();
165   test_states (NULL, NULL, "global clash");
166
167   /* get and call clashing plugin functions  */
168
169   if (!g_module_symbol (module_a, "gplugin_clash_func", (gpointer *) &f_a))
170     g_error ("error: %s", g_module_error ());
171   test_states (NULL, NULL, NULL);
172
173   if (!g_module_symbol (module_b, "gplugin_clash_func", (gpointer *) &f_b))
174     g_error ("error: %s", g_module_error ());
175   test_states (NULL, NULL, NULL);
176
177   plugin_clash_func = f_a;
178   plugin_clash_func ();
179   test_states (NULL, "plugin clash", NULL);
180
181   plugin_clash_func = f_b;
182   plugin_clash_func ();
183   test_states (NULL, NULL, "plugin clash");
184
185   /* call gmodule function from A  */
186
187   if (!g_module_symbol (module_a, "gplugin_a_module_func", (gpointer *) &gmod_f))
188     g_error ("error: %s", g_module_error ());
189   test_states (NULL, NULL, NULL);
190
191   gmod_f (module_b);
192   test_states (NULL, NULL, "BOOH");
193  
194   gmod_f (module_a);
195   test_states (NULL, "BOOH", NULL);
196
197   /* unload plugins  */
198
199   if (!g_module_close (module_a))
200     g_error ("error: %s", g_module_error ());
201
202   if (!g_module_close (module_b))
203     g_error ("error: %s", g_module_error ());
204  
205   return 0;
206 }