]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Thu, 21 Oct 2021 17:33:40 +0000 (10:33 -0700)
committeralex <[email protected]>
Thu, 21 Oct 2021 17:33:40 +0000 (10:33 -0700)
src/torrent.c
test/unit/torrent.tests.c

index d50f864b2debd66057c9f9b011e54b4d332bbde7..e1ff26a803bf09126fe9d2131c7fc27f995e50fe 100644 (file)
@@ -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);
index f88a1a74b234da835dc7e2278d20bedab6bf15e7..da41854c789c845fdc1cd4d496e4ba98d1a00cf4 100644 (file)
@@ -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;