Add unit test for dir iteration. Implementation of iso_dir_iter_free().

This commit is contained in:
Vreixo Formoso 2007-12-02 16:39:58 +01:00
parent 5ec93b50f4
commit 78fba4c14f
2 changed files with 124 additions and 0 deletions

View File

@ -256,6 +256,7 @@ int iso_dir_iter_next(IsoDirIter *iter, IsoNode **node)
} }
n = iter->pos; n = iter->pos;
if (n == NULL) { if (n == NULL) {
*node = NULL;
return 0; return 0;
} }
if (n->parent != iter->dir) { if (n->parent != iter->dir) {
@ -283,6 +284,11 @@ int iso_dir_iter_has_next(IsoDirIter *iter)
return iter->pos == NULL ? 0 : 1; return iter->pos == NULL ? 0 : 1;
} }
void iso_dir_iter_free(IsoDirIter *iter)
{
free(iter);
}
static IsoNode** static IsoNode**
iso_dir_find_node(IsoDir *dir, IsoNode *node) iso_dir_find_node(IsoDir *dir, IsoNode *node)
{ {

View File

@ -172,10 +172,128 @@ void test_iso_dir_get_node()
free(dir); free(dir);
} }
void test_iso_dir_get_children()
{
int result;
IsoDirIter *iter;
IsoDir *dir;
IsoNode *node, *node1, *node2, *node3;
/* init dir with default values, not all field need to be initialized */
dir = malloc(sizeof(IsoDir));
dir->children = NULL;
dir->nchildren = 0;
result = iso_dir_get_children(dir, &iter);
CU_ASSERT_EQUAL(result, 1);
/* item should have no items */
result = iso_dir_iter_has_next(iter);
CU_ASSERT_EQUAL(result, 0);
result = iso_dir_iter_next(iter, &node);
CU_ASSERT_EQUAL(result, 0);
CU_ASSERT_PTR_NULL(node);
iso_dir_iter_free(iter);
/* 1st node to be added */
node1 = calloc(1, sizeof(IsoNode));
node1->name = "Node1";
result = iso_dir_add_node(dir, node1);
CU_ASSERT_EQUAL(dir->nchildren, 1);
/* test iteration again */
result = iso_dir_get_children(dir, &iter);
CU_ASSERT_EQUAL(result, 1);
/* iter should have a single item... */
result = iso_dir_iter_has_next(iter);
CU_ASSERT_EQUAL(result, 1);
result = iso_dir_iter_next(iter, &node);
CU_ASSERT_EQUAL(result, 1);
CU_ASSERT_PTR_EQUAL(node, node1);
/* ...and no more */
result = iso_dir_iter_has_next(iter);
CU_ASSERT_EQUAL(result, 0);
result = iso_dir_iter_next(iter, &node);
CU_ASSERT_EQUAL(result, 0);
CU_ASSERT_PTR_NULL(node);
iso_dir_iter_free(iter);
/* add another node */
node2 = calloc(1, sizeof(IsoNode));
node2->name = "A node to be added first";
result = iso_dir_add_node(dir, node2);
CU_ASSERT_EQUAL(result, 2);
result = iso_dir_get_children(dir, &iter);
CU_ASSERT_EQUAL(result, 1);
/* iter should have two items... */
result = iso_dir_iter_has_next(iter);
CU_ASSERT_EQUAL(result, 1);
result = iso_dir_iter_next(iter, &node);
CU_ASSERT_EQUAL(result, 1);
CU_ASSERT_PTR_EQUAL(node, node2);
result = iso_dir_iter_has_next(iter);
CU_ASSERT_EQUAL(result, 1);
result = iso_dir_iter_next(iter, &node);
CU_ASSERT_EQUAL(result, 1);
CU_ASSERT_PTR_EQUAL(node, node1);
/* ...and no more */
result = iso_dir_iter_has_next(iter);
CU_ASSERT_EQUAL(result, 0);
result = iso_dir_iter_next(iter, &node);
CU_ASSERT_EQUAL(result, 0);
CU_ASSERT_PTR_NULL(node);
iso_dir_iter_free(iter);
/* addition of a 3rd node, to be inserted last */
node3 = calloc(1, sizeof(IsoNode));
node3->name = "This node will be inserted last";
result = iso_dir_add_node(dir, node3);
CU_ASSERT_EQUAL(result, 3);
result = iso_dir_get_children(dir, &iter);
CU_ASSERT_EQUAL(result, 1);
/* iter should have 3 items... */
result = iso_dir_iter_has_next(iter);
CU_ASSERT_EQUAL(result, 1);
result = iso_dir_iter_next(iter, &node);
CU_ASSERT_EQUAL(result, 1);
CU_ASSERT_PTR_EQUAL(node, node2);
result = iso_dir_iter_has_next(iter);
CU_ASSERT_EQUAL(result, 1);
result = iso_dir_iter_next(iter, &node);
CU_ASSERT_EQUAL(result, 1);
CU_ASSERT_PTR_EQUAL(node, node1);
result = iso_dir_iter_has_next(iter);
CU_ASSERT_EQUAL(result, 1);
result = iso_dir_iter_next(iter, &node);
CU_ASSERT_EQUAL(result, 1);
CU_ASSERT_PTR_EQUAL(node, node3);
/* ...and no more */
result = iso_dir_iter_has_next(iter);
CU_ASSERT_EQUAL(result, 0);
result = iso_dir_iter_next(iter, &node);
CU_ASSERT_EQUAL(result, 0);
CU_ASSERT_PTR_NULL(node);
iso_dir_iter_free(iter);
free(node1);
free(node2);
free(node3);
free(dir);
}
void add_node_suite() void add_node_suite()
{ {
CU_pSuite pSuite = CU_add_suite("Node Test Suite", NULL, NULL); CU_pSuite pSuite = CU_add_suite("Node Test Suite", NULL, NULL);
CU_add_test(pSuite, "iso_dir_add_node()", test_iso_dir_add_node); CU_add_test(pSuite, "iso_dir_add_node()", test_iso_dir_add_node);
CU_add_test(pSuite, "iso_dir_get_node()", test_iso_dir_get_node); CU_add_test(pSuite, "iso_dir_get_node()", test_iso_dir_get_node);
CU_add_test(pSuite, "iso_dir_get_children()", test_iso_dir_get_children);
} }