}
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;
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) {
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; }
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();
}