Add tree test.
[platform/upstream/glib.git] / tests / tree-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library 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.
8  *
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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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.
18  */
19 #undef G_LOG_DOMAIN
20
21 #include <stdio.h>
22 #include <string.h>
23 #include "glib.h"
24
25 int array[10000];
26 gboolean failed = FALSE;
27
28 #define TEST(m,cond)    G_STMT_START { failed = !(cond); \
29 if (failed) \
30   { if (!m) \
31       g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
32     else \
33       g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
34   } \
35 else \
36   g_print ("."); fflush (stdout); \
37 } G_STMT_END
38
39 #define C2P(c)          ((gpointer) ((long) (c)))
40 #define P2C(p)          ((gchar) ((long) (p)))
41
42 #define GLIB_TEST_STRING "el dorado "
43 #define GLIB_TEST_STRING_5 "el do"
44
45 typedef struct {
46         guint age;
47         gchar name[40];
48 } GlibTestInfo;
49
50
51 static gint
52 my_compare (gconstpointer a,
53             gconstpointer b)
54 {
55   const char *cha = a;
56   const char *chb = b;
57
58   return *cha - *chb;
59 }
60
61 static gint
62 my_traverse (gpointer key,
63              gpointer value,
64              gpointer data)
65 {
66   char *ch = key;
67   g_assert ((*ch) > 0);
68   return FALSE;
69 }
70
71 int
72 main (int   argc,
73       char *argv[])
74 {
75   gint i, j;
76   GTree *tree;
77   char chars[62];
78
79   tree = g_tree_new (my_compare);
80   i = 0;
81   for (j = 0; j < 10; j++, i++)
82     {
83       chars[i] = '0' + j;
84       g_tree_insert (tree, &chars[i], &chars[i]);
85     }
86   for (j = 0; j < 26; j++, i++)
87     {
88       chars[i] = 'A' + j;
89       g_tree_insert (tree, &chars[i], &chars[i]);
90     }
91   for (j = 0; j < 26; j++, i++)
92     {
93       chars[i] = 'a' + j;
94       g_tree_insert (tree, &chars[i], &chars[i]);
95     }
96
97   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
98
99   g_assert (g_tree_nnodes (tree) == (10 + 26 + 26));
100
101   for (i = 0; i < 10; i++)
102     g_tree_remove (tree, &chars[i]);
103
104   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
105
106   return 0;
107 }
108