...
authoralex <[email protected]>
Mon, 18 Oct 2021 17:37:23 +0000 (10:37 -0700)
committeralex <[email protected]>
Mon, 18 Oct 2021 17:37:23 +0000 (10:37 -0700)
src/file.c
src/tree.c
test/unit/Makefile.am
test/unit/file.tests.c [new file with mode: 0644]
test/unit/tree.tests.c

index 46eee42b0a5c8140e78825c8cbd2cf45b22fb245..45b6dd8623c708929f43b9b6b7c7dcd18a969da6 100644 (file)
@@ -2,13 +2,15 @@
 
 void file_free(struct file *p) {
        free(p->name);
+       free(p->path);
+       free(p);
 }
 
 int file_init(struct file **p, const char *path) {
        char *b;
 
        if(NULL==p) { return -1; }
-       if(0==strlen(path)) { return -1; }
+       if((NULL==path)||(0==strlen(path))) { return -1; }
 
        *p = malloc(sizeof(struct file));
        if(NULL==(*p)) {
index e971c66073dcf18a9d40f1e309978d4681aa8dd5..bef2cc22efd6cf4a6ab324762de3f32e38af0659 100644 (file)
@@ -1,10 +1,10 @@
 #include<tree.h>
 
 static struct tree* tree_add_directory(struct tree*,const char*);
-static int tree_add_file(struct tree_entry*,struct file*);
+static int tree_add_file(struct tree*,struct file*);
 
 int tree_add(struct tree *root, struct file *to_add) {
-       char *str, *p;
+       char *str, *p, *prev;
        struct tree *tree;
 
        if(NULL==root) { return -1; }
@@ -15,15 +15,19 @@ int tree_add(struct tree *root, struct file *to_add) {
        str = strdup(to_add->path);
        if(NULL==str) { return -1; }
 
+       prev = NULL;
        p = strtok(str,"/");
        while(p) {
-               tree = tree_add_directory(tree->directories,p);
-               if(NULL==tree) { goto clean; }
+               if(prev!=NULL) {
+                       tree = tree_add_directory(tree,prev);
+                       if(NULL==tree) { goto clean; }
+               }
 
+               prev = p;
                p = strtok(NULL,"/");
        }
 
-       if(tree_add_file(tree->files,to_add)<0) { goto clean; }
+       if(tree_add_file(tree,to_add)<0) { goto clean; }
 
        free(str);
        
@@ -33,10 +37,17 @@ int tree_add(struct tree *root, struct file *to_add) {
                return -1;
 }
 
-static struct tree* tree_add_directory(struct tree *p, const char *dirname) {
-       struct tree *prev, *next;
+static struct tree* tree_add_directory(struct tree *root, const char *dirname) {
+       struct tree *p, *prev, *next;
        int i;
 
+       if(NULL==root) { return NULL; }
+
+       if(NULL==dirname) { return NULL; }
+       if(0==strlen(dirname)) { return NULL; }
+
+       p = root->directories;
+
        prev = NULL;
        while(p!=NULL) {
                if((i = strcmp(p->name,dirname))>=0) {
@@ -52,6 +63,8 @@ static struct tree* tree_add_directory(struct tree *p, const char *dirname) {
 
        if(prev!=NULL) {
                prev->next = next;
+       } else {
+               root->directories = next;
        }
 
        next->next = p;
@@ -59,10 +72,15 @@ static struct tree* tree_add_directory(struct tree *p, const char *dirname) {
        return next;
 }
 
-static int tree_add_file(struct tree_entry *p, struct file *to_add) {
-       struct tree_entry *prev, *next;
+static int tree_add_file(struct tree *root, struct file *to_add) {
+       struct tree_entry *p, *prev, *next;
        int i;
 
+       if(NULL==root) { return -1; }
+       if(NULL==to_add) { return -1; }
+
+       p = root->files;
+
        prev = NULL;
        while(p!=NULL) {
                if((i = strcmp(p->file->name,to_add->name))>=0) {
@@ -82,6 +100,8 @@ static int tree_add_file(struct tree_entry *p, struct file *to_add) {
 
        if(prev!=NULL) {
                prev->next = next;
+       } else {
+               root->files = next;
        }
 
        next->next = p;
@@ -108,14 +128,12 @@ void tree_free(struct tree *p) {
                subdir = p->directories;
                p->directories = p->directories->next;
                tree_free(subdir);
-               free(subdir);
        }
        
        while(p->next!=NULL) {
                subdir = p->next;
                p->next = p->next->next;
                tree_free(subdir);
-               free(subdir);
        }
 
        free(p);
index 151d76277e61259c191c98aa42093ed4876ac436..4f7e3f40f866220175dd013ee36a38cb1396283e 100644 (file)
@@ -12,7 +12,7 @@ AM_CPPFLAGS += \
        -DNDEBUG
 endif
 
-check_PROGRAMS = hashmap.tests tree.tests util.filter.tests 
+check_PROGRAMS = file.tests hashmap.tests tree.tests util.filter.tests 
 TESTS = $(check_PROGRAMS)
 
 if ENABLE_MEMCHECK
@@ -22,6 +22,11 @@ endif
 
 common_SOURCES = test_utils.c
 
+file_tests_SOURCES = \
+       $(common_SOURCES) \
+       file.tests.c \
+       $(top_srcdir)/src/file.c
+
 hashmap_tests_SOURCES = \
        $(common_SOURCES) \
        hashmap.tests.c \
diff --git a/test/unit/file.tests.c b/test/unit/file.tests.c
new file mode 100644 (file)
index 0000000..0e0a31b
--- /dev/null
@@ -0,0 +1,31 @@
+#include<test_utils.h>
+
+#include<file.h>
+
+int main();
+static void file_init_basic_test();
+
+int main() {
+       setup_env();
+
+       file_init_basic_test();
+
+       clean_env();
+
+       return EXIT_SUCCESS;
+}
+
+static void file_init_basic_test() {
+       struct file *p;
+
+       assert(-1==file_init(NULL,NULL));
+       assert(-1==file_init(&p,NULL));
+       assert(-1==file_init(&p,""));
+
+       assert(1==file_init(&p,"testdir/test"));
+
+       assert(strcmp(p->name,"test")==0);
+       assert(strcmp(p->path,"testdir/test")==0);
+
+       file_free(p);
+}
index f0992b8f96b5a6b46b704e5a036649836e49e68f..cf530582cc4a775ee7323b7f9f667a4d78a7fb10 100644 (file)
@@ -4,12 +4,14 @@
 
 int main();
 static void tree_add_basic_test();
+static void tree_add_extended_test();
 static void tree_init_basic_test();
 
 int main() {
        setup_env();
 
        tree_add_basic_test();
+       tree_add_extended_test();
        tree_init_basic_test();
 
        clean_env();
@@ -50,9 +52,33 @@ static void tree_add_basic_test() {
        assert(tree->directories->directories->next==NULL);
 
        tree_free(tree);
+}
+
+static void tree_add_extended_test() {
+       struct tree *tree;
+       struct file *file;
+
+       char valid[] = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
+
 
-       file_free(file1);
-       file_free(file2);
+       char files[7][100] = {
+               "test/test1",
+               "hello/test2",
+               "/notalsdkfj asdfkkl /asdlfkj /asdflkjasdf/klaasdlfjasd",
+               "src/asldfkak/a/aslkdfj//a/lsd/as/alskdf/lasdfjasdfasdlfkajsdfa/sdf",
+               "//./asd//sd/f/sadfasldklfasld///s/d/flskdfkl/asdk",
+               "..",
+               "."
+       };
+
+       assert(1==tree_init(&tree,NULL));
+       
+       for(int i = 0;i<3;i++) {
+               assert(1==file_init(&file,files[i]));
+               assert(1==tree_add(tree,file));
+       }
+
+       tree_free(tree);
 }
 
 static void tree_init_basic_test() {