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);
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;