Browse Source

remove custom list implementation. Use the Unix-list from queue.h instead.

Julius Pfrommer 10 years ago
parent
commit
9b18fb2b02
3 changed files with 0 additions and 596 deletions
  1. 0 236
      src/util/ua_list.c
  2. 0 65
      src/util/ua_list.h
  3. 0 295
      tests/check_list.c

+ 0 - 236
src/util/ua_list.c

@@ -1,236 +0,0 @@
-#include "ua_list.h"
-#include "ua_util.h"
-
-void UA_list_defaultFreer(void *payload) {
-    if(payload)
-        UA_free(payload);
-}
-
-UA_Int32 UA_list_initElement(UA_list_Element *element) {
-    if(element == UA_NULL) return UA_ERROR;
-    element->next    = UA_NULL;
-    element->prev    = UA_NULL;
-    element->father  = UA_NULL;
-    element->payload = UA_NULL;
-    return UA_NO_ERROR;
-}
-
-UA_Int32 UA_list_init(UA_list_List *list) {
-    if(list == UA_NULL) return UA_ERROR;
-    list->first = UA_NULL;
-    list->last  = UA_NULL;
-    list->size  = 0;
-    return UA_NO_ERROR;
-}
-
-UA_Int32 UA_list_addElementToFront(UA_list_List *list, UA_list_Element *element) {
-    UA_list_Element *second = UA_NULL;
-    if(list == UA_NULL || element == UA_NULL) return UA_ERROR;
-    second          = list->first;
-    list->first     = element;
-    element->prev   = UA_NULL;
-    element->next   = second;
-    element->father = list;
-    if(second)
-        second->prev = element;
-    list->size++;
-    if(list->size == 1)
-        list->last = element;
-    return UA_NO_ERROR;
-}
-
-UA_Int32 UA_list_addPayloadToFront(UA_list_List *list, void *const payload) {
-    UA_list_Element *elem;
-    if(list == UA_NULL)
-        return UA_ERROR;
-    UA_alloc((void **)&elem, sizeof(*elem));
-    UA_list_initElement(elem);
-    elem->payload = payload;
-    UA_list_addElementToFront(list, elem);
-    return UA_NO_ERROR;
-}
-
-UA_Int32 UA_list_addElementToBack(UA_list_List *list, UA_list_Element *element) {
-    UA_list_Element *secondLast = UA_NULL;
-    if(list == UA_NULL || element == UA_NULL)
-        return UA_ERROR;
-    secondLast      = list->last;
-    list->last      = element;
-    element->prev   = secondLast;
-    element->next   = UA_NULL;
-    element->father = list;
-    if(secondLast)
-        secondLast->next = element;
-    list->size++;
-    if(list->size == 1)
-        list->first = element;
-    return UA_NO_ERROR;
-}
-
-UA_Int32 UA_list_addPayloadToBack(UA_list_List *list, void *const payload) {
-    UA_list_Element *elem;
-    if(list == UA_NULL)
-        return UA_ERROR;
-    UA_alloc((void **)&elem, sizeof(*elem));
-    UA_list_initElement(elem);
-    elem->payload = payload;
-    UA_list_addElementToBack(list, elem);
-    return UA_NO_ERROR;
-}
-
-UA_Int32 UA_list_removeFirst(UA_list_List *list, UA_list_PayloadVisitor visitor) {
-    UA_list_Element *temp = UA_NULL;
-    if(list == UA_NULL)
-        return UA_ERROR;
-    if(list->first) {
-        temp = list->first->next;
-        if(visitor)
-            (*visitor)(list->first->payload);
-        UA_free(list->first);
-        list->first = temp;
-        if(temp)
-            temp->prev = UA_NULL;
-        list->size--;
-        if(list->size == 1)
-            list->last = temp;
-        else if(list->size == 0)
-            list->last = UA_NULL;
-    }
-    return UA_NO_ERROR;
-}
-
-UA_Int32 UA_list_removeLast(UA_list_List *list, UA_list_PayloadVisitor visitor) {
-    UA_list_Element *temp = UA_NULL;
-    if(list == UA_NULL)
-        return UA_ERROR;
-    if(list->last) {
-        temp = list->last->prev;
-        if(visitor)
-            (*visitor)(list->last->payload);
-        UA_free(list->last);
-        list->last = temp;
-        if(temp)
-            temp->next = UA_NULL;
-        list->size--;
-        if(list->size == 1)
-            list->first = temp;
-        else if(list->size == 0)
-            list->first = UA_NULL;
-    }
-    return UA_NO_ERROR;
-}
-
-UA_Int32 UA_list_removeElement(UA_list_Element *const elem, UA_list_PayloadVisitor visitor) {
-    if(elem == UA_NULL)
-        return UA_ERROR;
-    if(elem == elem->father->first)
-        return UA_list_removeFirst(elem->father, visitor);
-    else if(elem == elem->father->last)
-        return UA_list_removeLast(elem->father, visitor);
-    else{
-        UA_list_Element *prev = elem->prev;
-        UA_list_Element *next = elem->next;
-        prev->next = next;
-        next->prev = prev;
-        if(visitor)
-            (*visitor)(elem->payload);
-        (elem->father)->size--;
-        UA_free(elem);
-    }
-    return UA_NO_ERROR;
-}
-
-UA_Int32 UA_list_destroy(UA_list_List *list, UA_list_PayloadVisitor visitor) {
-    UA_list_Element *current = UA_NULL;
-    if(list == UA_NULL)
-        return UA_ERROR;
-    current = list->first;
-    while(current) {
-        UA_list_Element *next = current->next;
-        if(visitor)
-            (*visitor)(current->payload);
-        UA_free(current);
-        current = next;
-    }
-    UA_list_init(list);
-    return UA_NO_ERROR;
-}
-
-UA_Int32 UA_list_iterateElement(UA_list_List *const list, UA_list_ElementVisitor visitor) {
-    UA_list_Element *current;
-    UA_list_Element *next = UA_NULL;
-    if(list == UA_NULL)
-        return UA_ERROR;
-    current = list->first;
-    while(current) {
-        next = current->next;
-        if(visitor)
-            (*visitor)(current);
-        current = next;
-    }
-    return UA_NO_ERROR;
-}
-
-/*Int32 UA_list_iteratePayload(UA_list_list* const list, UA_payloadVisitor visitor){
-    void visitorTemp(UA_list_element* element){
-        if(visitor){
-            (*visitor)(element->payload);
-        }
-    }
-    if(list==NULL)return UA_ERROR;
-    UA_list_iterateElement(list, visitorTemp);
-    return UA_NO_ERROR;
-   }*/
-/** ANSI C forbids function nesting - reworked ugly version **/
-UA_Int32 UA_list_iteratePayload(UA_list_List *const list, UA_list_PayloadVisitor visitor) {
-    UA_list_Element *current;
-    UA_list_Element *next = UA_NULL;
-    if(list == UA_NULL)
-        return UA_ERROR;
-    current = list->first;
-    while(current) {
-        next = current->next;
-        if(visitor)
-            (*visitor)(current->payload);
-        current = next;
-    }
-    return UA_NO_ERROR;
-}
-
-UA_list_Element *UA_list_find(UA_list_List *const list, UA_list_PayloadMatcher matcher) {
-    if(list == UA_NULL) return UA_NULL;
-    if(matcher) {
-        UA_list_Element *current = list->first;
-        while(current) {
-            if(matcher && (*matcher)(current->payload) == UA_TRUE)
-                return current;
-            current = current->next;
-        }
-    }
-    /* nothing found */
-    return UA_NULL;
-}
-
-UA_list_Element *UA_list_search(UA_list_List *const list, UA_list_PayloadComparer compare, void *payload) {
-    if(list == UA_NULL) return UA_NULL;
-    if(compare) {
-        UA_list_Element *current = list->first;
-        while(current) {
-            if(compare && (*compare)(current->payload, payload) == UA_TRUE)
-                return current;
-            current = current->next;
-        }
-    }
-    /* nothing found */
-    return UA_NULL;
-}
-
-UA_list_Element *UA_list_getFirst(UA_list_List *const list) {
-    if(list == UA_NULL) return UA_NULL;
-    return list->first;
-}
-
-UA_list_Element *UA_list_getLast(UA_list_List *const list) {
-    if(list == UA_NULL) return UA_NULL;
-    return list->last;
-}

+ 0 - 65
src/util/ua_list.h

@@ -1,65 +0,0 @@
-#ifndef UA_LIST_H_
-#define UA_LIST_H_
-
-#include "ua_types.h"
-
-/**********************/
-/* Doubly Linked List */
-/**********************/
-
-typedef void (*UA_list_PayloadVisitor)(void *payload);
-
-typedef struct UA_list_Element {
-    struct UA_list_List    *father;
-    void *payload;
-    struct UA_list_Element *next;
-    struct UA_list_Element *prev;
-}UA_list_Element;
-
-typedef struct UA_list_List {
-    struct UA_list_Element *first;
-    struct UA_list_Element *last;
-    UA_Int32 size;
-}UA_list_List;
-
-typedef void (*UA_list_ElementVisitor)(UA_list_Element *payload);
-
-//typedef Boolean (*UA_list_PayloadMatcher)(void* payload);
-typedef _Bool (*UA_list_PayloadMatcher)(void *payload);
-typedef _Bool (*UA_list_PayloadComparer)(void *payload, void *otherPayload);
-
-void UA_list_defaultFreer(void *payload);
-
-UA_Int32 UA_list_initElement(UA_list_Element *element);
-
-UA_Int32 UA_list_init(UA_list_List *list);
-
-UA_Int32 UA_list_addElementToFront(UA_list_List *list, UA_list_Element *element);
-
-UA_Int32 UA_list_addPayloadToFront(UA_list_List *list, void *const payload);
-
-UA_Int32 UA_list_addElementToBack(UA_list_List *list, UA_list_Element *element);
-
-UA_Int32 UA_list_addPayloadToBack(UA_list_List *list, void *const payload);
-
-UA_Int32 UA_list_removeFirst(UA_list_List *list, UA_list_PayloadVisitor visitor);
-
-UA_Int32 UA_list_removeLast(UA_list_List *list, UA_list_PayloadVisitor visitor);
-
-UA_Int32 UA_list_removeElement(UA_list_Element *const elem, UA_list_PayloadVisitor visitor);
-
-UA_Int32 UA_list_destroy(UA_list_List *list, UA_list_PayloadVisitor visitor);
-
-UA_Int32 UA_list_iterateElement(UA_list_List *const list, UA_list_ElementVisitor visitor);
-
-UA_Int32 UA_list_iteratePayload(UA_list_List *const list, UA_list_PayloadVisitor visitor);
-
-UA_list_Element *UA_list_find(UA_list_List *const list, UA_list_PayloadMatcher matcher);
-
-UA_list_Element *UA_list_search(UA_list_List *const list, UA_list_PayloadComparer compare, void *payload);
-
-UA_list_Element *UA_list_getFirst(UA_list_List *const list);
-
-UA_list_Element *UA_list_getLast(UA_list_List *const list);
-
-#endif /* UA_LIST_H_ */

+ 0 - 295
tests/check_list.c

@@ -1,295 +0,0 @@
-#include <stdlib.h> // EXIT_SUCCESS
-#include "util/ua_util.h"
-#include "util/ua_list.h"
-#include "check.h"
-
-/* global test counters */
-UA_Int32 visit_count = 0;
-UA_Int32 free_count = 0;
-
-void elementVisitor(UA_list_Element* payload){
-	visit_count++;
-}
-
-void visitor(void* payload){
-	visit_count++;
-}
-
-void freer(void* payload){
-	if(payload){
-		free_count++;
-		UA_free(payload);
-	}
-}
-
-_Bool matcher(void* payload){
-	if(payload == UA_NULL){
-		return UA_FALSE;
-	}
-	if(*((UA_Int32*)payload) == 42){
-		return UA_TRUE;
-	}
-	return UA_FALSE;
-}
-
-_Bool matcher2(void* payload){
-	if(payload == UA_NULL){
-		return UA_FALSE;
-	}
-	if(*((UA_Int32*)payload) == 43){
-		return UA_TRUE;
-	}
-	return UA_FALSE;
-}
-
-_Bool comparer(void* payload, void* otherPayload) {
-	if(payload == UA_NULL || otherPayload == UA_NULL){
-		return UA_FALSE;
-	} else {
-		return ( *((UA_Int32*)payload) == *((UA_Int32*)otherPayload) );
-	}
-}
-
-START_TEST(list_test_basic)
-{
-
-	UA_list_List list;
-	UA_list_init(&list);
-
-
-	ck_assert_int_eq(UA_list_addPayloadToFront(UA_NULL, UA_NULL), UA_ERROR);
-	ck_assert_int_eq(UA_list_addElementToBack(UA_NULL, UA_NULL), UA_ERROR);
-	ck_assert_int_eq(UA_list_addElementToFront(UA_NULL, UA_NULL), UA_ERROR);
-	ck_assert_int_eq(UA_list_addPayloadToBack(UA_NULL, UA_NULL), UA_ERROR);
-	ck_assert_int_eq(UA_list_removeFirst(UA_NULL, UA_NULL), UA_ERROR);
-	ck_assert_int_eq(UA_list_removeLast(UA_NULL, UA_NULL), UA_ERROR);
-	ck_assert_int_eq(UA_list_removeElement(UA_NULL, UA_NULL), UA_ERROR);
-	ck_assert_int_eq(UA_list_destroy(UA_NULL, UA_NULL), UA_ERROR);
-	ck_assert_int_eq(UA_list_iterateElement(UA_NULL, UA_NULL), UA_ERROR);
-	ck_assert_int_eq(UA_list_iteratePayload(UA_NULL, UA_NULL), UA_ERROR);
-
-	ck_assert_int_eq(list.size, 0);
-
-	UA_Int32* payload;
-	UA_alloc((void**)&payload, sizeof(*payload));
-	*payload = 42;
-	UA_list_addPayloadToFront(&list, payload);
-	UA_alloc((void**)&payload, sizeof(*payload));
-	*payload = 24;
-	UA_list_addPayloadToFront(&list, payload);
-	UA_alloc((void**)&payload, sizeof(*payload));
-	*payload = 1;
-	UA_list_addPayloadToBack(&list, payload);
-
-	ck_assert_int_eq(*(UA_Int32*)UA_list_getFirst(&list)->payload, 24);
-	ck_assert_int_eq(*(UA_Int32*)UA_list_getLast(&list)->payload, 1);
-
-	ck_assert_int_eq(list.size, 3);
-
-	visit_count = 0;
-	UA_list_iteratePayload(&list, visitor);
-	ck_assert_int_eq(visit_count, 3);
-
-	visit_count = 0;
-	UA_list_iterateElement(&list, elementVisitor);
-	ck_assert_int_eq(visit_count, 3);
-
-	UA_list_Element* elem = NULL;
-	elem = UA_list_find(&list, matcher);
-	if(elem){
-		ck_assert_int_eq((*((UA_Int32*)elem->payload)), 42);
-		UA_list_removeElement(elem, freer);
-		ck_assert_int_eq(free_count, 1);
-		free_count = 0; //reset free counter
-		ck_assert_int_eq(list.size, 2);
-	}else{
-		fail("Element 42 not found");
-	}
-
-	//search for a non-existent element
-	ck_assert_ptr_eq((UA_list_find(&list, matcher2)), UA_NULL);
-
-	UA_list_destroy(&list, freer);
-
-	ck_assert_int_eq(free_count, 2);
-
-	ck_assert_int_eq(list.size, 0);
-
-}
-END_TEST
-
-void myAddPayloadValueToFront(UA_list_List *list, UA_Int32 payloadValue) {
-	UA_Int32* payload;
-	UA_alloc((void**)&payload, sizeof(*payload));
-	*payload = payloadValue;
-	UA_list_addPayloadToFront(list, payload);
-}
-void myAddPayloadVectorToFront(UA_list_List *list, UA_Int32 payloadValues[], UA_Int32 n) {
-	UA_Int32 i = 0;
-	for (;i<n;i++) {
-		myAddPayloadValueToFront(list,payloadValues[i]);
-	}
-}
-
-START_TEST(addElementsShallResultInRespectiveSize)
-{
-	// given
-	UA_list_List list;
-	UA_list_init(&list);
-	// when
-	UA_Int32 plv[] = {42,24,1};
-	myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
-	// then
-	ck_assert_int_eq(list.size, 3);
-	// finally
-	UA_list_destroy(&list, freer);
-}
-END_TEST
-
-START_TEST(findElementShallFind42)
-{
-	// given
-	UA_list_List list;
-	UA_list_init(&list);
-	UA_Int32 plv[] = {42,24,1};
-	myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
-	// when
-	UA_list_Element* e = UA_list_find(&list,matcher);
-	// then
-	ck_assert_ptr_ne(e, UA_NULL);
-	ck_assert_int_eq(*(UA_Int32*)(e->payload), 42);
-	// finally
-	UA_list_destroy(&list, freer);
-}
-END_TEST
-
-START_TEST(searchElementShallFind24)
-{
-	// given
-	UA_list_List list;
-	UA_list_init(&list);
-	UA_Int32 plv[] = {42,24,1};
-	myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
-	UA_Int32 payload = 24;
-	// when
-	UA_list_Element* e = UA_list_search(&list,comparer,(void*)&payload);
-	// then
-	ck_assert_ptr_ne(e, UA_NULL);
-	ck_assert_int_eq(*(UA_Int32*)(e->payload), 24);
-	// finally
-	UA_list_destroy(&list, freer);
-}
-END_TEST
-
-START_TEST(addAndRemoveShallYieldEmptyList)
-{
-	// given
-	UA_list_List list;
-	UA_list_init(&list);
-	UA_Int32 plv[] = {42};
-	myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
-	// when
-	UA_list_Element* e = UA_list_search(&list,comparer,(void*)&plv[0]);
-	UA_list_removeElement(e,visitor);
-	ck_assert_int_eq(visit_count,1);
-	visit_count = 0;
-	UA_list_iteratePayload(&list,visitor);
-	// then
-	ck_assert_int_eq(list.size,0);
-	ck_assert_int_eq(visit_count,0);
-	// finally
-	UA_list_destroy(&list, freer);
-}
-END_TEST
-
-START_TEST(addAndRemoveShallYieldEmptyList2)
-{
-	// given
-	UA_list_List list;
-	UA_list_init(&list);
-	UA_Int32 plv[] = {42};
-	myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
-	// when
-	UA_list_search(&list,comparer,(void*)&plv[0]);
-	UA_list_removeLast(&list,visitor);  //additionally testing removeLast explicitly
-	ck_assert_int_eq(visit_count,1);
-	visit_count = 0;
-	UA_list_iterateElement(&list,elementVisitor); //additionally testing iterateElement
-	// then
-	ck_assert_int_eq(list.size,0);
-	ck_assert_int_eq(visit_count,0);
-	// finally
-	UA_list_destroy(&list, freer);
-}
-END_TEST
-
-START_TEST(addTwiceAndRemoveFirstShallYieldListWithOneElement)
-{
-	// given
-	UA_list_List list;
-	UA_list_init(&list);
-	UA_Int32 plv[] = {42,24};
-	myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
-	// when
-	UA_list_Element* e = UA_list_search(&list,comparer,(void*)&plv[0]);
-	UA_list_removeElement(e,UA_NULL);
-	visit_count = 0;
-	UA_list_iteratePayload(&list,visitor);
-	// then
-	ck_assert_int_eq(list.size,1);
-	ck_assert_int_eq(visit_count,1);
-	// finally
-	UA_list_destroy(&list, freer);
-}
-END_TEST
-
-START_TEST(addTwiceAndRemoveLastShallYieldListWithOneElement)
-{
-	// given
-	UA_list_List list;
-	UA_list_init(&list);
-	UA_Int32 plv[] = {42,24};
-	myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
-	// when
-	UA_list_Element* e = UA_list_search(&list,comparer,(void*)&plv[1]);
-	UA_list_removeElement(e,UA_NULL);
-	visit_count = 0;
-	UA_list_iteratePayload(&list,visitor);
-	// then
-	ck_assert_int_eq(list.size,1);
-	ck_assert_int_eq(visit_count,1);
-	// finally
-	UA_list_destroy(&list, freer);
-}
-END_TEST
-
-Suite*list_testSuite(void)
-{
-	Suite *s = suite_create("list_test");
-	TCase *tc_core = tcase_create("Core");
-	tcase_add_test(tc_core, list_test_basic);
-	tcase_add_test(tc_core, addElementsShallResultInRespectiveSize);
-	tcase_add_test(tc_core, findElementShallFind42);
-	tcase_add_test(tc_core, searchElementShallFind24);
-	tcase_add_test(tc_core, addAndRemoveShallYieldEmptyList);
-	tcase_add_test(tc_core, addAndRemoveShallYieldEmptyList2);
-	tcase_add_test(tc_core, addTwiceAndRemoveFirstShallYieldListWithOneElement);
-	tcase_add_test(tc_core, addTwiceAndRemoveLastShallYieldListWithOneElement);
-	suite_add_tcase(s,tc_core);
-	return s;
-}
-
-int main (void)
-{
-	int number_failed = 0;
-
-	Suite* s = list_testSuite();
-	SRunner* sr = srunner_create(s);
-	srunner_run_all(sr,CK_NORMAL);
-	number_failed += srunner_ntests_failed(sr);
-	srunner_free(sr);
-
-	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
-
-}
-