]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Tue, 19 Apr 2022 02:24:04 +0000 (19:24 -0700)
committeralex <[email protected]>
Tue, 19 Apr 2022 02:24:04 +0000 (19:24 -0700)
src/add.c
test/unit/add.tests.c

index b354724ae925b0e1f337c065d0c64c7b546f22cc..a1f2649bf0f8bbbc0fee339cf824eedbc02a5999 100644 (file)
--- a/src/add.c
+++ b/src/add.c
@@ -94,35 +94,39 @@ static void *add_hash(void *unused) {
 }
 
 static int add_queue_entry_add_torrent(struct add_queue_entry *entry,struct torrent *torrent) {
-       struct add_queue_torrent *prev, *p;
+       struct add_queue_torrent *prev, *p, *next;
 
-       p = malloc(sizeof(struct add_queue_torrent));
-       if(NULL==p) { return -1; }
+       if(NULL==entry) { return -1; }
+       if(NULL==torrent) { return -1; }
+
+       prev = NULL;
+       p = entry->torrents;
+       while(p!=NULL) {
+               if(p->torrent==torrent) { return 0; }
+               prev = p;
+               p = p->next;
+       }
 
-       p->torrent = torrent;
-       p->next = NULL;
+       next = malloc(sizeof(struct add_queue_torrent));
+       if(NULL==next) { return -1; }
 
-       if(entry->torrents==NULL) { goto first; }
+       next->torrent = torrent;
+       next->next = NULL;
 
-       prev = entry->torrents;
-       while(prev->next!=NULL) {
-               if(prev->torrent==torrent) { goto found; }
-               prev = prev->next;
+       if(NULL==prev) {
+               entry->torrents = next;
+       } else {
+               prev->next = next;
        }
 
-       prev->next = p;
        return 1;
-first:
-       entry->torrents = p;
-       return 1;
-found:
-       free(p);
-       return 0;
 }
 
 static void add_queue_entry_free(struct add_queue_entry *p) {
        struct add_queue_torrent *to_free;
 
+       file_free(p->file);
+
        while(p->torrents!=NULL) {
                to_free = p->torrents;
                p->torrents = p->torrents->next;
@@ -176,6 +180,8 @@ static int add_to_queue(struct add_queue_entry *to_add) {
        int ret;
        size_t len;
 
+       if(NULL==to_add) { return -1; }
+
        len = strlen(to_add->file->path);
 
        while((ret = hashmap_insert(add_queue,to_add->file->path,len,to_add))<=0) {
@@ -183,7 +189,14 @@ static int add_to_queue(struct add_queue_entry *to_add) {
                
                p = hashmap_find(add_queue,to_add->file->path,len);
                if(p!=NULL) {
-                       if(strcmp(p->file->path,to_add->file->path)==0) { return 1; }
+                       if(strcmp(p->file->path,to_add->file->path)==0) {
+                               if(add_queue_entry_add_torrent(
+                                       p, /* struct add_queue_entry *entry */
+                                       current_torrent /* struct torrent *torrent */
+                               )<0) { return -1; }
+                               add_queue_entry_free(to_add);
+                               return 1;
+                       }
                }
 
                if(add_queue_resize(add_queue->size<<1)<0) { return -1; }
index 7e1f40f05a81b5e71ff002fe5fcab57ff99abc86..944f900ed5594c1102a9cd1c9aa82b03fe41ad96 100644 (file)
@@ -52,24 +52,159 @@ static void add_find_all_basic_test() {
        f = add_queue->map[2];
        assert(f!=NULL);
        assert(strcmp(f->file->path,TEST_FILE_2)==0);
+       assert(f->torrents->torrent==p);
+       assert(f->torrents->next==NULL);
        
        f = add_queue->map[3];
        assert(f!=NULL);
        assert(strcmp(f->file->path,TEST_FILE_1)==0);
+       assert(f->torrents->torrent==p);
+       assert(f->torrents->next==NULL);
        
        f = add_queue->map[4];
        assert(f!=NULL);
        assert(strcmp(f->file->path,TEST_FILE_8)==0);
+       assert(f->torrents->torrent==p);
+       assert(f->torrents->next==NULL);
 
        current_torrent = p2;
        assert(1==add_find_all(p2));
 
-       assert(0);
+       add_queue_index = 0;
+
+       f = get_next();
+       assert(f!=NULL);
+       assert(strcmp(f->file->path,TEST_FILE_2)==0);
+       assert(f->torrents->torrent==p);
+       assert(f->torrents->next->torrent==p2);
+       add_queue_entry_free(f);
+       
+       f = get_next();
+       assert(f!=NULL);
+       assert(strcmp(f->file->path,TEST_FILE_1)==0);
+       assert(f->torrents->torrent==p);
+       assert(f->torrents->next->torrent==p2);
+       add_queue_entry_free(f);
+       
+       f = get_next();
+       assert(f!=NULL);
+       assert(strcmp(f->file->path,TEST_FILE_8)==0);
+       assert(f->torrents->torrent==p);
+       assert(f->torrents->next->torrent==p2);
+       add_queue_entry_free(f);
+
+       torrent_free(p2);
+       torrent_free(p);
+
+       hashmap_clear(add_queue);
+       hashmap_free(add_queue);
+
+       reset_env();
 }
 
 static void add_queue_resize_basic_test() {
-       assert(0);
+       struct torrent *p;
+       struct add_queue_entry *f;
+       int found_1, found_2, found_8;
+       
+       TORRENT_SETUP_EMPTY_EXAMPLE(&p);
+       
+       assert(hashmap_init(&add_queue,ADD_QUEUE_INITIAL_SIZE)==1);
+       memset(add_queue->key,0,crypto_shorthash_KEYBYTES);
+
+       current_torrent = p;
+       
+       assert(1==add_find_all(p));
+
+       assert(1==add_queue_resize(ADD_QUEUE_INITIAL_SIZE<<1));
+
+       add_queue_index = 0;
+       found_1 = 0; found_2 = 0; found_8 = 0;
+       while((f = get_next())!=NULL) {
+               if(strcmp(f->file->path,TEST_FILE_1)==0) { found_1++; }
+               if(strcmp(f->file->path,TEST_FILE_2)==0) { found_2++; }
+               if(strcmp(f->file->path,TEST_FILE_8)==0) { found_8++; }
+               add_queue_entry_free(f);
+       }
+
+       assert(found_1==1);
+       assert(found_2==1);
+       assert(found_8==1);
+
+       torrent_free(p);
+
+       hashmap_clear(add_queue);
+       hashmap_free(add_queue);
+
+       reset_env();
 }
 static void add_to_queue_basic_test() {
-       assert(0);
+       struct torrent *p;
+       struct file *file_p;
+       struct add_queue_entry *f;
+
+       TORRENT_SETUP_EMPTY_EXAMPLE(&p);
+       
+       assert(hashmap_init(&add_queue,ADD_QUEUE_INITIAL_SIZE)==1);
+       memset(add_queue->key,0,crypto_shorthash_KEYBYTES);
+
+       current_torrent = p;
+
+       /* add file 1 */
+       assert(file_init(&file_p,TEST_FILE_1)==1);
+       assert(add_queue_entry_init(&f)==1);
+
+       f->file = file_p;
+       assert(add_queue_entry_add_torrent(f,p));
+
+       assert(-1==add_to_queue(NULL));
+
+       assert(1==add_to_queue(f));
+       /* end file 1 */
+       
+       /* this is an intentional duplicate of the above */
+       assert(file_init(&file_p,TEST_FILE_1)==1);
+       assert(add_queue_entry_init(&f)==1);
+       
+       f->file = file_p;
+       assert(add_queue_entry_add_torrent(f,p));
+
+       assert(1==add_to_queue(f));
+       /* end duplicate */
+
+       /* add file 2 */        
+       assert(file_init(&file_p,TEST_FILE_2)==1);
+       assert(add_queue_entry_init(&f)==1);
+
+       f->file = file_p;
+       assert(add_queue_entry_add_torrent(f,p));
+       
+       assert(1==add_to_queue(f));
+       /* end file 2 */
+
+       add_queue_index = 0;
+
+       f = get_next();
+       assert(f!=NULL);
+       assert(strcmp(f->file->path,TEST_FILE_2)==0);
+       assert(f->torrents->torrent==p);
+       assert(f->torrents->next==NULL);
+       add_queue_entry_free(f);
+       
+       f = get_next();
+       assert(f!=NULL);
+       assert(strcmp(f->file->path,TEST_FILE_1)==0);
+       assert(f->torrents->torrent==p);
+       assert(f->torrents->next==NULL);
+       add_queue_entry_free(f);
+
+       f = get_next();
+       assert(NULL==f);
+
+       torrent_free(p);
+
+       hashmap_clear(add_queue);
+       hashmap_free(add_queue);
+
+       reset_env();
 }