int testRemoveCabeca() { int result; struct list_t *list; struct entry_t *e1, *e2, *e3, *entry; struct data_t *data; printf("Módulo list -> teste remover cabeça:"); if ((list = list_create()) == NULL) error(1, errno, " O teste não pode prosseguir"); if ((data = data_create(5)) == NULL) error(1, errno, " O teste não pode prosseguir"); e1 = entry_create("abc", data); e2 = entry_create("def", data); e3 = entry_create("ghi", data); if (e1 == NULL || e2 == NULL || e3 == NULL) error(1, errno, " O teste não pode prosseguir"); memcpy(e1->value->data, "abc1", 5); memcpy(e2->value->data, "def1", 5); memcpy(e3->value->data, "ghi1", 5); list_add(list,e1); list_add(list,e2); list_add(list,e3); assert(list_remove(NULL, "abc") < 0); result = (list_remove(NULL, "abc") < 0); assert(list_remove(list, NULL) < 0); result = result && (list_remove(list, NULL) < 0); result = result && list_remove(list, "abc") == 0 && list_size(list) == 2; entry = list_get(list, "def"); result = result && entry != e2 && strcmp(entry->key, e2->key) == 0; entry = list_get(list, "ghi"); result = result && entry != e3 && strcmp(entry->key, e3->key) == 0; list_destroy(list); entry_destroy(e1); entry_destroy(e2); entry_destroy(e3); data_destroy(data); printf(" %s\n", result ? "passou" : "não passou"); return result; }
int pfs_channel_alloc( pfs_size_t length, pfs_size_t *start ) { struct entry *e = head; struct entry *tail; pfs_size_t newsize; length = round_up(length); do { if(!e->inuse) { if(e->length==length) { e->inuse = 1; *start = e->start; memset(channel_base+*start+length-page_size,0,page_size); return 1; } else if(e->length>length) { struct entry *f; f = entry_create(e->start+length,e->length-length,e,e->next); if(!f) return 0; e->length = length; e->inuse = 1; *start = e->start; memset(channel_base+*start+length-page_size,0,page_size); return 1; } else { /* not big enough */ } } tail = e; e = e->next; } while(e!=head); debug(D_CHANNEL,"channel is full, attempting to expand it..."); newsize = channel_size + length; if(ftruncate64(channel_fd,newsize)==0) { void *newbase; newbase = mremap(channel_base,channel_size,newsize,MREMAP_MAYMOVE); if(newbase!=MAP_FAILED) { e = entry_create(channel_size,newsize-channel_size,tail,head); channel_size = newsize; channel_base = newbase; debug(D_CHANNEL,"channel expanded to 0x%x bytes at base 0x%x",(PTRINT_T)newsize,newbase); return pfs_channel_alloc(length,start); } ftruncate64(channel_fd,channel_size); } debug(D_CHANNEL|D_NOTICE,"out of channel space: %s",strerror(errno)); return 0; }
int testGetKeys() { int result; struct list_t *list; struct entry_t *e1, *e2, *e3; struct data_t *data; char **keys; printf("Módulo list -> teste busca chaves:"); if ((list = list_create()) == NULL) error(1, errno, " O teste não pode prosseguir"); if ((data = data_create(5)) == NULL) error(1, errno, " O teste não pode prosseguir"); assert(list_get_keys(NULL) == NULL); result = (list_get_keys(NULL) == NULL); e1 = entry_create("abc", data); e2 = entry_create("def", data); e3 = entry_create("ghi", data); if (e1 == NULL || e2 == NULL || e3 == NULL) error(1, errno, " O teste não pode prosseguir"); memcpy(e1->value->data, "abc1", 5); memcpy(e2->value->data, "def1", 5); memcpy(e3->value->data, "ghi1", 5); list_add(list,e1); list_add(list,e2); list_add(list,e3); if ((keys = list_get_keys(list)) == NULL) error(1, errno, " O teste não pode prosseguir"); result = strcmp(keys[0], e1->key) == 0 && keys[0] != e1->key && strcmp(keys[1], e2->key) == 0 && keys[1] != e2->key && strcmp(keys[2], e3->key) == 0 && keys[2] != e3->key && keys[3] == NULL; list_free_keys(keys); list_destroy(list); entry_destroy(e1); entry_destroy(e2); entry_destroy(e3); data_destroy(data); printf(" %s\n", result ? "passou" : "não passou"); return result; }
int pfs_channel_init( pfs_size_t size ) { char path[PATH_MAX]; sprintf(path,"%s/pfs.tmp.XXXXXX",pfs_temp_dir); channel_fd = mkstemp(path); if(channel_fd<0) return 0; unlink(path); ftruncate(channel_fd,size); channel_size = size; channel_base = (char*) mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,channel_fd,0); if(channel_base==MAP_FAILED) { close(channel_fd); return 0; } head = entry_create(0,size,0,0); if(!head) { close(channel_fd); munmap(channel_base,size); return 0; } debug(D_CHANNEL,"fd is %d",channel_fd); return 1; }
bag_t *generate_index(FILE *input, int min_word_len) { bag_t *index = bag_create(entry_cmp); if (index) { char word[LINE_LENGTH] = ""; entry_t new_word, *existing_entry; bag_elem_t new_entry; unsigned page = 0; while (get_word(input, word, &page)) { new_word.entry_word = word; // check if the length of the word is long enough if(strlen(word) >= min_word_len) { existing_entry = bag_contains(index, &new_word); if(existing_entry != NULL) // if the word is already in index { entry_add(existing_entry, page); // add the location to the list of locations for that word } else // if the word isn't in the index { new_entry = entry_create(word, page); // create the entry bag_insert(index, new_entry); // add the location } } } } return index; }
int attr_add_active (attr_t *attrs, const char *name, int flags, attr_get_f get, attr_set_f set, void *arg) { struct entry *e; int rc = -1; if ((e = zhash_lookup (attrs->hash, name))) { if (!set) { errno = EEXIST; goto done; } if (set (name, e->val, arg) < 0) goto done; } e = entry_create (name, NULL, flags); e->set = set; e->get = get; e->arg = arg; e->flags |= FLUX_ATTRFLAG_ACTIVE; zhash_update (attrs->hash, name, e); zhash_freefn (attrs->hash, name, entry_destroy); rc = 0; done: return rc; }
int testCreate() { char *key = strdup("123abc"); struct data_t *value = data_create2(strlen("1234567890abc")+1,strdup("1234567890abc")); struct entry_t *entry = entry_create(key,value); int result = (entry->key == key) && (entry->value == value); entry_destroy(entry); printf("Modulo entry -> teste entry_create: %s\n",result?"passou":"nao passou"); return result; }
int rtable_conditional_put(struct rtable_t *rtable, char *key, struct data_t *data){ int result; struct message_t *msg = (struct message_t*)malloc(sizeof(struct message_t)); struct entry_t *entry=(struct entry_t*)malloc(sizeof(struct entry_t)); entry = entry_create(key, data); msg->opcode = OC_COND_PUT; msg->c_type = CT_ENTRY; msg->content.entry=entry; if(network_send_receive(rtable->server, msg)->opcode==OC_COND_PUT+1){ //testar se foi colocado efectivamente no servidor result=1; } else { result = -1; } return result; }
int attr_add (attr_t *attrs, const char *name, const char *val, int flags) { struct entry *e; if (name == NULL || (flags & FLUX_ATTRFLAG_ACTIVE)) { errno = EINVAL; return -1; } if ((e = zhash_lookup (attrs->hash, name))) { errno = EEXIST; return -1; } e = entry_create (name, val, flags); zhash_update (attrs->hash, name, e); zhash_freefn (attrs->hash, name, entry_destroy); return 0; }
int testAddVarios() { int result,i,keysize; struct list_t *list; struct entry_t *entry[1024]; struct entry_t *aux; struct data_t *data; char key[16]; printf("Módulo list -> teste adicionar vários:"); if ((list = list_create()) == NULL) error(1, errno, " O teste não pode prosseguir"); for(i=0; i<1024; i++) { sprintf(key, "keyabc-%d",i); keysize = strlen(key) + 1; if ((data = data_create(keysize)) == NULL) error(1, errno, " O teste não pode prosseguir"); memcpy(data->data, key, keysize); if ((entry[i] = entry_create(key, data)) == NULL) error(1, errno, " O teste não pode prosseguir"); data_destroy(data); list_add(list, entry[i]); } assert(list_size(list) == 1024); result = (list_size(list) == 1024); for(i=0; i<1024; i++) { assert(list_get(list, entry[i]->key) != entry[i]); aux = list_get(list, entry[i]->key); result = result && (aux != entry[i]) && strcmp(aux->key, entry[i]->key) == 0; entry_destroy(entry[i]); } list_destroy(list); printf(" %s\n", result ? "passou" : "não passou"); return result; }
int testAddCabeca() { int result; struct list_t *list; struct entry_t *entry; struct entry_t *entry2; struct data_t *data; printf("Módulo list -> teste adicionar cabeça:"); if ((list = list_create()) == NULL) error(1, errno, " O teste não pode prosseguir"); if ((data = data_create(5)) == NULL) error(1, errno, " O teste não pode prosseguir"); if ((entry = entry_create("abc", data)) == NULL) error(1, errno, " O teste não pode prosseguir"); memcpy(entry->value->data, "abc1", 5); assert(list_add(NULL, entry) < 0); result = (list_add(NULL, entry) < 0); assert(list_add(list, NULL) < 0); result = result && (list_add(list, NULL) < 0); result = result && (list_add(list, entry) == 0); entry2 = list_get(list, "abc"); result = result && entry2 != entry && list_size(list) == 1 && strcmp(entry->key, entry2->key) == 0; entry_destroy(entry); data_destroy(data); list_destroy(list); printf(" %s\n",result ? "passou" : "não passou"); return result; }
int testDup() { char *key = strdup("123abc"); struct data_t *value = data_create2(strlen("1234567890abc")+1,strdup("1234567890abc")); struct entry_t *entry = entry_create(key,value); struct entry_t *entry2 = entry_dup(entry); int result = (entry->key != entry2->key) && (strcmp(entry->key,entry2->key) == 0) && (entry->value != entry2->value) && (entry->value->datasize == entry2->value->datasize) && (memcmp(entry->value->data, entry2->value->data, entry->value->datasize) == 0); entry_destroy(entry); entry_destroy(entry2); printf("Modulo entry -> teste entry_dup: %s\n",result?"passou":"não passou"); return result; }
/* Função para adicionar um par chave valor na tabela. * Devolve 0 (ok) ou -1 (problemas). */ int ptable_put(struct ptable_t *ptable, char *key, struct data_t *value) { uint16_t result,size; result = -1; if(ptable->isOpen) { struct message_t *msg; char *msg_buffer, *buffer; result = table_put(ptable->table,key,value); if(result==0) { msg = (struct message_t*) malloc(sizeof(struct message_t)); msg->opcode = OC_PUT; msg->c_type = CT_ENTRY; msg->content.entry = entry_create(key,value); // size = message_to_buffer(msg,&msg_buffer); //Alocar memoria buffer = (char *) malloc(size+sizeof(uint16_t)); if(buffer == NULL) { perror("Out of memory"); return -1; } //Copiar o tamanho da mensagem memcpy(buffer,&size,sizeof(uint16_t)); //Copiar a mensagem memcpy(buffer+sizeof(uint16_t),msg_buffer,size); //Escrever mensagem if(pmanager_log(ptable->pmanager,buffer)==-1) { printf("Falha na escrita\n"); } //Free(s) free(buffer); free(msg_buffer); free_message(msg); } } return result; }
static int bucket_insert_existed( bucket_t *bucket, const hashmap_key_t key, const hashmap_value_t value, unsigned int * const existed ) { entry_t *entry; int err = 0; assert( NULL != bucket ); assert( NULL != key ); assert( NULL != existed ); entry = bucket_find_entry( bucket, key, NULL ); if( NULL != entry ) { /* Key exists in this bucket, update the value. */ entry->value = value; *existed = 1; } else { /* Key doesn't exist in this bucket, add it. */ entry_t *new_entry = entry_create( key, value ); if( NULL != new_entry ) { new_entry->next = bucket->entries; bucket->entries = new_entry; ++bucket->stats.num_entries; *existed = 0; } else { err = -1; } } return err; }
int testEntry() { int result, size, keysize, datasize, datasize_conv; short opcode, c_type, keysize_conv; char *msg_str = NULL; char *datastr = strdup("abc"); struct message_t *msg; struct data_t *d; printf("Módulo mensagem -> teste - Entry"); d = data_create2(strlen(datastr) + 1, datastr); msg = (struct message_t *) malloc(sizeof(struct message_t)); msg->opcode = OC_PUT; msg->c_type = CT_ENTRY; msg->content.entry = entry_create(datastr, d); data_destroy(d); size = message_to_buffer(msg, &msg_str); opcode = htons(msg->opcode); c_type = htons(msg->c_type); keysize = strlen(msg->content.entry->key); keysize_conv = htons(keysize); char comp_key[keysize]; memcpy(comp_key, msg_str + 6, keysize); datasize = msg->content.entry->value->datasize; datasize_conv = htonl(datasize); char comp_data[datasize]; memcpy(comp_data, msg_str + 4 + 2 + keysize + 4, datasize); result = memcmp(msg_str, &opcode, 2) == 0 && memcmp(msg_str + 2, &c_type, 2) == 0 && memcmp(msg_str + 4, &keysize_conv, 2) == 0 && memcmp(msg_str + 6, &comp_key, keysize) == 0 && memcmp(msg_str + 6 + keysize, &datasize_conv, 4) == 0 && memcmp(msg_str + 6 + keysize + 4, &comp_data, datasize) == 0; free_message(msg); msg = buffer_to_message(msg_str, size); result = result && msg->opcode == OC_PUT && msg->c_type == CT_ENTRY && strcmp(msg->content.entry->key, datastr) == 0 && msg->content.entry->value->datasize == strlen(datastr) + 1 && strcmp(msg->content.entry->value->data, datastr) == 0; free(msg_str); free(datastr); //print_message(msg); free_message(msg); printf(" %s\n", result ? "passou" : "não passou"); return result; }