sl@0: /* sl@0: * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public sl@0: * License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #include sl@0: #include sl@0: #include "glib.h" sl@0: sl@0: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: gboolean func1(int *data) sl@0: { sl@0: *data = 1; sl@0: return TRUE; sl@0: } sl@0: sl@0: gboolean func2(int *data) sl@0: { sl@0: *data = 2; sl@0: return TRUE; sl@0: } sl@0: sl@0: gboolean func3(int *data) sl@0: { sl@0: *data = 3; sl@0: return FALSE; sl@0: } sl@0: sl@0: gboolean func4(int *data) sl@0: { sl@0: *data = 4; sl@0: return TRUE; sl@0: } sl@0: sl@0: gint sort_func(GHook *new_hook,GHook *sibling) sl@0: { sl@0: if(new_hook->hook_id < sibling->hook_id) sl@0: return 0; sl@0: else sl@0: return 1; sl@0: } sl@0: sl@0: gboolean find_func(GHook *hook,gpointer data) sl@0: { sl@0: if(hook->hook_id == 1) sl@0: return TRUE; sl@0: else sl@0: return FALSE; sl@0: } sl@0: sl@0: void marshaller(GHook *hook,gpointer mashal_data) sl@0: { sl@0: gint *data = (int *)hook->data; sl@0: *data = -1; sl@0: } sl@0: sl@0: gboolean check_marshaller(GHook *hook,gpointer mashal_data) sl@0: { sl@0: if(hook->hook_id == 2) // for hook2 id is 2 sl@0: return FALSE; sl@0: else sl@0: return TRUE; sl@0: } sl@0: sl@0: sl@0: void hook_test() sl@0: { sl@0: GHookList hooklist; sl@0: GHook *hook1 = NULL,*hook2 = NULL,*hook3 = NULL,*hook4 = NULL,*temp_hook; sl@0: int data1 = 0,data2 = 0,data3 = 0,data4 = 0; sl@0: int comp_value; sl@0: gboolean val; sl@0: sl@0: g_hook_list_init(&hooklist,sizeof(GHook)); sl@0: sl@0: hook1 = g_hook_alloc(&hooklist); sl@0: hook1->func = (gpointer)func1; sl@0: hook1->data = &data1; sl@0: sl@0: hook2 = g_hook_alloc(&hooklist); sl@0: hook2->func = (gpointer)func2; sl@0: hook2->data = &data2; sl@0: sl@0: hook3 = g_hook_alloc(&hooklist); sl@0: hook3->func = (gpointer)func3; sl@0: hook3->data = &data3; sl@0: sl@0: hook4 = g_hook_alloc(&hooklist); sl@0: hook4->func = (gpointer)func4; sl@0: hook4->data = &data4; sl@0: sl@0: g_hook_append(&hooklist,hook4); sl@0: g_hook_prepend(&hooklist,hook3); sl@0: g_hook_insert_before(&hooklist,hook3,hook2); sl@0: g_hook_insert_sorted(&hooklist,hook1,sort_func); sl@0: sl@0: g_hook_list_invoke(&hooklist,FALSE); sl@0: sl@0: // checks g_hook_list_init,g_hook_alloc,g_hook_append,g_hook_prepend,g_hook_insert_before,g_hook_insert_sorted sl@0: g_assert(data1 == 1 && data2 == 2 && data3 == 3 && data4 == 4); sl@0: sl@0: comp_value = g_hook_compare_ids(hook2,hook1); sl@0: sl@0: //checks g_hook_compare_ids sl@0: g_assert(comp_value < 0); sl@0: sl@0: temp_hook = g_hook_get(&hooklist,10); sl@0: sl@0: //checks g_hook_get sl@0: g_assert(temp_hook == NULL); sl@0: sl@0: temp_hook = g_hook_get(&hooklist,1); sl@0: sl@0: //checks g_hook_get sl@0: g_assert(temp_hook == hook4); sl@0: sl@0: temp_hook = NULL; sl@0: sl@0: temp_hook = g_hook_find(&hooklist,TRUE,find_func,NULL); sl@0: sl@0: //checks g_hook_find sl@0: g_assert(temp_hook == hook4); sl@0: sl@0: temp_hook = NULL; sl@0: sl@0: temp_hook = g_hook_find_data(&hooklist,TRUE,&data1); sl@0: sl@0: //checks g_hook_find_data sl@0: g_assert(temp_hook == hook1); sl@0: sl@0: temp_hook = NULL; sl@0: sl@0: temp_hook = g_hook_find_func(&hooklist,TRUE,(gpointer)func2); sl@0: sl@0: //checks g_hook_find_func sl@0: g_assert(temp_hook == hook2); sl@0: sl@0: temp_hook = g_hook_find_func_data(&hooklist,TRUE,(gpointer)func2,&data1); sl@0: sl@0: //checks g_hook_find_func_data sl@0: g_assert(temp_hook == NULL); sl@0: sl@0: temp_hook = g_hook_find_func_data(&hooklist,TRUE,(gpointer)func1,&data1); sl@0: sl@0: //checks g_hook_find_func_data sl@0: g_assert(temp_hook == hook1); sl@0: sl@0: temp_hook = NULL; sl@0: sl@0: temp_hook = g_hook_ref(&hooklist,hook3); sl@0: sl@0: //checks g_hook_ref sl@0: g_assert(hook3->ref_count == 2); sl@0: sl@0: g_hook_unref(&hooklist,hook3); sl@0: sl@0: //checks g_hook_unref sl@0: g_assert(hook3->ref_count == 1); sl@0: sl@0: g_hook_list_marshal(&hooklist,TRUE,marshaller,NULL); sl@0: sl@0: //checks g_hook_list_marshal sl@0: g_assert(data1 == -1 && data2 == -1 && data3 == -1 && data4 == -1); sl@0: sl@0: g_hook_list_marshal_check(&hooklist,TRUE,check_marshaller,NULL); sl@0: sl@0: // checks g_hook_list_marshal_check sl@0: // func3 is for hook3 and the check_marshaller returns FALSE for hook3 sl@0: // As a rsult the hook is deleted from the hook list. sl@0: g_assert(g_hook_find_func(&hooklist,TRUE,(gpointer)func3) == NULL); sl@0: sl@0: g_hook_list_clear(&hooklist); sl@0: sl@0: //checks g_hook_list_clear sl@0: g_assert(hooklist.hooks == NULL); sl@0: } sl@0: sl@0: int main (int argc, sl@0: char *argv[]) sl@0: { sl@0: #ifdef SYMBIAN sl@0: sl@0: sl@0: 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: #endif /*SYMBIAN*/ sl@0: sl@0: hook_test(); sl@0: sl@0: #if SYMBIAN sl@0: testResultXml("hook_test"); sl@0: #endif /* EMULATOR */ sl@0: sl@0: return 0; sl@0: sl@0: }