Merge pull request #7 from llyzs/list

Add double-linked list in utils
This commit is contained in:
Marc-André Moreau 2011-07-07 10:00:48 -07:00
commit 31b36d9274
5 changed files with 228 additions and 0 deletions

View File

@ -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

View File

@ -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();

85
cunit/test_list.c Normal file
View File

@ -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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <freerdp/freerdp.h>
#include <freerdp/utils/list.h>
#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);
}

26
cunit/test_list.h Normal file
View File

@ -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);

View File

@ -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