sl@0: /* sl@0: * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public sl@0: * License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: * sl@0: * Description: ?Description sl@0: * sl@0: */ sl@0: sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: #define C2P(c) ((gpointer) ((long) (c))) sl@0: #define GINT_TO_POINTER(i) ((gpointer) (i)) sl@0: #define GPOINTER_TO_INT(p) ((gint) (p)) sl@0: #define TESTPASS 1 sl@0: #define TESTFAIL 0 sl@0: sl@0: sl@0: //Support func for node tests sl@0: void myFun(GNode *node,gpointer data) sl@0: { sl@0: node->data=data; sl@0: } sl@0: sl@0: gpointer cpy_func(gconstpointer src, gpointer data) sl@0: { sl@0: return data; sl@0: } sl@0: sl@0: // g_node_tests sl@0: void tg_node_tests() sl@0: { sl@0: GNode *root; sl@0: GNode *node_B; sl@0: GNode *node_D; sl@0: GNode *node_F; sl@0: GNode *node_G; sl@0: GNode *node_J; sl@0: GNode *node_first_sibling; sl@0: GNode *node_get_root; sl@0: GNode *node_insert_after; sl@0: GNode *node_last_sibling; sl@0: GNode *copy_deep; sl@0: gint node_children_foreach; sl@0: gpointer dat; sl@0: sl@0: //All allocations done thro the allocators..Hence they also get tested! sl@0: GAllocator* alloc = g_allocator_new ("node_alloc",5000); sl@0: g_node_push_allocator (alloc); sl@0: sl@0: sl@0: root = g_node_new (C2P ('A')); sl@0: sl@0: //deep_copy test sl@0: copy_deep = g_node_copy_deep (root, cpy_func, C2P ('P')); sl@0: g_assert (copy_deep->data==C2P('P')); sl@0: sl@0: node_B = g_node_new (C2P ('B')); sl@0: g_node_append (root, node_B); sl@0: g_node_append_data (node_B, C2P ('E')); sl@0: g_node_prepend_data (node_B, C2P ('C')); sl@0: node_D = g_node_new (C2P ('D')); sl@0: g_node_insert (node_B, 1, node_D); sl@0: node_F = g_node_new (C2P ('F')); sl@0: g_node_append (root, node_F); sl@0: node_G = g_node_new (C2P ('G')); sl@0: g_node_append (node_F, node_G); sl@0: node_J = g_node_new (C2P ('J')); sl@0: g_node_prepend (node_G, node_J); sl@0: g_node_insert (node_G, 42, g_node_new (C2P ('K'))); sl@0: g_node_insert_data (node_G, 0, C2P ('H')); sl@0: g_node_insert (node_G, 1, g_node_new (C2P ('I'))); sl@0: sl@0: sl@0: /* we have built: A sl@0: * / \ sl@0: * B F sl@0: * / | \ \ sl@0: * C D E G sl@0: * / /\ \ sl@0: * H I J K sl@0: */ sl@0: sl@0: //Test for g_node_child_index sl@0: g_assert(g_node_child_index(node_B,C2P ('E'))==2); sl@0: g_assert(g_node_child_index(root,C2P ('E'))==-1); sl@0: g_assert(g_node_child_index(node_G,C2P ('K'))==3); sl@0: sl@0: //Test for g_node_children_foreach sl@0: //G_TRAVERSE_ALL test..sets C,D,E to Z sl@0: g_node_children_foreach(node_B,G_TRAVERSE_ALL,myFun,C2P ('Z')); sl@0: node_children_foreach=g_node_child_index(node_B,C2P ('Z')); sl@0: g_assert(node_children_foreach==0); sl@0: //G_TRAVERSE_LEAVES test..tries to set F to Y but fails cause its not a leaf sl@0: g_node_children_foreach(node_F,G_TRAVERSE_LEAVES,myFun,C2P ('Y')); sl@0: node_children_foreach=g_node_child_index(node_F,C2P ('Y')); sl@0: g_assert(node_children_foreach==-1); sl@0: //G_TRAVERSE_NON_LEAVES test..tries to set G to Z but fails cause its a leaf sl@0: g_node_children_foreach(node_G,G_TRAVERSE_NON_LEAVES,myFun,C2P ('Z')); sl@0: node_children_foreach=g_node_child_index(node_G,C2P ('Z')); sl@0: g_assert(node_children_foreach!=0); sl@0: sl@0: sl@0: /* now we have: A sl@0: * / \ sl@0: * B F sl@0: * / | \ \ sl@0: * Z Z Z G sl@0: * / /\ \ sl@0: * H I J K sl@0: */ sl@0: sl@0: //Test for g_node_first_sibling sl@0: node_first_sibling=g_node_first_sibling(node_D->next); sl@0: g_assert(node_first_sibling->data==C2P('Z')); sl@0: sl@0: //Test for g_node_get_root sl@0: node_get_root=g_node_get_root(node_J); sl@0: g_assert(node_get_root->data==C2P('A')); sl@0: sl@0: //Test for g_node_insert_after sl@0: node_insert_after = g_node_new (C2P ('X')); sl@0: g_node_insert_after(node_B,node_D,node_insert_after); sl@0: g_assert(g_node_child_index(node_B,C2P ('X'))==2); sl@0: sl@0: sl@0: /* now we have: A sl@0: * / \ sl@0: * B F sl@0: * / | \ \ \ sl@0: * Z Z X Z G sl@0: * / /\ \ sl@0: * H I J K sl@0: */ sl@0: sl@0: //Test for g_node_is_ancestor sl@0: g_assert(g_node_is_ancestor(root,node_G)); //Grandparent sl@0: g_assert(g_node_is_ancestor(node_G,node_J)); //Parent sl@0: g_assert(!g_node_is_ancestor(node_F,node_B)); //Sibling-negative test sl@0: sl@0: //Test for g_node_last_sibling sl@0: node_last_sibling=g_node_last_sibling(node_D); sl@0: g_assert(node_last_sibling->data==C2P('Z')); //Last sibling for D sl@0: sl@0: sl@0: g_node_destroy (root); sl@0: g_node_pop_allocator (); sl@0: } sl@0: sl@0: sl@0: int main (int argc,char *argv[]) sl@0: { sl@0: sl@0: #ifdef SYMBIAN sl@0: sl@0: 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: #endif /*SYMBIAN*/ sl@0: sl@0: tg_node_tests(); sl@0: sl@0: #ifdef SYMBIAN sl@0: testResultXml("tnode"); sl@0: #endif /* EMULATOR */ sl@0: return 0; sl@0: }