}
void tree_entry_free(struct tree_entry *p) {
+ if(NULL==p) { return; }
+
file_free(p->file);
+ tree_entry_free(p->next);
+
free(p);
}
}
void tree_free(struct tree *p) {
- struct tree_entry *entry;
- struct tree *subdir;
+ if(NULL==p) { return; }
if(p->name!=NULL) {
free(p->name);
}
- while(p->files!=NULL) {
- entry = p->files;
- p->files = p->files->next;
- tree_entry_free(entry);
- }
-
- while(p->directories!=NULL) {
- subdir = p->directories;
- p->directories = p->directories->next;
- tree_free(subdir);
- }
-
- while(p->next!=NULL) {
- subdir = p->next;
- p->next = p->next->next;
- tree_free(subdir);
- }
+ tree_entry_free(p->files);
+ tree_free(p->directories);
+ tree_free(p->next);
free(p);
}
assert(1==tree_init(&tree,NULL));
assert(1==tree_init(&root,NULL));
- assert(1==tree_init(&test,NULL));
- assert(1==tree_init(&test2,NULL));
- assert(1==tree_init(&hello,NULL));
- assert(1==tree_init(&nope,NULL));
- assert(1==tree_init(&helloo,NULL));
+ assert(1==tree_init(&test,"test"));
+ assert(1==tree_init(&test2,"test2"));
+ assert(1==tree_init(&hello,"hello"));
+ assert(1==tree_init(&nope,"nope"));
+ assert(1==tree_init(&helloo,"helloo"));
assert(1==file_init(&file1,"test/file1"));
assert(1==file_init(&file2,"test/hello/file2"));
hello->files = entry2;
test2->directories = nope;
nope->directories = helloo;
- hello->files = entry3;
+ helloo->files = entry3;
assert(-1==tree_add(NULL,NULL));
assert(-1==tree_add(tree,NULL));
assert(1==tree_add(tree,file1));
assert(1==tree_add(tree,file2));
assert(1==tree_add(tree,file3));
+ assert(1==tree_add(tree,file4));
assert(NULL==tree->name);
assert(NULL==tree->files);
assert(tree_deep_equals(tree->directories->next->directories->directories,helloo)==1);
tree_free(tree);
+
+ // remove pointers to already free'd entries
+ entry1->file = NULL;
+ entry2->file = NULL;
+ entry3->file = NULL;
+ entry4->file = NULL;
+
+ tree_free(root);
}
static int tree_deep_equals(struct tree *a, struct tree *b) {
+ if(a==NULL&&b==NULL) { return 1; }
if((a!=NULL&&b==NULL)||(a==NULL&&b!=NULL)) { return -1; }
if((a->name!=NULL&&b->name==NULL)||(a->name==NULL&&b->name!=NULL)) { return -1; }
if(a->name!=NULL) {
index = 0;
while(len>1) {
- for(int i=0;i<rand()%(len-1);i++) {
- str[index] = valid[rand()%(sizeof(valid)-1)];
- index++;
- len--;
- }
-
- str[index]='/';
+ str[index] = valid[rand()%(sizeof(valid)-1)];
index++;
len--;
}