Beispiel #1
0
int main() {
  int a = 5;
  int b = 10;
  int c = 20;

  List* list = list_create();

  Iterator* iterator = list_iterator(list);

  assert(false == iterator_has_next(iterator));

  iterator_destroy(iterator);

  list_push(list, &a);
  list_push(list, &b);
  list_push(list, &c);

  iterator = list_iterator(list);

  assert(true == iterator_has_next(iterator));
  assert(&a == iterator_next(iterator));
  assert(&b == iterator_next(iterator));
  assert(&c == iterator_next(iterator));
  assert(false == iterator_has_next(iterator));

  iterator_destroy(iterator);
  
  return 0;
}
Beispiel #2
0
io_library_t *__io_storeLibraryWithAddress(vm_address_t address)
{
	if(spinlock_tryLock(&__io_storeLock))
	{
		iterator_t *iterator = atree_iterator(__io_storeLibraries);
		io_library_t *library;

		while((library = iterator_nextObject(iterator)))
		{
			vm_address_t vlimit = library->vmemory + (library->pages * VM_PAGE_SIZE);
			if(address >= library->vmemory && address <= vlimit)
			{
				iterator_destroy(iterator);
				spinlock_unlock(&__io_storeLock);

				return library;
			}
		}

		iterator_destroy(iterator);
		spinlock_unlock(&__io_storeLock);
	}
	
	return NULL;
}
Beispiel #3
0
void
t_field_map(void)
{
    pool_reference list_pool = pool_create(LIST_TYPE_ID);
    CU_ASSERT_NOT_EQUAL_FATAL(list_pool, NULL_POOL);

    global_reference head = pool_alloc(&list_pool);
    pool_iterator itr = iterator_new(&list_pool, &head);

    size_t list_size = 10000;

    for (size_t i = 0 ; i < list_size ; ++i) {
        iterator_set_field(itr, 1, &i);
        iterator_list_insert(itr, pool_alloc(&list_pool));
        itr = iterator_next(list_pool, itr);
    }

    pool_reference long_pool = pool_create(LONG_TYPE_ID);
    CU_ASSERT_NOT_EQUAL_FATAL(long_pool, NULL_POOL);

    CU_ASSERT_EQUAL(field_map(list_pool, &long_pool, 1, square), 0);

    uint64_t *result = pool_to_array(long_pool);

    int cmp_error_count = 0;
    for (size_t i = 0 ; i < list_size ; ++i) {
        cmp_error_count += i*i != result[i];
    }

    CU_ASSERT_EQUAL(cmp_error_count, 0);

    iterator_destroy(&itr);
    pool_destroy(&long_pool);
    pool_destroy(&list_pool);
}
Beispiel #4
0
void test_next_nonEmptyList(void) {
  list_t* lista = list_init();

  addElement(lista, (void*)a);
  addElement(lista, (void*)b);
  addElement(lista, (void*)c);
  iterator_t* i=iterator_init(lista);

  payload_t * corrente = next(i);
  CU_ASSERT_PTR_NOT_NULL(corrente);
  CU_ASSERT_PTR_EQUAL(corrente, lista->head->payload);

  corrente = next(i);
  CU_ASSERT_PTR_NOT_NULL(corrente);
  CU_ASSERT_PTR_EQUAL(corrente, lista->head->next->payload);

  corrente = next(i);
  CU_ASSERT_PTR_NOT_NULL(corrente);
  CU_ASSERT_PTR_EQUAL(corrente, lista->tail->payload);

  corrente = next(i);
  CU_ASSERT_PTR_NULL(corrente);

  iterator_destroy(i);
  list_destroy(lista);
}
Beispiel #5
0
void test_next_emptyList(void) {
  list_t* lista = list_init();
  iterator_t* it=iterator_init(lista);

  CU_ASSERT_PTR_NULL(next(it));
  CU_ASSERT_PTR_NULL(it->currentNode);

  iterator_destroy(it);
  list_destroy(lista);
}
Beispiel #6
0
void test_hasNext_emptyList(void) {
  list_t* lista = list_init();

  iterator_t* i=iterator_init(lista);

  CU_ASSERT_EQUAL(hasNext(i), 0);

  iterator_destroy(i);
  list_destroy(lista);
}
Beispiel #7
0
void test_hasNext_nonEmptyList(void) {
  list_t* lista = list_init();

  addElement(lista, (void*)single);
  iterator_t* i=iterator_init(lista);

  CU_ASSERT_NOT_EQUAL(hasNext(i), 0);
  CU_ASSERT_PTR_EQUAL(i->currentNode, lista->head);

  iterator_destroy(i);
  list_destroy(lista);
}
Beispiel #8
0
void test_iterator_init_destroy(void) {
  list_t* list = list_init();

  iterator_t* i=iterator_init(list);

  CU_ASSERT_PTR_NOT_NULL(i);
  CU_ASSERT_PTR_EQUAL(i->list, list);
  CU_ASSERT_PTR_EQUAL(i->currentNode, list->head);

  iterator_destroy(i);
  list_destroy(list);
}
//controla se un messaggio e' presente nella lista
int containsValue(list_t* lista, char* name){
	int test = 0;
	iterator_t* iterator = iterator_init(lista);
	while(hasNext(iterator) && !test){
		reader_t* reader =((reader_t*)(iterator->currentNode->payload));
  		if(strcmp(reader->name, name) == 0)
  			test = 1;
		next(iterator);
	}
	iterator_destroy(iterator);
	return test;
}
Beispiel #10
0
int main(int argc, char* argv[])
{
    int i = 0;
    int n = 101;
    int last = n - 1;
    DList* dlist = dlist_create(NULL, NULL);

    for (i = 0; i < n; i++) {
        dlist_append(dlist, (void*)i);
    }

    Iterator* forward = dlist_iterator_create(dlist);
    Iterator* backward = dlist_iterator_create(dlist);

    iterator_advance(backward, last);
    invert(forward, backward);
    dlist_foreach(dlist, check_and_dec_int, &last);
    iterator_destroy(forward);
    iterator_destroy(backward);
    dlist_destroy(dlist);

    return 0;
}
void test_iterator_init_destroy(){
	Vector* vec = vector_init(1);
	int *a = NULL, *b = NULL, *c = NULL;

	vector_push_back(vec, a);
	vector_push_back(vec, b);
	vector_push_back(vec, c);

	Iterator* it = iterator_init(vec);
	assert(it->container == vec);
	assert(it->curent == vec->array);
	assert(*(it->curent) == a);

	iterator_destroy(it);
	vector_destroy(vec);
}
void test_iterator_end(){
	Vector* vec = vector_init(1);
	int a = 1, b = 2, c = 3, d = 4;

	vector_push_back(vec, &a);
	vector_push_back(vec, &b);
	vector_push_back(vec, &c);
	vector_push_back(vec, &d);

	Iterator* it = iterator_init(vec);

	assert(iterator_element(it) == &a);
	iterator_end(it);
	assert(iterator_element(it) == &d);

	iterator_destroy(it);
	vector_destroy(vec);

}
void test_iterator_valid(){
	Vector* vec = vector_init(1);
	int a = 1, b = 2, c = 3, d = 4;

	vector_push_back(vec, &a);
	vector_push_back(vec, &b);
	vector_push_back(vec, &c);
	vector_push_back(vec, &d);

	Iterator* it = iterator_init(vec);
	assert(iterator_valid(it) == 1);
	iterator_before(it);
	assert(iterator_valid(it) == 0);
	iterator_end(it);
	assert(iterator_valid(it) == 1);
	iterator_next(it);
	assert(iterator_valid(it) == 0);

	iterator_destroy(it);
	vector_destroy(vec);
}
iterator_t mpi_file_ntriples_node_iterator_create(MPI_File file, MPI_Offset start, MPI_Offset amount, size_t bufsize, MPI_Comm comm) {
	_mpi_file_ntriples_node_iterator_state_t state = malloc(sizeof(_mpi_file_ntriples_node_iterator_state_type));
	if(state == NULL) {
		fprintf(stderr, "%s:%u: Error in mpi_file_ntriples_node_iterator_create; unable to allocate %u bytes for internal state.\n", __FILE__, __LINE__, sizeof(_mpi_file_ntriples_node_iterator_state_type));
		return NULL;
	}
	state->ntriple = NULL;
	state->mark = NULL;
	state->part = _MPI_FILE_NTRIPLES_NODE_ITERATOR_SUBJECT;
	state->iter = mpi_file_ntriples_iterator_create(file, start, amount, bufsize, comm);
	if(state->iter == NULL) {
		fprintf(stderr, "%s:%u: Error in mpi_file_ntriples_node_iterator_create; unable to allocate underlying iterator.\n", __FILE__, __LINE__);
		free(state);
		return NULL;
	}
	iterator_t iter = iterator_create(&_mpi_file_ntriples_node_iterator_has_next, &_mpi_file_ntriples_node_iterator_next, &_mpi_file_ntriples_node_iterator_destroy, state);
	if(iter == NULL) {
		fprintf(stderr, "%s:%u: Error in mpi_file_ntriples_node_iterator_create; unable to allocate iterator.\n", __FILE__, __LINE__);
		iterator_destroy(state->iter);
		free(state);
	}
	return iter;
}
Beispiel #15
0
int invoke_packet_handlers(packet_t type, void *packet) {
	int forward = FORWARD_PACKET;

	packet_handler_t *handler;
	byte id;

	switch (type) {

	case BNCS_RECEIVED:
	case BNCS_SENT:
		id = BNCS_CAST(packet)->id;
		break;

	case MCP_RECEIVED:
	case MCP_SENT:
		id = MCP_CAST(packet)->id;
		break;

	case D2GS_RECEIVED:
	case D2GS_SENT:
		id = D2GS_CAST(packet)->id;
		break;

	case INTERNAL:
		id = INTERNAL_CAST(packet)->id;
		break;

	default:
		return forward;

	}

	struct iterator i = list_iterator(&packet_handlers[type][id]);
	while ((handler = iterator_next(&i))) {
		void *c;
		void *data = NULL;
		switch (type) {

		case BNCS_RECEIVED:
		case BNCS_SENT:
			c = calloc(1, sizeof(bncs_packet_t));
			BNCS_CAST(c)->id = id;
			BNCS_CAST(c)->len = BNCS_CAST(packet)->len;
			if (bncs_has_payload(BNCS_CAST(packet))) {
				BNCS_CAST(c)->data = malloc(BNCS_CAST(packet)->len);
				data = BNCS_CAST(c)->data;
				memcpy(BNCS_CAST(c)->data, BNCS_CAST(packet)->data, BNCS_CAST(packet)->len - BNCS_HEADER_SIZE);
			}
			break;

		case MCP_RECEIVED:
		case MCP_SENT:
			c = calloc(1, sizeof(mcp_packet_t));
			MCP_CAST(c)->id = id;
			MCP_CAST(c)->len = MCP_CAST(packet)->len;
			if (mcp_has_payload(MCP_CAST(packet))) {
				MCP_CAST(c)->data = malloc(MCP_CAST(packet)->len);
				data = MCP_CAST(c)->data;
				memcpy(MCP_CAST(c)->data, MCP_CAST(packet)->data, MCP_CAST(packet)->len - MCP_HEADER_SIZE);
			}
			break;

		case D2GS_RECEIVED:
		case D2GS_SENT:
			c = calloc(1, sizeof(d2gs_packet_t));
			D2GS_CAST(c)->id = id;
			D2GS_CAST(c)->len = D2GS_CAST(packet)->len;
			if (d2gs_has_payload(D2GS_CAST(packet))) {
				D2GS_CAST(c)->data = malloc(D2GS_CAST(packet)->len);
				data = D2GS_CAST(c)->data;
				memcpy(D2GS_CAST(c)->data, D2GS_CAST(packet)->data, D2GS_CAST(packet)->len - D2GS_HEADER_SIZE);
			}
			break;

		case INTERNAL:
			c = calloc(1, sizeof(internal_packet_t));
			INTERNAL_CAST(c)->id = id;
			INTERNAL_CAST(c)->len = INTERNAL_CAST(packet)->len;
			if (internal_has_payload(INTERNAL_CAST(packet))) {
				INTERNAL_CAST(c)->data = malloc(INTERNAL_CAST(packet)->len);
				data = INTERNAL_CAST(c)->data;
				memcpy(INTERNAL_CAST(c)->data, INTERNAL_CAST(packet)->data, INTERNAL_CAST(packet)->len - INTERNAL_HEADER_SIZE);
			}
			break;

		default:
			return forward;

		}

		forward = (*handler)((void *) c);
		free(c);
		if (data) {
			free(data);
		}
		if (forward == HIDE_PACKET || forward == BLOCK_PACKET) {
			iterator_destroy(&i);
			break;
		}
	}

	return forward;
}
void _mpi_file_ntriples_node_iterator_destroy(void* s) {
	_mpi_file_ntriples_node_iterator_state_t state = (_mpi_file_ntriples_node_iterator_state_t) s;
	iterator_destroy(state->iter);
	free(state);
}