1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/src/tnode.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,191 @@
1.4 +/*
1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.6 +*
1.7 +* This library is free software; you can redistribute it and/or
1.8 +* modify it under the terms of the GNU Lesser General Public
1.9 +* License as published by the Free Software Foundation; either
1.10 +* version 2 of the License, or (at your option) any later version.
1.11 +*
1.12 +* This library is distributed in the hope that it will be useful,
1.13 +* but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 +* Lesser General Public License for more details.
1.16 +*
1.17 +* You should have received a copy of the GNU Lesser General Public
1.18 +* License along with this library; if not, write to the
1.19 +* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.20 +* Boston, MA 02111-1307, USA.
1.21 +*
1.22 +* Description: ?Description
1.23 +*
1.24 +*/
1.25 +
1.26 +
1.27 +#undef G_DISABLE_ASSERT
1.28 +#undef G_LOG_DOMAIN
1.29 +
1.30 +
1.31 +#include <stdio.h>
1.32 +#include <string.h>
1.33 +#include <glib.h>
1.34 +#include <fcntl.h>
1.35 +#include <goption.h>
1.36 +
1.37 +#ifdef SYMBIAN
1.38 +#include "mrt2_glib2_test.h"
1.39 +#endif /*SYMBIAN*/
1.40 +
1.41 +#define C2P(c) ((gpointer) ((long) (c)))
1.42 +#define GINT_TO_POINTER(i) ((gpointer) (i))
1.43 +#define GPOINTER_TO_INT(p) ((gint) (p))
1.44 +#define TESTPASS 1
1.45 +#define TESTFAIL 0
1.46 +
1.47 +
1.48 +//Support func for node tests
1.49 +void myFun(GNode *node,gpointer data)
1.50 +{
1.51 + node->data=data;
1.52 +}
1.53 +
1.54 +gpointer cpy_func(gconstpointer src, gpointer data)
1.55 +{
1.56 + return data;
1.57 +}
1.58 +
1.59 +// g_node_tests
1.60 +void tg_node_tests()
1.61 +{
1.62 + GNode *root;
1.63 + GNode *node_B;
1.64 + GNode *node_D;
1.65 + GNode *node_F;
1.66 + GNode *node_G;
1.67 + GNode *node_J;
1.68 + GNode *node_first_sibling;
1.69 + GNode *node_get_root;
1.70 + GNode *node_insert_after;
1.71 + GNode *node_last_sibling;
1.72 + GNode *copy_deep;
1.73 + gint node_children_foreach;
1.74 + gpointer dat;
1.75 +
1.76 + //All allocations done thro the allocators..Hence they also get tested!
1.77 + GAllocator* alloc = g_allocator_new ("node_alloc",5000);
1.78 + g_node_push_allocator (alloc);
1.79 +
1.80 +
1.81 + root = g_node_new (C2P ('A'));
1.82 +
1.83 + //deep_copy test
1.84 + copy_deep = g_node_copy_deep (root, cpy_func, C2P ('P'));
1.85 + g_assert (copy_deep->data==C2P('P'));
1.86 +
1.87 + node_B = g_node_new (C2P ('B'));
1.88 + g_node_append (root, node_B);
1.89 + g_node_append_data (node_B, C2P ('E'));
1.90 + g_node_prepend_data (node_B, C2P ('C'));
1.91 + node_D = g_node_new (C2P ('D'));
1.92 + g_node_insert (node_B, 1, node_D);
1.93 + node_F = g_node_new (C2P ('F'));
1.94 + g_node_append (root, node_F);
1.95 + node_G = g_node_new (C2P ('G'));
1.96 + g_node_append (node_F, node_G);
1.97 + node_J = g_node_new (C2P ('J'));
1.98 + g_node_prepend (node_G, node_J);
1.99 + g_node_insert (node_G, 42, g_node_new (C2P ('K')));
1.100 + g_node_insert_data (node_G, 0, C2P ('H'));
1.101 + g_node_insert (node_G, 1, g_node_new (C2P ('I')));
1.102 +
1.103 +
1.104 + /* we have built: A
1.105 + * / \
1.106 + * B F
1.107 + * / | \ \
1.108 + * C D E G
1.109 + * / /\ \
1.110 + * H I J K
1.111 + */
1.112 +
1.113 + //Test for g_node_child_index
1.114 + g_assert(g_node_child_index(node_B,C2P ('E'))==2);
1.115 + g_assert(g_node_child_index(root,C2P ('E'))==-1);
1.116 + g_assert(g_node_child_index(node_G,C2P ('K'))==3);
1.117 +
1.118 + //Test for g_node_children_foreach
1.119 + //G_TRAVERSE_ALL test..sets C,D,E to Z
1.120 + g_node_children_foreach(node_B,G_TRAVERSE_ALL,myFun,C2P ('Z'));
1.121 + node_children_foreach=g_node_child_index(node_B,C2P ('Z'));
1.122 + g_assert(node_children_foreach==0);
1.123 + //G_TRAVERSE_LEAVES test..tries to set F to Y but fails cause its not a leaf
1.124 + g_node_children_foreach(node_F,G_TRAVERSE_LEAVES,myFun,C2P ('Y'));
1.125 + node_children_foreach=g_node_child_index(node_F,C2P ('Y'));
1.126 + g_assert(node_children_foreach==-1);
1.127 + //G_TRAVERSE_NON_LEAVES test..tries to set G to Z but fails cause its a leaf
1.128 + g_node_children_foreach(node_G,G_TRAVERSE_NON_LEAVES,myFun,C2P ('Z'));
1.129 + node_children_foreach=g_node_child_index(node_G,C2P ('Z'));
1.130 + g_assert(node_children_foreach!=0);
1.131 +
1.132 +
1.133 + /* now we have: A
1.134 + * / \
1.135 + * B F
1.136 + * / | \ \
1.137 + * Z Z Z G
1.138 + * / /\ \
1.139 + * H I J K
1.140 + */
1.141 +
1.142 + //Test for g_node_first_sibling
1.143 + node_first_sibling=g_node_first_sibling(node_D->next);
1.144 + g_assert(node_first_sibling->data==C2P('Z'));
1.145 +
1.146 + //Test for g_node_get_root
1.147 + node_get_root=g_node_get_root(node_J);
1.148 + g_assert(node_get_root->data==C2P('A'));
1.149 +
1.150 + //Test for g_node_insert_after
1.151 + node_insert_after = g_node_new (C2P ('X'));
1.152 + g_node_insert_after(node_B,node_D,node_insert_after);
1.153 + g_assert(g_node_child_index(node_B,C2P ('X'))==2);
1.154 +
1.155 +
1.156 + /* now we have: A
1.157 + * / \
1.158 + * B F
1.159 + * / | \ \ \
1.160 + * Z Z X Z G
1.161 + * / /\ \
1.162 + * H I J K
1.163 + */
1.164 +
1.165 + //Test for g_node_is_ancestor
1.166 + g_assert(g_node_is_ancestor(root,node_G)); //Grandparent
1.167 + g_assert(g_node_is_ancestor(node_G,node_J)); //Parent
1.168 + g_assert(!g_node_is_ancestor(node_F,node_B)); //Sibling-negative test
1.169 +
1.170 + //Test for g_node_last_sibling
1.171 + node_last_sibling=g_node_last_sibling(node_D);
1.172 + g_assert(node_last_sibling->data==C2P('Z')); //Last sibling for D
1.173 +
1.174 +
1.175 + g_node_destroy (root);
1.176 + g_node_pop_allocator ();
1.177 +}
1.178 +
1.179 +
1.180 +int main (int argc,char *argv[])
1.181 +{
1.182 +
1.183 + #ifdef SYMBIAN
1.184 +
1.185 + 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);
1.186 + #endif /*SYMBIAN*/
1.187 +
1.188 + tg_node_tests();
1.189 +
1.190 +#ifdef SYMBIAN
1.191 + testResultXml("tnode");
1.192 +#endif /* EMULATOR */
1.193 + return 0;
1.194 +}
1.195 \ No newline at end of file