os/ossrv/glib/tsrc/BC/src/main_loop_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/main_loop_test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,351 @@
     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 +#include <pthread.h>
    1.35 +#include <unistd.h>
    1.36 +
    1.37 +#ifdef SYMBIAN
    1.38 +#include "mrt2_glib2_test.h"
    1.39 +#endif /*SYMBIAN*/
    1.40 +
    1.41 +GMainLoop *loop,*idle_loop;
    1.42 +GMainContext *context;
    1.43 +int fd[2];
    1.44 +char *buf[100];
    1.45 +GSource *source1,*source2;
    1.46 +pthread_t thread1;
    1.47 +gboolean e1_complete,e2_complete;
    1.48 +
    1.49 +void quit()
    1.50 +{
    1.51 +	if(e1_complete && e2_complete)
    1.52 +		g_main_loop_quit(loop);
    1.53 +}
    1.54 +
    1.55 +
    1.56 +gboolean prepare(GSource *source,gint *timeout)
    1.57 +{
    1.58 +	*timeout = 0;
    1.59 +	return TRUE;
    1.60 +}
    1.61 +
    1.62 +gboolean check(GSource *source)
    1.63 +{
    1.64 +	return TRUE;
    1.65 +}
    1.66 +
    1.67 +gboolean dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
    1.68 +{
    1.69 +	if(callback(user_data))
    1.70 +		return TRUE;
    1.71 +	else
    1.72 +		return FALSE;
    1.73 +}
    1.74 +
    1.75 +gboolean my_callback(int* data)
    1.76 +{
    1.77 +	static int i = 100;
    1.78 +	int depth;
    1.79 +
    1.80 +	//checks g_main_loop_is_running
    1.81 +	g_assert(g_main_loop_is_running(loop));
    1.82 +
    1.83 +	depth = g_main_depth();
    1.84 +
    1.85 +	//checks g_main_depth
    1.86 +	g_assert(depth == 1);
    1.87 +
    1.88 +	i--;
    1.89 +
    1.90 +	//printf("%d\n",i);
    1.91 +
    1.92 +	if(i<0)
    1.93 +	{
    1.94 +		e1_complete = TRUE;
    1.95 +		quit();
    1.96 +		return FALSE;
    1.97 +	}
    1.98 +	else
    1.99 +	{
   1.100 +		++(*data);
   1.101 +		return TRUE;
   1.102 +	}
   1.103 +}
   1.104 +
   1.105 +
   1.106 +gboolean fd_prepare(GSource *source,gint *timeout)
   1.107 +{
   1.108 +	*timeout = -1;
   1.109 +	return FALSE;
   1.110 +}
   1.111 +
   1.112 +gboolean fd_check(GSource *source)
   1.113 +{
   1.114 +	GPollFD *pollfd = source->poll_fds->data;
   1.115 +	if(pollfd->revents && (G_IO_IN | G_IO_HUP | G_IO_ERR))
   1.116 +		return TRUE;
   1.117 +	else
   1.118 +		return FALSE;
   1.119 +}
   1.120 +
   1.121 +gboolean fd_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
   1.122 +{
   1.123 +	if(callback(user_data))
   1.124 +		return TRUE;
   1.125 +	else
   1.126 +		return FALSE;
   1.127 +}
   1.128 +
   1.129 +gboolean fd_callback(int* data)
   1.130 +{
   1.131 +	pthread_join(thread1, NULL);
   1.132 +	read(fd[0],buf,sizeof(buf));
   1.133 +
   1.134 +	// This asserts actually checks whether the callback for fd is successful or not
   1.135 +	// In other words it checks if g_source_add_poll was successful or not.
   1.136 +	g_assert(!strcmp((const char *)buf,"fd poll example"));
   1.137 +
   1.138 +	*data = 1;
   1.139 +	//printf("%s",buf);
   1.140 +	//getchar();
   1.141 +
   1.142 +	g_source_unref(source2);
   1.143 +
   1.144 +	e2_complete = TRUE;
   1.145 +	quit();
   1.146 +	return FALSE;
   1.147 +}
   1.148 +
   1.149 +
   1.150 +void* thread_function(void *data)
   1.151 +{
   1.152 +	sleep(1);
   1.153 +	write(fd[1],"fd poll example",15);
   1.154 +	return NULL;
   1.155 +}
   1.156 +
   1.157 +void main_loop_test()
   1.158 +{
   1.159 +	GMainContext *default_context;
   1.160 +	int depth;
   1.161 +	int id;
   1.162 +	GTimeVal time ={0,};
   1.163 +	int user_data = 0,fd_data = 0;
   1.164 +	GPollFD pollfd;
   1.165 +	GSource *source3;
   1.166 +
   1.167 +	GSourceFuncs SourceFuncs =
   1.168 +	{
   1.169 +		prepare,
   1.170 +		check,
   1.171 +		dispatch,
   1.172 +		NULL
   1.173 +	};
   1.174 +
   1.175 +	GSourceFuncs fd_SourceFuncs =
   1.176 +	{
   1.177 +		fd_prepare,
   1.178 +		fd_check,
   1.179 +		fd_dispatch,
   1.180 +		NULL
   1.181 +	};
   1.182 +
   1.183 +	e1_complete = FALSE;
   1.184 +	e2_complete = FALSE;
   1.185 +
   1.186 +	pipe(fd);
   1.187 +
   1.188 +	pollfd.fd = fd[0];
   1.189 +	pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
   1.190 +	pollfd.revents = 0;
   1.191 +
   1.192 +	pthread_create(&thread1, NULL, thread_function, NULL);
   1.193 +
   1.194 +	context = g_main_context_new();
   1.195 +
   1.196 +	//g_main_context_add_poll(context,&pollfd,0);
   1.197 +
   1.198 +	source1 = g_source_new(&SourceFuncs,sizeof(GSource));
   1.199 +	g_source_set_callback(source1,(GSourceFunc)my_callback,&user_data,NULL);
   1.200 +
   1.201 +	id = g_source_attach(source1,context);
   1.202 +
   1.203 +	g_assert(g_source_get_id(source1) == id);
   1.204 +
   1.205 +	g_source_set_priority(source1,0);
   1.206 +
   1.207 +	g_assert(g_source_get_priority(source1) == 0);
   1.208 +
   1.209 +	loop = g_main_loop_new(context, FALSE);
   1.210 +
   1.211 +	default_context = g_main_loop_get_context(loop);
   1.212 +
   1.213 +	//checks g_main_loop_get_context
   1.214 +	g_assert(default_context == context);
   1.215 +
   1.216 +	//checks g_main_loop_is_running
   1.217 +	g_assert(g_main_loop_is_running(loop) == FALSE);
   1.218 +
   1.219 +	depth = g_main_depth();
   1.220 +
   1.221 +	//checks g_main_depth
   1.222 +	g_assert(depth == 0);
   1.223 +
   1.224 +	g_source_get_current_time(source1,&time);
   1.225 +
   1.226 +	g_assert(time.tv_usec > 0);
   1.227 +
   1.228 +	g_source_set_can_recurse(source1,TRUE);
   1.229 +
   1.230 +	g_assert(g_source_get_can_recurse(source1) == TRUE);
   1.231 +
   1.232 +	source2 = g_source_new(&fd_SourceFuncs,sizeof(GSource));
   1.233 +	g_source_set_callback(source2,(GSourceFunc)fd_callback,&fd_data,NULL);
   1.234 +	g_source_add_poll(source2,&pollfd);
   1.235 +
   1.236 +	g_source_remove_poll(source2,&pollfd);
   1.237 +
   1.238 +	// checks g_source_remove_poll
   1.239 +	g_assert(source2->poll_fds == NULL);
   1.240 +
   1.241 +	g_source_add_poll(source2,&pollfd);
   1.242 +
   1.243 +	// checks whether g_source_add_poll is successful.
   1.244 +	// one more check is done in fd_callback.
   1.245 +	// If that function is callled we are sure that add poll was successful
   1.246 +	g_assert(source2->poll_fds->data == &pollfd);
   1.247 +
   1.248 +	source3 = g_source_ref(source2);
   1.249 +
   1.250 +	g_assert(source3 == source2 && source2->ref_count == 2);
   1.251 +
   1.252 +	g_source_unref(source3);
   1.253 +
   1.254 +	id = g_source_attach(source2,context);
   1.255 +
   1.256 +	//checks g_main_context_pending
   1.257 +	g_assert(g_main_context_pending(context));
   1.258 +
   1.259 +	g_main_loop_run(loop);
   1.260 +
   1.261 +	// ref is called here. Thats why two unrefs are called. If the 2nd unref is
   1.262 +	// callled with the unref, code should crash
   1.263 +	g_main_loop_ref(loop);
   1.264 +
   1.265 +	g_main_loop_unref(loop);
   1.266 +
   1.267 +	g_main_loop_unref(loop);
   1.268 +
   1.269 +	//checks the number of times the call back function is called
   1.270 +	g_assert(user_data == 100);
   1.271 +
   1.272 +	// checks whether set poll was successful and call back for the same
   1.273 +	// was called
   1.274 +	g_assert(fd_data == 1);
   1.275 +}
   1.276 +
   1.277 +gint dummy_poll_func(GPollFD *ufds,guint nfsd,gint timeout_)
   1.278 +{
   1.279 +	return 0;
   1.280 +}
   1.281 +
   1.282 +
   1.283 +void misc_test()
   1.284 +{
   1.285 +	GPollFunc func = g_main_context_get_poll_func(context);
   1.286 +
   1.287 +	g_assert(func != NULL);
   1.288 +
   1.289 +	g_main_context_set_poll_func(context,dummy_poll_func);
   1.290 +	func = g_main_context_get_poll_func(context);
   1.291 +
   1.292 +	g_assert(func == dummy_poll_func);
   1.293 +}
   1.294 +
   1.295 +gboolean function(int *data)
   1.296 +{
   1.297 +	*data = 1;
   1.298 +	g_main_loop_quit(idle_loop);
   1.299 +	return FALSE;
   1.300 +}
   1.301 +
   1.302 +void idle_test()
   1.303 +{
   1.304 +	int user_data = 0;
   1.305 +	gboolean retVal;
   1.306 +	GSource *s = g_idle_source_new();
   1.307 +
   1.308 +	idle_loop = g_main_loop_new(NULL, FALSE);
   1.309 +	g_source_attach(s,context);
   1.310 +
   1.311 +	g_idle_add((GSourceFunc)function,&user_data);
   1.312 +
   1.313 +	retVal = g_idle_remove_by_data(&user_data);
   1.314 +
   1.315 +	//checks g_idle_remove_by_data
   1.316 +	g_assert(retVal == TRUE);
   1.317 +	retVal = FALSE;
   1.318 +
   1.319 +	g_idle_add((GSourceFunc)function,&user_data);
   1.320 +
   1.321 +	retVal = g_source_remove_by_user_data(&user_data);
   1.322 +
   1.323 +	//checks g_source_remove_by_user_data
   1.324 +	g_assert(retVal == TRUE);
   1.325 +
   1.326 +	g_idle_add((GSourceFunc)function,&user_data);
   1.327 +
   1.328 +	g_main_loop_run(idle_loop);
   1.329 +
   1.330 +	g_main_loop_unref(idle_loop);
   1.331 +
   1.332 +	//checks whether the function was run or not
   1.333 +	g_assert(user_data == 1);
   1.334 +}
   1.335 +
   1.336 +int main()
   1.337 +{
   1.338 +
   1.339 +	#ifdef SYMBIAN
   1.340 +
   1.341 +	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.342 +	#endif
   1.343 +
   1.344 +	//main_loop_test();
   1.345 +	//misc_test();
   1.346 +	//idle_test();
   1.347 +
   1.348 +
   1.349 +	#ifdef SYMBIAN
   1.350 +  	testResultXml("main_loop_test");
   1.351 +  	#endif /* EMULATOR */
   1.352 +
   1.353 +	return 0;
   1.354 +}
   1.355 \ No newline at end of file