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