Compare commits

...

9 Commits

11 changed files with 1603 additions and 457 deletions

View File

@ -17,33 +17,33 @@ static void demo_deque_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
struct _deque dq;
deque_init(&dq, sizeof(int));
dq.print_obj = print_num;
deque_t deque = deque_new();
deque_init(deque, sizeof(int));
deque->print_obj = print_num;
printf("\n\n----- demo_deque_num -----\n");
printf("----- after push_back -----\n");
for (i = 0; i < len; i++)
{
dq.push_back(&dq, &data[i]);
deque->push_back(deque, &data[i]);
dq.front(&dq, &temp);
deque->front(deque, &temp);
printf("front = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
dq.back(&dq, &temp);
deque->back(deque, &temp);
printf("\tback = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
printf("\tsize = %2d\n", dq.size(&dq));
printf("\tsize = %2d\n", deque->size(deque));
}
printf("----- print -----\n");
dq.print(&dq);
deque->print(deque);
printf("\n");
dq.clear(&dq);
if (dq.empty(&dq))
deque->clear(deque);
if (deque->empty(deque))
{
printf("----- empty -----\n");
}
@ -51,37 +51,37 @@ static void demo_deque_num(void)
printf("----- push_back -----\n");
for (i = 0; i < len; i++)
{
dq.push_back(&dq, &data[i]);
deque->push_back(deque, &data[i]);
}
printf("----- after pop_back -----\n");
for (i = 0; i < len + 1; i++)
{
if (true == dq.pop_back(&dq, &temp))
if (true == deque->pop_back(deque, &temp))
{
printf("pop = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
if (true == dq.front(&dq, &temp))
if (true == deque->front(deque, &temp))
{
printf("front = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
}
if (dq.back(&dq, &temp))
if (deque->back(deque, &temp))
{
printf("back = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
}
printf("size = %2d\n", dq.size(&dq));
printf("size = %2d\n", deque->size(deque));
}
else
{
printf("pop failed! because it is empty\n");
}
if (dq.empty(&dq))
if (deque->empty(deque))
{
printf("----- empty -----\n");
}
@ -90,24 +90,24 @@ static void demo_deque_num(void)
printf("----- after push_front -----\n");
for (i = 0; i < len; i++)
{
dq.push_front(&dq, &data[i]);
deque->push_front(deque, &data[i]);
dq.front(&dq, &temp);
deque->front(deque, &temp);
printf("front = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
dq.back(&dq, &temp);
deque->back(deque, &temp);
printf("\tback = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
printf("\tsize = %2d\n", dq.size(&dq));
printf("\tsize = %2d\n", deque->size(deque));
}
printf("----- print -----\n");
dq.print(&dq);
deque->print(deque);
printf("\n");
dq.clear(&dq);
if (dq.empty(&dq))
deque->clear(deque);
if (deque->empty(deque))
{
printf("----- empty -----\n");
}
@ -115,67 +115,67 @@ static void demo_deque_num(void)
printf("----- push_front -----\n");
for (i = 0; i < len; i++)
{
dq.push_front(&dq, &data[i]);
deque->push_front(deque, &data[i]);
}
for (i = 0; i < len + 1; i++)
{
if (true == dq.pop_front(&dq, &temp))
if (true == deque->pop_front(deque, &temp))
{
printf("pop = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
if (true == dq.front(&dq, &temp))
if (true == deque->front(deque, &temp))
{
printf("front = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
}
if (dq.back(&dq, &temp))
if (deque->back(deque, &temp))
{
printf("back = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
}
printf("size = %2d\n", dq.size(&dq));
printf("size = %2d\n", deque->size(deque));
}
}
printf("----- push_front -----\n");
for (i = 0; i < len; i++)
{
dq.push_front(&dq, &data[i]);
deque->push_front(deque, &data[i]);
}
printf("----- print -----\n");
dq.print(&dq);
deque->print(deque);
printf("\n");
printf("----- set -----\n");
temp = 11;
dq.set(&dq, 0, &temp);
deque->set(deque, 0, &temp);
temp = 22;
dq.set(&dq, len/2, &temp);
deque->set(deque, len/2, &temp);
temp = 33;
dq.set(&dq, len - 1, &temp);
deque->set(deque, len - 1, &temp);
printf("----- print -----\n");
dq.print(&dq);
deque->print(deque);
printf("\n");
printf("----- get -----\n");
for (i = 0; i < len; i++)
{
if (true == dq.get(&dq, i, &temp))
if (true == deque->get(deque, i, &temp))
{
printf("deque[%2d] = ", i);
dq.print_obj(&temp);
deque->print_obj(&temp);
printf("\n");
}
}
dq.destory(&dq);
deque_free(&deque);
}
static void demo_deque_struct(void)
@ -189,33 +189,33 @@ static void demo_deque_struct(void)
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
struct _deque dq;
deque_init(&dq, sizeof(struct _student));
dq.print_obj = print_struct;
deque_t deque = deque_new();
deque_init(deque, sizeof(struct _student));
deque->print_obj = print_struct;
printf("\n\n----- demo_deque_struct -----\n");
printf("----- after push_back -----\n");
for (i = 0; i < len; i++)
{
dq.push_back(&dq, &data[i]);
deque->push_back(deque, &data[i]);
dq.front(&dq, &temp);
deque->front(deque, &temp);
printf("front = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
dq.back(&dq, &temp);
deque->back(deque, &temp);
printf("\tback = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
printf("\tsize = %2d\n", dq.size(&dq));
printf("\tsize = %2d\n", deque->size(deque));
}
printf("----- print -----\n");
dq.print(&dq);
deque->print(deque);
printf("\n");
dq.clear(&dq);
if (dq.empty(&dq))
deque->clear(deque);
if (deque->empty(deque))
{
printf("----- empty -----\n");
}
@ -223,37 +223,37 @@ static void demo_deque_struct(void)
printf("----- push_back -----\n");
for (i = 0; i < len; i++)
{
dq.push_back(&dq, &data[i]);
deque->push_back(deque, &data[i]);
}
printf("----- after pop_back -----\n");
for (i = 0; i < len + 1; i++)
{
if (true == dq.pop_back(&dq, &temp))
if (true == deque->pop_back(deque, &temp))
{
printf("pop = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
if (true == dq.front(&dq, &temp))
if (true == deque->front(deque, &temp))
{
printf("front = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
}
if (dq.back(&dq, &temp))
if (deque->back(deque, &temp))
{
printf("back = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
}
printf("size = %2d\n", dq.size(&dq));
printf("size = %2d\n", deque->size(deque));
}
else
{
printf("pop failed! because it is empty\n");
}
if (dq.empty(&dq))
if (deque->empty(deque))
{
printf("----- empty -----\n");
}
@ -262,24 +262,24 @@ static void demo_deque_struct(void)
printf("----- after push_front -----\n");
for (i = 0; i < len; i++)
{
dq.push_front(&dq, &data[i]);
deque->push_front(deque, &data[i]);
dq.front(&dq, &temp);
deque->front(deque, &temp);
printf("front = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
dq.back(&dq, &temp);
deque->back(deque, &temp);
printf("\tback = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
printf("\tsize = %2d\n", dq.size(&dq));
printf("\tsize = %2d\n", deque->size(deque));
}
printf("----- print -----\n");
dq.print(&dq);
deque->print(deque);
printf("\n");
dq.clear(&dq);
if (dq.empty(&dq))
deque->clear(deque);
if (deque->empty(deque))
{
printf("----- empty -----\n");
}
@ -287,69 +287,69 @@ static void demo_deque_struct(void)
printf("----- push_front -----\n");
for (i = 0; i < len; i++)
{
dq.push_front(&dq, &data[i]);
deque->push_front(deque, &data[i]);
}
for (i = 0; i < len + 1; i++)
{
if (true == dq.pop_front(&dq, &temp))
if (true == deque->pop_front(deque, &temp))
{
printf("pop = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
if (true == dq.front(&dq, &temp))
if (true == deque->front(deque, &temp))
{
printf("front = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
}
if (dq.back(&dq, &temp))
if (deque->back(deque, &temp))
{
printf("back = ");
dq.print_obj(&temp);
deque->print_obj(&temp);
}
printf("size = %2d\n", dq.size(&dq));
printf("size = %2d\n", deque->size(deque));
}
}
printf("----- push_front -----\n");
for (i = 0; i < len; i++)
{
dq.push_front(&dq, &data[i]);
deque->push_front(deque, &data[i]);
}
printf("----- print -----\n");
dq.print(&dq);
deque->print(deque);
printf("\n");
#if 0
printf("----- set -----\n");
temp = 11;
dq.set(&dq, 0, &temp);
deque->set(deque, 0, &temp);
temp = 22;
dq.set(&dq, len / 2, &temp);
deque->set(deque, len / 2, &temp);
temp = 33;
dq.set(&dq, len - 1, &temp);
deque->set(deque, len - 1, &temp);
printf("----- print -----\n");
dq.print(&dq);
deque->print(deque);
printf("\n");
#endif
printf("----- get -----\n");
for (i = 0; i < len; i++)
{
if (true == dq.get(&dq, i, &temp))
if (true == deque->get(deque, i, &temp))
{
printf("deque[%2d] = ", i);
dq.print_obj(&temp);
deque->print_obj(&temp);
printf("\n");
}
}
dq.destory(&dq);
deque_free(&deque);
}
void demo_deque(void)

View File

@ -1,5 +1,5 @@
/**
* @file demo_list.c
* @file demo_list->c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
@ -18,128 +18,128 @@ static void demo_list_num(void)
int index = 0;
int len = sizeof(data) / sizeof(data[0]);
struct _list list;
list_init2(&list, sizeof(int), 64);
list.print_obj = print_num;
list_t list = list_new();
list_init2(list, sizeof(int), 64);
list->print_obj = print_num;
printf("\n\n----- list_demo_num -----\n");
printf("----- push -----\n");
for (i = 0; i < len; i++)
{
list.append(&list, &data[i]);
list->append(list, &data[i]);
}
printf("----- print -----\n");
list.print(&list);
list->print(list);
printf("\n");
printf("----- pop -----\n");
list.pop(&list, 9, NULL);
list.print(&list);
list->pop(list, 9, NULL);
list->print(list);
printf("\n");
list.pop(&list, 0, NULL);
list.print(&list);
list->pop(list, 0, NULL);
list->print(list);
printf("\n");
list.pop(&list, 4, NULL);
list.print(&list);
list->pop(list, 4, NULL);
list->print(list);
printf("\n");
printf("----- clear -----\n");
list.clear(&list);
list.print(&list);
list->clear(list);
list->print(list);
printf("\n");
printf("----- push -----\n");
for (i = 0; i < len; i++)
{
list.append(&list, &data[i]);
list->append(list, &data[i]);
}
printf("----- print -----\n");
list.print(&list);
list->print(list);
printf("\n");
printf("----- get -----\n");
index = 0;
list.get(&list, index, &temp);
list->get(list, index, &temp);
printf("list[%4d] = %2d\n", index, temp);
index = 4;
list.get(&list, index, &temp);
list->get(list, index, &temp);
printf("list[%4d] = %2d\n", index, temp);
index = 9;
list.get(&list, index, &temp);
list->get(list, index, &temp);
printf("list[%4d] = %2d\n", index, temp);
printf("----- set -----\n");
index = 0;
temp = 11;
list.set(&list, index, &temp);
list->set(list, index, &temp);
printf("list[%4d] = %2d\n", index, temp);
index = 4;
temp = 22;
list.set(&list, index, &temp);
list->set(list, index, &temp);
printf("list[%4d] = %2d\n", index, temp);
index = 9;
temp = 33;
list.set(&list, index, &temp);
list->set(list, index, &temp);
printf("list[%4d] = %2d\n", index, temp);
printf("----- print -----\n");
list.print(&list);
list->print(list);
printf("\n");
printf("----- at like python -----\n");
index = -1;
list.get(&list, index, &temp);
list->get(list, index, &temp);
printf("list[%4d] = %2d\n", index, temp);
index = -6;
list.get(&list, index, &temp);
list->get(list, index, &temp);
printf("list[%4d] = %2d\n", index, temp);
index = -10;
list.get(&list, index, &temp);
list->get(list, index, &temp);
printf("list[%4d] = %2d\n", index, temp);
printf("----- set like python -----\n");
index = -1;
temp = 99;
list.set(&list, index, &temp);
list->set(list, index, &temp);
printf("list[%4d] = %2d\n", index, temp);
index = -6;
temp = 98;
list.set(&list, index, &temp);
list->set(list, index, &temp);
printf("list[%4d] = %2d\n", index, temp);
index = -10;
temp = 97;
list.set(&list, index, &temp);
list->set(list, index, &temp);
printf("list[%4d] = %2d\n", index, temp);
printf("----- print -----\n");
list.print(&list);
list->print(list);
printf("\n");
printf("----- pop -----\n");
for (i = 0; i < len + 1; i++)
{
list.pop(&list, 0, &temp);
list->pop(list, 0, &temp);
if (list.empty(&list))
if (list->empty(list))
{
printf("----- empty -----\n");
break;
}
}
list.destory(&list);
list_free(&list);
}
static void demo_list_struct(void)
@ -154,121 +154,121 @@ static void demo_list_struct(void)
int index = 0;
int len = sizeof(data) / sizeof(data[0]);
struct _list list;
list_init2(&list, sizeof(struct _student), 64);
list.print_obj = print_struct;
list_t list = list_new();
list_init2(list, sizeof(struct _student), 64);
list->print_obj = print_struct;
printf("\n\n----- list_demo_num -----\n");
printf("----- push -----\n");
for (i = 0; i < len; i++)
{
list.append(&list, &data[i]);
list->append(list, &data[i]);
}
printf("----- print -----\n");
list.print(&list);
list->print(list);
printf("\n");
printf("----- pop -----\n");
list.pop(&list, 9, NULL);
list.print(&list);
list->pop(list, 9, NULL);
list->print(list);
printf("\n");
list.pop(&list, 0, NULL);
list.print(&list);
list->pop(list, 0, NULL);
list->print(list);
printf("\n");
list.pop(&list, 4, NULL);
list.print(&list);
list->pop(list, 4, NULL);
list->print(list);
printf("\n");
printf("----- clear -----\n");
list.clear(&list);
list.print(&list);
list->clear(list);
list->print(list);
printf("\n");
printf("----- push -----\n");
for (i = 0; i < len; i++)
{
list.append(&list, &data[i]);
list->append(list, &data[i]);
}
printf("----- print -----\n");
list.print(&list);
list->print(list);
printf("\n");
printf("----- at -----\n");
index = 0;
list.get(&list, index, &temp);
list->get(list, index, &temp);
printf("list[%4d] = ", index);
list.print_obj(&temp); printf("\n");
list->print_obj(&temp); printf("\n");
index = 4;
list.get(&list, index, &temp);
list->get(list, index, &temp);
printf("list[%4d] = ", index);
list.print_obj(&temp); printf("\n");
list->print_obj(&temp); printf("\n");
index = 9;
list.get(&list, index, &temp);
list->get(list, index, &temp);
printf("list[%4d] = ", index);
list.print_obj(&temp); printf("\n");
list->print_obj(&temp); printf("\n");
printf("----- set -----\n");
index = 0;
temp.id = 11;
sprintf(temp.name, "robot_%02d", temp.id);
list.set(&list, index, &temp);
list->set(list, index, &temp);
printf("list[%4d] = ", index);
list.print_obj(&temp); printf("\n");
list->print_obj(&temp); printf("\n");
index = 4;
temp.id = 22;
sprintf(temp.name, "robot_%02d", temp.id);
list.set(&list, index, &temp);
list->set(list, index, &temp);
printf("list[%4d] = ", index);
list.print_obj(&temp); printf("\n");
list->print_obj(&temp); printf("\n");
index = 9;
temp.id = 33;
sprintf(temp.name, "robot_%02d", temp.id);
list.set(&list, index, &temp);
list->set(list, index, &temp);
printf("list[%4d] = ", index);
list.print_obj(&temp); printf("\n");
list->print_obj(&temp); printf("\n");
printf("----- print -----\n");
list.print(&list);
list->print(list);
printf("\n");
printf("----- at like python -----\n");
index = -1;
list.get(&list, index, &temp);
list->get(list, index, &temp);
printf("list[%4d] = ", index);
list.print_obj(&temp); printf("\n");
list->print_obj(&temp); printf("\n");
index = -6;
list.get(&list, index, &temp);
list->get(list, index, &temp);
printf("list[%4d] = ", index);
list.print_obj(&temp); printf("\n");
list->print_obj(&temp); printf("\n");
index = -10;
list.get(&list, index, &temp);
list->get(list, index, &temp);
printf("list[%4d] = ", index);
list.print_obj(&temp); printf("\n");
list->print_obj(&temp); printf("\n");
printf("----- set like python -----\n");
index = -1;
temp.id = 99;
sprintf(temp.name, "robot_%02d", temp.id);
list.set(&list, index, &temp);
list->set(list, index, &temp);
printf("list[%4d] = ", index);
list.print_obj(&temp); printf("\n");
list->print_obj(&temp); printf("\n");
index = -6;
temp.id = 98;
sprintf(temp.name, "robot_%02d", temp.id);
list.set(&list, index, &temp);
list->set(list, index, &temp);
printf("list[%4d] = ", index);
list.print_obj(&temp); printf("\n");
list->print_obj(&temp); printf("\n");
index = -10;
//temp.id = 97;
@ -277,27 +277,27 @@ static void demo_list_struct(void)
// struct _student robot = {"robot", 97};
// temp = robot;
temp = (struct _student){"robot", 97};
list.set(&list, index, &temp);
list->set(list, index, &temp);
printf("list[%4d] = ", index);
list.print_obj(&temp); printf("\n");
list->print_obj(&temp); printf("\n");
printf("----- print -----\n");
list.print(&list);
list->print(list);
printf("\n");
printf("----- pop -----\n");
for (i = 0; i < len + 1; i++)
{
list.pop(&list, 0, &temp);
list->pop(list, 0, &temp);
if (list.empty(&list))
if (list->empty(list))
{
printf("----- empty -----\n");
break;
}
}
list.destory(&list);
list_free(&list);
}
void demo_list(void)

View File

@ -17,33 +17,33 @@ static void demo_queue_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
struct _queue queue;
queue_init(&queue, sizeof(int));
queue.print_obj = print_num;
queue_t queue = queue_new();
queue_init(queue, sizeof(int));
queue->print_obj = print_num;
printf("\n\n----- demo_queue_num -----\n");
printf("----- after push-----\n");
for (i = 0; i < len; i++)
{
queue.push(&queue, &data[i]);
queue->push(queue, &data[i]);
queue.front(&queue, &temp);
queue->front(queue, &temp);
printf("front = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
queue.back(&queue, &temp);
queue->back(queue, &temp);
printf("\tback = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
printf("\tsize = %2d\n", queue.size(&queue));
printf("\tsize = %2d\n", queue->size(queue));
}
printf("----- print -----\n");
queue.print(&queue);
queue->print(queue);
printf("\n");
queue.clear(&queue);
if (queue.empty(&queue))
queue->clear(queue);
if (queue->empty(queue))
{
printf("----- empty -----\n");
}
@ -51,47 +51,47 @@ static void demo_queue_num(void)
printf("----- push -----\n");
for (i = 0; i < len; i++)
{
queue.push(&queue, &data[i]);
queue->push(queue, &data[i]);
}
printf("----- after pop -----\n");
for (i = 0; i < len + 1; i++)
{
if (true == queue.pop(&queue, &temp))
if (true == queue->pop(queue, &temp))
{
printf("pop = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
if (true == queue.front(&queue, &temp))
if (true == queue->front(queue, &temp))
{
printf("front = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
}
if (queue.back(&queue, &temp))
if (queue->back(queue, &temp))
{
printf("back = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
}
printf("size = %2d\n", queue.size(&queue));
printf("size = %2d\n", queue->size(queue));
}
else
{
printf("pop failed! because it is empty\n");
}
if (queue.empty(&queue))
if (queue->empty(queue))
{
printf("----- empty -----\n");
}
}
printf("----- print -----\n");
queue.print(&queue);
queue->print(queue);
printf("\n");
queue.destory(&queue);
queue_free(&queue);
}
@ -102,44 +102,43 @@ static void demo_queue_char(void)
char temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
struct _queue queue;
// queue_init2(&queue, sizeof(char), 64);
queue_init2(&queue, sizeof(char), 10);
queue.print_obj = print_char;
queue_t queue = queue_new();
queue_init2(queue, sizeof(char), 10);
queue->print_obj = print_char;
printf("\n\n----- demo_queue_char -----\n");
printf("----- after push-----\n");
for (i = 0; i < len; i++)
{
if(queue.push(&queue, &data[i]))
if(queue->push(queue, &data[i]))
{
queue.front(&queue, &temp);
queue->front(queue, &temp);
printf("front = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
queue.back(&queue, &temp);
queue->back(queue, &temp);
printf("\tback = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
printf("\tsize = %2d\n", queue.size(&queue));
printf("\tsize = %2d\n", queue->size(queue));
}
else
{
printf("push failed! because it is full\n");
}
if(queue.full(&queue))
if(queue->full(queue))
{
printf("----- full -----\n");
}
}
printf("----- print -----\n");
queue.print(&queue);
queue->print(queue);
printf("\n");
queue.clear(&queue);
if (queue.empty(&queue))
queue->clear(queue);
if (queue->empty(queue))
{
printf("----- empty -----\n");
}
@ -147,63 +146,63 @@ static void demo_queue_char(void)
printf("----- push -----\n");
for (i = 0; i < len; i++)
{
queue.push(&queue, &data[i]);
queue->push(queue, &data[i]);
}
printf("----- after pop -----\n");
while(!queue.empty(&queue))
while(!queue->empty(queue))
{
if (true == queue.pop(&queue, &temp))
if (true == queue->pop(queue, &temp))
{
printf("pop = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
if (true == queue.front(&queue, &temp))
if (true == queue->front(queue, &temp))
{
printf("front = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
}
if (queue.back(&queue, &temp))
if (queue->back(queue, &temp))
{
printf("back = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
}
printf("size = %2d\n", queue.size(&queue));
printf("size = %2d\n", queue->size(queue));
}
}
if (true == queue.pop(&queue, &temp))
if (true == queue->pop(queue, &temp))
{
printf("pop = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
if (true == queue.front(&queue, &temp))
if (true == queue->front(queue, &temp))
{
printf("front = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
}
if (queue.back(&queue, &temp))
if (queue->back(queue, &temp))
{
printf("back = ");
queue.print_obj(&temp);
queue->print_obj(&temp);
}
printf("size = %2d\n", queue.size(&queue));
printf("size = %2d\n", queue->size(queue));
}
if (queue.empty(&queue))
if (queue->empty(queue))
{
printf("----- empty -----\n");
}
printf("----- print -----\n");
queue.print(&queue);
queue->print(queue);
printf("\n");
queue.destory(&queue);
queue_free(&queue);
}
static void demo_queue_struct(void)

View File

@ -17,58 +17,58 @@ static void demo_stack_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
struct _stack s;
stack_init(&s, sizeof(int));
s.print_obj = print_num;
stack_t stack = stack_new();
stack_init(stack, sizeof(int));
stack->print_obj = print_num;
printf("\n\n----- demo_stack_num -----\n");
// get top if stack is empty
s.peek(&s, &temp);
stack->peek(stack, &temp);
printf("----- push -----\n");
for (i = 0; i < len; i++)
{
s.push(&s, &data[i]);
stack->push(stack, &data[i]);
s.peek(&s, &temp);
stack->peek(stack, &temp);
printf("top = ");
s.print_obj(&temp);
stack->print_obj(&temp);
printf("size = %2d\n", s.size(&s));
printf("size = %2d\n", stack->size(stack));
}
printf("----- print -----\n");
s.print(&s);
stack->print(stack);
printf("\n");
printf("----- pop -----\n");
for (i = 0; i < len + 1; i++)
{
if (true == s.pop(&s, &temp))
if (true == stack->pop(stack, &temp))
{
printf("pop = ");
s.print_obj(&temp);
stack->print_obj(&temp);
if (s.peek(&s, &temp))
if (stack->peek(stack, &temp))
{
printf("top = ");
s.print_obj(&temp);
stack->print_obj(&temp);
}
printf("size = %2d\n", s.size(&s));
printf("size = %2d\n", stack->size(stack));
}
else
{
printf("pop failed! because it is empty\n");
}
if (s.empty(&s))
if (stack->empty(stack))
{
printf("----- empty -----\n");
}
}
s.destory(&s);
stack_free(&stack);
}
static void demo_stack_char(void)
@ -78,57 +78,57 @@ static void demo_stack_char(void)
char temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
struct _stack s;
stack_init2(&s, sizeof(char), 64);
s.print_obj = print_char;
stack_t stack = stack_new();
stack_init2(stack, sizeof(char), 64);
stack->print_obj = print_char;
printf("\n\n----- demo_stack_char -----\n");
// get top if stack is empty
s.peek(&s, &temp);
stack->peek(stack, &temp);
printf("----- push -----\n");
for (i = 0; i < len; i++)
{
s.push(&s, &data[i]);
stack->push(stack, &data[i]);
s.peek(&s, &temp);
stack->peek(stack, &temp);
printf("top = ");
s.print_obj(&temp);
stack->print_obj(&temp);
printf("size = %2d\n", s.size(&s));
printf("size = %2d\n", stack->size(stack));
}
printf("----- print -----\n");
s.print(&s);
stack->print(stack);
printf("\n");
printf("----- pop -----\n");
for (i = 0; i < len + 1; i++)
{
if (true == s.pop(&s, &temp))
if (true == stack->pop(stack, &temp))
{
printf("pop = ");
s.print_obj(&temp);
stack->print_obj(&temp);
if (s.peek(&s, &temp))
if (stack->peek(stack, &temp))
{
printf("top = ");
s.print_obj(&temp);
stack->print_obj(&temp);
}
printf("size = %2d\n", s.size(&s));
printf("size = %2d\n", stack->size(stack));
}
else
{
printf("pop failed! because it is empty\n");
}
if (s.empty(&s))
if (stack->empty(stack))
{
printf("----- empty -----\n");
}
}
s.destory(&s);
stack_free(&stack);
}
static void demo_stack_struct(void)
@ -141,8 +141,7 @@ static void demo_stack_struct(void)
struct _student temp = { 0 };
uint32_t len = sizeof(data) / sizeof(data[0]);
stack_t stack;
stack = stack_new();
stack_t stack = stack_new();
stack_init(stack, sizeof(struct _student));
stack->print_obj = print_struct;

View File

@ -13,11 +13,32 @@
#include "common.h"
#include "stack.h"
#include "queue.h"
typedef enum {
RBT_RED = 0x00,
RBT_BLACK = 0x01,
}rbt_color;
enum _order{
ORDER_LEFT_PRE = 0x01,
ORDER_LEFT_IN = 0x02,
ORDER_LEFT_POST = 0x03,
ORDER_LEFT_BREADTH = 0x04,
ORDER_RIGHT_PRE = 0x05,
ORDER_RIGHT_IN = 0x06,
ORDER_RIGHT_POST = 0x07,
ORDER_RIGHT_BREADTH = 0x08,
ORDER_PRE = ORDER_LEFT_PRE,
ORDER_IN = ORDER_LEFT_IN,
ORDER_POST = ORDER_LEFT_POST,
ORDER_BREADTH = ORDER_LEFT_BREADTH,
};
struct _tree_node
{
void *obj;
@ -41,8 +62,13 @@ struct _tree
uint32_t _capacity;
uint32_t _ratio;
enum _order _order;
bool _right_priority;
stack_t stack;
queue_t queue;
struct _tree_node * cur_node;
// kernel
bool (*insert)(struct _tree* self, void* obj);
bool (*delete)(struct _tree* self, void* obj);
@ -57,10 +83,21 @@ struct _tree
bool (*min)(struct _tree* self, void* obj);
bool (*max)(struct _tree* self, void* obj);
// base
bool (*clear)(struct _tree* self);
bool (*empty)(struct _tree* self);
uint32_t (*size)(struct _tree* self);
// iter
/**
* @brief
*
*/
void (*set_order)(struct _tree* self, enum _order order);
void* (*begin)(struct _tree* self);
void* (*next)(struct _tree* self);
void* (*end)(struct _tree* self);
/**
* @brief obj compare with obj2
*

View File

@ -566,7 +566,8 @@ bool tree_clear(struct _tree* self)
assert(self != NULL);
if(self->_root == NULL)
{
return false;
// return false;
return true;
}
struct _tree_node* node = self->_root;
@ -611,6 +612,15 @@ void tree_destory(struct _tree* self)
assert(self != NULL);
self->clear(self);
self->_root = NULL;
if(self->stack != NULL)
{
stack_free(&self->stack);
}
if(self->queue != NULL)
{
queue_free(&self->queue);
}
}
void tree_order(struct _tree* self, bool right_priority)
@ -1013,40 +1023,7 @@ bool tree_max(struct _tree* self, void* obj)
return true;
}
bool tree_avl_init(struct _tree *self, uint32_t obj_size)
{
assert(self != NULL);
self->_obj_size = obj_size;
self->_size = 0;
// self->_capacity = 64;
// self->_ratio = 2;
self->_right_priority = false;
self->insert = tree_avl_insert;
self->delete = tree_avl_delete;
self->clear = tree_clear;
self->empty = tree_empty;
self->size = tree_size;
self->destory = tree_destory;
self->preorder = tree_preorder;
self->inorder = tree_inorder;
self->postorder = tree_postorder;
self->breadth = tree_breadth;
self->order = tree_order;
self->find = tree_find;
self->height = tree_height;
self->rebalance = tree_avl_rebalance;
self->find_max = tree_find_max;
self->find_min = tree_find_min;
self->max = tree_max;
self->min = tree_min;
self->_root = NULL;
return true;
}
rbt_color tree_color(struct _tree_node* node)
{
@ -1493,6 +1470,250 @@ bool tree_rb_delete(struct _tree* self, void* obj)
return true;
}
void tree_set_order(struct _tree* self, enum _order order)
{
assert(self != NULL);
self->_order = order;
}
void* tree_begin(struct _tree* self)
{
assert(self != NULL);
switch (self->_order)
{
case ORDER_LEFT_PRE:
{
struct _tree_node* node = NULL;
self->cur_node = self->_root;
while(!self->stack->empty(self->stack) || self->cur_node != NULL)
{
if(self->cur_node != NULL)
{
node = self->cur_node;
self->stack->push(self->stack, &self->cur_node);
self->cur_node = self->cur_node->left;
break;
}
else
{
self->stack->pop(self->stack, &self->cur_node);
self->cur_node = self->cur_node->right;
}
}
if(node == NULL)
{
return NULL;
}
return node->obj;
}break;
case ORDER_LEFT_IN:
{
}break;
case ORDER_LEFT_POST:
{
}break;
case ORDER_LEFT_BREADTH:
{
}break;
case ORDER_RIGHT_PRE:
{
}break;
case ORDER_RIGHT_IN:
{
}break;
case ORDER_RIGHT_POST:
{
}break;
case ORDER_RIGHT_BREADTH:
{
}break;
default:
{
}break;
}
}
void* tree_next(struct _tree* self)
{
assert(self != NULL);
switch (self->_order)
{
case ORDER_LEFT_PRE:
{
struct _tree_node* node = NULL;
while(!self->stack->empty(self->stack) || self->cur_node != NULL)
{
if(self->cur_node != NULL)
{
node = self->cur_node;
self->stack->push(self->stack, &self->cur_node);
self->cur_node = self->cur_node->left;
break;
}
else
{
self->stack->pop(self->stack, &self->cur_node);
self->cur_node = self->cur_node->right;
}
}
if(node == NULL)
{
return NULL;
}
return node->obj;
}break;
case ORDER_LEFT_IN:
{
}break;
case ORDER_LEFT_POST:
{
}break;
case ORDER_LEFT_BREADTH:
{
}break;
case ORDER_RIGHT_PRE:
{
}break;
case ORDER_RIGHT_IN:
{
}break;
case ORDER_RIGHT_POST:
{
}break;
case ORDER_RIGHT_BREADTH:
{
}break;
default:
{
}break;
}
}
void* tree_end(struct _tree* self)
{
assert(self != NULL);
switch (self->_order)
{
case ORDER_LEFT_PRE:
{
return NULL;
}break;
case ORDER_LEFT_IN:
{
}break;
case ORDER_LEFT_POST:
{
}break;
case ORDER_LEFT_BREADTH:
{
}break;
case ORDER_RIGHT_PRE:
{
}break;
case ORDER_RIGHT_IN:
{
}break;
case ORDER_RIGHT_POST:
{
}break;
case ORDER_RIGHT_BREADTH:
{
}break;
default:
{
}break;
}
}
bool tree_avl_init(struct _tree *self, uint32_t obj_size)
{
assert(self != NULL);
self->_obj_size = obj_size;
self->_size = 0;
// self->_capacity = 64;
// self->_ratio = 2;
self->_right_priority = false;
self->_order = ORDER_PRE;
self->insert = tree_avl_insert;
self->delete = tree_avl_delete;
self->clear = tree_clear;
self->empty = tree_empty;
self->size = tree_size;
self->destory = tree_destory;
self->preorder = tree_preorder;
self->inorder = tree_inorder;
self->postorder = tree_postorder;
self->breadth = tree_breadth;
self->order = tree_order;
self->find = tree_find;
self->height = tree_height;
self->rebalance = tree_avl_rebalance;
self->find_max = tree_find_max;
self->find_min = tree_find_min;
self->max = tree_max;
self->min = tree_min;
self->set_order = tree_set_order;
self->begin = tree_begin;
self->next = tree_next;
self->end = tree_end;
self->_root = NULL;
self->stack = stack_new();
if(self->stack == NULL)
{
goto done;
}
stack_init(self->stack, sizeof(struct _tree_node*));
self->queue = queue_new();
if(self->queue == NULL)
{
goto done1;
}
queue_init(self->queue, sizeof(struct _tree_node*));
self->cur_node = NULL;
return true;
done1:
stack_free(&self->stack);
done:
return false;
return true;
}
bool tree_rb_init(struct _tree *self, uint32_t obj_size)
{
assert(self != NULL);
@ -1502,6 +1723,7 @@ bool tree_rb_init(struct _tree *self, uint32_t obj_size)
// self->_ratio = 2;
self->_right_priority = false;
self->_order = ORDER_PRE;
self->insert = tree_rb_insert;
self->delete = tree_rb_delete;
@ -1523,8 +1745,32 @@ bool tree_rb_init(struct _tree *self, uint32_t obj_size)
self->max = tree_max;
self->min = tree_min;
self->set_order = tree_set_order;
self->begin = tree_begin;
self->next = tree_next;
self->end = tree_end;
self->_root = NULL;
self->stack = stack_new();
if(self->stack == NULL)
{
goto done;
}
stack_init(self->stack, sizeof(struct _tree_node*));
self->queue = queue_new();
if(self->queue == NULL)
{
goto done1;
}
queue_init(self->queue, sizeof(struct _tree_node*));
self->cur_node = NULL;
return true;
done1:
stack_free(&self->stack);
done:
return false;
}
tree_t tree_new(void)

View File

@ -81,6 +81,9 @@ int main(int argc, char const *argv[])
TEST_ADD(test_queue);
TEST_ADD(test_stack);
TEST_ADD(test_list);
TEST_ADD(test_deque);
TEST_ADD(test_heap);
TEST_ADD(test_tree);
return UNITY_END();
}

274
test/test_deque.c Normal file
View File

@ -0,0 +1,274 @@
/**
* @file test_deque.c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-09-01
*
* @copyright Copyright (c) 2024
*
*/
#include "test.h"
static void test_deque_num(void)
{
uint32_t i = 0;
int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new();
TEST_ASSERT_NOT_NULL(deque);
deque_init(deque, sizeof(int));
deque->print_obj = print_num;
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->push_back(deque, &data[i]));
TEST_ASSERT_TRUE(deque->front(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[0], temp);
TEST_ASSERT_TRUE(deque->back(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[i], temp);
TEST_ASSERT_EQUAL_INT(i + 1, deque->size(deque));
}
TEST_ASSERT_FALSE(deque->empty(deque));
TEST_ASSERT_TRUE(deque->clear(deque));
TEST_ASSERT_TRUE(deque->empty(deque));
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->push_back(deque, &data[i]));
}
for (i = 0; i < len + 1; i++)
{
if (!deque->empty(deque))
{
TEST_ASSERT_TRUE(deque->pop_back(deque, &temp));
}
else
{
TEST_ASSERT_FALSE(deque->pop_back(deque, &temp));
}
if (!deque->empty(deque))
{
TEST_ASSERT_TRUE(deque->front(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[0], temp);
TEST_ASSERT_TRUE(deque->back(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[deque->size(deque) - 1], temp);
}
else
{
TEST_ASSERT_FALSE(deque->front(deque, &temp));
TEST_ASSERT_FALSE(deque->back(deque, &temp));
}
}
TEST_ASSERT_TRUE(deque->empty(deque));
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->push_front(deque, &data[i]));
TEST_ASSERT_TRUE(deque->front(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[deque->size(deque) - 1], temp);
TEST_ASSERT_TRUE(deque->back(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[0], temp);
TEST_ASSERT_EQUAL_INT(i + 1, deque->size(deque));
}
TEST_ASSERT_TRUE(deque->clear(deque));
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->push_front(deque, &data[i]));
}
for (i = 0; i < len + 1; i++)
{
if (!deque->empty(deque))
{
TEST_ASSERT_TRUE(deque->pop_front(deque, &temp));
}
else
{
TEST_ASSERT_FALSE(deque->pop_front(deque, &temp));
}
if (!deque->empty(deque))
{
TEST_ASSERT_TRUE(deque->front(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[deque->size(deque) - 1], temp);
TEST_ASSERT_TRUE(deque->back(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[0], temp);
}
else
{
TEST_ASSERT_FALSE(deque->front(deque, &temp));
TEST_ASSERT_FALSE(deque->back(deque, &temp));
}
}
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->push_front(deque, &data[i]));
}
temp = 11;
deque->set(deque, 0, &temp);
temp = 22;
deque->set(deque, len / 2, &temp);
temp = 33;
deque->set(deque, len - 1, &temp);
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->get(deque, i, &temp));
}
deque_free(&deque);
TEST_ASSERT_NULL(deque);
}
static void test_deque_struct(void)
{
uint32_t i = 0;
struct _student data[] = {
"zhao", 1001, "qian", 1002, "sun", 1003, "li", 1004,
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
"feng", 1009, "cheng",1010,
};
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new();
TEST_ASSERT_NOT_NULL(deque);
deque_init(deque, sizeof(struct _student));
deque->print_obj = print_struct;
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->push_back(deque, &data[i]));
TEST_ASSERT_TRUE(deque->front(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[0].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[0].name, temp.name);
TEST_ASSERT_TRUE(deque->back(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[i].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[i].name, temp.name);
TEST_ASSERT_EQUAL_INT(i + 1, deque->size(deque));
}
TEST_ASSERT_FALSE(deque->empty(deque));
TEST_ASSERT_TRUE(deque->clear(deque));
TEST_ASSERT_TRUE(deque->empty(deque));
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->push_back(deque, &data[i]));
}
for (i = 0; i < len + 1; i++)
{
if(!deque->empty(deque))
{
TEST_ASSERT_TRUE(deque->pop_back(deque, &temp));
}
else
{
TEST_ASSERT_FALSE(deque->pop_back(deque, &temp));
}
if(!deque->empty(deque))
{
TEST_ASSERT_TRUE(deque->front(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[0].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[0].name, temp.name);
TEST_ASSERT_TRUE(deque->back(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[deque->size(deque) - 1].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[deque->size(deque) - 1].name, temp.name);
}
else
{
TEST_ASSERT_FALSE(deque->front(deque, &temp));
TEST_ASSERT_FALSE(deque->back(deque, &temp));
}
}
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->push_front(deque, &data[i]));
TEST_ASSERT_TRUE(deque->front(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[i].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[i].name, temp.name);
TEST_ASSERT_TRUE(deque->back(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[0].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[0].name, temp.name);
TEST_ASSERT_EQUAL_INT(i + 1, deque->size(deque));
}
TEST_ASSERT_TRUE(deque->clear(deque));
TEST_ASSERT_TRUE(deque->empty(deque));
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->push_front(deque, &data[i]));
}
for (i = 0; i < len + 1; i++)
{
if(!deque->empty(deque))
{
TEST_ASSERT_TRUE(deque->pop_front(deque, &temp));
}
else
{
TEST_ASSERT_FALSE(deque->pop_front(deque, &temp));
}
if(!deque->empty(deque))
{
TEST_ASSERT_TRUE(deque->front(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[deque->size(deque) - 1].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[deque->size(deque) - 1].name, temp.name);
TEST_ASSERT_TRUE(deque->back(deque, &temp));
TEST_ASSERT_EQUAL_INT(data[0].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[0].name, temp.name);
}
else
{
TEST_ASSERT_FALSE(deque->front(deque, &temp));
TEST_ASSERT_FALSE(deque->back(deque, &temp));
}
}
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->push_front(deque, &data[i]));
}
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(deque->get(deque, i, &temp));
}
deque_free(&deque);
TEST_ASSERT_NULL(deque);
}
void test_deque(void)
{
RUN_TEST(test_deque_num);
RUN_TEST(test_deque_struct);
}

140
test/test_heap.c Normal file
View File

@ -0,0 +1,140 @@
/**
* @file test_heap.c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-09-02
*
* @copyright Copyright (c) 2024
*
*/
#include "test.h"
static void* get_max(struct _heap* heap, void *array, int start, int end)
{
void* max = array;
for (int i = start; i < end; i++)
{
if (heap->compare((char*)array + heap->_obj_size * i, max) > 0)
{
max = (char*)array + heap->_obj_size * i;
}
}
return max;
}
static void* get_min(struct _heap* heap, void *array, int start, int end)
{
void* min = array;
for (int i = start; i < end; i++)
{
if (heap->compare((char*)array + heap->_obj_size * i, min) < 0)
{
min = (char*)array + heap->_obj_size * i;
}
}
return min;
}
static void test_heap_num(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_new();
TEST_ASSERT_NOT_NULL(heap);
heap_init2(heap, sizeof(int), 64);
heap->print_obj = print_num;
heap->compare = compare_num;
// default: maxheap
// maxheap or minheap
heap->setmin(heap, true);
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap));
}
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_EQUAL_INT(*(int *)get_min(heap, data, 0, heap->size(heap)), temp);
TEST_ASSERT_TRUE(heap->clear(heap));
TEST_ASSERT_TRUE(heap->empty(heap));
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
}
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->pop(heap, &temp));
}
TEST_ASSERT_TRUE(heap->empty(heap));
heap_free(&heap);
TEST_ASSERT_NULL(heap);
}
static void test_heap_struct(void)
{
uint32_t i = 0;
struct _student data[] = {
{"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004},
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
};
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_new();
TEST_ASSERT_NOT_NULL(heap);
heap_init2(heap, sizeof(struct _student), 64);
heap->print_obj = print_struct;
heap->compare = compare_struct;
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap));
}
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_EQUAL_INT(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->id, temp.id);
TEST_ASSERT_EQUAL_STRING(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->name, temp.name);
TEST_ASSERT_TRUE(heap->clear(heap));
TEST_ASSERT_TRUE(heap->empty(heap));
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
}
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->pop(heap, &temp));
}
TEST_ASSERT_TRUE(heap->empty(heap));
heap_free(&heap);
TEST_ASSERT_NULL(heap);
}
void test_heap(void)
{
RUN_TEST(test_heap_num);
RUN_TEST(test_heap_struct);
}

View File

@ -1,5 +1,5 @@
/**
* @file test_list.c
* @file test_list->c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
@ -16,8 +16,8 @@ static void test_list_init2(void)
// ------------------------------
#ifdef NDEBUG
TEST_ASSERT_FALSE(list_init2(NULL, sizeof(int), 1));
TEST_ASSERT_FALSE(list_init2(&list, 0, 1));
TEST_ASSERT_FALSE(list_init2(&list, sizeof(int), 0));
TEST_ASSERT_FALSE(list_init2(list, 0, 1));
TEST_ASSERT_FALSE(list_init2(list, sizeof(int), 0));
#endif
TEST_ASSERT_TRUE(list_init2(&list, sizeof(int), 1));
list.destory(&list);
@ -218,186 +218,97 @@ static void test_list_num(void)
TEST_ASSERT_NULL(list);
}
#if 0
static void test_list_struct(void)
{
uint32_t i = 0;
int i = 0;
struct _student data[] = {
{"zhao", 1001}, {"qian", 1002}, {"sun", 1003}, {"li", 1004},
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
"zhao", 1001, "qian", 1002, "sun", 1003, "li", 1004,
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
"feng", 1009, "cheng",1010,
};
struct _student temp;
uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
struct _student temp = {0};
int index = 0;
int len = sizeof(data) / sizeof(data[0]);
list_t list = list_new();
TEST_ASSERT_NOT_NULL(list);
list_init2(list, sizeof(struct _student));
list_init2(list, sizeof(struct _student), 64);
list->print_obj = print_struct;
TEST_ASSERT_FALSE(list->peek(list, &temp));
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(list->append(list, &data[i]));
}
TEST_ASSERT_TRUE(list->pop(list, 9, NULL));
TEST_ASSERT_TRUE(list->pop(list, 0, NULL));
TEST_ASSERT_TRUE(list->pop(list, 4, NULL));
TEST_ASSERT_TRUE(list->clear(list));
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(list->push(list, &data[i]));
TEST_ASSERT_EQUAL_INT(i + 1, list->size(list));
TEST_ASSERT_TRUE(list->peek(list, &temp));
TEST_ASSERT_EQUAL_INT(data[i].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[i].name, temp.name);
TEST_ASSERT_TRUE(list->append(list, &data[i]));
}
TEST_ASSERT_FALSE(list->empty(list));
TEST_ASSERT_TRUE(list->clear(list));
TEST_ASSERT_TRUE(list->empty(list));
for (i = 0; i < len; i++)
index = 0;
TEST_ASSERT_TRUE(list->get(list, index, &temp));
index = 4;
TEST_ASSERT_TRUE(list->get(list, index, &temp));
index = 9;
TEST_ASSERT_TRUE(list->get(list, index, &temp));
index = 0;
temp.id = 11;
sprintf(temp.name, "robot_%02d", temp.id);
TEST_ASSERT_TRUE(list->set(list, index, &temp));
index = 4;
temp.id = 22;
sprintf(temp.name, "robot_%02d", temp.id);
TEST_ASSERT_TRUE(list->set(list, index, &temp));
index = 9;
temp.id = 33;
sprintf(temp.name, "robot_%02d", temp.id);
TEST_ASSERT_TRUE(list->set(list, index, &temp));
index = -1;
TEST_ASSERT_TRUE(list->get(list, index, &temp));
index = -6;
TEST_ASSERT_TRUE(list->get(list, index, &temp));
index = -10;
TEST_ASSERT_TRUE(list->get(list, index, &temp));
index = -1;
temp.id = 99;
sprintf(temp.name, "robot_%02d", temp.id);
TEST_ASSERT_TRUE(list->set(list, index, &temp));
index = -6;
temp.id = 98;
sprintf(temp.name, "robot_%02d", temp.id);
TEST_ASSERT_TRUE(list->set(list, index, &temp));
index = -10;
temp = (struct _student){"robot", 97};
TEST_ASSERT_TRUE(list->set(list, index, &temp));
for (i = 0; i < len + 1; i++)
{
TEST_ASSERT_TRUE(list->push(list, &data[i]));
}
TEST_ASSERT_TRUE(list->pop(list, 0, &temp));
for(i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(list->peek(list, &temp));
TEST_ASSERT_EQUAL_INT(data[list->size(list) - 1].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[list->size(list) - 1].name, temp.name);
TEST_ASSERT_TRUE(list->pop(list, &temp));
if (!list->empty(list))
if (list->empty(list))
{
TEST_ASSERT_TRUE(list->peek(list, &temp));
TEST_ASSERT_EQUAL_INT(data[list->size(list) - 1].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[list->size(list) - 1].name, temp.name);
break;
}
}
TEST_ASSERT_TRUE(list->empty(list));
TEST_ASSERT_FALSE(list->pop(list, &temp));
list_free(&list);
TEST_ASSERT_NULL(list);
}
static void test_list2_num(void)
{
uint32_t i = 0;
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
uint32_t capacity = len;
list_t list = NULL;
list = list_new();
TEST_ASSERT_NOT_NULL(list);
TEST_ASSERT_TRUE(list_init2(list, sizeof(int), capacity));
list->print_obj = print_num;
TEST_ASSERT_FALSE(list->peek(list, &temp));
TEST_ASSERT_TRUE(list->clear(list));
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(list->push(list, &data[i]));
TEST_ASSERT_EQUAL_INT(i + 1, list->size(list));
TEST_ASSERT_TRUE(list->peek(list, &temp));
TEST_ASSERT_EQUAL_INT(data[i], temp);
}
TEST_ASSERT_FALSE(list->empty(list));
TEST_ASSERT_TRUE(list->clear(list));
TEST_ASSERT_TRUE(list->empty(list));
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(list->push(list, &data[i]));
}
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(list->peek(list, &temp));
TEST_ASSERT_EQUAL_INT(data[list->size(list) - 1], temp);
TEST_ASSERT_TRUE(list->pop(list, &temp));
if (!list->empty(list))
{
TEST_ASSERT_TRUE(list->peek(list, &temp));
TEST_ASSERT_EQUAL_INT(data[list->size(list) - 1], temp);
}
}
TEST_ASSERT_TRUE(list->empty(list));
TEST_ASSERT_FALSE(list->pop(list, &temp));
list_free(&list);
TEST_ASSERT_NULL(list);
}
static void test_list2_struct(void)
{
uint32_t i = 0;
struct _student data[] = {
{"zhao", 1001}, {"qian", 1002}, {"sun", 1003}, {"li", 1004},
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
};
struct _student temp;
uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
uint32_t capacity = len - 2;
list_t list = NULL;
list = list_new();
TEST_ASSERT_NOT_NULL(list);
TEST_ASSERT_TRUE(list_init2(list, sizeof(struct _student), capacity));
list->print_obj = print_struct;
TEST_ASSERT_FALSE(list->peek(list, &temp));
TEST_ASSERT_TRUE(list->empty(list));
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(list->push(list, &data[i]));
TEST_ASSERT_EQUAL_INT(i + 1, list->size(list));
TEST_ASSERT_TRUE(list->peek(list, &temp));
TEST_ASSERT_EQUAL_INT(data[list->size(list) - 1].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[list->size(list) - 1].name, temp.name);
}
TEST_ASSERT_FALSE(list->empty(list));
TEST_ASSERT_TRUE(list->clear(list));
TEST_ASSERT_TRUE(list->empty(list));
for (i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(list->push(list, &data[i]));
TEST_ASSERT_EQUAL_INT(i + 1, list->size(list));
}
for(i = 0; i < len; i++)
{
if (!list->empty(list))
{
TEST_ASSERT_TRUE(list->pop(list, &temp));
}
else
{
TEST_ASSERT_FALSE(list->pop(list, &temp));
}
if (!list->empty(list))
{
TEST_ASSERT_TRUE(list->peek(list, &temp));
TEST_ASSERT_EQUAL_INT(data[list->size(list) - 1].id, temp.id);
TEST_ASSERT_EQUAL_STRING(data[list->size(list) - 1].name, temp.name);
}
else
{
TEST_ASSERT_FALSE(list->peek(list, &temp));
}
}
list_free(&list);
TEST_ASSERT_NULL(list);
}
#endif
static void test_list_iter(void)
{
int temp = 0;
@ -452,7 +363,7 @@ void test_list(void)
RUN_TEST(test_list_clear);
RUN_TEST(test_list_num);
// RUN_TEST(test_list_struct);
RUN_TEST(test_list_struct);
RUN_TEST(test_list_iter);
}

537
test/test_tree.c Normal file
View File

@ -0,0 +1,537 @@
/**
* @file test_tree.c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-09-02
*
* @copyright Copyright (c) 2024
*
*/
#include "test.h"
// if vs2022 has error: 'max': macro redefinition
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
#if 0
/**
* @brief
* int data[] = { 5,2,3,1,7,8,6 };
* 5
* | |
* 2 7
* | | | |
* 1 3 6 8
*/
void test_avltree_num(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new,;
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
printf("\n\n----- demo_avltree_num -----\n");
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
}
printf("----- max -----\n");
tree->max(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- min -----\n");
tree->min(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- tree -----\n");
tree->clear(tree);
if(tree->empty(tree))
{
printf("----- empty -----\n");
}
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
}
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder(right) -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder(right) -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth(right) -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
for (i = 0; i < len; i++)
{
temp = data[i];
// delete
tree->delete(tree, &temp);
printf("delete = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
}
if(tree->empty(tree))
{
printf("----- empty -----\n");
}
tree_free(&tree);
}
static bool tree_rb_check_color(struct _tree *self, struct _tree_node* root, int black_num, int black_num_expected)
{
if(root == NULL)
{
if(black_num != black_num_expected)
{
printf("black_num != black_num_expected\n");
return false;
}
return true;
}
if(root->color == RBT_BLACK)
{
black_num++;
}
if(root->color == RBT_RED && root->parent && root->parent->color == RBT_RED)
{
printf("The red node is adjacent to the red node\n");
return false;
}
return tree_rb_check_color(self, root->left, black_num, black_num_expected) &&
tree_rb_check_color(self, root->right, black_num, black_num_expected);
}
static bool tree_rb_check(struct _tree* self)
{
assert(self != NULL);
if(self->_root == NULL)
{
return true;
}
if(self->_root->color != RBT_BLACK)
{
printf("self->_root->color != RBT_BLACK\n");
return false;
}
int black_num_expected = 0;
struct _tree_node* root = self->_root;
while(root)
{
if(root->color == RBT_BLACK)
{
black_num_expected++;
}
root = root->left;
}
return tree_rb_check_color(self, self->_root, 0, black_num_expected);
}
/**
* @brief
*/
void test_rbtree_num(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new,;
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
printf("\n\n----- demo_rbtree_num -----\n");
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
if(true != tree_rb_check(tree))
{
printf("----- rb_check_error -----\n");
return;
}
}
printf("----- max -----\n");
tree->max(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- min -----\n");
tree->min(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- tree -----\n");
tree->clear(tree);
if(tree->empty(tree))
{
printf("----- empty -----\n");
}
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
}
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder(right) -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder(right) -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth(right) -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
#if 1
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
for (i = 0; i < len; i++)
{
temp = data[i];
printf("delete = ");
tree->print_obj(&temp);
// delete
tree->delete(tree, &temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
if(true != tree_rb_check(tree))
{
printf("----- rb_check_error -----\n");
return;
}
}
if(tree->empty(tree))
{
printf("----- empty -----\n");
}
#endif
tree_free(&tree);
}
/**
* @brief
*/
void test_rbtree_struct(void)
{
uint32_t i = 0;
struct _student data[] = {
{"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004},
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
};
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new,;
tree_rb_init(tree, sizeof(struct _student));
tree->print_obj = print_struct;
tree->compare = compare_struct;
printf("\n\n----- demo_rbtree_struct -----\n");
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
if(true != tree_rb_check(tree))
{
printf("----- rb_check_error -----\n");
return;
}
}
printf("----- max -----\n");
tree->max(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- min -----\n");
tree->min(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- tree -----\n");
tree->clear(tree);
if(tree->empty(tree))
{
printf("----- empty -----\n");
}
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
}
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder(right) -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder(right) -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth(right) -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
#if 1
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
for (i = 0; i < len; i++)
{
temp = data[i];
printf("delete = ");
tree->print_obj(&temp);
// delete
tree->delete(tree, &temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
if(true != tree_rb_check(tree))
{
printf("----- rb_check_error -----\n");
return;
}
}
if(tree->empty(tree))
{
printf("----- empty -----\n");
}
#endif
tree_free(&tree);
}
#endif
static const int const expected_int_array[9][15] = {
{ 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, }, // original data
{ 7, 3, 2, 1, 5, 4, 6, 11, 9, 8, 10, 14, 12, 13, 15, }, // order_left_pre
};
static void test_tree_iter(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
// int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[32];
int count = 0;
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new();
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
// printf("insert = ");
// tree->print_obj(&temp);
// printf("size = %2d : ", tree->size(tree));
// tree->preorder(tree, tree->_root);
// printf("\n");
}
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : \n", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
// set order
tree->order(tree, ORDER_LEFT_PRE);
// tree->set_order(tree, ORDER_LEFT_IN);
printf("\niter test\n");
int * iter = NULL;
for(iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
printf("(%2d ) ", *iter);
buff[count++] = *iter;
}
printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[1], buff, count);
TEST_ASSERT_FALSE(tree->empty(tree));
TEST_ASSERT_TRUE(tree->clear(tree));
TEST_ASSERT_TRUE(tree->empty(tree));
TEST_ASSERT_TRUE(tree->clear(tree));
tree_free(&tree);
TEST_ASSERT_NULL(tree);
}
void test_tree(void)
{
// RUN_TEST(test_avltree_num);
// RUN_TEST(test_rbtree_num);
// RUN_TEST(test_rbtree_struct);
RUN_TEST(test_tree_iter);
}