...
authoralex <[email protected]>
Mon, 18 Oct 2021 05:28:42 +0000 (22:28 -0700)
committeralex <[email protected]>
Mon, 18 Oct 2021 05:28:42 +0000 (22:28 -0700)
inc/tree.h
src/torrent.c
src/tree.c
test/unit/Makefile.am
test/unit/tree.tests.c [new file with mode: 0644]

index ca7a4153c315a58245e01207e731352acb609cf8..f007f6cf39e1a3e7509fb2b11eb76a0b0b036e9f 100644 (file)
@@ -11,7 +11,7 @@ struct tree_entry {
 };
 
 struct tree {
-       const char *name;
+       char *name;
        struct tree_entry *files;
        struct tree *directories;
        struct tree *next;
@@ -19,6 +19,6 @@ struct tree {
 
 int tree_add(struct tree*,struct file*);
 void tree_free(struct tree*);
-int tree_init(struct tree**);
+int tree_init(struct tree**, const char*);
 
 #endif
index 89ceb2bd6d91ba450e4c15afd06cd7efaae48d0e..1b479b6ee759ecfe828bb74414a909bfc307ab52 100644 (file)
@@ -48,7 +48,7 @@ int torrent_init(struct torrent **torrent_p, const char *root, const char *name)
        (*torrent_p)->name = strdup(name);
        if(NULL==(*torrent_p)->name) { goto clean; }
 
-       if(tree_init(&((*torrent_p)->file_tree))<0) { goto clean; }
+       if(tree_init(&((*torrent_p)->file_tree),NULL)<0) { goto clean; }
 
        if(hashmap_init(&((*torrent_p)->files),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; }
 
index aaaf7aeb4fd06ff45e4ebb4be6d5e386c0a3d3cf..e971c66073dcf18a9d40f1e309978d4681aa8dd5 100644 (file)
@@ -8,6 +8,8 @@ int tree_add(struct tree *root, struct file *to_add) {
        struct tree *tree;
 
        if(NULL==root) { return -1; }
+       if(NULL==to_add) { return -1; }
+
        tree = root;
 
        str = strdup(to_add->path);
@@ -15,7 +17,7 @@ int tree_add(struct tree *root, struct file *to_add) {
 
        p = strtok(str,"/");
        while(p) {
-               tree = tree_add_directory(tree,p);
+               tree = tree_add_directory(tree->directories,p);
                if(NULL==tree) { goto clean; }
 
                p = strtok(NULL,"/");
@@ -46,7 +48,7 @@ static struct tree* tree_add_directory(struct tree *p, const char *dirname) {
                p = p->next;
        }
 
-       if(tree_init(&next)<0) { return NULL; }
+       if(tree_init(&next,dirname)<0) { return NULL; }
 
        if(prev!=NULL) {
                prev->next = next;
@@ -91,6 +93,10 @@ void tree_free(struct tree *p) {
        struct tree_entry *entry;
        struct tree *subdir;
 
+       if(p->name!=NULL) {
+               free(p->name);
+       }
+
        while(p->files!=NULL) {
                entry = p->files;
                p->files = p->files->next;
@@ -115,13 +121,21 @@ void tree_free(struct tree *p) {
        free(p);
 }
 
-int tree_init(struct tree **p) {
+int tree_init(struct tree **p, const char *name) {
+       if(NULL==p) { return -1; }
+
        (*p) = malloc(sizeof(struct tree));
        if(NULL==(*p)) {
                perror("malloc");
                return -1;
        }
 
+       if(name!=NULL) {
+               (*p)->name = strdup(name);
+       } else {
+               (*p)->name = NULL;
+       }
+
        (*p)->files = NULL;
        (*p)->directories = NULL;
        (*p)->next = NULL;
index a362d57e23ab20651af146d71d75ef6c79ad7994..151d76277e61259c191c98aa42093ed4876ac436 100644 (file)
@@ -12,7 +12,7 @@ AM_CPPFLAGS += \
        -DNDEBUG
 endif
 
-check_PROGRAMS = hashmap.tests util.filter.tests 
+check_PROGRAMS = hashmap.tests tree.tests util.filter.tests 
 TESTS = $(check_PROGRAMS)
 
 if ENABLE_MEMCHECK
@@ -27,6 +27,12 @@ hashmap_tests_SOURCES = \
        hashmap.tests.c \
        $(top_srcdir)/src/hashmap.c
 
+tree_tests_SOURCES = \
+       $(common_SOURCES) \
+       tree.tests.c \
+       $(top_srcdir)/src/file.c \
+       $(top_srcdir)/src/tree.c
+
 util_filter_tests_SOURCES = \
        $(common_SOURCES) \
        util.filter.tests.c \
diff --git a/test/unit/tree.tests.c b/test/unit/tree.tests.c
new file mode 100644 (file)
index 0000000..f0992b8
--- /dev/null
@@ -0,0 +1,81 @@
+#include<test_utils.h>
+
+#include<tree.h>
+
+int main();
+static void tree_add_basic_test();
+static void tree_init_basic_test();
+
+int main() {
+       setup_env();
+
+       tree_add_basic_test();
+       tree_init_basic_test();
+
+       clean_env();
+
+       return EXIT_SUCCESS;
+}
+
+static void tree_add_basic_test() {
+       struct tree *tree;
+       struct file *file1, *file2;
+
+       assert(1==tree_init(&tree,NULL));
+
+       assert(1==file_init(&file1,"test/file1"));
+       assert(1==file_init(&file2,"test/hello/file2"));
+
+       assert(-1==tree_add(NULL,NULL));
+       assert(-1==tree_add(tree,NULL));
+
+       assert(1==tree_add(tree,file1));
+       assert(1==tree_add(tree,file1));
+       assert(1==tree_add(tree,file2));
+
+       assert(NULL==tree->name);
+       assert(NULL==tree->files);
+       
+       assert(strcmp(tree->directories->name,"test")==0);
+       assert(tree->directories->files!=NULL);
+       assert(tree->directories->files->file==file1);
+       assert(tree->directories->files->next==NULL);
+       assert(tree->directories->next==NULL);
+
+       assert(strcmp(tree->directories->directories->name,"hello")==0);
+       assert(tree->directories->directories->files!=NULL);
+       assert(tree->directories->directories->files->file==file2);
+       assert(tree->directories->directories->files->next==NULL);
+       assert(tree->directories->directories->directories==NULL);
+       assert(tree->directories->directories->next==NULL);
+
+       tree_free(tree);
+
+       file_free(file1);
+       file_free(file2);
+}
+
+static void tree_init_basic_test() {
+       struct tree *p;
+
+       char str[] = "this is a test string";
+
+       assert(-1==tree_init(NULL,NULL));
+       assert(1==tree_init(&p,NULL));
+
+       assert(NULL==p->name);
+       assert(NULL==p->files);
+       assert(NULL==p->directories);
+       assert(NULL==p->next);
+
+       tree_free(p);
+
+       assert(1==tree_init(&p,str));
+
+       assert(strcmp(p->name,str)==0);
+       assert(NULL==p->files);
+       assert(NULL==p->directories);
+       assert(NULL==p->next);
+       
+       tree_free(p);
+}