Add to git.
[c2lib.git] / test_tree.c
1 /* Test the tree class.
2  * By Richard W.M. Jones <rich@annexia.org>
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 Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: test_tree.c,v 1.1 2002/09/15 15:08:52 rich Exp $
19  *
20  * XXX This test is incomplete.
21  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #endif
31
32 #ifdef HAVE_ASSERT_H
33 #include <assert.h>
34 #endif
35
36 #include "pool.h"
37 #include "tree.h"
38
39 int
40 main ()
41 {
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";
48   char *str;
49
50   /* Create a simple node and test node access functions. */
51   root = new_tree (pool1, char *);
52
53   tree_set_data (root, root_node);
54
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);
64
65   assert (tree_size (root) == 3);
66
67   tree_get (root, 0, t);
68   assert (t == leaf1);
69   tree_get (root, 1, t);
70   assert (t == leaf2);
71   tree_get (root, 2, t);
72   assert (t == leaf3);
73
74   tree_pop_front (root, t);
75   assert (t == leaf1);
76   tree_pop_back (root, t);
77   assert (t == leaf3);
78   tree_pop_front (root, t);
79   assert (t == leaf2);
80
81   assert (tree_size (root) == 0);
82
83   tree_insert (root, 0, leaf1);
84   tree_insert (root, 1, leaf2);
85   tree_insert (root, 2, leaf3);
86
87   tree_get (root, 0, t);
88   assert (t == leaf1);
89   tree_get (root, 1, t);
90   assert (t == leaf2);
91   tree_get (root, 2, t);
92   assert (t == leaf3);
93
94   tree_replace (root, 0, leaf3);
95   tree_replace (root, 1, leaf1);
96   tree_replace (root, 2, leaf2);
97
98   tree_get (root, 0, t);
99   assert (t == leaf3);
100   tree_get (root, 1, t);
101   assert (t == leaf1);
102   tree_get (root, 2, t);
103   assert (t == leaf2);
104
105   tree_erase (root, 0);
106   assert (tree_size (root) == 2);
107
108   tree_erase_range (root, 0, 2);
109   assert (tree_size (root) == 0);
110
111   tree_insert (root, 0, leaf1);
112   tree_insert (root, 1, leaf2);
113   tree_insert (root, 2, leaf3);
114
115   tree_clear (root);
116   assert (tree_size (root) == 0);
117
118   tree_get_data (root, str);
119   assert (strcmp (str, root_node) == 0);
120
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);
125
126   pool2 = new_subpool (global_pool);
127   root = copy_tree (pool2, root);
128   delete_pool (pool1);
129
130   assert (tree_size (root) == 3);
131
132   tree_get_data (root, str);
133   assert (strcmp (str, root_node) == 0);
134
135   tree_get (root, 0, t);
136   tree_get_data (t, str);
137   assert (strcmp (str, leaf1_node) == 0);
138
139   tree_get (root, 1, t);
140   tree_get_data (t, str);
141   assert (strcmp (str, leaf2_node) == 0);
142
143   tree_get (root, 2, t);
144   tree_get_data (t, str);
145   assert (strcmp (str, leaf3_node) == 0);
146
147   exit (0);
148 }