glib/tsrc/BC/src/hook_test.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
       
     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 * Description:
       
    20 *
       
    21 */
       
    22 
       
    23 
       
    24 
       
    25 #undef G_DISABLE_ASSERT
       
    26 #undef G_LOG_DOMAIN
       
    27 
       
    28 #include <stdio.h>
       
    29 #include <string.h>
       
    30 #include "glib.h"
       
    31 
       
    32 #ifdef SYMBIAN
       
    33 #include "mrt2_glib2_test.h"
       
    34 #endif /*SYMBIAN*/
       
    35 
       
    36 gboolean func1(int *data)
       
    37 {
       
    38 	*data = 1;
       
    39 	return TRUE;
       
    40 }
       
    41 
       
    42 gboolean func2(int *data)
       
    43 {
       
    44 	*data = 2;
       
    45 	return TRUE;
       
    46 }
       
    47 
       
    48 gboolean func3(int *data)
       
    49 {
       
    50 	*data = 3;
       
    51 	return FALSE;
       
    52 }
       
    53 
       
    54 gboolean func4(int *data)
       
    55 {
       
    56 	*data = 4;
       
    57 	return TRUE;
       
    58 }
       
    59 
       
    60 gint sort_func(GHook *new_hook,GHook *sibling)
       
    61 {
       
    62 	if(new_hook->hook_id < sibling->hook_id)
       
    63 		return 0;
       
    64 	else
       
    65 		return 1;
       
    66 }
       
    67 
       
    68 gboolean find_func(GHook *hook,gpointer data)
       
    69 {
       
    70 	if(hook->hook_id == 1)
       
    71 		return TRUE;
       
    72 	else
       
    73 		return FALSE;
       
    74 }
       
    75 
       
    76 void marshaller(GHook *hook,gpointer mashal_data)
       
    77 {
       
    78 	gint *data = (int *)hook->data;
       
    79 	*data = -1;
       
    80 }
       
    81 
       
    82 gboolean check_marshaller(GHook *hook,gpointer mashal_data)
       
    83 {
       
    84 	if(hook->hook_id == 2) // for hook2 id is 2
       
    85 		return FALSE;
       
    86 	else
       
    87 		return TRUE;
       
    88 }
       
    89 
       
    90 
       
    91 void hook_test()
       
    92 {
       
    93 	GHookList hooklist;
       
    94 	GHook *hook1 = NULL,*hook2 = NULL,*hook3 = NULL,*hook4 = NULL,*temp_hook;
       
    95 	int data1 = 0,data2 = 0,data3 = 0,data4 = 0;
       
    96 	int comp_value;
       
    97 	gboolean val;
       
    98 	
       
    99 	g_hook_list_init(&hooklist,sizeof(GHook));
       
   100 	
       
   101 	hook1 = g_hook_alloc(&hooklist);
       
   102 	hook1->func = (gpointer)func1;
       
   103 	hook1->data = &data1;
       
   104 	
       
   105 	hook2 = g_hook_alloc(&hooklist);
       
   106 	hook2->func = (gpointer)func2;
       
   107 	hook2->data = &data2;
       
   108 	
       
   109 	hook3 = g_hook_alloc(&hooklist);
       
   110 	hook3->func = (gpointer)func3;
       
   111 	hook3->data = &data3;
       
   112 	
       
   113 	hook4 = g_hook_alloc(&hooklist);
       
   114 	hook4->func = (gpointer)func4;
       
   115 	hook4->data = &data4;
       
   116 	
       
   117 	g_hook_append(&hooklist,hook4);
       
   118 	g_hook_prepend(&hooklist,hook3);
       
   119 	g_hook_insert_before(&hooklist,hook3,hook2);
       
   120 	g_hook_insert_sorted(&hooklist,hook1,sort_func);
       
   121 	
       
   122 	g_hook_list_invoke(&hooklist,FALSE);
       
   123 	
       
   124 	// checks g_hook_list_init,g_hook_alloc,g_hook_append,g_hook_prepend,g_hook_insert_before,g_hook_insert_sorted
       
   125 	g_assert(data1 == 1 && data2 == 2 && data3 == 3 && data4 == 4);
       
   126 	
       
   127 	comp_value = g_hook_compare_ids(hook2,hook1);
       
   128 	
       
   129 	//checks g_hook_compare_ids
       
   130 	g_assert(comp_value < 0);
       
   131 	
       
   132 	temp_hook = g_hook_get(&hooklist,10);
       
   133 	
       
   134 	//checks g_hook_get
       
   135 	g_assert(temp_hook == NULL);
       
   136 	
       
   137 	temp_hook = g_hook_get(&hooklist,1);
       
   138 	
       
   139 	//checks g_hook_get
       
   140 	g_assert(temp_hook == hook4);
       
   141 	
       
   142 	temp_hook = NULL;
       
   143 	
       
   144 	temp_hook = g_hook_find(&hooklist,TRUE,find_func,NULL);
       
   145 	
       
   146 	//checks g_hook_find
       
   147 	g_assert(temp_hook == hook4);
       
   148 	
       
   149 	temp_hook = NULL;
       
   150 	
       
   151 	temp_hook = g_hook_find_data(&hooklist,TRUE,&data1);
       
   152 	
       
   153 	//checks g_hook_find_data
       
   154 	g_assert(temp_hook == hook1);
       
   155 	
       
   156 	temp_hook = NULL;
       
   157 	
       
   158 	temp_hook = g_hook_find_func(&hooklist,TRUE,(gpointer)func2);
       
   159 	
       
   160 	//checks g_hook_find_func
       
   161 	g_assert(temp_hook == hook2);
       
   162 	
       
   163 	temp_hook = g_hook_find_func_data(&hooklist,TRUE,(gpointer)func2,&data1);
       
   164 	
       
   165 	//checks g_hook_find_func_data
       
   166 	g_assert(temp_hook == NULL);
       
   167 	
       
   168 	temp_hook = g_hook_find_func_data(&hooklist,TRUE,(gpointer)func1,&data1);
       
   169 	
       
   170 	//checks g_hook_find_func_data
       
   171 	g_assert(temp_hook == hook1);
       
   172 		
       
   173 	temp_hook = NULL;
       
   174 	
       
   175 	temp_hook = g_hook_ref(&hooklist,hook3);
       
   176 	
       
   177 	//checks g_hook_ref
       
   178 	g_assert(hook3->ref_count == 2);
       
   179 	
       
   180 	g_hook_unref(&hooklist,hook3);
       
   181 	
       
   182 	//checks g_hook_unref
       
   183 	g_assert(hook3->ref_count == 1);
       
   184 	
       
   185 	g_hook_list_marshal(&hooklist,TRUE,marshaller,NULL);
       
   186 	
       
   187 	//checks g_hook_list_marshal
       
   188 	g_assert(data1 == -1 && data2 == -1 && data3 == -1 && data4 == -1);
       
   189 	
       
   190 	g_hook_list_marshal_check(&hooklist,TRUE,check_marshaller,NULL);
       
   191 	
       
   192 	// checks g_hook_list_marshal_check
       
   193 	// func3 is for hook3 and the check_marshaller returns FALSE for hook3
       
   194 	// As a rsult the hook is deleted from the hook list.
       
   195 	g_assert(g_hook_find_func(&hooklist,TRUE,(gpointer)func3) == NULL);
       
   196 	
       
   197 	g_hook_list_clear(&hooklist);
       
   198 	
       
   199 	//checks g_hook_list_clear
       
   200 	g_assert(hooklist.hooks == NULL);
       
   201 }
       
   202 
       
   203 int main (int   argc,
       
   204       char *argv[])
       
   205 {
       
   206 	#ifdef SYMBIAN
       
   207 	
       
   208 	
       
   209 	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);
       
   210 	#endif /*SYMBIAN*/
       
   211 	
       
   212 	hook_test();
       
   213 	
       
   214 	#if SYMBIAN
       
   215   	testResultXml("hook_test");
       
   216   	#endif /* EMULATOR */
       
   217 	
       
   218 	return 0;
       
   219 	
       
   220 }