os/ossrv/glib/tsrc/BC/src/hook_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.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     3
*
sl@0
     4
* This library is free software; you can redistribute it and/or
sl@0
     5
* modify it under the terms of the GNU Lesser General Public
sl@0
     6
* License as published by the Free Software Foundation; either
sl@0
     7
* version 2 of the License, or (at your option) any later version.
sl@0
     8
*
sl@0
     9
* This library is distributed in the hope that it will be useful,
sl@0
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    12
* Lesser General Public License for more details.
sl@0
    13
*
sl@0
    14
* You should have received a copy of the GNU Lesser General Public
sl@0
    15
* License along with this library; if not, write to the
sl@0
    16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
sl@0
    17
* Boston, MA 02111-1307, USA.
sl@0
    18
*
sl@0
    19
* Description:
sl@0
    20
*
sl@0
    21
*/
sl@0
    22
sl@0
    23
sl@0
    24
sl@0
    25
#undef G_DISABLE_ASSERT
sl@0
    26
#undef G_LOG_DOMAIN
sl@0
    27
sl@0
    28
#include <stdio.h>
sl@0
    29
#include <string.h>
sl@0
    30
#include "glib.h"
sl@0
    31
sl@0
    32
#ifdef SYMBIAN
sl@0
    33
#include "mrt2_glib2_test.h"
sl@0
    34
#endif /*SYMBIAN*/
sl@0
    35
sl@0
    36
gboolean func1(int *data)
sl@0
    37
{
sl@0
    38
	*data = 1;
sl@0
    39
	return TRUE;
sl@0
    40
}
sl@0
    41
sl@0
    42
gboolean func2(int *data)
sl@0
    43
{
sl@0
    44
	*data = 2;
sl@0
    45
	return TRUE;
sl@0
    46
}
sl@0
    47
sl@0
    48
gboolean func3(int *data)
sl@0
    49
{
sl@0
    50
	*data = 3;
sl@0
    51
	return FALSE;
sl@0
    52
}
sl@0
    53
sl@0
    54
gboolean func4(int *data)
sl@0
    55
{
sl@0
    56
	*data = 4;
sl@0
    57
	return TRUE;
sl@0
    58
}
sl@0
    59
sl@0
    60
gint sort_func(GHook *new_hook,GHook *sibling)
sl@0
    61
{
sl@0
    62
	if(new_hook->hook_id < sibling->hook_id)
sl@0
    63
		return 0;
sl@0
    64
	else
sl@0
    65
		return 1;
sl@0
    66
}
sl@0
    67
sl@0
    68
gboolean find_func(GHook *hook,gpointer data)
sl@0
    69
{
sl@0
    70
	if(hook->hook_id == 1)
sl@0
    71
		return TRUE;
sl@0
    72
	else
sl@0
    73
		return FALSE;
sl@0
    74
}
sl@0
    75
sl@0
    76
void marshaller(GHook *hook,gpointer mashal_data)
sl@0
    77
{
sl@0
    78
	gint *data = (int *)hook->data;
sl@0
    79
	*data = -1;
sl@0
    80
}
sl@0
    81
sl@0
    82
gboolean check_marshaller(GHook *hook,gpointer mashal_data)
sl@0
    83
{
sl@0
    84
	if(hook->hook_id == 2) // for hook2 id is 2
sl@0
    85
		return FALSE;
sl@0
    86
	else
sl@0
    87
		return TRUE;
sl@0
    88
}
sl@0
    89
sl@0
    90
sl@0
    91
void hook_test()
sl@0
    92
{
sl@0
    93
	GHookList hooklist;
sl@0
    94
	GHook *hook1 = NULL,*hook2 = NULL,*hook3 = NULL,*hook4 = NULL,*temp_hook;
sl@0
    95
	int data1 = 0,data2 = 0,data3 = 0,data4 = 0;
sl@0
    96
	int comp_value;
sl@0
    97
	gboolean val;
sl@0
    98
	
sl@0
    99
	g_hook_list_init(&hooklist,sizeof(GHook));
sl@0
   100
	
sl@0
   101
	hook1 = g_hook_alloc(&hooklist);
sl@0
   102
	hook1->func = (gpointer)func1;
sl@0
   103
	hook1->data = &data1;
sl@0
   104
	
sl@0
   105
	hook2 = g_hook_alloc(&hooklist);
sl@0
   106
	hook2->func = (gpointer)func2;
sl@0
   107
	hook2->data = &data2;
sl@0
   108
	
sl@0
   109
	hook3 = g_hook_alloc(&hooklist);
sl@0
   110
	hook3->func = (gpointer)func3;
sl@0
   111
	hook3->data = &data3;
sl@0
   112
	
sl@0
   113
	hook4 = g_hook_alloc(&hooklist);
sl@0
   114
	hook4->func = (gpointer)func4;
sl@0
   115
	hook4->data = &data4;
sl@0
   116
	
sl@0
   117
	g_hook_append(&hooklist,hook4);
sl@0
   118
	g_hook_prepend(&hooklist,hook3);
sl@0
   119
	g_hook_insert_before(&hooklist,hook3,hook2);
sl@0
   120
	g_hook_insert_sorted(&hooklist,hook1,sort_func);
sl@0
   121
	
sl@0
   122
	g_hook_list_invoke(&hooklist,FALSE);
sl@0
   123
	
sl@0
   124
	// checks g_hook_list_init,g_hook_alloc,g_hook_append,g_hook_prepend,g_hook_insert_before,g_hook_insert_sorted
sl@0
   125
	g_assert(data1 == 1 && data2 == 2 && data3 == 3 && data4 == 4);
sl@0
   126
	
sl@0
   127
	comp_value = g_hook_compare_ids(hook2,hook1);
sl@0
   128
	
sl@0
   129
	//checks g_hook_compare_ids
sl@0
   130
	g_assert(comp_value < 0);
sl@0
   131
	
sl@0
   132
	temp_hook = g_hook_get(&hooklist,10);
sl@0
   133
	
sl@0
   134
	//checks g_hook_get
sl@0
   135
	g_assert(temp_hook == NULL);
sl@0
   136
	
sl@0
   137
	temp_hook = g_hook_get(&hooklist,1);
sl@0
   138
	
sl@0
   139
	//checks g_hook_get
sl@0
   140
	g_assert(temp_hook == hook4);
sl@0
   141
	
sl@0
   142
	temp_hook = NULL;
sl@0
   143
	
sl@0
   144
	temp_hook = g_hook_find(&hooklist,TRUE,find_func,NULL);
sl@0
   145
	
sl@0
   146
	//checks g_hook_find
sl@0
   147
	g_assert(temp_hook == hook4);
sl@0
   148
	
sl@0
   149
	temp_hook = NULL;
sl@0
   150
	
sl@0
   151
	temp_hook = g_hook_find_data(&hooklist,TRUE,&data1);
sl@0
   152
	
sl@0
   153
	//checks g_hook_find_data
sl@0
   154
	g_assert(temp_hook == hook1);
sl@0
   155
	
sl@0
   156
	temp_hook = NULL;
sl@0
   157
	
sl@0
   158
	temp_hook = g_hook_find_func(&hooklist,TRUE,(gpointer)func2);
sl@0
   159
	
sl@0
   160
	//checks g_hook_find_func
sl@0
   161
	g_assert(temp_hook == hook2);
sl@0
   162
	
sl@0
   163
	temp_hook = g_hook_find_func_data(&hooklist,TRUE,(gpointer)func2,&data1);
sl@0
   164
	
sl@0
   165
	//checks g_hook_find_func_data
sl@0
   166
	g_assert(temp_hook == NULL);
sl@0
   167
	
sl@0
   168
	temp_hook = g_hook_find_func_data(&hooklist,TRUE,(gpointer)func1,&data1);
sl@0
   169
	
sl@0
   170
	//checks g_hook_find_func_data
sl@0
   171
	g_assert(temp_hook == hook1);
sl@0
   172
		
sl@0
   173
	temp_hook = NULL;
sl@0
   174
	
sl@0
   175
	temp_hook = g_hook_ref(&hooklist,hook3);
sl@0
   176
	
sl@0
   177
	//checks g_hook_ref
sl@0
   178
	g_assert(hook3->ref_count == 2);
sl@0
   179
	
sl@0
   180
	g_hook_unref(&hooklist,hook3);
sl@0
   181
	
sl@0
   182
	//checks g_hook_unref
sl@0
   183
	g_assert(hook3->ref_count == 1);
sl@0
   184
	
sl@0
   185
	g_hook_list_marshal(&hooklist,TRUE,marshaller,NULL);
sl@0
   186
	
sl@0
   187
	//checks g_hook_list_marshal
sl@0
   188
	g_assert(data1 == -1 && data2 == -1 && data3 == -1 && data4 == -1);
sl@0
   189
	
sl@0
   190
	g_hook_list_marshal_check(&hooklist,TRUE,check_marshaller,NULL);
sl@0
   191
	
sl@0
   192
	// checks g_hook_list_marshal_check
sl@0
   193
	// func3 is for hook3 and the check_marshaller returns FALSE for hook3
sl@0
   194
	// As a rsult the hook is deleted from the hook list.
sl@0
   195
	g_assert(g_hook_find_func(&hooklist,TRUE,(gpointer)func3) == NULL);
sl@0
   196
	
sl@0
   197
	g_hook_list_clear(&hooklist);
sl@0
   198
	
sl@0
   199
	//checks g_hook_list_clear
sl@0
   200
	g_assert(hooklist.hooks == NULL);
sl@0
   201
}
sl@0
   202
sl@0
   203
int main (int   argc,
sl@0
   204
      char *argv[])
sl@0
   205
{
sl@0
   206
	#ifdef SYMBIAN
sl@0
   207
	
sl@0
   208
	
sl@0
   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);
sl@0
   210
	#endif /*SYMBIAN*/
sl@0
   211
	
sl@0
   212
	hook_test();
sl@0
   213
	
sl@0
   214
	#if SYMBIAN
sl@0
   215
  	testResultXml("hook_test");
sl@0
   216
  	#endif /* EMULATOR */
sl@0
   217
	
sl@0
   218
	return 0;
sl@0
   219
	
sl@0
   220
}