From: alex Date: Mon, 18 Oct 2021 05:28:42 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=b08f9f8fb1b47f880f43ed1abcf45be59b6570eb;p=seeder ... --- diff --git a/inc/tree.h b/inc/tree.h index ca7a415..f007f6c 100644 --- a/inc/tree.h +++ b/inc/tree.h @@ -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 diff --git a/src/torrent.c b/src/torrent.c index 89ceb2b..1b479b6 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -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; } diff --git a/src/tree.c b/src/tree.c index aaaf7ae..e971c66 100644 --- a/src/tree.c +++ b/src/tree.c @@ -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; diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index a362d57..151d762 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -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 index 0000000..f0992b8 --- /dev/null +++ b/test/unit/tree.tests.c @@ -0,0 +1,81 @@ +#include + +#include + +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); +}