From 85baf1338eade33339e3b279f311e2837819517f Mon Sep 17 00:00:00 2001 From: Vic Lee Date: Fri, 8 Jul 2011 00:50:45 +0800 Subject: [PATCH 1/2] libfreerdp-utils: add double-linked list utils. --- include/freerdp/utils/list.h | 109 +++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 include/freerdp/utils/list.h diff --git a/include/freerdp/utils/list.h b/include/freerdp/utils/list.h new file mode 100644 index 000000000..37f3b69aa --- /dev/null +++ b/include/freerdp/utils/list.h @@ -0,0 +1,109 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * Double-linked List Utils + * + * Copyright 2011 Vic Lee + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __LIST_UTILS_H +#define __LIST_UTILS_H + +#define DEFINE_LIST_TYPE(_list_type, _item_type) \ +\ +struct _item_type##_full \ +{ \ + struct _item_type item; \ + struct _item_type* prev; \ + struct _item_type* next; \ +}; \ +\ +static struct _item_type* _item_type##_new(void) \ +{ \ + struct _item_type* item; \ + item = (struct _item_type*)malloc(sizeof(struct _item_type##_full));\ + memset(item, 0, sizeof(struct _item_type##_full)); \ + return item; \ +} \ +\ +static void _item_type##_free(struct _item_type* item); \ +\ +static struct _item_type* _item_type##_next(struct _item_type* item) \ +{ \ + return ((struct _item_type##_full*)item)->next; \ +} \ +\ +static struct _item_type* _item_type##_prev(struct _item_type* item) \ +{ \ + return ((struct _item_type##_full*)item)->prev; \ +} \ +\ +struct _list_type \ +{ \ + struct _item_type* head; \ + struct _item_type* tail; \ +}; \ +\ +static struct _list_type* _list_type##_new(void) \ +{ \ + struct _list_type* list; \ + list = (struct _list_type*)malloc(sizeof(struct _list_type)); \ + memset(list, 0, sizeof(struct _list_type)); \ + return list; \ +} \ +\ +static void _list_type##_enqueue(struct _list_type* list, struct _item_type* item) \ +{ \ + if (list->tail == NULL) \ + { \ + list->head = item; \ + list->tail = item; \ + } \ + else \ + { \ + ((struct _item_type##_full*)item)->prev = list->tail; \ + ((struct _item_type##_full*)(list->tail))->next = item; \ + list->tail = item; \ + } \ +} \ +\ +static struct _item_type* _list_type##_dequeue(struct _list_type* list) \ +{ \ + struct _item_type* item; \ + item = list->head; \ + if (item != NULL) \ + { \ + list->head = ((struct _item_type##_full*)item)->next; \ + ((struct _item_type##_full*)item)->next = NULL; \ + if (list->head == NULL) \ + list->tail = NULL; \ + else \ + ((struct _item_type##_full*)(list->head))->prev = NULL; \ + } \ + return item; \ +} \ +\ +void _list_type##_free(struct _list_type* list) \ +{ \ + struct _item_type* item; \ + while (list->head) \ + { \ + item = _list_type##_dequeue(list); \ + _item_type##_free(item); \ + free(item); \ + } \ + free(list); \ +} + +#endif From e4a5c982ef2deefa71bf88472244f7644a31ca5f Mon Sep 17 00:00:00 2001 From: Vic Lee Date: Fri, 8 Jul 2011 00:51:24 +0800 Subject: [PATCH 2/2] cunit: add unit tests for double-linked list. --- cunit/CMakeLists.txt | 2 ++ cunit/test_freerdp.c | 6 ++++ cunit/test_list.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ cunit/test_list.h | 26 ++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 cunit/test_list.c create mode 100644 cunit/test_list.h diff --git a/cunit/CMakeLists.txt b/cunit/CMakeLists.txt index ef3f0e25b..c96a21254 100644 --- a/cunit/CMakeLists.txt +++ b/cunit/CMakeLists.txt @@ -33,6 +33,8 @@ add_executable(test_freerdp test_color.h test_libgdi.c test_libgdi.h + test_list.c + test_list.h test_stream.c test_stream.h test_transport.c diff --git a/cunit/test_freerdp.c b/cunit/test_freerdp.c index 1d1926e52..42957ec97 100644 --- a/cunit/test_freerdp.c +++ b/cunit/test_freerdp.c @@ -24,6 +24,7 @@ #include "test_gcc.h" #include "test_color.h" #include "test_libgdi.h" +#include "test_list.h" #include "test_stream.h" #include "test_transport.h" #include "test_freerdp.h" @@ -110,6 +111,7 @@ int main(int argc, char* argv[]) add_ber_suite(); add_color_suite(); add_libgdi_suite(); + add_list_suite(); add_stream_suite(); add_transport_suite(); } @@ -125,6 +127,10 @@ int main(int argc, char* argv[]) { add_libgdi_suite(); } + else if (strcmp("list", argv[*pindex]) == 0) + { + add_list_suite(); + } else if (strcmp("stream", argv[*pindex]) == 0) { add_stream_suite(); diff --git a/cunit/test_list.c b/cunit/test_list.c new file mode 100644 index 000000000..13942917a --- /dev/null +++ b/cunit/test_list.c @@ -0,0 +1,85 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * List Unit Tests + * + * Copyright 2011 Vic Lee + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include "test_list.h" + +int init_list_suite(void) +{ + return 0; +} + +int clean_list_suite(void) +{ + return 0; +} + +int add_list_suite(void) +{ + add_test_suite(list); + + add_test_function(list); + + return 0; +} + +struct my_list_item +{ + uint32 a; + uint32 b; +}; + +DEFINE_LIST_TYPE(my_list, my_list_item); + +void my_list_item_free(struct my_list_item* item) +{ + item->a = 0; + item->b = 0; +} + +void test_list(void) +{ + struct my_list* list; + struct my_list_item* item; + int i; + + list = my_list_new(); + + for (i = 0; i < 10; i++) + { + item = my_list_item_new(); + item->a = i; + item->b = i * i; + my_list_enqueue(list, item); + } + + for (i = 0, item = list->head; item; i++, item = my_list_item_next(item)) + { + CU_ASSERT(item->a == i); + CU_ASSERT(item->b == i * i); + /*printf("%d %d\n", item->a, item->b);*/ + } + + my_list_free(list); +} diff --git a/cunit/test_list.h b/cunit/test_list.h new file mode 100644 index 000000000..01afbb02d --- /dev/null +++ b/cunit/test_list.h @@ -0,0 +1,26 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * List Unit Tests + * + * Copyright 2011 Vic Lee + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "test_freerdp.h" + +int init_list_suite(void); +int clean_list_suite(void); +int add_list_suite(void); + +void test_list(void);