glib/tests/module-test.c
branchRCL_3
changeset 56 acd3cd4aaceb
equal deleted inserted replaced
54:4332f0f7be53 56:acd3cd4aaceb
       
     1 /* module-test.c - test program for GMODULE
       
     2  * Copyright (C) 1998 Tim Janik
       
     3  * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
       
     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 #ifdef __SYMBIAN32__
       
    33 #include "mrt2_glib2_test.h"
       
    34 #endif /*__SYMBIAN32__*/
       
    35 
       
    36 gchar* global_state;
       
    37 
       
    38 G_MODULE_EXPORT void
       
    39 g_clash_func (void)
       
    40 {
       
    41   global_state = "global clash";
       
    42 }
       
    43 
       
    44 typedef	void (*SimpleFunc) (void);
       
    45 typedef	void (*GModuleFunc) (GModule *);
       
    46 
       
    47 static gchar **gplugin_a_state;
       
    48 static gchar **gplugin_b_state;
       
    49 
       
    50 static void
       
    51 compare (const gchar *desc, const gchar *expected, const gchar *found)
       
    52 {
       
    53   if (!expected && !found)
       
    54     return;
       
    55   
       
    56   if (expected && found && strcmp (expected, found) == 0)
       
    57     return;
       
    58     
       
    59   g_error ("error: %s state should have been \"%s\", but is \"%s\"",
       
    60 	   desc, expected ? expected : "NULL", found ? found : "NULL");
       
    61 }
       
    62 
       
    63 static void 
       
    64 test_states (const gchar *global, const gchar *gplugin_a, 
       
    65 	     const gchar *gplugin_b)
       
    66 {	
       
    67   compare ("global", global, global_state);
       
    68   compare ("Plugin A", gplugin_a, *gplugin_a_state);
       
    69   compare ("Plugin B", gplugin_b, *gplugin_b_state);
       
    70   
       
    71   global_state = *gplugin_a_state = *gplugin_b_state = NULL;
       
    72 }
       
    73  
       
    74 static SimpleFunc plugin_clash_func = NULL;
       
    75 
       
    76 int
       
    77 main (int   arg,
       
    78       char *argv[])
       
    79 {
       
    80   GModule *module_self, *module_a, *module_b;
       
    81   gchar *dir;
       
    82   gchar *plugin_a, *plugin_b;
       
    83   SimpleFunc f_a, f_b, f_self;
       
    84   GModuleFunc gmod_f;
       
    85 
       
    86   #ifdef __SYMBIAN32__
       
    87   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
       
    88   g_set_print_handler(mrtPrintHandler);
       
    89   #endif /*__SYMBIAN32__*/
       
    90 	  
       
    91 
       
    92   if (!g_module_supported ())
       
    93     g_error ("dynamic modules not supported");
       
    94 
       
    95 #ifndef __SYMBIAN32__
       
    96   dir = g_get_current_dir ()
       
    97 #endif  
       
    98 
       
    99   plugin_a = "libmoduletestplugin_a.dll";
       
   100   plugin_b = "libmoduletestplugin_b.dll";
       
   101 
       
   102 #ifndef __SYMBIAN32__
       
   103   g_free (dir);
       
   104 #endif  
       
   105 
       
   106   /* module handles */
       
   107 #ifndef __SYMBIAN32__ 
       
   108   module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
       
   109   if (!module_self)
       
   110     g_error ("error: %s", g_module_error ());
       
   111 
       
   112   if (!g_module_symbol (module_self, "g_module_close", (gpointer *) &f_self))
       
   113     g_error ("error: %s", g_module_error ());
       
   114 #endif
       
   115   module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
       
   116   if (!module_a)
       
   117     g_error ("error: %s", g_module_error ());
       
   118 
       
   119   module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
       
   120   if (!module_b)
       
   121     g_error ("error: %s", g_module_error ());
       
   122 
       
   123   /* get plugin state vars */
       
   124 
       
   125   if (!g_module_symbol (module_a, "gplugin_a_state", 
       
   126 			(gpointer *) &gplugin_a_state))
       
   127     g_error ("error: %s", g_module_error ());
       
   128   
       
   129   if (!g_module_symbol (module_b, "gplugin_b_state", 
       
   130 			(gpointer *) &gplugin_b_state))
       
   131     g_error ("error: %s", g_module_error ());
       
   132   test_states (NULL, NULL, "check-init");
       
   133   
       
   134   /* get plugin specific symbols and call them
       
   135    */
       
   136   if (!g_module_symbol (module_a, "gplugin_a_func", (gpointer *) &f_a))
       
   137     g_error ("error: %s", g_module_error ());
       
   138   test_states (NULL, NULL, NULL);
       
   139  
       
   140   if (!g_module_symbol (module_b, "gplugin_b_func", (gpointer *) &f_b))
       
   141     g_error ("error: %s", g_module_error ());
       
   142   test_states (NULL, NULL, NULL);
       
   143  
       
   144   f_a ();
       
   145   test_states (NULL, "Hello world", NULL);
       
   146   
       
   147   f_b ();
       
   148   test_states (NULL, NULL, "Hello world");
       
   149   
       
   150   /* get and call globally clashing functions
       
   151    */
       
   152 #ifndef __SYMBIAN32__
       
   153   if (!g_module_symbol (module_self, "g_clash_func", (gpointer *) &f_self))
       
   154     g_error ("error: %s", g_module_error ());
       
   155   test_states (NULL, NULL, NULL);
       
   156 #endif
       
   157   if (!g_module_symbol (module_a, "g_clash_func", (gpointer *) &f_a))
       
   158     g_error ("error: %s", g_module_error ());
       
   159   test_states (NULL, NULL, NULL);
       
   160  
       
   161   if (!g_module_symbol (module_b, "g_clash_func", (gpointer *) &f_b))
       
   162     g_error ("error: %s", g_module_error ());
       
   163   test_states (NULL, NULL, NULL);
       
   164 #ifndef __SYMBIAN32__ 
       
   165   f_self ();
       
   166   test_states ("global clash", NULL, NULL);
       
   167 #endif  
       
   168   f_a ();
       
   169   test_states (NULL, "global clash", NULL);
       
   170 
       
   171   f_b ();
       
   172   test_states (NULL, NULL, "global clash");
       
   173 
       
   174   /* get and call clashing plugin functions  */
       
   175 
       
   176   if (!g_module_symbol (module_a, "gplugin_clash_func", (gpointer *) &f_a))
       
   177     g_error ("error: %s", g_module_error ());
       
   178   test_states (NULL, NULL, NULL);
       
   179 
       
   180   if (!g_module_symbol (module_b, "gplugin_clash_func", (gpointer *) &f_b))
       
   181     g_error ("error: %s", g_module_error ());
       
   182   test_states (NULL, NULL, NULL);
       
   183 
       
   184   plugin_clash_func = f_a;
       
   185   plugin_clash_func ();
       
   186   test_states (NULL, "plugin clash", NULL);
       
   187 
       
   188   plugin_clash_func = f_b;
       
   189   plugin_clash_func ();
       
   190   test_states (NULL, NULL, "plugin clash");
       
   191 
       
   192   /* call gmodule function from A  */
       
   193 
       
   194   if (!g_module_symbol (module_a, "gplugin_a_module_func", (gpointer *) &gmod_f))
       
   195     g_error ("error: %s", g_module_error ());
       
   196   test_states (NULL, NULL, NULL);
       
   197 
       
   198   gmod_f (module_b);
       
   199   test_states (NULL, NULL, "BOOH");
       
   200  
       
   201   gmod_f (module_a);
       
   202   test_states (NULL, "BOOH", NULL);
       
   203 
       
   204   /* unload plugins  */
       
   205 
       
   206   if (!g_module_close (module_a))
       
   207     g_error ("error: %s", g_module_error ());
       
   208 
       
   209   if (!g_module_close (module_b))
       
   210     g_error ("error: %s", g_module_error ());
       
   211   #ifdef __SYMBIAN32__
       
   212   testResultXml("module-test");
       
   213   #endif /* EMULATOR */
       
   214  
       
   215   return 0;
       
   216 }