os/ossrv/glib/tsrc/BC/src/tmisc.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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:  ?Description
    20 *
    21 */
    22 
    23 
    24 #undef G_DISABLE_ASSERT
    25 #undef G_LOG_DOMAIN
    26 
    27 
    28 #include <stdio.h>
    29 #include <string.h>
    30 #include <glib.h>
    31 #include <fcntl.h>
    32 #include <goption.h>
    33 
    34 #ifdef SYMBIAN
    35 #include "mrt2_glib2_test.h"
    36 #endif /*SYMBIAN*/
    37 
    38 #define	C2P(c)		((gpointer) ((long) (c)))
    39 #define GINT_TO_POINTER(i)	((gpointer)  (i))
    40 #define GPOINTER_TO_INT(p)	((gint)   (p))
    41 #define TESTPASS	1
    42 #define TESTFAIL	0
    43 
    44 //Test for g_nullify_pointer
    45 void tg_nullify_pointer()
    46 {
    47 	char nullify_pointer[]="abcd";
    48 	g_nullify_pointer((void*)nullify_pointer);
    49 	g_assert(!strcmp(nullify_pointer,"\0"));
    50 }
    51 
    52 //Ascending
    53 gint compare_fun_gr(gconstpointer a,gconstpointer b)
    54 {
    55 	return ((*(int *)a==*(int *)b)?0:((*(int *)a>*(int *)b)?1:-1));
    56 }
    57 
    58 //Data
    59 gint compare_fun_gr_data(gconstpointer a,gconstpointer b,gpointer data)
    60 {
    61 	return ((*(int *)a==*(int *)b)?0:((*(int *)a>*(int *)b)?1:-1));
    62 }
    63 
    64 
    65 //Tests for g_ptr_array
    66 void tg_ptr_array_tests()
    67 {
    68 	GPtrArray *gparray;
    69 	int i;
    70 	gint str_ds[12]=
    71 	{
    72 		12,11,10,9,8,7,6,5,4,3,2,1
    73 	};
    74 	
    75 	gparray = g_ptr_array_new ();
    76 	for(i=0;i<12;i++)
    77 	{
    78 		g_ptr_array_add (gparray, (gpointer)str_ds[i] );
    79 	}
    80 	
    81 	g_ptr_array_sort(gparray,compare_fun_gr);
    82 	g_ptr_array_remove_range(gparray,2,4);
    83 	g_ptr_array_sort_with_data(gparray,compare_fun_gr_data,0);
    84 	
    85 	
    86 }
    87 
    88 int cmp_func(gconstpointer a,gconstpointer b)
    89 {
    90 	if(a==b)
    91 		return 0;
    92 	else
    93 		return -1;
    94 }
    95 
    96 //Test for g_queue_find_custom
    97 void tg_queue_find_custom()
    98 {
    99 	GQueue *q;
   100 	GList *node;
   101 	gpointer data;
   102 	gpointer srch_data=GINT_TO_POINTER(5);
   103 	int i;
   104 	int j=10;
   105 	int g_queue_find_custom_pass=TESTFAIL;
   106 	
   107 	q = g_queue_new ();
   108 	for(i=0;i<10;i++)
   109 	{
   110 		g_queue_push_head (q, GINT_TO_POINTER (j));
   111 		j--;
   112 	}
   113 	g_queue_push_nth(q,GINT_TO_POINTER (5),9);
   114 	
   115 	node= g_queue_find_custom(q,GINT_TO_POINTER (5),cmp_func);
   116 
   117 	if(node->data==srch_data)
   118 	{
   119 		g_queue_find_custom_pass=TESTPASS;
   120 	}
   121 			
   122 	g_assert(g_queue_find_custom_pass==TESTPASS);
   123 }
   124 
   125 
   126 //Test for g_timer_reset
   127 void tg_timer_test()
   128 {
   129 	int i=0;
   130 	GTimer *timer;
   131 	timer = g_timer_new ();
   132 	g_timer_start (timer);
   133 	do
   134 	{
   135 		while (g_timer_elapsed (timer, NULL) < 1);
   136 		g_timer_reset(timer);
   137 		i++;
   138 	}while(i<3);
   139 		
   140 	g_timer_stop (timer);
   141 	g_timer_destroy (timer);
   142 	
   143 }
   144 
   145 //Test for g_try_malloc0
   146 void tg_try_malloc0()
   147 {
   148 	char* s;
   149 	gpointer try = g_try_malloc0 (sizeof(s));
   150 	g_assert (try != NULL);
   151 }
   152 
   153 int main (int argc,char *argv[])
   154 {
   155 
   156 	#ifdef SYMBIAN
   157  
   158  	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);
   159  	#endif /*SYMBIAN*/
   160  	
   161  	tg_nullify_pointer();
   162 	tg_ptr_array_tests();
   163 	tg_queue_find_custom();
   164 	tg_timer_test();
   165 	tg_try_malloc0();
   166  
   167 #ifdef SYMBIAN
   168   testResultXml("tmisc");
   169 #endif /* EMULATOR */
   170  	return 0;
   171 }