From 92a20144a836ac84c68d24064e66f63a456ff47e Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 20 Oct 2021 14:08:48 -0700 Subject: [PATCH] ... --- src/file.c | 12 ++++++++++-- src/tree.c | 27 ++++++++------------------- test/unit/test_utils.c | 2 +- test/unit/tree.tests.c | 30 +++++++++++++++++------------- 4 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/file.c b/src/file.c index 45b6dd8..c34a9dc 100644 --- a/src/file.c +++ b/src/file.c @@ -1,8 +1,16 @@ #include void file_free(struct file *p) { - free(p->name); - free(p->path); + if(NULL==p) { return; } + + if(p->name!=NULL) { + free(p->name); + } + + if(p->path!=NULL) { + free(p->path); + } + free(p); } diff --git a/src/tree.c b/src/tree.c index 30d8bd5..6aacd62 100644 --- a/src/tree.c +++ b/src/tree.c @@ -73,7 +73,11 @@ static struct tree* tree_add_directory(struct tree *root, const char *dirname) { } void tree_entry_free(struct tree_entry *p) { + if(NULL==p) { return; } + file_free(p->file); + tree_entry_free(p->next); + free(p); } @@ -124,30 +128,15 @@ static int tree_add_file(struct tree *root, struct file *to_add) { } 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); } diff --git a/test/unit/test_utils.c b/test/unit/test_utils.c index 6320b86..fe98e51 100644 --- a/test/unit/test_utils.c +++ b/test/unit/test_utils.c @@ -15,7 +15,7 @@ void reset_env() { void setup_env() { clean_env(); - //srand(time(NULL)); + srand(time(NULL)); create_test_directory(TEST_DIRECTORY); create_test_file(TEST_FILE_1,TEST_FILE_1_CONTENTS); diff --git a/test/unit/tree.tests.c b/test/unit/tree.tests.c index 2ce9a56..c644c40 100644 --- a/test/unit/tree.tests.c +++ b/test/unit/tree.tests.c @@ -29,11 +29,11 @@ static void tree_add_basic_test() { 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")); @@ -66,7 +66,7 @@ static void tree_add_basic_test() { 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)); @@ -75,6 +75,7 @@ static void tree_add_basic_test() { 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); @@ -87,9 +88,18 @@ static void tree_add_basic_test() { 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) { @@ -131,13 +141,7 @@ static char *create_random_path() { index = 0; while(len>1) { - for(int i=0;i