First public contribution.
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
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.
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.
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.
19 * Description: ?Description
24 #undef G_DISABLE_ASSERT
35 #include "mrt2_glib2_test.h"
38 #define C2P(c) ((gpointer) ((long) (c)))
39 #define GINT_TO_POINTER(i) ((gpointer) (i))
40 #define GPOINTER_TO_INT(p) ((gint) (p))
45 //Support func for node tests
46 void myFun(GNode *node,gpointer data)
51 gpointer cpy_func(gconstpointer src, gpointer data)
65 GNode *node_first_sibling;
67 GNode *node_insert_after;
68 GNode *node_last_sibling;
70 gint node_children_foreach;
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);
78 root = g_node_new (C2P ('A'));
81 copy_deep = g_node_copy_deep (root, cpy_func, C2P ('P'));
82 g_assert (copy_deep->data==C2P('P'));
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')));
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);
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);
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'));
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'));
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);
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
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
172 g_node_destroy (root);
173 g_node_pop_allocator ();
177 int main (int argc,char *argv[])
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);
188 testResultXml("tnode");
189 #endif /* EMULATOR */