10000 Turn on warnings; remove unneeded variables. Add more test cases. · fragglet/c-algorithms@cf167e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit cf167e5

Browse files
committed
Turn on warnings; remove unneeded variables. Add more test cases.
1 parent 06f9f75 commit cf167e5

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

test/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ TESTS = \
1313

1414
noinst_PROGRAMS = $(TESTS)
1515

16-
AM_CFLAGS = -I../src
16+
AM_CFLAGS = -I../src -Wall
1717
LDADD = $(top_builddir)/src/libcalg.la
1818

1919
test_arraylist_SOURCES = test-arraylist.c

test/test-avltree.c

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void test_avltree_new(void)
131131
{
132132
AVLTree *tree;
133133

134-
tree = avltree_new(int_compare);
134+
tree = avltree_new((AVLTreeCompareFunc) int_compare);
135135

136136
assert(tree != NULL);
137137
assert(avltree_root_node(tree) == NULL);
@@ -147,7 +147,7 @@ void test_avltree_insert_lookup(void)
147147
/* Create a tree containing some values. Validate the
148148
* tree is consistent at all stages. */
149149

150-
tree = avltree_new(int_compare);
150+
tree = avltree_new((AVLTreeCompareFunc) int_compare);
151151

152152
for (i=0; i<1000; ++i) {
153153
key = (int *) malloc(sizeof(int));
@@ -166,45 +166,84 @@ void test_avltree_insert_lookup(void)
166166
node = avltree_lookup_node(tree, &i);
167167
assert(node != NULL);
168168
}
169+
170+
/* Check that invalid nodes are not found */
171+
172+
i = -1;
173+
assert(avltree_lookup_node(tree, &i) == NULL);
174+
i = 100000;
175+
assert(avltree_lookup_node(tree, &i) == NULL);
169176
}
170177

171-
void test_avltree_remove(void)
178+
AVLTree *create_tree(void)
172179
{
173180
AVLTree *tree;
174-
AVLTreeNode *node;
175-
int i;
176181
int *key;
182+
int i;
177183

178184
/* Create a tree and fill with nodes */
179185

180-
tree = avltree_new(int_compare);
186+
tree = avltree_new((AVLTreeCompareFunc) int_compare);
181187

182188
for (i=0; i<1000; ++i) {
183189
key = (int *) malloc(sizeof(int));
184190
*key = i;
185191

186192
avltree_insert(tree, key, NULL);
187-
188-
validate_tree(tree);
189193
}
194+
195+
return tree;
196+
}
197+
198+
void test_avltree_free(void)
199+
{
200+
AVLTree *tree;
201+
202+
/* Try freeing an empty tree */
203+
204+
tree = avltree_new((AVLTreeCompareFunc) int_compare);
205+
avltree_free(tree);
206+
207+
/* Create a big tree and free it */
190208

209+
tree = create_tree();
210+
avltree_free(tree);
211+
}
212+
213+
void test_avltree_remove(void)
214+
{
215+
AVLTree *tree;
216+
int i;
217+
218+
tree = create_tree();
219+
220+
/* Try removing invalid entries */
221+
222+
i = 100000;
223+
assert(avltree_remove(tree, &i) == 0);
224+
i = -1;
225+
assert(avltree_remove(tree, &i) == 0);
191226

192227
/* Delete the nodes from the tree */
193228

194229
for (i=0; i<1000; ++i) {
195-
node = avltree_lookup_node(tree, &i);
196-
197-
assert(node != NULL);
230+
/* Remove this entry */
198231

199-
avltree_remove_node(tree, node);
232+
assert(avltree_remove(tree, &i) != 0);
200233

201234
validate_tree(tree);
202235
}
236+
237+
/* All entries removed, should be empty now */
238+
239+
assert(avltree_root_node(tree) == NULL);
240+
203241
}
204242

205243
int main(int argc, char *argv[])
206244
{
207245
test_avltree_new();
246+
test_avltree_free();
208247
test_avltree_insert_lookup();
209248
test_avltree_remove();
210249

0 commit comments

Comments
 (0)
0