sl@0: /* GLIB - Library of useful routines for C programming sl@0: * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald sl@0: * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 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: sl@0: /* sl@0: * Modified by the GLib Team and others 1997-2000. See the AUTHORS sl@0: * file for a list of people on the GLib Team. See the ChangeLog sl@0: * files for a list of changes. These files are distributed with sl@0: * GLib at ftp://ftp.gtk.org/pub/gtk/. sl@0: */ sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #include sl@0: #include sl@0: #include "glib.h" sl@0: sl@0: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: static gint sl@0: my_compare (gconstpointer a, sl@0: gconstpointer b) sl@0: { sl@0: const char *cha = a; sl@0: const char *chb = b; sl@0: sl@0: return *cha - *chb; sl@0: } sl@0: sl@0: static gint sl@0: my_search (gconstpointer a, sl@0: gconstpointer b) sl@0: { sl@0: return my_compare (b, a); sl@0: } sl@0: sl@0: static gpointer destroyed_key = NULL; sl@0: static gpointer destroyed_value = NULL; sl@0: sl@0: static void sl@0: my_key_destroy (gpointer key) sl@0: { sl@0: destroyed_key = key; sl@0: } sl@0: sl@0: static void sl@0: my_value_destroy (gpointer value) sl@0: { sl@0: destroyed_value = value; sl@0: } sl@0: sl@0: static gint sl@0: my_traverse (gpointer key, sl@0: gpointer value, sl@0: gpointer data) sl@0: { sl@0: char *ch = key; sl@0: g_assert ((*ch) > 0); sl@0: return FALSE; sl@0: } sl@0: sl@0: char chars[] = sl@0: "0123456789" sl@0: "ABCDEFGHIJKLMNOPQRSTUVWXYZ" sl@0: "abcdefghijklmnopqrstuvwxyz"; sl@0: sl@0: char chars2[] = sl@0: "0123456789" sl@0: "abcdefghijklmnopqrstuvwxyz"; sl@0: sl@0: static gint sl@0: check_order (gpointer key, sl@0: gpointer value, sl@0: gpointer data) sl@0: { sl@0: char **p = data; sl@0: char *ch = key; sl@0: sl@0: g_assert (**p == *ch); sl@0: sl@0: (*p)++; sl@0: sl@0: return FALSE; sl@0: } sl@0: sl@0: sl@0: sl@0: int sl@0: main (int argc, sl@0: char *argv[]) sl@0: { sl@0: gint i; sl@0: GTree *tree; sl@0: gboolean removed; sl@0: char c, d; sl@0: char *p; sl@0: #ifdef SYMBIAN 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: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: tree = g_tree_new (my_compare); sl@0: sl@0: for (i = 0; chars[i]; i++) sl@0: g_tree_insert (tree, &chars[i], &chars[i]); sl@0: sl@0: g_tree_foreach (tree, my_traverse, NULL); sl@0: sl@0: g_assert (g_tree_nnodes (tree) == strlen (chars)); sl@0: g_assert (g_tree_height (tree) == 6); sl@0: sl@0: p = chars; sl@0: g_tree_foreach (tree, check_order, &p); sl@0: sl@0: for (i = 0; i < 26; i++) sl@0: { sl@0: removed = g_tree_remove (tree, &chars[i + 10]); sl@0: g_assert (removed); sl@0: } sl@0: sl@0: c = '\0'; sl@0: removed = g_tree_remove (tree, &c); sl@0: g_assert (removed == FALSE); sl@0: sl@0: g_tree_foreach (tree, my_traverse, NULL); sl@0: sl@0: g_assert (g_tree_nnodes (tree) == strlen (chars2)); sl@0: g_assert (g_tree_height (tree) == 6); sl@0: sl@0: p = chars2; sl@0: g_tree_foreach (tree, check_order, &p); sl@0: sl@0: for (i = 25; i >= 0; i--) sl@0: g_tree_insert (tree, &chars[i + 10], &chars[i + 10]); sl@0: sl@0: p = chars; sl@0: g_tree_foreach (tree, check_order, &p); sl@0: sl@0: c = '0'; sl@0: p = g_tree_lookup (tree, &c); sl@0: g_assert (p && *p == c); sl@0: sl@0: c = 'A'; sl@0: p = g_tree_lookup (tree, &c); sl@0: g_assert (p && *p == c); sl@0: sl@0: c = 'a'; sl@0: p = g_tree_lookup (tree, &c); sl@0: g_assert (p && *p == c); sl@0: sl@0: c = 'z'; sl@0: p = g_tree_lookup (tree, &c); sl@0: g_assert (p && *p == c); sl@0: sl@0: c = '!'; sl@0: p = g_tree_lookup (tree, &c); sl@0: g_assert (p == NULL); sl@0: sl@0: c = '='; sl@0: p = g_tree_lookup (tree, &c); sl@0: g_assert (p == NULL); sl@0: sl@0: c = '|'; sl@0: p = g_tree_lookup (tree, &c); sl@0: g_assert (p == NULL); sl@0: sl@0: c = '0'; sl@0: p = g_tree_search (tree, my_search, &c); sl@0: g_assert (p && *p == c); sl@0: sl@0: c = 'A'; sl@0: p = g_tree_search (tree, my_search, &c); sl@0: g_assert (p && *p == c); sl@0: sl@0: c = 'a'; sl@0: p = g_tree_search (tree, my_search, &c); sl@0: g_assert (p &&*p == c); sl@0: sl@0: c = 'z'; sl@0: p = g_tree_search (tree, my_search, &c); sl@0: g_assert (p && *p == c); sl@0: sl@0: c = '!'; sl@0: p = g_tree_search (tree, my_search, &c); sl@0: g_assert (p == NULL); sl@0: sl@0: c = '='; sl@0: p = g_tree_search (tree, my_search, &c); sl@0: g_assert (p == NULL); sl@0: sl@0: c = '|'; sl@0: p = g_tree_search (tree, my_search, &c); sl@0: g_assert (p == NULL); sl@0: sl@0: sl@0: g_tree_destroy (tree); sl@0: sl@0: tree = g_tree_new_full ((GCompareDataFunc)my_compare, NULL, my_key_destroy, my_value_destroy); sl@0: sl@0: for (i = 0; chars[i]; i++) sl@0: g_tree_insert (tree, &chars[i], &chars[i]); sl@0: sl@0: c = '0'; sl@0: g_tree_insert (tree, &c, &c); sl@0: g_assert (destroyed_key == &c); sl@0: g_assert (destroyed_value == &chars[0]); sl@0: destroyed_key = NULL; sl@0: destroyed_value = NULL; sl@0: sl@0: d = '1'; sl@0: g_tree_replace (tree, &d, &d); sl@0: g_assert (destroyed_key == &chars[1]); sl@0: g_assert (destroyed_value == &chars[1]); sl@0: destroyed_key = NULL; sl@0: destroyed_value = NULL; sl@0: sl@0: c = '2'; sl@0: removed = g_tree_remove (tree, &c); sl@0: g_assert (removed); sl@0: g_assert (destroyed_key == &chars[2]); sl@0: g_assert (destroyed_value == &chars[2]); sl@0: destroyed_key = NULL; sl@0: destroyed_value = NULL; sl@0: sl@0: c = '3'; sl@0: removed = g_tree_steal (tree, &c); sl@0: g_assert (removed); sl@0: g_assert (destroyed_key == NULL); sl@0: g_assert (destroyed_value == NULL); sl@0: #ifdef SYMBIAN sl@0: testResultXml("tree-test"); sl@0: #endif /* EMULATOR */ sl@0: sl@0: return 0; sl@0: } sl@0: