mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-03 23:56:54 +08:00
list单测完成
This commit is contained in:
parent
51b761d120
commit
a0a49277b0
296
test/test_list.c
296
test/test_list.c
@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* @file test_list.c
|
* @file test_list->c
|
||||||
* @author wenjf (Orig5826@163.com)
|
* @author wenjf (Orig5826@163.com)
|
||||||
* @brief
|
* @brief
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
@ -16,8 +16,8 @@ static void test_list_init2(void)
|
|||||||
// ------------------------------
|
// ------------------------------
|
||||||
#ifdef NDEBUG
|
#ifdef NDEBUG
|
||||||
TEST_ASSERT_FALSE(list_init2(NULL, sizeof(int), 1));
|
TEST_ASSERT_FALSE(list_init2(NULL, sizeof(int), 1));
|
||||||
TEST_ASSERT_FALSE(list_init2(&list, 0, 1));
|
TEST_ASSERT_FALSE(list_init2(list, 0, 1));
|
||||||
TEST_ASSERT_FALSE(list_init2(&list, sizeof(int), 0));
|
TEST_ASSERT_FALSE(list_init2(list, sizeof(int), 0));
|
||||||
#endif
|
#endif
|
||||||
TEST_ASSERT_TRUE(list_init2(&list, sizeof(int), 1));
|
TEST_ASSERT_TRUE(list_init2(&list, sizeof(int), 1));
|
||||||
list.destory(&list);
|
list.destory(&list);
|
||||||
@ -218,8 +218,7 @@ static void test_list_num(void)
|
|||||||
TEST_ASSERT_NULL(list);
|
TEST_ASSERT_NULL(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
static void test_list_struct(void)
|
||||||
static void demo_list_struct(void)
|
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
struct _student data[] = {
|
struct _student data[] = {
|
||||||
@ -231,326 +230,85 @@ static void demo_list_struct(void)
|
|||||||
int index = 0;
|
int index = 0;
|
||||||
int len = sizeof(data) / sizeof(data[0]);
|
int len = sizeof(data) / sizeof(data[0]);
|
||||||
|
|
||||||
list_t list = NULL;
|
list_t list = list_new();
|
||||||
list = list_new();
|
list_init2(list, sizeof(struct _student), 64);
|
||||||
list_init(list, sizeof(struct _student), 64);
|
|
||||||
list->print_obj = print_struct;
|
list->print_obj = print_struct;
|
||||||
|
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
TEST_ASSERT_TRUE(list->append(&list, &data[i]));
|
TEST_ASSERT_TRUE(list->append(list, &data[i]));
|
||||||
}
|
}
|
||||||
TEST_ASSERT_TRUE(list->pop(&list, 9, NULL));
|
TEST_ASSERT_TRUE(list->pop(list, 9, NULL));
|
||||||
list.print(&list);
|
TEST_ASSERT_TRUE(list->pop(list, 0, NULL));
|
||||||
printf("\n");
|
TEST_ASSERT_TRUE(list->pop(list, 4, NULL));
|
||||||
|
|
||||||
list.pop(&list, 0, NULL);
|
TEST_ASSERT_TRUE(list->clear(list));
|
||||||
list.print(&list);
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
list.pop(&list, 4, NULL);
|
|
||||||
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++)
|
for (i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
list.append(&list, &data[i]);
|
TEST_ASSERT_TRUE(list->append(list, &data[i]));
|
||||||
}
|
}
|
||||||
printf("----- print -----\n");
|
|
||||||
list.print(&list);
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("----- at -----\n");
|
|
||||||
index = 0;
|
index = 0;
|
||||||
list.get(&list, index, &temp);
|
TEST_ASSERT_TRUE(list->get(list, index, &temp));
|
||||||
printf("list[%4d] = ", index);
|
|
||||||
list.print_obj(&temp); printf("\n");
|
|
||||||
|
|
||||||
index = 4;
|
index = 4;
|
||||||
list.get(&list, index, &temp);
|
TEST_ASSERT_TRUE(list->get(list, index, &temp));
|
||||||
printf("list[%4d] = ", index);
|
|
||||||
list.print_obj(&temp); printf("\n");
|
|
||||||
|
|
||||||
index = 9;
|
index = 9;
|
||||||
list.get(&list, index, &temp);
|
TEST_ASSERT_TRUE(list->get(list, index, &temp));
|
||||||
printf("list[%4d] = ", index);
|
|
||||||
list.print_obj(&temp); printf("\n");
|
|
||||||
|
|
||||||
|
|
||||||
printf("----- set -----\n");
|
|
||||||
index = 0;
|
index = 0;
|
||||||
temp.id = 11;
|
temp.id = 11;
|
||||||
sprintf(temp.name, "robot_%02d", temp.id);
|
sprintf(temp.name, "robot_%02d", temp.id);
|
||||||
list.set(&list, index, &temp);
|
TEST_ASSERT_TRUE(list->set(list, index, &temp));
|
||||||
printf("list[%4d] = ", index);
|
|
||||||
list.print_obj(&temp); printf("\n");
|
|
||||||
|
|
||||||
index = 4;
|
index = 4;
|
||||||
temp.id = 22;
|
temp.id = 22;
|
||||||
sprintf(temp.name, "robot_%02d", temp.id);
|
sprintf(temp.name, "robot_%02d", temp.id);
|
||||||
list.set(&list, index, &temp);
|
TEST_ASSERT_TRUE(list->set(list, index, &temp));
|
||||||
printf("list[%4d] = ", index);
|
|
||||||
list.print_obj(&temp); printf("\n");
|
|
||||||
|
|
||||||
index = 9;
|
index = 9;
|
||||||
temp.id = 33;
|
temp.id = 33;
|
||||||
sprintf(temp.name, "robot_%02d", temp.id);
|
sprintf(temp.name, "robot_%02d", temp.id);
|
||||||
list.set(&list, index, &temp);
|
TEST_ASSERT_TRUE(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;
|
index = -1;
|
||||||
list.get(&list, index, &temp);
|
TEST_ASSERT_TRUE(list->get(list, index, &temp));
|
||||||
printf("list[%4d] = ", index);
|
|
||||||
list.print_obj(&temp); printf("\n");
|
|
||||||
|
|
||||||
index = -6;
|
index = -6;
|
||||||
list.get(&list, index, &temp);
|
TEST_ASSERT_TRUE(list->get(list, index, &temp));
|
||||||
printf("list[%4d] = ", index);
|
|
||||||
list.print_obj(&temp); printf("\n");
|
|
||||||
|
|
||||||
index = -10;
|
index = -10;
|
||||||
list.get(&list, index, &temp);
|
TEST_ASSERT_TRUE(list->get(list, index, &temp));
|
||||||
printf("list[%4d] = ", index);
|
|
||||||
list.print_obj(&temp); printf("\n");
|
|
||||||
|
|
||||||
printf("----- set like python -----\n");
|
|
||||||
index = -1;
|
index = -1;
|
||||||
temp.id = 99;
|
temp.id = 99;
|
||||||
sprintf(temp.name, "robot_%02d", temp.id);
|
sprintf(temp.name, "robot_%02d", temp.id);
|
||||||
list.set(&list, index, &temp);
|
TEST_ASSERT_TRUE(list->set(list, index, &temp));
|
||||||
printf("list[%4d] = ", index);
|
|
||||||
list.print_obj(&temp); printf("\n");
|
|
||||||
|
|
||||||
index = -6;
|
index = -6;
|
||||||
temp.id = 98;
|
temp.id = 98;
|
||||||
sprintf(temp.name, "robot_%02d", temp.id);
|
sprintf(temp.name, "robot_%02d", temp.id);
|
||||||
list.set(&list, index, &temp);
|
TEST_ASSERT_TRUE(list->set(list, index, &temp));
|
||||||
printf("list[%4d] = ", index);
|
|
||||||
list.print_obj(&temp); printf("\n");
|
|
||||||
|
|
||||||
index = -10;
|
index = -10;
|
||||||
//temp.id = 97;
|
|
||||||
//sprintf(temp.name, "robot_%02d", temp.id);
|
|
||||||
// temp = data[0];
|
|
||||||
// struct _student robot = {"robot", 97};
|
|
||||||
// temp = robot;
|
|
||||||
temp = (struct _student){"robot", 97};
|
temp = (struct _student){"robot", 97};
|
||||||
list.set(&list, index, &temp);
|
TEST_ASSERT_TRUE(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++)
|
for (i = 0; i < len + 1; i++)
|
||||||
{
|
{
|
||||||
list.pop(&list, 0, &temp);
|
TEST_ASSERT_TRUE(list->pop(list, 0, &temp));
|
||||||
|
|
||||||
if (list.empty(&list))
|
if (list->empty(list))
|
||||||
{
|
{
|
||||||
printf("----- empty -----\n");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
list.destory(&list);
|
|
||||||
}
|
|
||||||
|
|
||||||
// #if 0
|
|
||||||
static void test_list_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;
|
|
||||||
|
|
||||||
list_t list = list_new();
|
|
||||||
TEST_ASSERT_NOT_NULL(list);
|
|
||||||
|
|
||||||
list_init2(list, sizeof(struct _student));
|
|
||||||
list->print_obj = print_struct;
|
|
||||||
|
|
||||||
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].id, temp.id);
|
|
||||||
TEST_ASSERT_EQUAL_STRING(data[i].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]));
|
|
||||||
}
|
|
||||||
|
|
||||||
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))
|
|
||||||
{
|
|
||||||
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->empty(list));
|
|
||||||
TEST_ASSERT_FALSE(list->pop(list, &temp));
|
|
||||||
list_free(&list);
|
list_free(&list);
|
||||||
TEST_ASSERT_NULL(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)
|
static void test_list_iter(void)
|
||||||
{
|
{
|
||||||
int temp = 0;
|
int temp = 0;
|
||||||
@ -605,7 +363,7 @@ void test_list(void)
|
|||||||
RUN_TEST(test_list_clear);
|
RUN_TEST(test_list_clear);
|
||||||
|
|
||||||
RUN_TEST(test_list_num);
|
RUN_TEST(test_list_num);
|
||||||
// RUN_TEST(test_list_struct);
|
RUN_TEST(test_list_struct);
|
||||||
|
|
||||||
RUN_TEST(test_list_iter);
|
RUN_TEST(test_list_iter);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user