os/ossrv/glib/tsrc/BC/src/main_loop_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 /*
     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 #include <pthread.h>
    32 #include <unistd.h>
    33 
    34 #ifdef SYMBIAN
    35 #include "mrt2_glib2_test.h"
    36 #endif /*SYMBIAN*/
    37 
    38 GMainLoop *loop,*idle_loop;
    39 GMainContext *context;
    40 int fd[2];
    41 char *buf[100];
    42 GSource *source1,*source2;
    43 pthread_t thread1;
    44 gboolean e1_complete,e2_complete;
    45 
    46 void quit()
    47 {
    48 	if(e1_complete && e2_complete)
    49 		g_main_loop_quit(loop);
    50 }
    51 
    52 
    53 gboolean prepare(GSource *source,gint *timeout)
    54 {
    55 	*timeout = 0;
    56 	return TRUE;
    57 }
    58 
    59 gboolean check(GSource *source)
    60 {
    61 	return TRUE;
    62 }
    63 
    64 gboolean dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
    65 {
    66 	if(callback(user_data))
    67 		return TRUE;
    68 	else
    69 		return FALSE;
    70 }
    71 
    72 gboolean my_callback(int* data)
    73 {
    74 	static int i = 100;
    75 	int depth;
    76 
    77 	//checks g_main_loop_is_running
    78 	g_assert(g_main_loop_is_running(loop));
    79 
    80 	depth = g_main_depth();
    81 
    82 	//checks g_main_depth
    83 	g_assert(depth == 1);
    84 
    85 	i--;
    86 
    87 	//printf("%d\n",i);
    88 
    89 	if(i<0)
    90 	{
    91 		e1_complete = TRUE;
    92 		quit();
    93 		return FALSE;
    94 	}
    95 	else
    96 	{
    97 		++(*data);
    98 		return TRUE;
    99 	}
   100 }
   101 
   102 
   103 gboolean fd_prepare(GSource *source,gint *timeout)
   104 {
   105 	*timeout = -1;
   106 	return FALSE;
   107 }
   108 
   109 gboolean fd_check(GSource *source)
   110 {
   111 	GPollFD *pollfd = source->poll_fds->data;
   112 	if(pollfd->revents && (G_IO_IN | G_IO_HUP | G_IO_ERR))
   113 		return TRUE;
   114 	else
   115 		return FALSE;
   116 }
   117 
   118 gboolean fd_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
   119 {
   120 	if(callback(user_data))
   121 		return TRUE;
   122 	else
   123 		return FALSE;
   124 }
   125 
   126 gboolean fd_callback(int* data)
   127 {
   128 	pthread_join(thread1, NULL);
   129 	read(fd[0],buf,sizeof(buf));
   130 
   131 	// This asserts actually checks whether the callback for fd is successful or not
   132 	// In other words it checks if g_source_add_poll was successful or not.
   133 	g_assert(!strcmp((const char *)buf,"fd poll example"));
   134 
   135 	*data = 1;
   136 	//printf("%s",buf);
   137 	//getchar();
   138 
   139 	g_source_unref(source2);
   140 
   141 	e2_complete = TRUE;
   142 	quit();
   143 	return FALSE;
   144 }
   145 
   146 
   147 void* thread_function(void *data)
   148 {
   149 	sleep(1);
   150 	write(fd[1],"fd poll example",15);
   151 	return NULL;
   152 }
   153 
   154 void main_loop_test()
   155 {
   156 	GMainContext *default_context;
   157 	int depth;
   158 	int id;
   159 	GTimeVal time ={0,};
   160 	int user_data = 0,fd_data = 0;
   161 	GPollFD pollfd;
   162 	GSource *source3;
   163 
   164 	GSourceFuncs SourceFuncs =
   165 	{
   166 		prepare,
   167 		check,
   168 		dispatch,
   169 		NULL
   170 	};
   171 
   172 	GSourceFuncs fd_SourceFuncs =
   173 	{
   174 		fd_prepare,
   175 		fd_check,
   176 		fd_dispatch,
   177 		NULL
   178 	};
   179 
   180 	e1_complete = FALSE;
   181 	e2_complete = FALSE;
   182 
   183 	pipe(fd);
   184 
   185 	pollfd.fd = fd[0];
   186 	pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
   187 	pollfd.revents = 0;
   188 
   189 	pthread_create(&thread1, NULL, thread_function, NULL);
   190 
   191 	context = g_main_context_new();
   192 
   193 	//g_main_context_add_poll(context,&pollfd,0);
   194 
   195 	source1 = g_source_new(&SourceFuncs,sizeof(GSource));
   196 	g_source_set_callback(source1,(GSourceFunc)my_callback,&user_data,NULL);
   197 
   198 	id = g_source_attach(source1,context);
   199 
   200 	g_assert(g_source_get_id(source1) == id);
   201 
   202 	g_source_set_priority(source1,0);
   203 
   204 	g_assert(g_source_get_priority(source1) == 0);
   205 
   206 	loop = g_main_loop_new(context, FALSE);
   207 
   208 	default_context = g_main_loop_get_context(loop);
   209 
   210 	//checks g_main_loop_get_context
   211 	g_assert(default_context == context);
   212 
   213 	//checks g_main_loop_is_running
   214 	g_assert(g_main_loop_is_running(loop) == FALSE);
   215 
   216 	depth = g_main_depth();
   217 
   218 	//checks g_main_depth
   219 	g_assert(depth == 0);
   220 
   221 	g_source_get_current_time(source1,&time);
   222 
   223 	g_assert(time.tv_usec > 0);
   224 
   225 	g_source_set_can_recurse(source1,TRUE);
   226 
   227 	g_assert(g_source_get_can_recurse(source1) == TRUE);
   228 
   229 	source2 = g_source_new(&fd_SourceFuncs,sizeof(GSource));
   230 	g_source_set_callback(source2,(GSourceFunc)fd_callback,&fd_data,NULL);
   231 	g_source_add_poll(source2,&pollfd);
   232 
   233 	g_source_remove_poll(source2,&pollfd);
   234 
   235 	// checks g_source_remove_poll
   236 	g_assert(source2->poll_fds == NULL);
   237 
   238 	g_source_add_poll(source2,&pollfd);
   239 
   240 	// checks whether g_source_add_poll is successful.
   241 	// one more check is done in fd_callback.
   242 	// If that function is callled we are sure that add poll was successful
   243 	g_assert(source2->poll_fds->data == &pollfd);
   244 
   245 	source3 = g_source_ref(source2);
   246 
   247 	g_assert(source3 == source2 && source2->ref_count == 2);
   248 
   249 	g_source_unref(source3);
   250 
   251 	id = g_source_attach(source2,context);
   252 
   253 	//checks g_main_context_pending
   254 	g_assert(g_main_context_pending(context));
   255 
   256 	g_main_loop_run(loop);
   257 
   258 	// ref is called here. Thats why two unrefs are called. If the 2nd unref is
   259 	// callled with the unref, code should crash
   260 	g_main_loop_ref(loop);
   261 
   262 	g_main_loop_unref(loop);
   263 
   264 	g_main_loop_unref(loop);
   265 
   266 	//checks the number of times the call back function is called
   267 	g_assert(user_data == 100);
   268 
   269 	// checks whether set poll was successful and call back for the same
   270 	// was called
   271 	g_assert(fd_data == 1);
   272 }
   273 
   274 gint dummy_poll_func(GPollFD *ufds,guint nfsd,gint timeout_)
   275 {
   276 	return 0;
   277 }
   278 
   279 
   280 void misc_test()
   281 {
   282 	GPollFunc func = g_main_context_get_poll_func(context);
   283 
   284 	g_assert(func != NULL);
   285 
   286 	g_main_context_set_poll_func(context,dummy_poll_func);
   287 	func = g_main_context_get_poll_func(context);
   288 
   289 	g_assert(func == dummy_poll_func);
   290 }
   291 
   292 gboolean function(int *data)
   293 {
   294 	*data = 1;
   295 	g_main_loop_quit(idle_loop);
   296 	return FALSE;
   297 }
   298 
   299 void idle_test()
   300 {
   301 	int user_data = 0;
   302 	gboolean retVal;
   303 	GSource *s = g_idle_source_new();
   304 
   305 	idle_loop = g_main_loop_new(NULL, FALSE);
   306 	g_source_attach(s,context);
   307 
   308 	g_idle_add((GSourceFunc)function,&user_data);
   309 
   310 	retVal = g_idle_remove_by_data(&user_data);
   311 
   312 	//checks g_idle_remove_by_data
   313 	g_assert(retVal == TRUE);
   314 	retVal = FALSE;
   315 
   316 	g_idle_add((GSourceFunc)function,&user_data);
   317 
   318 	retVal = g_source_remove_by_user_data(&user_data);
   319 
   320 	//checks g_source_remove_by_user_data
   321 	g_assert(retVal == TRUE);
   322 
   323 	g_idle_add((GSourceFunc)function,&user_data);
   324 
   325 	g_main_loop_run(idle_loop);
   326 
   327 	g_main_loop_unref(idle_loop);
   328 
   329 	//checks whether the function was run or not
   330 	g_assert(user_data == 1);
   331 }
   332 
   333 int main()
   334 {
   335 
   336 	#ifdef SYMBIAN
   337 
   338 	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);
   339 	#endif
   340 
   341 	//main_loop_test();
   342 	//misc_test();
   343 	//idle_test();
   344 
   345 
   346 	#ifdef SYMBIAN
   347   	testResultXml("main_loop_test");
   348   	#endif /* EMULATOR */
   349 
   350 	return 0;
   351 }