os/ossrv/glib/tsrc/BC/src/hook_test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/src/hook_test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,220 @@
     1.4 +/*
     1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     1.6 +*
     1.7 +* This library is free software; you can redistribute it and/or
     1.8 +* modify it under the terms of the GNU Lesser General Public
     1.9 +* License as published by the Free Software Foundation; either
    1.10 +* version 2 of the License, or (at your option) any later version.
    1.11 +*
    1.12 +* This library is distributed in the hope that it will be useful,
    1.13 +* but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.15 +* Lesser General Public License for more details.
    1.16 +*
    1.17 +* You should have received a copy of the GNU Lesser General Public
    1.18 +* License along with this library; if not, write to the
    1.19 +* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    1.20 +* Boston, MA 02111-1307, USA.
    1.21 +*
    1.22 +* Description:
    1.23 +*
    1.24 +*/
    1.25 +
    1.26 +
    1.27 +
    1.28 +#undef G_DISABLE_ASSERT
    1.29 +#undef G_LOG_DOMAIN
    1.30 +
    1.31 +#include <stdio.h>
    1.32 +#include <string.h>
    1.33 +#include "glib.h"
    1.34 +
    1.35 +#ifdef SYMBIAN
    1.36 +#include "mrt2_glib2_test.h"
    1.37 +#endif /*SYMBIAN*/
    1.38 +
    1.39 +gboolean func1(int *data)
    1.40 +{
    1.41 +	*data = 1;
    1.42 +	return TRUE;
    1.43 +}
    1.44 +
    1.45 +gboolean func2(int *data)
    1.46 +{
    1.47 +	*data = 2;
    1.48 +	return TRUE;
    1.49 +}
    1.50 +
    1.51 +gboolean func3(int *data)
    1.52 +{
    1.53 +	*data = 3;
    1.54 +	return FALSE;
    1.55 +}
    1.56 +
    1.57 +gboolean func4(int *data)
    1.58 +{
    1.59 +	*data = 4;
    1.60 +	return TRUE;
    1.61 +}
    1.62 +
    1.63 +gint sort_func(GHook *new_hook,GHook *sibling)
    1.64 +{
    1.65 +	if(new_hook->hook_id < sibling->hook_id)
    1.66 +		return 0;
    1.67 +	else
    1.68 +		return 1;
    1.69 +}
    1.70 +
    1.71 +gboolean find_func(GHook *hook,gpointer data)
    1.72 +{
    1.73 +	if(hook->hook_id == 1)
    1.74 +		return TRUE;
    1.75 +	else
    1.76 +		return FALSE;
    1.77 +}
    1.78 +
    1.79 +void marshaller(GHook *hook,gpointer mashal_data)
    1.80 +{
    1.81 +	gint *data = (int *)hook->data;
    1.82 +	*data = -1;
    1.83 +}
    1.84 +
    1.85 +gboolean check_marshaller(GHook *hook,gpointer mashal_data)
    1.86 +{
    1.87 +	if(hook->hook_id == 2) // for hook2 id is 2
    1.88 +		return FALSE;
    1.89 +	else
    1.90 +		return TRUE;
    1.91 +}
    1.92 +
    1.93 +
    1.94 +void hook_test()
    1.95 +{
    1.96 +	GHookList hooklist;
    1.97 +	GHook *hook1 = NULL,*hook2 = NULL,*hook3 = NULL,*hook4 = NULL,*temp_hook;
    1.98 +	int data1 = 0,data2 = 0,data3 = 0,data4 = 0;
    1.99 +	int comp_value;
   1.100 +	gboolean val;
   1.101 +	
   1.102 +	g_hook_list_init(&hooklist,sizeof(GHook));
   1.103 +	
   1.104 +	hook1 = g_hook_alloc(&hooklist);
   1.105 +	hook1->func = (gpointer)func1;
   1.106 +	hook1->data = &data1;
   1.107 +	
   1.108 +	hook2 = g_hook_alloc(&hooklist);
   1.109 +	hook2->func = (gpointer)func2;
   1.110 +	hook2->data = &data2;
   1.111 +	
   1.112 +	hook3 = g_hook_alloc(&hooklist);
   1.113 +	hook3->func = (gpointer)func3;
   1.114 +	hook3->data = &data3;
   1.115 +	
   1.116 +	hook4 = g_hook_alloc(&hooklist);
   1.117 +	hook4->func = (gpointer)func4;
   1.118 +	hook4->data = &data4;
   1.119 +	
   1.120 +	g_hook_append(&hooklist,hook4);
   1.121 +	g_hook_prepend(&hooklist,hook3);
   1.122 +	g_hook_insert_before(&hooklist,hook3,hook2);
   1.123 +	g_hook_insert_sorted(&hooklist,hook1,sort_func);
   1.124 +	
   1.125 +	g_hook_list_invoke(&hooklist,FALSE);
   1.126 +	
   1.127 +	// checks g_hook_list_init,g_hook_alloc,g_hook_append,g_hook_prepend,g_hook_insert_before,g_hook_insert_sorted
   1.128 +	g_assert(data1 == 1 && data2 == 2 && data3 == 3 && data4 == 4);
   1.129 +	
   1.130 +	comp_value = g_hook_compare_ids(hook2,hook1);
   1.131 +	
   1.132 +	//checks g_hook_compare_ids
   1.133 +	g_assert(comp_value < 0);
   1.134 +	
   1.135 +	temp_hook = g_hook_get(&hooklist,10);
   1.136 +	
   1.137 +	//checks g_hook_get
   1.138 +	g_assert(temp_hook == NULL);
   1.139 +	
   1.140 +	temp_hook = g_hook_get(&hooklist,1);
   1.141 +	
   1.142 +	//checks g_hook_get
   1.143 +	g_assert(temp_hook == hook4);
   1.144 +	
   1.145 +	temp_hook = NULL;
   1.146 +	
   1.147 +	temp_hook = g_hook_find(&hooklist,TRUE,find_func,NULL);
   1.148 +	
   1.149 +	//checks g_hook_find
   1.150 +	g_assert(temp_hook == hook4);
   1.151 +	
   1.152 +	temp_hook = NULL;
   1.153 +	
   1.154 +	temp_hook = g_hook_find_data(&hooklist,TRUE,&data1);
   1.155 +	
   1.156 +	//checks g_hook_find_data
   1.157 +	g_assert(temp_hook == hook1);
   1.158 +	
   1.159 +	temp_hook = NULL;
   1.160 +	
   1.161 +	temp_hook = g_hook_find_func(&hooklist,TRUE,(gpointer)func2);
   1.162 +	
   1.163 +	//checks g_hook_find_func
   1.164 +	g_assert(temp_hook == hook2);
   1.165 +	
   1.166 +	temp_hook = g_hook_find_func_data(&hooklist,TRUE,(gpointer)func2,&data1);
   1.167 +	
   1.168 +	//checks g_hook_find_func_data
   1.169 +	g_assert(temp_hook == NULL);
   1.170 +	
   1.171 +	temp_hook = g_hook_find_func_data(&hooklist,TRUE,(gpointer)func1,&data1);
   1.172 +	
   1.173 +	//checks g_hook_find_func_data
   1.174 +	g_assert(temp_hook == hook1);
   1.175 +		
   1.176 +	temp_hook = NULL;
   1.177 +	
   1.178 +	temp_hook = g_hook_ref(&hooklist,hook3);
   1.179 +	
   1.180 +	//checks g_hook_ref
   1.181 +	g_assert(hook3->ref_count == 2);
   1.182 +	
   1.183 +	g_hook_unref(&hooklist,hook3);
   1.184 +	
   1.185 +	//checks g_hook_unref
   1.186 +	g_assert(hook3->ref_count == 1);
   1.187 +	
   1.188 +	g_hook_list_marshal(&hooklist,TRUE,marshaller,NULL);
   1.189 +	
   1.190 +	//checks g_hook_list_marshal
   1.191 +	g_assert(data1 == -1 && data2 == -1 && data3 == -1 && data4 == -1);
   1.192 +	
   1.193 +	g_hook_list_marshal_check(&hooklist,TRUE,check_marshaller,NULL);
   1.194 +	
   1.195 +	// checks g_hook_list_marshal_check
   1.196 +	// func3 is for hook3 and the check_marshaller returns FALSE for hook3
   1.197 +	// As a rsult the hook is deleted from the hook list.
   1.198 +	g_assert(g_hook_find_func(&hooklist,TRUE,(gpointer)func3) == NULL);
   1.199 +	
   1.200 +	g_hook_list_clear(&hooklist);
   1.201 +	
   1.202 +	//checks g_hook_list_clear
   1.203 +	g_assert(hooklist.hooks == NULL);
   1.204 +}
   1.205 +
   1.206 +int main (int   argc,
   1.207 +      char *argv[])
   1.208 +{
   1.209 +	#ifdef SYMBIAN
   1.210 +	
   1.211 +	
   1.212 +	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);
   1.213 +	#endif /*SYMBIAN*/
   1.214 +	
   1.215 +	hook_test();
   1.216 +	
   1.217 +	#if SYMBIAN
   1.218 +  	testResultXml("hook_test");
   1.219 +  	#endif /* EMULATOR */
   1.220 +	
   1.221 +	return 0;
   1.222 +	
   1.223 +}
   1.224 \ No newline at end of file