mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-03 15:56:52 +08:00
list测试对象
This commit is contained in:
parent
6e367209ca
commit
12a9e80b91
@ -56,7 +56,7 @@ struct _list
|
||||
bool (*remove)(struct _list* self, int index); // 根据索引,移除对象
|
||||
|
||||
int (*index)(struct _list* self, void* obj); // 在列表中,查找数据是否存在,若存在则返回其索引。否则返回-1
|
||||
bool (*at)(struct _list* self, int index, void* obj); // 根据索引,获取对象
|
||||
bool (*at)(struct _list* self, int index, void* obj); // 根据索引,获取对象
|
||||
bool (*set)(struct _list* self, int index, void* obj); // 根据索引,修改对象
|
||||
|
||||
// base
|
||||
|
@ -1,73 +1,6 @@
|
||||
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
#include "list.h"
|
||||
|
||||
#if 0
|
||||
void list_test(void)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
list_data_t data = 0;
|
||||
struct _list list;
|
||||
list_init(&list);
|
||||
|
||||
printf("----- append -----\n");
|
||||
for (i = 0; i < 18; i++)
|
||||
{
|
||||
list_append(&list, i);
|
||||
list_print(&list);
|
||||
}
|
||||
|
||||
printf("----- delete -----\n");
|
||||
list_delete(&list, 17);
|
||||
list_print(&list);
|
||||
|
||||
list_delete(&list, 0);
|
||||
list_print(&list);
|
||||
|
||||
list_delete(&list, 9);
|
||||
list_print(&list);
|
||||
|
||||
printf("----- clear -----\n");
|
||||
list_clear(&list);
|
||||
printf("----- insert -----\n");
|
||||
for (i = 0; i < 18; i++)
|
||||
{
|
||||
list_insert(&list, 0, i);
|
||||
list_print(&list);
|
||||
}
|
||||
|
||||
printf("----- get -----\n");
|
||||
data = list_get(&list, 0);
|
||||
printf("list[0] = %2d\n", data);
|
||||
|
||||
data = list_get(&list, 17);
|
||||
printf("list[17] = %2d\n", data);
|
||||
|
||||
data = list_get(&list, 5);
|
||||
printf("list[5] = %2d\n", data);
|
||||
|
||||
printf("----- like python -----\n");
|
||||
data = list_get(&list, -1);
|
||||
printf("list[-1] = %2d\n", data);
|
||||
|
||||
data = list_get(&list, -5);
|
||||
printf("list[-5] = %2d\n", data);
|
||||
|
||||
data = list_get(&list, -18);
|
||||
printf("list[-18] = %2d\n", data);
|
||||
|
||||
printf("----- set -----\n");
|
||||
for (i = 0; i < 18; i++)
|
||||
{
|
||||
list_set(&list, i, i);
|
||||
list_print(&list);
|
||||
}
|
||||
|
||||
list_print(&list);
|
||||
list_destory(&list);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void print_num(void* obj)
|
||||
{
|
||||
printf("(%2d )", *(int*)obj);
|
||||
@ -125,32 +58,32 @@ static void list_test_num(void)
|
||||
printf("----- at -----\n");
|
||||
index = 0;
|
||||
list.at(&list, index, &temp);
|
||||
printf("list[%2d] = %2d\n", index, temp);
|
||||
printf("list[%4d] = %2d\n", index, temp);
|
||||
|
||||
index = 4;
|
||||
list.at(&list, index, &temp);
|
||||
printf("list[%2d] = %2d\n", index, temp);
|
||||
printf("list[%4d] = %2d\n", index, temp);
|
||||
|
||||
index = 9;
|
||||
list.at(&list, index, &temp);
|
||||
printf("list[%2d] = %2d\n", index, temp);
|
||||
printf("list[%4d] = %2d\n", index, temp);
|
||||
|
||||
|
||||
printf("----- set -----\n");
|
||||
index = 0;
|
||||
temp = 11;
|
||||
list.set(&list, index, &temp);
|
||||
printf("list[%2d] = %2d\n", index, temp);
|
||||
printf("list[%4d] = %2d\n", index, temp);
|
||||
|
||||
index = 4;
|
||||
temp = 22;
|
||||
list.set(&list, index, &temp);
|
||||
printf("list[%2d] = %2d\n", index, temp);
|
||||
printf("list[%4d] = %2d\n", index, temp);
|
||||
|
||||
index = 9;
|
||||
temp = 33;
|
||||
list.set(&list, index, &temp);
|
||||
printf("list[%2d] = %2d\n", index, temp);
|
||||
printf("list[%4d] = %2d\n", index, temp);
|
||||
|
||||
printf("----- print -----\n");
|
||||
list.print(&list);
|
||||
@ -160,31 +93,31 @@ static void list_test_num(void)
|
||||
printf("----- at like python -----\n");
|
||||
index = -1;
|
||||
list.at(&list, index, &temp);
|
||||
printf("list[%2d] = %2d\n", index, temp);
|
||||
printf("list[%4d] = %2d\n", index, temp);
|
||||
|
||||
index = -6;
|
||||
list.at(&list, index, &temp);
|
||||
printf("list[%2d] = %2d\n", index, temp);
|
||||
printf("list[%4d] = %2d\n", index, temp);
|
||||
|
||||
index = -10;
|
||||
list.at(&list, index, &temp);
|
||||
printf("list[%2d] = %2d\n", index, temp);
|
||||
printf("list[%4d] = %2d\n", index, temp);
|
||||
|
||||
printf("----- set like python -----\n");
|
||||
index = -1;
|
||||
temp = 99;
|
||||
list.set(&list, index, &temp);
|
||||
printf("list[%2d] = %2d\n", index, temp);
|
||||
printf("list[%4d] = %2d\n", index, temp);
|
||||
|
||||
index = -6;
|
||||
temp = 98;
|
||||
list.set(&list, index, &temp);
|
||||
printf("list[%2d] = %2d\n", index, temp);
|
||||
printf("list[%4d] = %2d\n", index, temp);
|
||||
|
||||
index = -10;
|
||||
temp = 97;
|
||||
list.set(&list, index, &temp);
|
||||
printf("list[%2d] = %2d\n", index, temp);
|
||||
printf("list[%4d] = %2d\n", index, temp);
|
||||
|
||||
printf("----- print -----\n");
|
||||
list.print(&list);
|
||||
@ -193,16 +126,7 @@ static void list_test_num(void)
|
||||
printf("----- pop -----\n");
|
||||
for (i = 0; i < len + 1; i++)
|
||||
{
|
||||
if (true == list.pop(&list, 0, &temp))
|
||||
{
|
||||
printf("top = ");
|
||||
list.print_obj(&temp);
|
||||
printf("\tsize after push = %2d\n", list.size(&list));
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("pop failed! because stack is empty\n");
|
||||
}
|
||||
list.pop(&list, 0, &temp);
|
||||
|
||||
if (list.empty(&list))
|
||||
{
|
||||
@ -215,7 +139,174 @@ static void list_test_num(void)
|
||||
}
|
||||
|
||||
|
||||
struct _student
|
||||
{
|
||||
char name[16];
|
||||
int id;
|
||||
};
|
||||
|
||||
static void print_struct(void* obj)
|
||||
{
|
||||
struct _student* student = (struct _student*)obj;
|
||||
printf("(%4d:%-8s) ", student->id, student->name);
|
||||
}
|
||||
|
||||
static void list_test_struct(void)
|
||||
{
|
||||
int 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};
|
||||
int index = 0;
|
||||
int len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
struct _list list;
|
||||
list_init(&list, sizeof(struct _student));
|
||||
list.print_obj = print_struct;
|
||||
|
||||
printf("\n\n----- list_test_num -----\n");
|
||||
printf("----- push -----\n");
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
list.append(&list, &data[i]);
|
||||
}
|
||||
printf("----- print -----\n");
|
||||
list.print(&list);
|
||||
printf("\n");
|
||||
|
||||
printf("----- remove -----\n");
|
||||
list.remove(&list, 9);
|
||||
list.print(&list);
|
||||
printf("\n");
|
||||
|
||||
list.remove(&list, 0);
|
||||
list.print(&list);
|
||||
printf("\n");
|
||||
|
||||
list.remove(&list, 4);
|
||||
list.print(&list);
|
||||
printf("\n");
|
||||
|
||||
printf("----- clear -----\n");
|
||||
list.clear(&list);
|
||||
list.print(&list);
|
||||
printf("\n");
|
||||
|
||||
printf("----- push -----\n");
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
list.append(&list, &data[i]);
|
||||
}
|
||||
printf("----- print -----\n");
|
||||
list.print(&list);
|
||||
printf("\n");
|
||||
|
||||
printf("----- at -----\n");
|
||||
index = 0;
|
||||
list.at(&list, index, &temp);
|
||||
printf("list[%4d] = ", index);
|
||||
list.print_obj(&temp); printf("\n");
|
||||
|
||||
index = 4;
|
||||
list.at(&list, index, &temp);
|
||||
printf("list[%4d] = ", index);
|
||||
list.print_obj(&temp); printf("\n");
|
||||
|
||||
index = 9;
|
||||
list.at(&list, index, &temp);
|
||||
printf("list[%4d] = ", index);
|
||||
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);
|
||||
printf("list[%4d] = ", index);
|
||||
list.print_obj(&temp); printf("\n");
|
||||
|
||||
index = 4;
|
||||
temp.id = 22;
|
||||
sprintf(temp.name, "robot_%02d", temp.id);
|
||||
list.set(&list, index, &temp);
|
||||
printf("list[%4d] = ", index);
|
||||
list.print_obj(&temp); printf("\n");
|
||||
|
||||
index = 9;
|
||||
temp.id = 33;
|
||||
sprintf(temp.name, "robot_%02d", temp.id);
|
||||
list.set(&list, index, &temp);
|
||||
printf("list[%4d] = ", index);
|
||||
list.print_obj(&temp); printf("\n");
|
||||
|
||||
printf("----- print -----\n");
|
||||
list.print(&list);
|
||||
printf("\n");
|
||||
|
||||
|
||||
printf("----- at like python -----\n");
|
||||
index = -1;
|
||||
list.at(&list, index, &temp);
|
||||
printf("list[%4d] = ", index);
|
||||
list.print_obj(&temp); printf("\n");
|
||||
|
||||
index = -6;
|
||||
list.at(&list, index, &temp);
|
||||
printf("list[%4d] = ", index);
|
||||
list.print_obj(&temp); printf("\n");
|
||||
|
||||
index = -10;
|
||||
list.at(&list, index, &temp);
|
||||
printf("list[%4d] = ", index);
|
||||
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);
|
||||
printf("list[%4d] = ", index);
|
||||
list.print_obj(&temp); printf("\n");
|
||||
|
||||
index = -6;
|
||||
temp.id = 98;
|
||||
sprintf(temp.name, "robot_%02d", temp.id);
|
||||
list.set(&list, index, &temp);
|
||||
printf("list[%4d] = ", index);
|
||||
list.print_obj(&temp); printf("\n");
|
||||
|
||||
index = -10;
|
||||
temp.id = 97;
|
||||
sprintf(temp.name, "robot_%02d", temp.id);
|
||||
list.set(&list, index, &temp);
|
||||
printf("list[%4d] = ", index);
|
||||
list.print_obj(&temp); printf("\n");
|
||||
|
||||
printf("----- print -----\n");
|
||||
list.print(&list);
|
||||
printf("\n");
|
||||
|
||||
printf("----- pop -----\n");
|
||||
for (i = 0; i < len + 1; i++)
|
||||
{
|
||||
list.pop(&list, 0, &temp);
|
||||
|
||||
if (list.empty(&list))
|
||||
{
|
||||
printf("----- empty -----\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
list.destory(&list);
|
||||
}
|
||||
|
||||
void list_test(void)
|
||||
{
|
||||
list_test_num();
|
||||
list_test_struct();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user