Exemplo n.º 1
0
void test_clist_remove(void)
{
  struct key k1;
  struct key k2;
  struct key k3;
  k1.id = 1;
  k2.id = 2;
  k3.id = 3;
  struct clist list;
  clist_init(&list, match_function, empty_destroy_function, CLIST_UNSORTED);

  int ret = clist_append(&list, (void *)&k1);
  CU_ASSERT(ret == CLIST_TRUE);
  ret = clist_append(&list, (void *)&k2);
  CU_ASSERT(ret == CLIST_TRUE);
  ret = clist_append(&list, (void *)&k3);
  CU_ASSERT(ret == CLIST_TRUE);

  struct key *data;

  ret = clist_get_by_id(&list, (void **)&data, 1);
  CU_ASSERT(ret == CLIST_TRUE);
  if (ret == CLIST_TRUE)
    CU_ASSERT(data->id == 2);

  ret = clist_remove(&list, (void **)&data, 1);
  CU_ASSERT(ret == CLIST_TRUE);

  ret = clist_get_by_id(&list, (void **)&data, 1);
  CU_ASSERT(ret == CLIST_TRUE);
  if (ret == CLIST_TRUE)
    CU_ASSERT(data->id == 3);
  
  clist_destroy(&list);

  /* test corner case (one element list) */

  k1.id = 1;
  clist_init(&list, match_function, empty_destroy_function, CLIST_UNSORTED);

  ret = clist_append(&list, (void *)&k1);
  CU_ASSERT(ret == CLIST_TRUE);

  ret = clist_get_by_id(&list, (void **)&data, 0);
  CU_ASSERT(ret == CLIST_TRUE);
  if (ret == CLIST_TRUE)
    CU_ASSERT(data->id == 1);

  ret = clist_remove(&list, (void **)&data, 0);
  CU_ASSERT(ret == CLIST_TRUE);
  
  clist_destroy(&list);

}
Exemplo n.º 2
0
int main()
{
    CList *list = (CList*)malloc(sizeof(CList));
	clist_init(list,destroy);
	Stu *stu = (Stu*)malloc(sizeof(Stu));
	stu->age = 11;
	stu->name ="HeXinyuan";
	clist_ins_next(list,NULL,(void *)stu);
	Stu *stu2 = (Stu*)malloc(sizeof(Stu));
	stu2->age = 22;
	stu2->name ="ZhangJingli";
    clist_ins_next(list,clist_head(list),(void *)stu2);
    Stu *newStu = createStu(33,"HeXiaoxiao");
    clist_ins_next(list,clist_head(list),(void *)newStu);
    printf("clist_size()= %d\n",clist_size(list));
    Stu *result = (Stu*)(clist_head(list)->data);
    CListElmt *head = clist_head(list);
    while(head!=NULL){
    	Stu* res = (Stu*)(head->data);
    	printf("res->age = %d,res->name=%s\n",res->age,res->name);
    	head = head->next;
    	if(clist_head(list) == head){
    		break;
    	}
    }
    void *dataStu;
    clist_rem_next(list,clist_head(list),&dataStu);
    printf("%d,%s\n",((Stu*)(dataStu))->age,((Stu*)(dataStu))->name);
    clist_destroy(list);
	return 0;
}
Exemplo n.º 3
0
void test_clist_insert(void)
{
  struct key k1;
  struct key k2;
  struct key k3;
  k1.id = 1;
  k2.id = 2;
  k3.id = 3;
  struct clist list;
  clist_init(&list, match_function, empty_destroy_function, CLIST_UNSORTED);
  int ret = clist_append(&list, (void *)&k1);

  CU_ASSERT(ret == CLIST_TRUE);
  ret = clist_append(&list, (void *)&k2);
  CU_ASSERT(ret == CLIST_TRUE);
  ret = clist_insert(&list, (void *)&k3, 1);
  CU_ASSERT(ret == CLIST_TRUE);

  struct key *data;
  ret = clist_get_by_id(&list, (void **)&data, 1);
  CU_ASSERT(ret == CLIST_TRUE);
  if (ret == CLIST_TRUE)
    CU_ASSERT(data->id == 3);

  ret = clist_destroy(&list);
  CU_ASSERT(ret == CLIST_TRUE);
}
Exemplo n.º 4
0
void test_clist_append_insert_append(void)
{
#define NUM_ELEMENTS 10
  struct clist list;
  int ret = clist_init(&list, match_function, free_destroy_function, CLIST_UNSORTED);
  struct key *k = NULL;
  int ctr = NUM_ELEMENTS;
  for (; ctr>0; ctr--)
  {
    k = (struct key*)malloc(sizeof(struct key));
    k->id = ctr;
    printf("adding id: %i\n", k->id);
    ret = clist_append(&list, (void *)k);
    CU_ASSERT(ret==CLIST_TRUE);
  }
  for (ctr=NUM_ELEMENTS; ctr>0; ctr--)
  {
    k = (struct key*)malloc(sizeof(struct key));
    k->id = ctr;
    printf("adding id: %i\n", k->id);
    ret = clist_insert(&list, (void *)k, ctr);
    CU_ASSERT(ret==CLIST_TRUE);
  }
  for (ctr=NUM_ELEMENTS; ctr>0; ctr--)
  {
    k = (struct key*)malloc(sizeof(struct key));
    k->id = ctr;
    printf("adding id: %i\n", k->id);
    ret = clist_insert(&list, (void *)k, 0);
    CU_ASSERT(ret==CLIST_TRUE);
  }
  for (ctr=NUM_ELEMENTS; ctr>0; ctr--)
  {
    k = (struct key*)malloc(sizeof(struct key));
    k->id = ctr;
    printf("adding id: %i\n", k->id);
    ret = clist_append(&list, (void *)k);
    CU_ASSERT(ret==CLIST_TRUE);
  }

  struct clist_iterator *it = malloc(sizeof(struct clist_iterator));
  ret = clist_get_iterator(&list, it, 0);

  ret = clist_iterate(&list, it, (void **)&k, 0);
  CU_ASSERT(ret==CLIST_TRUE);
  ctr = 0;
  do
  {
    printf("k->id: %i\n", k->id);
    ctr ++;
  } while (clist_iterate(&list, it, (void **)&k, 1)==CLIST_TRUE);
  CU_ASSERT(ctr==NUM_ELEMENTS*4);
  clist_destroy(&list);

}
Exemplo n.º 5
0
void test_clist_sort(void)
{
  int items[] = {5, 10, 23, 12, 1,
                 91, 32, 12, 3, 2};
#define NUM_ELEMENTS 10
  struct clist list;
  int ret = clist_init(&list, match_function, free_destroy_function, CLIST_UNSORTED);
  struct key *k = NULL;
  int ctr = 0;
  for (; ctr<NUM_ELEMENTS; ctr++)
  {
    k = (struct key*)malloc(sizeof(struct key));
    k->id = items[ctr];
    printf("adding id: %i\n", k->id);
    ret = clist_append(&list, (void *)k);
    CU_ASSERT(ret==CLIST_TRUE);
  }

/*traverse list before sort*/
  struct clist_iterator *it = malloc(sizeof(struct clist_iterator));
  ret = clist_get_iterator(&list, it, 0);
  CU_ASSERT(ret==CLIST_TRUE);

  ret = clist_iterate(&list, it, (void **)&k, 0);
  CU_ASSERT(ret==CLIST_TRUE);
  ctr = 0;
  do
  {
    printf("k->id: %i\n", k->id);
    ctr ++;
  } while (clist_iterate(&list, it, (void **)&k, 1)==CLIST_TRUE);
  CU_ASSERT(ctr==NUM_ELEMENTS);
  
  clist_qsort(&list, ASCENDING);

/*traverse list after sort*/
  ret = clist_get_iterator(&list, it, 0);
  CU_ASSERT(ret==CLIST_TRUE);
  ret = clist_iterate(&list, it, (void **)&k, 0);
  CU_ASSERT(ret==CLIST_TRUE);
  ctr = 0;
  int prev = k->id;;
  do
  {
    printf("k->id: %i, prev: %i\n", k->id, prev);
    CU_ASSERT(prev<=k->id);
    prev = k->id;
    ctr ++;
  } while (clist_iterate(&list, it, (void **)&k, 1)==CLIST_TRUE);
  CU_ASSERT(ctr==NUM_ELEMENTS);
  free(it);
  
  clist_destroy(&list);
}
Exemplo n.º 6
0
void cevents_destroy(cevents *cevts) {
	if(cevts == NULL)
		return;
	if(cevts->events != NULL) {
		for(size_t i = 0; i < MAX_EVENTS; i++) {
			cevents_clear_fired_events(cevts, i);
			clist_destroy(cevts->events[i].fired_queue);
		}
		jfree(cevts->events);
	}
	if(cevts->fired_fds != NULL)
		clist_destroy(cevts->fired_fds);
	if(cevts->timers != NULL)
		ctimer_base_destroy(cevts->timers);
	LOCK_DESTROY(&cevts->lock);
	LOCK_DESTROY(&cevts->qlock);
	cevts->events = NULL;
	cevts->fired_fds = NULL;
	cevts->timers = NULL;
	//cevts->fired_queue = NULL;
	cevents_destroy_priv_impl(cevts);
	jfree(cevts);
}
Exemplo n.º 7
0
int main(int argc, char const *argv[]) {
	size_t i;
	lock = SL_UNLOCK;
	cl = clist_create();
	count = 0;
	for(i = 0; i < 6; i++)
		clist_lpush(cl, NULL);

	clist_rpop(cl);
	clist_rpop(cl);
	clist_rpop(cl);
	clist_rpop(cl);
	clist_rpop(cl);
	clist_rpop(cl);

	for(i = 0; i < 6; i++)
		clist_lpush(cl, NULL);
	clist_destroy(cl);

	cl = clist_create();
	for(i = 0; i < 1000; i++)
		clist_rpush(cl, NULL);
	for(i = 0; i < 1000; i++)
		clist_lpush(cl, NULL);

	for(i = 0; i < 1000; i++)
		clist_lpop(cl);

	for(i = 0; i < 1000; i++)
		clist_rpop(cl);

	clist_destroy(cl);
	printf("%llu\n", used_mem());

	return 0;
}
Exemplo n.º 8
0
int main(void)
{
	int i;
	int* data[10];
	CList list;

	// 10 test data
	for (i = 0; i< 10; i++)
	{
		data[i] = (int*)malloc(sizeof(int));
		*data[i] = i;
	}

	list = clist_create(debug_free);

	//insert node after head
	for (i = 0;i < 5 ;i++)
	{
		clist_ins_next(list,clist_head(list),data[i]);
		clist_debug(list);
	}

	//insert node as head
	for (i = 0;i < 5 ;i++)
	{
		clist_ins_next(list,NULL,data[i+5]);
		clist_debug(list);
	}

	//remove node after head
	for (i = 0; i < 5 ;i++)
	{
		clist_rmv_next(list, clist_head(list), &data[i]);
		free(data[i]);
		clist_debug(list);
	}
	//remove head node
	for (i = 0; i < 3 ;i++)
	{
		clist_rmv_next(list, NULL, &data[i+5]);
		free(data[i+5]);
		clist_debug(list);
	}

	clist_destroy(list);

	return 0;
}
Exemplo n.º 9
0
void cevents_destroy(cevents *cevts) {
	if(cevts == NULL)
		return;
	if(cevts->events != NULL)
		jfree(cevts->events);
	if(cevts->fired != NULL)
		jfree(cevts->fired);
	if(cevts->fired_queue != NULL)
		clist_destroy(cevts->fired_queue);
	LOCK_DESTROY(&cevts->lock);
	LOCK_DESTROY(&cevts->qlock);
	cevts->events = NULL;
	cevts->fired = NULL;
	cevts->fired_queue = NULL;
	cevents_destroy_priv_impl(cevts);
	jfree(cevts);
}
Exemplo n.º 10
0
int main() {

    //linked list
    CList *l;

    int *data = 0;
    int *data1 = 1;
    int *data2 = 2;

    clist_init(l, (void *) &data);
    printf("Circular list has been initalized\n");

    clist_ins_next(l, NULL, data);
    clist_ins_next(l, clist_head(l), data1);
    clist_ins_next(l, clist_next(clist_head(l)), data2);

    printf("Current linked list size %d\n", clist_size(l));
    printf("Head elem %d\n", clist_head(l)->data);

    printf("Next of head %d\n", clist_head(l)->next->data);
    printf("Next next of head %d\n", clist_head(l)->next->next->data);

    clist_rem_next(l, clist_head(l)->next, (void *) &data2);
    clist_rem_next(l, clist_head(l), (void *) &data1);
    clist_rem_next(l, clist_head(l), (void *) &data);

    printf("Linked list has been cleared\n");

    if (clist_size(l) == 0) {
        clist_destroy(l);
        printf("Circular list has been destroyed\n");
    } else {
        printf("Can't delete list. It's not empty %d\n", clist_size(l));

    };
    return 0;
}
Exemplo n.º 11
0
/* --------------------------------- main -------------------------------- */
int main(int argc, char **argv) {

    CList              list;
    CListElmt          *element;

    int                *data,
		       i;

    /* Initialize the circular list */
    clist_init(&list, free);

    /* Perform some circular list operations */
    element = clist_head(&list);

    for (i = 0; i < 10; i++) {

       if ((data = (int *)malloc(sizeof(int))) == NULL)
	  return 1;

       *data = i + 1;

       if (clist_ins_next(&list, element, data) != 0)
	  return 1;

       if (element == NULL)
	  element = clist_next(clist_head(&list));
       else
	  element = clist_next(element);

    }

    print_list(&list);

    element = clist_head(&list);

    for (i = 0; i < 10; i++)
       element = clist_next(element);

    data = clist_data(element);
    fprintf(stdout, "Circling and removing an element after the one containing "
       "%03d\n",*data);

    if (clist_rem_next(&list, element, (void **)&data) != 0)
       return 1;

    free(data);
    print_list(&list);

    element = clist_head(&list);

    for (i = 0; i < 15; i++)
       element = clist_next(element);

    data = clist_data(element);
    fprintf(stdout, "Circling and inserting 011 after the element containing "
       "%03d\n", *data);

    if ((data = (int *)malloc(sizeof(int))) == NULL)
       return 1;

    *data = 11;
    if (clist_ins_next(&list, element, data) != 0)
       return 1;

    print_list(&list);

    /* Destroy the circular list */
    fprintf(stdout, "Destroying the list\n");
    clist_destroy(&list);

    return 0;

}
Exemplo n.º 12
0
void
close_radar(void)
{
    if (radar_q_counter)
        clist_destroy(&radar_q, &radar_q_counter);
}
Exemplo n.º 13
0
int main()
{
	CList list;
	CListElmt *current;

	Page *p;
	int i;

	/* Initialize the circilar list. */
	clist_init(&list, free);

	/* Load the pages into the list. */
	current = NULL;
	for (i = 0; i < 10; ++i) {
		if ((p = (Page *)malloc(sizeof(Page))) == NULL) {
			return 1;
		}

		if (i < 5) {
			p->reference = 1;
		} else {
			p->reference = 0;
		}

		p->number = i;

		if (clist_ins_next(&list, current, p) != 0) {
			return 1;
		}

		if (current == NULL) {
			current = clist_next(clist_head(&list));
		} else {
			current = clist_next(current);
		}
	}

	current = clist_head(&list);

	for (i = 0; i < 10; ++i) {
		p = clist_data(current);

		fprintf(stdout, "p[%d].number=%d, p[%d].reference=%d\n",i, p->number,
				i, p->reference);

		current = clist_next(current);
	}

	/* Get the number of the page to replace. */
	current = clist_head(&list);
	i = replace_page(&current);
	fprintf(stdout, "Selected %d\n", i);

	current = clist_head(&list);

	for (i = 0; i < 10; ++i) {
		p = clist_data(current);
		fprintf(stdout, "p[%d].number=%d, p[%d].reference=%d\n",i, p->number,
				i, p->reference);

		current = clist_next(current);
	}

	/* Destroy the circular list. */
	fprintf(stdout, "Destroying the list\n");
	clist_destroy(&list);

	return 0;
}
Exemplo n.º 14
0
void test_clist_iterator(void)
{
#define NUM_ELEMENTS 10
  struct clist list;
  int ret = clist_init(&list, match_function, free_destroy_function, CLIST_UNSORTED);
  struct key *k = NULL;
  int ctr = NUM_ELEMENTS;
  for (; ctr>0; ctr--)
  {
    k = (struct key*)malloc(sizeof(struct key));
    k->id = ctr;
    printf("adding id: %i\n", k->id);
    ret = clist_append(&list, (void *)k);
    CU_ASSERT(ret==CLIST_TRUE);
  }

  struct clist_iterator *it = malloc(sizeof(struct clist_iterator));
  ret = clist_get_iterator(&list, it, 0);
  CU_ASSERT(ret==CLIST_TRUE);

  ret = clist_iterate(&list, it, (void **)&k, 0);
  CU_ASSERT(ret==CLIST_TRUE);
  ctr = 0;
  do
  {
    printf("k->id: %i\n", k->id);
    CU_ASSERT(NUM_ELEMENTS-ctr==k->id);
    ctr ++;
  } while (clist_iterate(&list, it, (void **)&k, 1)==CLIST_TRUE);
  CU_ASSERT(ctr==NUM_ELEMENTS);
  clist_destroy(&list);


/* corner case test (iterator for 1 element list) */

  struct clist list2;
  ret = clist_init(&list2, match_function, free_destroy_function, CLIST_UNSORTED);
  struct key *k2 = NULL;
  
  k2 = (struct key*)malloc(sizeof(struct key));
  k2->id = 100;
  printf("adding id: %i\n", k2->id);
  ret = clist_append(&list2, (void *)k2);
  CU_ASSERT(ret==CLIST_TRUE);

  struct clist_iterator *it2 = malloc(sizeof(struct clist_iterator));
  ret = clist_get_iterator(&list2, it2, 0);
  CU_ASSERT(ret==CLIST_TRUE);

  ret = clist_iterate(&list2, it2, (void **)&k2, 0);
  CU_ASSERT(ret==CLIST_TRUE);
  ctr = 0;
  do
  {
    printf("k2->id: %i\n", k2->id);
    ctr ++;
  } while (clist_iterate(&list2, it2, (void **)&k, 1)==CLIST_TRUE);
  CU_ASSERT(ctr==1);
  clist_destroy(&list2);

}