}
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; }
-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
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 \
--- /dev/null
+#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);
+}