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