1 /* Test the tree class.
2 * By Richard W.M. Jones <rich@annexia.org>
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.
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.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: test_tree.c,v 1.1 2002/09/15 15:08:52 rich Exp $
20 * XXX This test is incomplete.
42 pool pool1 = new_subpool (global_pool), pool2;
43 tree root, leaf1, leaf2, leaf3, t;
44 const char *root_node = "root node";
45 const char *leaf1_node = "leaf1 node";
46 const char *leaf2_node = "leaf2 node";
47 const char *leaf3_node = "leaf3 node";
50 /* Create a simple node and test node access functions. */
51 root = new_tree (pool1, char *);
53 tree_set_data (root, root_node);
55 leaf1 = new_tree (pool1, char *);
56 tree_set_data (leaf1, leaf1_node);
57 leaf2 = new_tree (pool1, char *);
58 tree_set_data (leaf2, leaf2_node);
59 leaf3 = new_tree (pool1, char *);
60 tree_set_data (leaf3, leaf3_node);
61 tree_push_back (root, leaf1);
62 tree_push_back (root, leaf2);
63 tree_push_back (root, leaf3);
65 assert (tree_size (root) == 3);
67 tree_get (root, 0, t);
69 tree_get (root, 1, t);
71 tree_get (root, 2, t);
74 tree_pop_front (root, t);
76 tree_pop_back (root, t);
78 tree_pop_front (root, t);
81 assert (tree_size (root) == 0);
83 tree_insert (root, 0, leaf1);
84 tree_insert (root, 1, leaf2);
85 tree_insert (root, 2, leaf3);
87 tree_get (root, 0, t);
89 tree_get (root, 1, t);
91 tree_get (root, 2, t);
94 tree_replace (root, 0, leaf3);
95 tree_replace (root, 1, leaf1);
96 tree_replace (root, 2, leaf2);
98 tree_get (root, 0, t);
100 tree_get (root, 1, t);
102 tree_get (root, 2, t);
105 tree_erase (root, 0);
106 assert (tree_size (root) == 2);
108 tree_erase_range (root, 0, 2);
109 assert (tree_size (root) == 0);
111 tree_insert (root, 0, leaf1);
112 tree_insert (root, 1, leaf2);
113 tree_insert (root, 2, leaf3);
116 assert (tree_size (root) == 0);
118 tree_get_data (root, str);
119 assert (strcmp (str, root_node) == 0);
121 /* Copy the tree into another pool, delete the original. */
122 tree_insert (root, 0, leaf1);
123 tree_insert (root, 1, leaf2);
124 tree_insert (root, 2, leaf3);
126 pool2 = new_subpool (global_pool);
127 root = copy_tree (pool2, root);
130 assert (tree_size (root) == 3);
132 tree_get_data (root, str);
133 assert (strcmp (str, root_node) == 0);
135 tree_get (root, 0, t);
136 tree_get_data (t, str);
137 assert (strcmp (str, leaf1_node) == 0);
139 tree_get (root, 1, t);
140 tree_get_data (t, str);
141 assert (strcmp (str, leaf2_node) == 0);
143 tree_get (root, 2, t);
144 tree_get_data (t, str);
145 assert (strcmp (str, leaf3_node) == 0);