From 6915aa9a8c3e97def7a66b834a35dbde3cc3c7e7 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 21 Oct 2021 10:33:40 -0700 Subject: [PATCH] ... --- src/torrent.c | 8 +++++++- test/unit/torrent.tests.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/torrent.c b/src/torrent.c index d50f864..e1ff26a 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -25,7 +25,13 @@ void torrent_free(struct torrent *p) { if(p->root!=NULL) { free(p->root); } if(p->name!=NULL) { free(p->name); } - if(p->files!=NULL) { hashmap_free(p->files); } + + if(p->files!=NULL) { + // clear first, tree_free will also free files + hashmap_clear(p->files); + hashmap_free(p->files); + } + if(p->file_tree!=NULL) { tree_free(p->file_tree); } free(p); diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c index f88a1a7..da41854 100644 --- a/test/unit/torrent.tests.c +++ b/test/unit/torrent.tests.c @@ -4,17 +4,46 @@ int main(); static void torrent_init_basic_test(); +static void torrent_add_basic_test(); int main() { setup_env(); torrent_init_basic_test(); + torrent_add_basic_test(); clean_env(); return EXIT_SUCCESS; } +static void torrent_add_basic_test() { + struct torrent *torrent; + struct file *file1, *file2, *file3, *file4; + + assert(file_init(&file1,TEST_FILE_1)==1); + assert(file_init(&file2,TEST_FILE_2)==1); + assert(file_init(&file3,TEST_FILE_3)==1); + assert(file_init(&file4,TEST_FILE_4)==1); + + assert(torrent_init(&torrent,TEST_DIRECTORY,TEST_DIRECTORY)==1); + + assert(torrent_add(torrent,file1)==1); + assert(torrent_add(torrent,file2)==1); + assert(torrent_add(torrent,file3)==1); + assert(torrent_add(torrent,file4)==1); + + assert(strcmp(torrent->file_tree->directories->name,TEST_DIRECTORY)==0); + assert(torrent->file_tree->directories->files->file==file3); + assert(torrent->file_tree->directories->files->next->file==file4); + assert(torrent->file_tree->directories->files->next->next->file==file1); + assert(torrent->file_tree->directories->files->next->next->next->file==file2); + + torrent_free(torrent); + + reset_env(); +} + static void torrent_init_basic_test() { struct torrent *torrent; -- 2.39.5