Exemplo n.º 1
0
void test_dlist_prepend_to_empty(void)
{
    unsigned long *val = NULL;

    assert_true(test_dlist == NULL);

    test_dlist = dlist_create();

    assert_true(test_dlist != NULL);
    assert_true(dlist_is_empty(test_dlist));

    val = make_ulong_ptr(9999);
    assert_true(val != NULL);

    assert_true(dlist_prepend(test_dlist, val) == 0);

    /* Verify */
    val = NULL;
    val = dlist_index(test_dlist, 0);
    assert_true(val != NULL);
    assert_ulong_equal(9999, *val);
    assert_true(dlist_size(test_dlist) == 1);

    dlist_free_all(test_dlist, NULL);
    test_dlist = NULL;
}
Exemplo n.º 2
0
void  queue_push (Queue *queue, void *data)
{
        queue->head = dlist_prepend(queue->head, data);
        if (queue->tail == NULL)
                queue->tail = queue->head;
 
}
Exemplo n.º 3
0
static void test_int_dlist(void)
{
	int i = 0;
	int n = 100;
	int data = 0;
	DList* dlist = dlist_create(NULL, NULL, NULL);

	for(i = 0; i < n; i++)
	{
		assert(dlist_append(dlist, (void*)i) == RET_OK);
		assert(dlist_length(dlist) == (i + 1));
		assert(dlist_get_by_index(dlist, i, (void**)&data) == RET_OK);
		assert(data == i);
		assert(dlist_set_by_index(dlist, i, (void*)(2*i)) == RET_OK);
		assert(dlist_get_by_index(dlist, i, (void**)&data) == RET_OK);
		assert(data == 2*i);
		assert(dlist_set_by_index(dlist, i, (void*)i) == RET_OK);
		assert(dlist_find(dlist, cmp_int, (void*)i) == i);
	}

	for(i = 0; i < n; i++)
	{
		assert(dlist_get_by_index(dlist, 0, (void**)&data) == RET_OK);
		assert(data == (i));
		assert(dlist_length(dlist) == (n-i));
		assert(dlist_delete(dlist, 0) == RET_OK);
		assert(dlist_length(dlist) == (n-i-1));
		if((i + 1) < n)
		{
			assert(dlist_get_by_index(dlist, 0, (void**)&data) == RET_OK);
			assert((int)data == (i+1));
		}
	}
	
	assert(dlist_length(dlist) == 0);

	for(i = 0; i < n; i++)
	{
		assert(dlist_prepend(dlist, (void*)i) == RET_OK);
		assert(dlist_length(dlist) == (i + 1));
		assert(dlist_get_by_index(dlist, 0, (void**)&data) == RET_OK);
		assert(data == i);
		assert(dlist_set_by_index(dlist, 0, (void*)(2*i)) == RET_OK);
		assert(dlist_get_by_index(dlist, 0, (void**)&data) == RET_OK);
		assert(data == 2*i);
		assert(dlist_set_by_index(dlist, 0, (void*)i) == RET_OK);
	}

	i = n - 1;
	assert(dlist_foreach(dlist, check_and_dec_int, &i) == RET_OK);

	dlist_destroy(dlist);

	return;
}
Exemplo n.º 4
0
Ret      hash_table_insert(HashTable* thiz, void* data)
{
	size_t index = 0;

	return_val_if_fail(thiz != NULL, RET_INVALID_PARAMS);

	index = thiz->hash(data)%thiz->slot_nr;
	if(thiz->slots[index] == NULL)
	{
		thiz->slots[index] = dlist_create(thiz->data_destroy, thiz->data_destroy_ctx);
	}

	return dlist_prepend(thiz->slots[index], data);
}
Exemplo n.º 5
0
Arquivo: clib.c Projeto: FreeWave/clib
int main (int argc, char *argv[])
{
     DList* list = NULL;
     list = dlist_append(list, (void*)"ASD");
     list = dlist_append(list, (void*)"DSA");
     list = dlist_prepend(list, (void*)"ASDD");

     list = dlist_insert_by_index(list,0, (void*)"DDD");

     list = dlist_remove_element(list, (void*)"DSA");
     
     printf("%d", dlist_get_length(list)); 

     return 0;
}
Exemplo n.º 6
0
void test_dlist_prepend_to_existing(void)
{
    unsigned long *val;
    unsigned long old_size;

    old_size = dlist_size(test_dlist);
    val = make_ulong_ptr(7777);
    assert_true(dlist_prepend(test_dlist, val) == 0);

    /* Verify */
    val = NULL;
    val = dlist_index(test_dlist, 0);
    assert_ulong_equal(7777, *val);
    assert_true((old_size + 1) == dlist_size(test_dlist));
}
Exemplo n.º 7
0
static void test_invalid_params(void)
{
	printf("===========Warning is normal begin==============\n");
	assert(dlist_length(NULL) == 0);
	assert(dlist_prepend(NULL, 0) == RET_INVALID_PARAMS);
	assert(dlist_append(NULL, 0) == RET_INVALID_PARAMS);
	assert(dlist_delete(NULL, 0) == RET_INVALID_PARAMS);
	assert(dlist_insert(NULL, 0, 0) == RET_INVALID_PARAMS);
	assert(dlist_set_by_index(NULL, 0, 0) == RET_INVALID_PARAMS);
	assert(dlist_get_by_index(NULL, 0, NULL) == RET_INVALID_PARAMS);
	assert(dlist_find(NULL, NULL, NULL) < 0);
	assert(dlist_foreach(NULL, NULL, NULL) == RET_INVALID_PARAMS);
	printf("===========Warning is normal end==============\n");

	return;
}
Exemplo n.º 8
0
static void* producer(void* param)
{
	int i = 0;
	DList* dlist = (DList*)param;

	for(i = 0; i < 100; i++)
	{
		assert(dlist_append(dlist, (void*)i) == RET_OK);
	}
	sleep(1);
	for(i = 0; i < 100; i++)
	{
		assert(dlist_prepend(dlist, (void*)i) == RET_OK);
	}

	return NULL;
}
Exemplo n.º 9
0
static void* producer(void* param)
{
	int i = 0;
	DList* dlist = (DList*)param;

	for(i = 0; i < 100; i++)
	{
		assert(dlist_append(dlist, (void*)i) == RET_OK);
		printf("%s: product one member\n", __func__);
	}
	for(i = 0; i < 100; i++)
	{
		assert(dlist_prepend(dlist, (void*)i) == RET_OK);
		printf("%s: product one member\n", __func__);
	}

	return NULL;
}
Exemplo n.º 10
0
static void *producer_thread(void *arg)
{
	int i = 0;
	DList *dlist = (DList *)arg;
	
	printf("producer thread executed\n");
	printf("append data\n");
	
	for(i = 0; i < 1000; i++) 
		dlist_insert(dlist, 0, (void *)i);

	sleep(1);
	printf("prepend data to dlist \n");
	
	for(i = 0; i < 1000; i++) 
		dlist_prepend(dlist, (void *)i);
	
	return NULL;
}
Exemplo n.º 11
0
static struct connline_context *__connline_context_new(void)
{
	struct connline_context *context;
	dlist *new_list;

	context = calloc(1, sizeof(struct connline_context));
	if (context == NULL)
		return NULL;

	new_list = dlist_prepend(contexts_list, context);
	if (new_list == contexts_list) {
		free(context);
		return NULL;
	}

	contexts_list = new_list;

	return context;
}
Exemplo n.º 12
0
int main(int argc, char* argv[])
{
    int i = 0;
    int n = 100;
    DList* dlist = dlist_create();

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

    dlist_print(dlist, print_int);

    dlist_destroy(dlist);

    return 0;
}
Exemplo n.º 13
0
/**
 * @brief Insert a list element to the specified position
 * @param list The list list pointer
 * @param data The data for the new list
 * @param pos The position to put the new list
 * @return The new list list
 */
struct dlist *dlist_insert(struct dlist *list, void *data, int pos)
{
	struct dlist *tmp = NULL, *next = NULL;

	if (pos < 0)
		return dlist_append(list, data);
	else if (pos == 0)
		return dlist_prepend(list, data);

	tmp = dlist_nth(list, pos);
	if (!tmp)
		return dlist_append(list, data);

	next = dlist_alloc();
	next->data = data;
	next->prev = tmp->prev;

	if (tmp->prev)
		tmp->prev->next = next;
	next->next = tmp;
	tmp->prev = next;

	return (tmp == list) ? next : list;
}
Exemplo n.º 14
0
static Ret linear_container_dlist_prepend(LinearContainer* thiz, void* data)
{
	PrivInfo* priv = (PrivInfo*)thiz->priv;

	return dlist_prepend(priv->dlist, data);
}