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