os/ossrv/glib/tsrc/BC/src/tnode.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
sl@0
    45
//Support func for node tests
sl@0
    46
void myFun(GNode *node,gpointer data)
sl@0
    47
{
sl@0
    48
	node->data=data;	
sl@0
    49
}
sl@0
    50
sl@0
    51
gpointer cpy_func(gconstpointer src, gpointer data)
sl@0
    52
{
sl@0
    53
	return data;
sl@0
    54
}
sl@0
    55
sl@0
    56
// g_node_tests
sl@0
    57
void tg_node_tests()
sl@0
    58
{
sl@0
    59
	GNode *root;
sl@0
    60
	GNode *node_B;
sl@0
    61
	GNode *node_D;
sl@0
    62
	GNode *node_F;
sl@0
    63
	GNode *node_G;
sl@0
    64
	GNode *node_J;
sl@0
    65
	GNode *node_first_sibling;
sl@0
    66
	GNode *node_get_root;
sl@0
    67
	GNode *node_insert_after;
sl@0
    68
	GNode *node_last_sibling;
sl@0
    69
	GNode *copy_deep;
sl@0
    70
	gint node_children_foreach;
sl@0
    71
	gpointer dat;
sl@0
    72
	
sl@0
    73
	//All allocations done thro the allocators..Hence they also get tested!
sl@0
    74
	GAllocator* alloc = g_allocator_new ("node_alloc",5000);
sl@0
    75
	g_node_push_allocator (alloc);
sl@0
    76
	
sl@0
    77
	
sl@0
    78
	root = g_node_new (C2P ('A'));
sl@0
    79
	
sl@0
    80
	//deep_copy test
sl@0
    81
	copy_deep = g_node_copy_deep (root, cpy_func, C2P ('P'));
sl@0
    82
	g_assert (copy_deep->data==C2P('P'));
sl@0
    83
	
sl@0
    84
	node_B = g_node_new (C2P ('B'));
sl@0
    85
  	g_node_append (root, node_B);
sl@0
    86
  	g_node_append_data (node_B, C2P ('E'));
sl@0
    87
 	g_node_prepend_data (node_B, C2P ('C'));
sl@0
    88
 	node_D = g_node_new (C2P ('D'));
sl@0
    89
 	g_node_insert (node_B, 1, node_D); 
sl@0
    90
	node_F = g_node_new (C2P ('F'));
sl@0
    91
  	g_node_append (root, node_F);
sl@0
    92
 	node_G = g_node_new (C2P ('G'));
sl@0
    93
 	g_node_append (node_F, node_G);
sl@0
    94
	node_J = g_node_new (C2P ('J'));
sl@0
    95
	g_node_prepend (node_G, node_J);
sl@0
    96
	g_node_insert (node_G, 42, g_node_new (C2P ('K')));
sl@0
    97
	g_node_insert_data (node_G, 0, C2P ('H'));
sl@0
    98
 	g_node_insert (node_G, 1, g_node_new (C2P ('I')));
sl@0
    99
 	
sl@0
   100
 	
sl@0
   101
   /* we have built:                   A
sl@0
   102
   *                                 /   \
sl@0
   103
   *                               B       F
sl@0
   104
   *                             / | \       \
sl@0
   105
   *                           C   D   E       G
sl@0
   106
   *                                         / /\ \
sl@0
   107
   *                                       H  I  J  K
sl@0
   108
   */
sl@0
   109
   
sl@0
   110
   //Test for g_node_child_index
sl@0
   111
 	g_assert(g_node_child_index(node_B,C2P ('E'))==2);	
sl@0
   112
 	g_assert(g_node_child_index(root,C2P ('E'))==-1);	
sl@0
   113
 	g_assert(g_node_child_index(node_G,C2P ('K'))==3);	
sl@0
   114
sl@0
   115
	//Test for g_node_children_foreach
sl@0
   116
	//G_TRAVERSE_ALL test..sets C,D,E to Z
sl@0
   117
	g_node_children_foreach(node_B,G_TRAVERSE_ALL,myFun,C2P ('Z'));
sl@0
   118
	node_children_foreach=g_node_child_index(node_B,C2P ('Z'));
sl@0
   119
	g_assert(node_children_foreach==0);
sl@0
   120
	//G_TRAVERSE_LEAVES test..tries to set F to Y but fails cause its not a leaf
sl@0
   121
	g_node_children_foreach(node_F,G_TRAVERSE_LEAVES,myFun,C2P ('Y'));
sl@0
   122
	node_children_foreach=g_node_child_index(node_F,C2P ('Y'));
sl@0
   123
	g_assert(node_children_foreach==-1);
sl@0
   124
	//G_TRAVERSE_NON_LEAVES test..tries to set G to Z but fails cause its a leaf
sl@0
   125
	g_node_children_foreach(node_G,G_TRAVERSE_NON_LEAVES,myFun,C2P ('Z'));
sl@0
   126
	node_children_foreach=g_node_child_index(node_G,C2P ('Z'));
sl@0
   127
	g_assert(node_children_foreach!=0);
sl@0
   128
	
sl@0
   129
	 	
sl@0
   130
   /* now we have:	                   A
sl@0
   131
   *                                 /   \
sl@0
   132
   *                               B       F
sl@0
   133
   *                             / | \       \
sl@0
   134
   *                           	Z  Z   Z       G
sl@0
   135
   *                                         / /\ \
sl@0
   136
   *                                       H  I  J  K
sl@0
   137
   */
sl@0
   138
	
sl@0
   139
	//Test for g_node_first_sibling
sl@0
   140
	node_first_sibling=g_node_first_sibling(node_D->next);
sl@0
   141
	g_assert(node_first_sibling->data==C2P('Z')); 	
sl@0
   142
	
sl@0
   143
	//Test for g_node_get_root
sl@0
   144
	node_get_root=g_node_get_root(node_J);
sl@0
   145
	g_assert(node_get_root->data==C2P('A'));
sl@0
   146
	 
sl@0
   147
	//Test for g_node_insert_after
sl@0
   148
	node_insert_after = g_node_new (C2P ('X'));
sl@0
   149
	g_node_insert_after(node_B,node_D,node_insert_after);
sl@0
   150
	g_assert(g_node_child_index(node_B,C2P ('X'))==2);	
sl@0
   151
	
sl@0
   152
	
sl@0
   153
   /* now we have:	                   A
sl@0
   154
   *                                 /   \
sl@0
   155
   *                               B       F
sl@0
   156
   *                            /  | \ \    \
sl@0
   157
   *                           Z   Z  X  Z    G
sl@0
   158
   *                                        / /\ \
sl@0
   159
   *                                       H I  J K
sl@0
   160
   */
sl@0
   161
	
sl@0
   162
	//Test for g_node_is_ancestor
sl@0
   163
	g_assert(g_node_is_ancestor(root,node_G)); //Grandparent
sl@0
   164
	g_assert(g_node_is_ancestor(node_G,node_J)); //Parent
sl@0
   165
	g_assert(!g_node_is_ancestor(node_F,node_B)); //Sibling-negative test
sl@0
   166
	
sl@0
   167
	//Test for g_node_last_sibling
sl@0
   168
	node_last_sibling=g_node_last_sibling(node_D);
sl@0
   169
	g_assert(node_last_sibling->data==C2P('Z')); //Last sibling for D
sl@0
   170
sl@0
   171
	
sl@0
   172
	g_node_destroy (root);
sl@0
   173
	g_node_pop_allocator ();
sl@0
   174
}
sl@0
   175
sl@0
   176
sl@0
   177
int main (int argc,char *argv[])
sl@0
   178
{
sl@0
   179
sl@0
   180
	#ifdef SYMBIAN
sl@0
   181
 
sl@0
   182
 	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
   183
 	#endif /*SYMBIAN*/
sl@0
   184
 	
sl@0
   185
 	tg_node_tests();
sl@0
   186
 
sl@0
   187
#ifdef SYMBIAN
sl@0
   188
  testResultXml("tnode");
sl@0
   189
#endif /* EMULATOR */
sl@0
   190
 	return 0;
sl@0
   191
}