os/ossrv/glib/tsrc/BC/src/tslist.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.
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
sl@0
    45
//Ascending
sl@0
    46
gint compare_fun_gr(gconstpointer a,gconstpointer b)
sl@0
    47
{
sl@0
    48
	return ((*(int *)a==*(int *)b)?0:((*(int *)a>*(int *)b)?1:-1));
sl@0
    49
}
sl@0
    50
sl@0
    51
//Data
sl@0
    52
gint compare_fun_gr_data(gconstpointer a,gconstpointer b,gpointer data)
sl@0
    53
{
sl@0
    54
	return ((*(int *)a==*(int *)b)?0:((*(int *)a>*(int *)b)?1:-1));
sl@0
    55
}
sl@0
    56
sl@0
    57
// Tests for slist
sl@0
    58
void tg_slist_tests()
sl@0
    59
{
sl@0
    60
	GSList *slist,*st,*rem;
sl@0
    61
	gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
sl@0
    62
	gint chk_buf[20];
sl@0
    63
	gint i;
sl@0
    64
	gint g_slist_insert_data;
sl@0
    65
	gint g_slist_insert_before_data;
sl@0
    66
	gint ip1 = 10;
sl@0
    67
	gint ip2 = 15;
sl@0
    68
	gint ip3 = 5;
sl@0
    69
	gint ip4 = 12;
sl@0
    70
	gint g_slist_nth_data_op,g_slist_find_custom_op;
sl@0
    71
	
sl@0
    72
	//Trying to use the allocators so that even they get tested!
sl@0
    73
	GAllocator* alloc = g_allocator_new ("alloc_slist",5000);
sl@0
    74
	g_slist_push_allocator (alloc);
sl@0
    75
	
sl@0
    76
	
sl@0
    77
	slist = NULL;
sl@0
    78
	for (i = 0; i < 10; i++)
sl@0
    79
		slist = g_slist_append (slist, &nums[i]);
sl@0
    80
	
sl@0
    81
	//List looks like:
sl@0
    82
	// 0   1   2   3   4   5   6   7   8   9
sl@0
    83
	
sl@0
    84
	//Test for g_slist_insert....inserted 10 at pos 4
sl@0
    85
	g_slist_insert(slist,&ip1,4);
sl@0
    86
	st = g_slist_nth (slist,0);
sl@0
    87
	for(i = 0;i < 4;i++)
sl@0
    88
		st = st->next;
sl@0
    89
	g_slist_insert_data = *((gint*) st->data);
sl@0
    90
	g_assert(g_slist_insert_data == 10);
sl@0
    91
	
sl@0
    92
/*	for (i = 0; i < 10; i++)
sl@0
    93
    {
sl@0
    94
      st = g_slist_nth (slist, i);
sl@0
    95
      chk_buf[i] = *((gint*) st->data);
sl@0
    96
    }*/
sl@0
    97
    
sl@0
    98
	//List looks like:
sl@0
    99
	// 0   1   2   3   10   4   5   6   7   8   9
sl@0
   100
	
sl@0
   101
	//Test for g_slist_insert_before....inserted 15 at pos 7
sl@0
   102
	st = g_slist_nth (slist,7);
sl@0
   103
	g_slist_insert_before(slist,st,&ip2);
sl@0
   104
	st = g_slist_nth (slist,0);
sl@0
   105
	for(i = 0;i < 7;i++)
sl@0
   106
		st = st->next;
sl@0
   107
	g_slist_insert_before_data = *((gint*) st->data);
sl@0
   108
	g_assert(g_slist_insert_before_data == 15);
sl@0
   109
	
sl@0
   110
	//List looks like:
sl@0
   111
	// 0   1   2   3   10   4   5   15   6   7   8   9
sl@0
   112
	
sl@0
   113
	//Test for g_slist_index....finding 15 at pos 7
sl@0
   114
	st = g_slist_nth (slist,0);
sl@0
   115
	g_assert(g_slist_index(st,&ip2)==7);
sl@0
   116
sl@0
   117
	//Test for g_slist_nth_data....getting 6 at position 8
sl@0
   118
	g_slist_nth_data_op = *((gint*) g_slist_nth_data(slist,8));
sl@0
   119
	g_assert(g_slist_nth_data_op == 6)	;
sl@0
   120
sl@0
   121
	//Test for g_slist_position
sl@0
   122
	st = g_slist_nth (slist,7);
sl@0
   123
	g_assert(g_slist_position (slist,st) == 7);
sl@0
   124
sl@0
   125
	//Test for g_slist_find_custom
sl@0
   126
	st = g_slist_find_custom(slist,&ip3,compare_fun_gr);
sl@0
   127
	g_slist_find_custom_op = *((gint*) st->data);
sl@0
   128
	g_assert(g_slist_find_custom_op == 5);
sl@0
   129
	
sl@0
   130
	//Test for g_slist_sort_with_data
sl@0
   131
	st = g_slist_sort_with_data(slist,compare_fun_gr_data,&ip3);
sl@0
   132
	for (i = 0; i < 10; i++)
sl@0
   133
    {
sl@0
   134
      st = g_slist_nth (slist, i);
sl@0
   135
      g_assert (*((gint*) st->data) == i);
sl@0
   136
    }
sl@0
   137
sl@0
   138
	//List looks like:
sl@0
   139
	// 0   1   2   3   4   5   6   7   8   9   10   15
sl@0
   140
	
sl@0
   141
	//Test for g_slist_remove_link
sl@0
   142
	st = g_slist_nth (slist, 5);
sl@0
   143
	rem = g_slist_remove_link(slist , st);
sl@0
   144
	st = g_slist_nth (slist, 5);
sl@0
   145
    g_assert (*((gint*) st->data) == 6);
sl@0
   146
sl@0
   147
	//List looks like:
sl@0
   148
	// 0   1   2   3   4   6   7   8   9   10   15
sl@0
   149
sl@0
   150
	//Test for g_slist_remove_all
sl@0
   151
	g_slist_insert(slist,&ip4,4);
sl@0
   152
	g_slist_insert(slist,&ip4,6);
sl@0
   153
	g_slist_insert(slist,&ip4,8);
sl@0
   154
	//List looks like:
sl@0
   155
	// 0   1   2   3   4   12   6   7   12   8   12   9   10   15
sl@0
   156
	g_slist_remove_all(slist ,&ip4);
sl@0
   157
    
sl@0
   158
	g_slist_free (slist);
sl@0
   159
	g_slist_pop_allocator ();
sl@0
   160
}
sl@0
   161
sl@0
   162
sl@0
   163
int main (int argc,char *argv[])
sl@0
   164
{
sl@0
   165
sl@0
   166
	#ifdef SYMBIAN
sl@0
   167
 	
sl@0
   168
 	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
   169
 	#endif /*SYMBIAN*/
sl@0
   170
 	
sl@0
   171
 	tg_slist_tests();
sl@0
   172
 #ifdef SYMBIAN
sl@0
   173
  testResultXml("tslist");
sl@0
   174
#endif /* EMULATOR */
sl@0
   175
 	return 0;
sl@0
   176
}