]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Wed, 20 Oct 2021 21:08:48 +0000 (14:08 -0700)
committeralex <[email protected]>
Wed, 20 Oct 2021 21:08:48 +0000 (14:08 -0700)
src/file.c
src/tree.c
test/unit/test_utils.c
test/unit/tree.tests.c

index 45b6dd8623c708929f43b9b6b7c7dcd18a969da6..c34a9dc628ba7fd62dd52252aa05ab592c5e4746 100644 (file)
@@ -1,8 +1,16 @@
 #include<file.h>
 
 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);
 }
 
index 30d8bd54ceb141eecc31ab7fc6335f88cf8a31fc..6aacd624698f4b2ce7346158d97b75d071e2d2e4 100644 (file)
@@ -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);
 }
index 6320b8677e23be5f09a226bb2cbfb6d4926fac45..fe98e51102e85a88f91d1c2e85954fc61a469638 100644 (file)
@@ -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);
index 2ce9a5697f1b33e83da3213b1ff6eae5dffc22b9..c644c402dd17e44da664c2a336150d528687f4f1 100644 (file)
@@ -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<rand()%(len-1);i++) {
-                       str[index] = valid[rand()%(sizeof(valid)-1)];
-                       index++;
-                       len--;
-               }
-
-               str[index]='/';
+               str[index] = valid[rand()%(sizeof(valid)-1)];
                index++;
                len--;
        }