tests: fix a few recently-broken tests
[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 static const gchar *datapath;
34
35 gchar* global_state;
36
37 G_MODULE_EXPORT void g_clash_func (void);
38
39 G_MODULE_EXPORT 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 *plugin_a, *plugin_b;
83   SimpleFunc f_a, f_b, f_self;
84   GModuleFunc gmod_f;
85
86   if (!g_module_supported ())
87     g_error ("dynamic modules not supported");
88
89   if (g_getenv ("G_TEST_DATA"))
90     datapath = g_getenv ("G_TEST_DATA");
91   else
92     datapath = ".";
93
94   plugin_a = g_build_filename (datapath, "libmoduletestplugin_a", NULL);
95   plugin_b = g_build_filename (datapath, "libmoduletestplugin_b", NULL);
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 }