os/ossrv/glib/tsrc/BC/tests/module-test.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 #include <stdio.h>
    33 
    34 #ifdef SYMBIAN
    35 #include "mrt2_glib2_test.h"
    36 #endif /*SYMBIAN*/
    37 
    38 
    39 typedef	int (*SimpleFunc) (void);
    40 
    41 int
    42 main (int   arg,
    43       char *argv[])
    44 {
    45   GModule *module_a, *module_b;
    46   gchar *plugin_a, *plugin_b;
    47   SimpleFunc f_a, f_b;
    48   int retVal;
    49   void *fun1,*fun2;
    50   gchar *build_path = NULL;
    51   gchar temp_build_path[100];
    52   gchar *lib_name = "xyz";
    53 
    54   #ifdef SYMBIAN
    55   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);
    56   g_set_print_handler(mrtPrintHandler);
    57   #endif /*SYMBIAN*/
    58 	  
    59 
    60   if (!g_module_supported ())
    61     g_print ("dynamic modules not supported");
    62  
    63   plugin_a  = "libmoduletestplugin_a.dll";
    64   plugin_b  = "libmoduletestplugin_b.dll";
    65   
    66   build_path = g_module_build_path("c:\\sys\\bin",lib_name);
    67   g_assert(!strcmp(build_path,"c:\\sys\\bin\\xyz.dll"));
    68   g_free(build_path);
    69   
    70   build_path = g_module_build_path(NULL,lib_name);
    71   g_assert(!strcmp(build_path,"xyz.dll"));
    72   g_free(build_path);
    73 
    74   /* module handles */
    75 
    76   module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
    77   if (!module_a)
    78     g_print ("error: %s", g_module_error ());
    79 
    80   module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
    81   if (!module_b)
    82     g_print ("error: %s", g_module_error ());
    83 
    84   /* get plugin specific symbols and call them
    85    */
    86   if (!g_module_symbol (module_a, /*"gplugin_a_func1"*/"1", /*(gpointer *) &f_a*/&fun1))
    87     g_print ("error: %s", g_module_error ());
    88    
    89   if (!g_module_symbol (module_b, /*"gplugin_b_func1"*/"1", /*(gpointer *) &f_b)*/&fun2))
    90       g_print ("error: %s", g_module_error ());
    91    
    92   f_a = (SimpleFunc)fun1;
    93   f_b = (SimpleFunc)fun2;
    94   
    95   retVal = f_a ();
    96   g_assert(retVal == 1);
    97   
    98   retVal = f_b ();
    99   g_assert(retVal == 1);
   100   
   101   if (!g_module_symbol (module_a, /*"gplugin_a_func2"*/"2", (gpointer *) &f_a))
   102     g_print ("error: %s", g_module_error ());
   103    
   104   if (!g_module_symbol (module_b, /*"gplugin_b_func2"*/"2", (gpointer *) &f_b))
   105     g_print ("error: %s", g_module_error ());
   106   
   107   retVal = f_a ();
   108   g_assert(retVal == 2);
   109   
   110   retVal = f_b ();
   111   g_assert(retVal == 2);
   112   
   113   //checks g_module_name
   114   g_assert(!strcmp(g_module_name(module_a),"libmoduletestplugin_a.dll"));
   115   
   116   g_module_make_resident(module_a);
   117   
   118   /* unload plugins  */
   119   
   120   //g_module_close is ignored because g_module_make_resident(module_a) is called
   121   if (!g_module_close (module_a))
   122     g_print ("error: %s", g_module_error ());
   123 
   124   if (!g_module_close (module_b))
   125     g_print ("error: %s", g_module_error ());
   126   
   127   
   128   // As g_module_make_resident(module_a) is called g_module_close is ignored and therefore
   129   // we are able to get the symbol from the library and execute the funtion also
   130   if (!g_module_symbol (module_a, /*"gplugin_a_func1"*/"1", /*(gpointer *) &f_a*/&fun1))
   131     g_print ("error: %s", g_module_error ());
   132       
   133   f_a = (SimpleFunc)fun1;
   134   
   135   retVal = f_a ();
   136   g_assert(retVal == 1);
   137   
   138   #ifdef SYMBIAN
   139   testResultXml("module-test");
   140   #endif /* EMULATOR */
   141  
   142   return 0;
   143 }