glib/tsrc/glib_nonstif/src/tnode.c
branchRCL_3
changeset 57 2efc27d87e1c
parent 0 e4d67989cc36
equal deleted inserted replaced
56:acd3cd4aaceb 57:2efc27d87e1c
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #undef G_DISABLE_ASSERT
       
    21 #undef G_LOG_DOMAIN
       
    22 
       
    23 #include <stdio.h>
       
    24 #include <string.h>
       
    25 #include <glib.h>
       
    26 #include <fcntl.h>
       
    27 #include <goption.h>
       
    28 
       
    29 #ifdef __SYMBIAN32__
       
    30 #include "mrt2_glib2_test.h"
       
    31 #endif /*__SYMBIAN32__*/
       
    32 
       
    33 #define	C2P(c)		((gpointer) ((long) (c)))
       
    34 #define GINT_TO_POINTER(i)	((gpointer)  (i))
       
    35 #define GPOINTER_TO_INT(p)	((gint)   (p))
       
    36 #define TESTPASS	1
       
    37 #define TESTFAIL	0
       
    38 
       
    39 
       
    40 //Support func for node tests
       
    41 void myFun(GNode *node,gpointer data)
       
    42 {
       
    43 	node->data=data;	
       
    44 }
       
    45 
       
    46 gpointer cpy_func(gconstpointer src, gpointer data)
       
    47 {
       
    48 	return data;
       
    49 }
       
    50 
       
    51 // g_node_tests
       
    52 void tg_node_tests()
       
    53 {
       
    54 	GNode *root;
       
    55 	GNode *node_B;
       
    56 	GNode *node_D;
       
    57 	GNode *node_F;
       
    58 	GNode *node_G;
       
    59 	GNode *node_J;
       
    60 	GNode *node_first_sibling;
       
    61 	GNode *node_get_root;
       
    62 	GNode *node_insert_after;
       
    63 	GNode *node_last_sibling;
       
    64 	GNode *copy_deep;
       
    65 	gint node_children_foreach;
       
    66 	gpointer dat;
       
    67 	
       
    68 	//All allocations done thro the allocators..Hence they also get tested!
       
    69 	GAllocator* alloc = g_allocator_new ("node_alloc",5000);
       
    70 	g_node_push_allocator (alloc);
       
    71 	
       
    72 	
       
    73 	root = g_node_new (C2P ('A'));
       
    74 	
       
    75 	//deep_copy test
       
    76 	copy_deep = g_node_copy_deep (root, cpy_func, C2P ('P'));
       
    77 	g_assert (copy_deep->data==C2P('P'));
       
    78 	
       
    79 	node_B = g_node_new (C2P ('B'));
       
    80   	g_node_append (root, node_B);
       
    81   	g_node_append_data (node_B, C2P ('E'));
       
    82  	g_node_prepend_data (node_B, C2P ('C'));
       
    83  	node_D = g_node_new (C2P ('D'));
       
    84  	g_node_insert (node_B, 1, node_D); 
       
    85 	node_F = g_node_new (C2P ('F'));
       
    86   	g_node_append (root, node_F);
       
    87  	node_G = g_node_new (C2P ('G'));
       
    88  	g_node_append (node_F, node_G);
       
    89 	node_J = g_node_new (C2P ('J'));
       
    90 	g_node_prepend (node_G, node_J);
       
    91 	g_node_insert (node_G, 42, g_node_new (C2P ('K')));
       
    92 	g_node_insert_data (node_G, 0, C2P ('H'));
       
    93  	g_node_insert (node_G, 1, g_node_new (C2P ('I')));
       
    94  	
       
    95  	
       
    96    /* we have built:                   A
       
    97    *                                 /   \
       
    98    *                               B       F
       
    99    *                             / | \       \
       
   100    *                           C   D   E       G
       
   101    *                                         / /\ \
       
   102    *                                       H  I  J  K
       
   103    */
       
   104    
       
   105    //Test for g_node_child_index
       
   106  	g_assert(g_node_child_index(node_B,C2P ('E'))==2);	
       
   107  	g_assert(g_node_child_index(root,C2P ('E'))==-1);	
       
   108  	g_assert(g_node_child_index(node_G,C2P ('K'))==3);	
       
   109 
       
   110 	//Test for g_node_children_foreach
       
   111 	//G_TRAVERSE_ALL test..sets C,D,E to Z
       
   112 	g_node_children_foreach(node_B,G_TRAVERSE_ALL,myFun,C2P ('Z'));
       
   113 	node_children_foreach=g_node_child_index(node_B,C2P ('Z'));
       
   114 	g_assert(node_children_foreach==0);
       
   115 	//G_TRAVERSE_LEAVES test..tries to set F to Y but fails cause its not a leaf
       
   116 	g_node_children_foreach(node_F,G_TRAVERSE_LEAVES,myFun,C2P ('Y'));
       
   117 	node_children_foreach=g_node_child_index(node_F,C2P ('Y'));
       
   118 	g_assert(node_children_foreach==-1);
       
   119 	//G_TRAVERSE_NON_LEAVES test..tries to set G to Z but fails cause its a leaf
       
   120 	g_node_children_foreach(node_G,G_TRAVERSE_NON_LEAVES,myFun,C2P ('Z'));
       
   121 	node_children_foreach=g_node_child_index(node_G,C2P ('Z'));
       
   122 	g_assert(node_children_foreach!=0);
       
   123 	
       
   124 	 	
       
   125    /* now we have:	                   A
       
   126    *                                 /   \
       
   127    *                               B       F
       
   128    *                             / | \       \
       
   129    *                           	Z  Z   Z       G
       
   130    *                                         / /\ \
       
   131    *                                       H  I  J  K
       
   132    */
       
   133 	
       
   134 	//Test for g_node_first_sibling
       
   135 	node_first_sibling=g_node_first_sibling(node_D->next);
       
   136 	g_assert(node_first_sibling->data==C2P('Z')); 	
       
   137 	
       
   138 	//Test for g_node_get_root
       
   139 	node_get_root=g_node_get_root(node_J);
       
   140 	g_assert(node_get_root->data==C2P('A'));
       
   141 	 
       
   142 	//Test for g_node_insert_after
       
   143 	node_insert_after = g_node_new (C2P ('X'));
       
   144 	g_node_insert_after(node_B,node_D,node_insert_after);
       
   145 	g_assert(g_node_child_index(node_B,C2P ('X'))==2);	
       
   146 	
       
   147 	
       
   148    /* now we have:	                   A
       
   149    *                                 /   \
       
   150    *                               B       F
       
   151    *                            /  | \ \    \
       
   152    *                           Z   Z  X  Z    G
       
   153    *                                        / /\ \
       
   154    *                                       H I  J K
       
   155    */
       
   156 	
       
   157 	//Test for g_node_is_ancestor
       
   158 	g_assert(g_node_is_ancestor(root,node_G)); //Grandparent
       
   159 	g_assert(g_node_is_ancestor(node_G,node_J)); //Parent
       
   160 	g_assert(!g_node_is_ancestor(node_F,node_B)); //Sibling-negative test
       
   161 	
       
   162 	//Test for g_node_last_sibling
       
   163 	node_last_sibling=g_node_last_sibling(node_D);
       
   164 	g_assert(node_last_sibling->data==C2P('Z')); //Last sibling for D
       
   165 
       
   166 	
       
   167 	g_node_destroy (root);
       
   168 	g_node_pop_allocator ();
       
   169 }
       
   170 
       
   171 int main (int argc,char *argv[])
       
   172 {
       
   173 
       
   174 	#ifdef __SYMBIAN32__
       
   175  
       
   176  	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);
       
   177  	#endif /*__SYMBIAN32__*/
       
   178  	
       
   179  	tg_node_tests();
       
   180  
       
   181 #ifdef __SYMBIAN32__
       
   182   testResultXml("tnode");
       
   183 #endif /* EMULATOR */
       
   184  	return 0;
       
   185 }