struct tree *tree;
if(NULL==root) { return -1; }
+ if(NULL==to_add) { return -1; }
+
tree = root;
str = strdup(to_add->path);
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,"/");
p = p->next;
}
- if(tree_init(&next)<0) { return NULL; }
+ if(tree_init(&next,dirname)<0) { return NULL; }
if(prev!=NULL) {
prev->next = next;
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;
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;
--- /dev/null
+#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);
+}