- iterator for linked list implemented

- first test for iterator written
- linked list values capsulated into private struct
This commit is contained in:
Jan Hutter
2005-11-04 10:28:12 +00:00
parent 4afbee1e53
commit 7c2228f165
7 changed files with 413 additions and 95 deletions
+10 -3
View File
@@ -85,7 +85,7 @@ struct private_job_queue_s {
status_t get_count(private_job_queue_t *this, int *count) status_t get_count(private_job_queue_t *this, int *count)
{ {
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
*count = this->list->count; this->list->get_count(this->list,count);
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
return SUCCESS; return SUCCESS;
} }
@@ -95,13 +95,16 @@ status_t get_count(private_job_queue_t *this, int *count)
*/ */
status_t get(private_job_queue_t *this, job_t **job) status_t get(private_job_queue_t *this, job_t **job)
{ {
int count;
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
// add mutex unlock handler for cancellation // add mutex unlock handler for cancellation
pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex)); pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex));
// go to wait while no jobs available // go to wait while no jobs available
while(this->list->count == 0) this->list->get_count(this->list,&count);
while(count == 0)
{ {
pthread_cond_wait( &(this->condvar), &(this->mutex)); pthread_cond_wait( &(this->condvar), &(this->mutex));
this->list->get_count(this->list,&count);
} }
// remove mutex-unlock handler (without executing) // remove mutex-unlock handler (without executing)
pthread_cleanup_pop(0); pthread_cleanup_pop(0);
@@ -129,7 +132,10 @@ status_t add(private_job_queue_t *this, job_t *job)
*/ */
status_t job_queue_destroy (private_job_queue_t *this) status_t job_queue_destroy (private_job_queue_t *this)
{ {
while (this->list->count > 0) int count;
this->list->get_count(this->list,&count);
while (count > 0)
{ {
job_t *job; job_t *job;
if (this->list->remove_first(this->list,(void *) &job) != SUCCESS) if (this->list->remove_first(this->list,(void *) &job) != SUCCESS)
@@ -138,6 +144,7 @@ status_t job_queue_destroy (private_job_queue_t *this)
break; break;
} }
job->destroy(job); job->destroy(job);
this->list->get_count(this->list,&count);
} }
this->list->destroy(this->list); this->list->destroy(this->list);
+226 -54
View File
@@ -28,12 +28,36 @@
#include "linked_list.h" #include "linked_list.h"
typedef struct private_linked_list_element_s private_linked_list_element_t;
/**
* Private Data of a linked list element
*
*/
struct private_linked_list_element_s{
/**
* public data of element
*/
linked_list_element_t public;
/**
* previous list element
* NULL if first element in list
*/
private_linked_list_element_t *previous;
/**
* next list element
* NULL if last element in list
*/
private_linked_list_element_t *next;
};
/** /**
* @brief implements function destroy of linked_list_item_t * @brief implements function destroy of linked_list_item_t
*/ */
static status_t linked_list_element_destroy(linked_list_element_t *linked_list_element) static status_t linked_list_element_destroy(private_linked_list_element_t *this)
{ {
linked_list_element_t * this = linked_list_element;
if (this == NULL) if (this == NULL)
{ {
return FAILED; return FAILED;
@@ -47,30 +71,186 @@ static status_t linked_list_element_destroy(linked_list_element_t *linked_list_e
*/ */
linked_list_element_t *linked_list_element_create(void *value) linked_list_element_t *linked_list_element_create(void *value)
{ {
linked_list_element_t *this = alloc_thing(linked_list_element_t, "linked_list_element_t"); private_linked_list_element_t *this = alloc_thing(private_linked_list_element_t, "private_linked_list_element_t");
if (this == NULL) if (this == NULL)
{ {
return NULL; return NULL;
} }
this->destroy = linked_list_element_destroy; this->public.destroy = (status_t (*) (linked_list_element_t *this) ) linked_list_element_destroy;
this->previous=NULL; this->previous=NULL;
this->next=NULL; this->next=NULL;
this->value = value; this->public.value = value;
return this; return (&this->public);
} }
/**
* Private variables and functions of linked list
*
*/
typedef struct private_linked_list_s private_linked_list_t;
struct private_linked_list_s{
/**
* Public part of linked list
*/
linked_list_t public;
/**
* number of items in the list
*/
int count;
/**
* First element in list
* NULL if no elements in list
*/
private_linked_list_element_t *first;
/**
* Last element in list
* NULL if no elements in list
*/
private_linked_list_element_t *last;
};
/**
* Private variables and functions of linked list iterator
*
*/
typedef struct private_linked_list_iterator_s private_linked_list_iterator_t;
struct private_linked_list_iterator_s{
/**
* Public part of linked list iterator
*/
linked_list_iterator_t public;
/**
* associated linked list
*/
private_linked_list_t * list;
/**
* current element of the iterator
*/
private_linked_list_element_t *current;
/**
* direction of iterator
*/
bool forward;
};
/**
* Implements function has_next of linked_list_iteratr
*/
static status_t iterator_has_next(private_linked_list_iterator_t *this, bool * has_next)
{
if (this->list->count == 0)
{
*has_next = FALSE;
return SUCCESS;
}
if (this->current == NULL)
{
this->current = (this->forward) ? this->list->first : this->list->last;
*has_next = TRUE;
return SUCCESS;
}
if (this->forward)
{
if (this->current->next == NULL)
{
*has_next = FALSE;
return SUCCESS;
}
this->current = this->current->next;
*has_next = TRUE;
return SUCCESS;
}
/* backward */
if (this->current->previous == NULL)
{
*has_next = FALSE;
return SUCCESS;
}
this->current = this->current->previous;
*has_next = TRUE;
return SUCCESS;
}
/**
* Implements function current of linked_list_iteratr
*/
static status_t iterator_current(private_linked_list_iterator_t *this, linked_list_element_t **element)
{
*element = &(this->current->public);
return SUCCESS;
}
/**
* Implements function current of linked_list_iteratr
*/
static status_t iterator_reset(private_linked_list_iterator_t *this)
{
this->current = NULL;
return SUCCESS;
}
/**
* Implements function destroy of linked_list_iteratr
*/
static status_t iterator_destroy(private_linked_list_iterator_t *this)
{
pfree(this);
return SUCCESS;
}
/**
* @brief implements function get_count of linked_list_t
*/
static status_t get_count(private_linked_list_t *this, int *count)
{
*count = this->count;
return SUCCESS;
}
static status_t create_iterator (private_linked_list_t *linked_list, linked_list_iterator_t **iterator,bool forward)
{
private_linked_list_iterator_t *this = alloc_thing(private_linked_list_iterator_t, "private_linked_list_iterator_t");
if (this == NULL)
{
return FAILED;
}
this->public.has_next = (status_t (*) (linked_list_iterator_t *this, bool * has_next)) iterator_has_next;
this->public.current = (status_t (*) (linked_list_iterator_t *this, linked_list_element_t **element)) iterator_current;
this->public.reset = (status_t (*) (linked_list_iterator_t *this)) iterator_reset;
this->public.destroy = (status_t (*) (linked_list_iterator_t *this)) iterator_destroy;
this->forward = forward;
this->current = NULL;
this->list = linked_list;
*iterator = &(this->public);
return (SUCCESS);
}
/** /**
* @brief implements function insert_first of linked_list_t * @brief implements function insert_first of linked_list_t
*/ */
static status_t insert_first(linked_list_t *linked_list, void *item) static status_t insert_first(private_linked_list_t *this, void *item)
{ {
linked_list_t *this = linked_list; private_linked_list_element_t *element =(private_linked_list_element_t *) linked_list_element_create(item);
linked_list_element_t *element = linked_list_element_create(item);
if (element == NULL) if (element == NULL)
{ {
@@ -90,11 +270,12 @@ static status_t insert_first(linked_list_t *linked_list, void *item)
if ((this->first == NULL) || (this->last == NULL)) if ((this->first == NULL) || (this->last == NULL))
{ {
/* should never happen */ /* should never happen */
element->destroy(element); element->public.destroy(&(element->public));
return FAILED; return FAILED;
} }
linked_list_element_t *old_first_element = this->first; private_linked_list_element_t *old_first_element = this->first;
element->next = old_first_element; element->next = old_first_element;
element->previous = NULL;
old_first_element->previous = element; old_first_element->previous = element;
this->first = element; this->first = element;
} }
@@ -107,10 +288,8 @@ static status_t insert_first(linked_list_t *linked_list, void *item)
/** /**
* @brief implements function remove_first of linked_list_t * @brief implements function remove_first of linked_list_t
*/ */
static status_t remove_first(linked_list_t *linked_list, void **item) static status_t remove_first(private_linked_list_t *this, void **item)
{ {
linked_list_t *this = linked_list;
if (this->count == 0) if (this->count == 0)
{ {
return FAILED; return FAILED;
@@ -121,7 +300,7 @@ static status_t remove_first(linked_list_t *linked_list, void **item)
return FAILED; return FAILED;
} }
linked_list_element_t *element = this->first; private_linked_list_element_t *element = this->first;
if (element->next != NULL) if (element->next != NULL)
{ {
@@ -129,20 +308,18 @@ static status_t remove_first(linked_list_t *linked_list, void **item)
} }
this->first = element->next; this->first = element->next;
*item = element->value; *item = element->public.value;
this->count--; this->count--;
return (element->destroy(element)); return (element->public.destroy(&(element->public)));
} }
/** /**
* @brief implements function get_first of linked_list_t * @brief implements function get_first of linked_list_t
*/ */
static status_t get_first(linked_list_t *linked_list, void **item) static status_t get_first(private_linked_list_t *this, void **item)
{ {
linked_list_t *this = linked_list;
if (this->count == 0) if (this->count == 0)
{ {
return FAILED; return FAILED;
@@ -153,7 +330,7 @@ static status_t get_first(linked_list_t *linked_list, void **item)
return FAILED; return FAILED;
} }
*item = this->first->value; *item = this->first->public.value;
return SUCCESS; return SUCCESS;
} }
@@ -161,11 +338,9 @@ static status_t get_first(linked_list_t *linked_list, void **item)
/** /**
* @brief implements function insert_last of linked_list_t * @brief implements function insert_last of linked_list_t
*/ */
static status_t insert_last(linked_list_t *linked_list, void *item) static status_t insert_last(private_linked_list_t *this, void *item)
{ {
linked_list_t *this = linked_list; private_linked_list_element_t *element = (private_linked_list_element_t *) linked_list_element_create(item);
linked_list_element_t *element = linked_list_element_create(item);
if (element == NULL) if (element == NULL)
{ {
@@ -184,11 +359,12 @@ static status_t insert_last(linked_list_t *linked_list, void *item)
if ((this->first == NULL) || (this->last == NULL)) if ((this->first == NULL) || (this->last == NULL))
{ {
/* should never happen */ /* should never happen */
element->destroy(element); element->public.destroy(&(element->public));
return FAILED; return FAILED;
} }
linked_list_element_t *old_last_element = this->last; private_linked_list_element_t *old_last_element = this->last;
element->previous = old_last_element; element->previous = old_last_element;
element->next = NULL;
old_last_element->next = element; old_last_element->next = element;
this->last = element; this->last = element;
} }
@@ -201,10 +377,8 @@ static status_t insert_last(linked_list_t *linked_list, void *item)
/** /**
* @brief implements function remove_last of linked_list_t * @brief implements function remove_last of linked_list_t
*/ */
static status_t remove_last(linked_list_t *linked_list, void **item) static status_t remove_last(private_linked_list_t *this, void **item)
{ {
linked_list_t *this = linked_list;
if (this->count == 0) if (this->count == 0)
{ {
return FAILED; return FAILED;
@@ -215,7 +389,7 @@ static status_t remove_last(linked_list_t *linked_list, void **item)
return FAILED; return FAILED;
} }
linked_list_element_t *element = this->last; private_linked_list_element_t *element = this->last;
if (element->previous != NULL) if (element->previous != NULL)
{ {
@@ -223,20 +397,18 @@ static status_t remove_last(linked_list_t *linked_list, void **item)
} }
this->last = element->previous; this->last = element->previous;
*item = element->value; *item = element->public.value;
this->count--; this->count--;
return (element->destroy(element)); return (element->public.destroy(&(element->public)));
} }
/** /**
* @brief implements function get_last of linked_list_t * @brief implements function get_last of linked_list_t
*/ */
static status_t get_last(linked_list_t *linked_list, void **item) static status_t get_last(private_linked_list_t *this, void **item)
{ {
linked_list_t *this = linked_list;
if (this->count == 0) if (this->count == 0)
{ {
return FAILED; return FAILED;
@@ -247,7 +419,7 @@ static status_t get_last(linked_list_t *linked_list, void **item)
return FAILED; return FAILED;
} }
*item = this->last->value; *item = this->last->public.value;
return SUCCESS; return SUCCESS;
} }
@@ -255,17 +427,15 @@ static status_t get_last(linked_list_t *linked_list, void **item)
/** /**
* @brief implements function destroy of linked_list_t * @brief implements function destroy of linked_list_t
*/ */
static status_t linked_list_destroy(linked_list_t *linked_list) static status_t linked_list_destroy(private_linked_list_t *this)
{ {
linked_list_t *this = linked_list;
/* Remove all list items before destroying list */ /* Remove all list items before destroying list */
while (this->count > 0) while (this->count > 0)
{ {
void * value; void * value;
/* values are not destroyed so memory leaks are possible /* values are not destroyed so memory leaks are possible
* if list is not empty when deleting */ * if list is not empty when deleting */
if (this->remove_first(this,&value) != SUCCESS) if (this->public.remove_first(&(this->public),&value) != SUCCESS)
{ {
pfree(this); pfree(this);
return FAILED; return FAILED;
@@ -280,19 +450,21 @@ static status_t linked_list_destroy(linked_list_t *linked_list)
*/ */
linked_list_t *linked_list_create() linked_list_t *linked_list_create()
{ {
linked_list_t *this = alloc_thing(linked_list_t, "linked_list_t"); private_linked_list_t *this = alloc_thing(private_linked_list_t, "private_linked_list_t");
this->get_first = get_first; this->public.get_count = (status_t (*) (linked_list_t *linked_list, int *count)) get_count;
this->get_last = get_last; this->public.create_iterator = (status_t (*) (linked_list_t *linked_list, linked_list_iterator_t **iterator,bool forward)) create_iterator;
this->insert_first = insert_first; this->public.get_first = (status_t (*) (linked_list_t *linked_list, void **item)) get_first;
this->insert_last = insert_last; this->public.get_last = (status_t (*) (linked_list_t *linked_list, void **item)) get_last;
this->remove_first = remove_first; this->public.insert_first = (status_t (*) (linked_list_t *linked_list, void *item))insert_first;
this->remove_last = remove_last; this->public.insert_last = (status_t (*) (linked_list_t *linked_list, void *item))insert_last;
this->destroy = linked_list_destroy; this->public.remove_first = (status_t (*) (linked_list_t *linked_list, void **item)) remove_first;
this->public.remove_last = (status_t (*) (linked_list_t *linked_list, void **item)) remove_last;
this->public.destroy = (status_t (*) (linked_list_t *linked_list)) linked_list_destroy;
this->count = 0; this->count = 0;
this->first = NULL; this->first = NULL;
this->last = NULL; this->last = NULL;
return this; return (&(this->public));
} }
+69 -22
View File
@@ -23,6 +23,10 @@
#ifndef LINKED_LIST_H_ #ifndef LINKED_LIST_H_
#define LINKED_LIST_H_ #define LINKED_LIST_H_
#include <freeswan.h>
#include <pluto/constants.h>
#include <pluto/defs.h>
#include "types.h" #include "types.h"
/** /**
@@ -33,16 +37,7 @@
typedef struct linked_list_element_s linked_list_element_t; typedef struct linked_list_element_s linked_list_element_t;
struct linked_list_element_s { struct linked_list_element_s {
/**
* previous list element
* NULL if first element in list
*/
linked_list_element_t *previous;
/**
* next list element
* NULL if last element in list
*/
linked_list_element_t *next;
/** /**
* value of a list item * value of a list item
*/ */
@@ -68,6 +63,51 @@ struct linked_list_element_s {
*/ */
linked_list_element_t *linked_list_element_create(void *value); linked_list_element_t *linked_list_element_create(void *value);
/**
* @brief Iterator for a linked list
*
* This element holds a pointer to the current element in the linked list
*
* @warning the iterator is NOT thread-save
*/
typedef struct linked_list_iterator_s linked_list_iterator_t;
struct linked_list_iterator_s {
/**
* @brief returns TRUE if more elements are available
*
* @param this calling object
* @param[out] has_next if more elements are avaiable TRUE is set, FALSE otherwise
* @returns SUCCESS if succeeded, FAILED otherwise
*/
status_t (*has_next) (linked_list_iterator_t *this, bool * has_next);
/**
* @brief returns the current element at the iterator position
*
* @param this calling object
* @param[out] element element is set to the current element in iterator
* @returns SUCCESS if succeeded, FAILED otherwise
*/
status_t (*current) (linked_list_iterator_t *this, linked_list_element_t **element);
/**
* @brief Resets a linked_list_iterator object
*
* @param this calling object
* @returns SUCCESS if succeeded, FAILED otherwise
*/
status_t (*reset) (linked_list_iterator_t *this);
/**
* @brief Destroys a linked_list_iterator object
*
* @param this calling object
* @returns SUCCESS if succeeded, FAILED otherwise
*/
status_t (*destroy) (linked_list_iterator_t *this);
};
/** /**
* @brief Double Linked List (named only as linked list) * @brief Double Linked List (named only as linked list)
@@ -82,21 +122,28 @@ typedef struct linked_list_s linked_list_t;
struct linked_list_s { struct linked_list_s {
/** /**
* number of items in the list * @brief gets the count of items in the list
*
* @param linked_list calling object
* @param[in] count place where the count is written
* @returns SUCCESS if succeeded, FAILED otherwise
*/ */
int count; status_t (*get_count) (linked_list_t *linked_list, int *count);
/**
* First element in list
* NULL if no elements in list
*/
linked_list_element_t *first;
/**
* Last element in list
* NULL if no elements in list
*/
linked_list_element_t *last;
/**
* @brief creates a iterator for the given list
*
* @warning has to get destroyed
*
* @param linked_list calling object
* @param[out] iterator place where the iterator is written
* @param[in] forward iterator direction (TRUE: front to end)
* @returns SUCCESS if succeeded, FAILED otherwise
*/
status_t (*create_iterator) (linked_list_t *linked_list, linked_list_iterator_t **iterator,bool forward);
/** /**
* @brief inserts a new item at the beginning of the list * @brief inserts a new item at the beginning of the list
* *
+4
View File
@@ -78,6 +78,10 @@ static status_t test_all(tester_t *tester,test_t **tests)
* @param end_time end time * @param end_time end time
* @param start_time start time * @param start_time start time
* *
* @warning this function is also defined in the event queue
* in later improvements, this function can be added to a general
* class type!
*
* @return difference in microseconds * @return difference in microseconds
*/ */
static long time_difference(struct timeval *end_time, struct timeval *start_time) static long time_difference(struct timeval *end_time, struct timeval *start_time)
+83 -14
View File
@@ -31,55 +31,124 @@
void test_linked_list(tester_t *tester) void test_linked_list(tester_t *tester)
{ {
void *test_value = NULL; void *test_value = NULL;
int count;
linked_list_t *linked_list = linked_list_create(); linked_list_t *linked_list = linked_list_create();
tester->assert_true(tester,(linked_list->count == 0), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 0), "count check");
linked_list->insert_first(linked_list,"one"); linked_list->insert_first(linked_list,"one");
tester->assert_true(tester,(linked_list->count == 1), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 1), "count check");
linked_list->insert_first(linked_list,"two"); linked_list->insert_first(linked_list,"two");
tester->assert_true(tester,(linked_list->count == 2), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 2), "count check");
linked_list->insert_first(linked_list,"three"); linked_list->insert_first(linked_list,"three");
tester->assert_true(tester,(linked_list->count == 3), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 3), "count check");
linked_list->insert_first(linked_list,"four"); linked_list->insert_first(linked_list,"four");
tester->assert_true(tester,(linked_list->count == 4), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 4), "count check");
linked_list->insert_first(linked_list,"five"); linked_list->insert_first(linked_list,"five");
tester->assert_true(tester,(linked_list->count == 5), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 5), "count check");
tester->assert_true(tester,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check"); tester->assert_true(tester,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
tester->assert_true(tester,(strcmp((char *) test_value,"five") == 0), "get_first value check"); tester->assert_true(tester,(strcmp((char *) test_value,"five") == 0), "get_first value check");
tester->assert_true(tester,(linked_list->count == 5), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 5), "count check");
tester->assert_true(tester,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check"); tester->assert_true(tester,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
tester->assert_true(tester,(strcmp((char *) test_value,"one") == 0), "get_last value check"); tester->assert_true(tester,(strcmp((char *) test_value,"one") == 0), "get_last value check");
tester->assert_true(tester,(linked_list->count == 5), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 5), "count check");
tester->assert_true(tester,(linked_list->remove_first(linked_list,&test_value) == SUCCESS), "remove_first call check"); tester->assert_true(tester,(linked_list->remove_first(linked_list,&test_value) == SUCCESS), "remove_first call check");
tester->assert_true(tester,(strcmp((char *) test_value,"five") == 0), "remove_first value check"); tester->assert_true(tester,(strcmp((char *) test_value,"five") == 0), "remove_first value check");
tester->assert_true(tester,(linked_list->count == 4), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 4), "count check");
tester->assert_true(tester,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check"); tester->assert_true(tester,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
tester->assert_true(tester,(strcmp((char *) test_value,"four") == 0), "get_first value check"); tester->assert_true(tester,(strcmp((char *) test_value,"four") == 0), "get_first value check");
tester->assert_true(tester,(linked_list->count == 4), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 4), "count check");
tester->assert_true(tester,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check"); tester->assert_true(tester,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
tester->assert_true(tester,(strcmp((char *) test_value,"one") == 0), "get_last value check"); tester->assert_true(tester,(strcmp((char *) test_value,"one") == 0), "get_last value check");
tester->assert_true(tester,(linked_list->count == 4), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 4), "count check");
tester->assert_true(tester,(linked_list->remove_last(linked_list,&test_value) == SUCCESS), "remove_last call check"); tester->assert_true(tester,(linked_list->remove_last(linked_list,&test_value) == SUCCESS), "remove_last call check");
tester->assert_true(tester,(strcmp((char *) test_value,"one") == 0), "remove_last value check"); tester->assert_true(tester,(strcmp((char *) test_value,"one") == 0), "remove_last value check");
tester->assert_true(tester,(linked_list->count == 3), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 3), "count check");
tester->assert_true(tester,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check"); tester->assert_true(tester,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
tester->assert_true(tester,(strcmp((char *) test_value,"two") == 0), "get_last value check"); tester->assert_true(tester,(strcmp((char *) test_value,"two") == 0), "get_last value check");
tester->assert_true(tester,(linked_list->count == 3), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 3), "count check");
tester->assert_true(tester,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check"); tester->assert_true(tester,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
tester->assert_true(tester,(strcmp((char *) test_value,"four") == 0), "get_first value check"); tester->assert_true(tester,(strcmp((char *) test_value,"four") == 0), "get_first value check");
tester->assert_true(tester,(linked_list->count == 3), "count check"); linked_list->get_count(linked_list,&count);
tester->assert_true(tester,(count == 3), "count check");
tester->assert_true(tester,(linked_list->destroy(linked_list) == SUCCESS), "destroy call check"); tester->assert_true(tester,(linked_list->destroy(linked_list) == SUCCESS), "destroy call check");
} }
/*
* Description in header-file
*/
void test_linked_list_forward_iterator(tester_t *tester)
{
bool has_next;
linked_list_element_t * element;
linked_list_t *linked_list = linked_list_create();
linked_list->insert_first(linked_list,"one");
linked_list->insert_first(linked_list,"two");
linked_list->insert_first(linked_list,"three");
linked_list->insert_first(linked_list,"four");
linked_list->insert_first(linked_list,"five");
linked_list_iterator_t * iterator;
tester->assert_true(tester,(linked_list->create_iterator(linked_list,&iterator,TRUE) == SUCCESS), "create_iterator call check");
iterator->has_next(iterator,&has_next);
tester->assert_true(tester,has_next, "has_next value check");
iterator->current(iterator,&element);
tester->assert_true(tester,(strcmp((char *) element->value,"five") == 0), "current value check");
iterator->has_next(iterator,&has_next);
tester->assert_true(tester,has_next, "has_next value check");
iterator->current(iterator,&element);
tester->assert_true(tester,(strcmp((char *) element->value,"four") == 0), "current value check");
iterator->has_next(iterator,&has_next);
tester->assert_true(tester,has_next, "has_next value check");
iterator->current(iterator,&element);
tester->assert_true(tester,(strcmp((char *) element->value,"three") == 0), "current value check");
iterator->has_next(iterator,&has_next);
tester->assert_true(tester,has_next, "has_next value check");
iterator->current(iterator,&element);
tester->assert_true(tester,(strcmp((char *) element->value,"two") == 0), "current value check");
iterator->has_next(iterator,&has_next);
tester->assert_true(tester,has_next, "has_next value check");
iterator->current(iterator,&element);
tester->assert_true(tester,(strcmp((char *) element->value,"one") == 0), "current value check");
iterator->has_next(iterator,&has_next);
tester->assert_false(tester,has_next, "has_next value check");
tester->assert_true(tester,(iterator->destroy(iterator) == SUCCESS), "destroy call check");
linked_list->destroy(linked_list);
}
+18
View File
@@ -36,9 +36,27 @@
*/ */
void test_linked_list(tester_t *tester); void test_linked_list(tester_t *tester);
/**
* @brief Test function for the type linked_list_t and its forward iterator
*
* Performs different kinds of assertions to check the functionality
* of the linked_list_t and its iterator in a Single-Threaded environment.
*
* @warning To be usable in multi-threaded software
* this list has to get protected with locks.
*
* @param tester tester object
*/
void test_linked_list_forward_iterator(tester_t *tester);
/** /**
* Test for linked_list_t * Test for linked_list_t
*/ */
test_t linked_list_test = {test_linked_list,"Linked List"}; test_t linked_list_test = {test_linked_list,"Linked List"};
/**
* Test for linked_list_t with iterator
*/
test_t linked_list_forward_iterator_test = {test_linked_list_forward_iterator,"Linked List Forward Iterator"};
#endif /*LINKED_LIST_TEST_H_*/ #endif /*LINKED_LIST_TEST_H_*/
+3 -2
View File
@@ -36,8 +36,9 @@
*/ */
test_t *tests[] ={ test_t *tests[] ={
&linked_list_test, &linked_list_test,
&thread_pool_test, &linked_list_forward_iterator_test,
&job_queue_test1, // &thread_pool_test,
// &job_queue_test1,
NULL NULL
}; };