1 /* module-test.c - test program for GMODULE
2 * Copyright (C) 1998 Tim Janik
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.
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.
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.
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/.
27 #undef G_DISABLE_ASSERT
33 static const gchar *datapath;
37 G_MODULE_EXPORT void g_clash_func (void);
42 global_state = "global clash";
45 typedef void (*SimpleFunc) (void);
46 typedef void (*GModuleFunc) (GModule *);
48 static gchar **gplugin_a_state;
49 static gchar **gplugin_b_state;
52 compare (const gchar *desc, const gchar *expected, const gchar *found)
54 if (!expected && !found)
57 if (expected && found && strcmp (expected, found) == 0)
60 g_error ("error: %s state should have been \"%s\", but is \"%s\"",
61 desc, expected ? expected : "NULL", found ? found : "NULL");
65 test_states (const gchar *global, const gchar *gplugin_a,
66 const gchar *gplugin_b)
68 compare ("global", global, global_state);
69 compare ("Plugin A", gplugin_a, *gplugin_a_state);
70 compare ("Plugin B", gplugin_b, *gplugin_b_state);
72 global_state = *gplugin_a_state = *gplugin_b_state = NULL;
75 static SimpleFunc plugin_clash_func = NULL;
81 GModule *module_self, *module_a, *module_b;
82 gchar *plugin_a, *plugin_b;
83 SimpleFunc f_a, f_b, f_self;
86 if (!g_module_supported ())
87 g_error ("dynamic modules not supported");
89 if (g_getenv ("G_TEST_DATA"))
90 datapath = g_getenv ("G_TEST_DATA");
94 plugin_a = g_build_filename (datapath, "libmoduletestplugin_a", NULL);
95 plugin_b = g_build_filename (datapath, "libmoduletestplugin_b", NULL);
99 module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
101 g_error ("error: %s", g_module_error ());
103 if (!g_module_symbol (module_self, "g_module_close", (gpointer *) &f_self))
104 g_error ("error: %s", g_module_error ());
106 module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
108 g_error ("error: %s", g_module_error ());
110 module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
112 g_error ("error: %s", g_module_error ());
114 /* get plugin state vars */
116 if (!g_module_symbol (module_a, "gplugin_a_state",
117 (gpointer *) &gplugin_a_state))
118 g_error ("error: %s", g_module_error ());
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");
125 /* get plugin specific symbols and call them
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);
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);
136 test_states (NULL, "Hello world", NULL);
139 test_states (NULL, NULL, "Hello world");
141 /* get and call globally clashing functions
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);
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);
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);
157 test_states ("global clash", NULL, NULL);
160 test_states (NULL, "global clash", NULL);
163 test_states (NULL, NULL, "global clash");
165 /* get and call clashing plugin functions */
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);
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);
175 plugin_clash_func = f_a;
176 plugin_clash_func ();
177 test_states (NULL, "plugin clash", NULL);
179 plugin_clash_func = f_b;
180 plugin_clash_func ();
181 test_states (NULL, NULL, "plugin clash");
183 /* call gmodule function from A */
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);
190 test_states (NULL, NULL, "BOOH");
193 test_states (NULL, "BOOH", NULL);
197 if (!g_module_close (module_a))
198 g_error ("error: %s", g_module_error ());
200 if (!g_module_close (module_b))
201 g_error ("error: %s", g_module_error ());