...
authoralex <[email protected]>
Wed, 20 Oct 2021 22:29:25 +0000 (15:29 -0700)
committeralex <[email protected]>
Wed, 20 Oct 2021 22:29:25 +0000 (15:29 -0700)
src/torrent.c
test/unit/Makefile.am
test/unit/torrent.tests.c [new file with mode: 0644]

index 1b479b6ee759ecfe828bb74414a909bfc307ab52..d50f864b2debd66057c9f9b011e54b4d332bbde7 100644 (file)
@@ -21,27 +21,29 @@ static int torrent_add_file(struct torrent *p, struct file *f) {
 }
 
 void torrent_free(struct torrent *p) {
+       if(NULL==p) { return; }
+
        if(p->root!=NULL) { free(p->root); }
        if(p->name!=NULL) { free(p->name); }
        if(p->files!=NULL) { hashmap_free(p->files); }
        if(p->file_tree!=NULL) { tree_free(p->file_tree); }
+       
        free(p);
 }
 
 #define TORRENT_FILES_HASHMAP_INITIAL_SIZE 8
 
 int torrent_init(struct torrent **torrent_p, const char *root, const char *name) {
+       if(NULL==torrent_p) { return -1; }
+       if(NULL==root) { return -1; }
+       if(NULL==name) { return -1; }
+
        *(torrent_p) = malloc(sizeof(struct torrent));
        if(NULL==(*torrent_p)) {
                perror("malloc");
                return -1;
        }
 
-       (*torrent_p)->root = NULL;
-       (*torrent_p)->name = NULL;
-       (*torrent_p)->file_tree = NULL;
-       (*torrent_p)->files = NULL;
-
        (*torrent_p)->root = strdup(root);
        if(NULL==(*torrent_p)->root) { goto clean; }
 
index 4f7e3f40f866220175dd013ee36a38cb1396283e..fd1acd0a8258570388acc228f78acb34958d25dd 100644 (file)
@@ -12,7 +12,7 @@ AM_CPPFLAGS += \
        -DNDEBUG
 endif
 
-check_PROGRAMS = file.tests hashmap.tests tree.tests util.filter.tests 
+check_PROGRAMS = file.tests hashmap.tests torrent.tests tree.tests util.filter.tests 
 TESTS = $(check_PROGRAMS)
 
 if ENABLE_MEMCHECK
@@ -32,6 +32,14 @@ hashmap_tests_SOURCES = \
        hashmap.tests.c \
        $(top_srcdir)/src/hashmap.c
 
+torrent_tests_SOURCES = \
+       $(common_SOURCES) \
+       torrent.tests.c \
+       $(top_srcdir)/src/file.c \
+       $(top_srcdir)/src/hashmap.c \
+       $(top_srcdir)/src/torrent.c \
+       $(top_srcdir)/src/tree.c
+
 tree_tests_SOURCES = \
        $(common_SOURCES) \
        tree.tests.c \
diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c
new file mode 100644 (file)
index 0000000..f88a1a7
--- /dev/null
@@ -0,0 +1,26 @@
+#include<test_utils.h>
+
+#include<torrent.h>
+
+int main();
+static void torrent_init_basic_test();
+
+int main() {
+       setup_env();
+
+       torrent_init_basic_test();
+
+       clean_env();
+
+       return EXIT_SUCCESS;
+}
+
+static void torrent_init_basic_test() {
+       struct torrent *torrent;
+
+       assert(torrent_init(&torrent,NULL,NULL)<0);
+       assert(torrent_init(&torrent,"src",NULL)<0);
+
+       assert(torrent_init(&torrent,"src","src")==1);
+       torrent_free(torrent);
+}