mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-03 15:56:52 +08:00
iter测试还存在点问题
This commit is contained in:
parent
ee19e77ab8
commit
d6811b824c
@ -21,6 +21,7 @@ struct _list
|
||||
uint32_t _size; // 栈大小
|
||||
uint32_t _capacity; // 总容量
|
||||
uint32_t _ratio; // 扩展比率
|
||||
uint32_t _cur; // µ±Ç°Ë÷Òý
|
||||
|
||||
// kernel
|
||||
bool (*append)(struct _list* self, void* obj); // Append object to the end of the list.
|
||||
@ -33,6 +34,11 @@ struct _list
|
||||
bool (*get)(struct _list* self, int index, void* obj); // 根据索引,获取对象
|
||||
bool (*set)(struct _list* self, int index, void* obj); // 根据索引,修改对象
|
||||
|
||||
// iter
|
||||
void* (*begin)(struct _list* self);
|
||||
void* (*next)(struct _list* self);
|
||||
void* (*end)(struct _list* self);
|
||||
|
||||
// base
|
||||
uint32_t(*size)(struct _list* self);
|
||||
bool (*empty)(struct _list* self);
|
||||
|
24
src/list.c
24
src/list.c
@ -179,6 +179,23 @@ void list_print(struct _list* self)
|
||||
}
|
||||
}
|
||||
|
||||
void* list_begin(struct _list* self)
|
||||
{
|
||||
return self->obj;
|
||||
}
|
||||
|
||||
void* list_next(struct _list* self)
|
||||
{
|
||||
void *obj = (char*)self->obj + self->_cur * self->_obj_size;
|
||||
self->_cur += 1;
|
||||
return obj;
|
||||
}
|
||||
|
||||
void* list_end(struct _list* self)
|
||||
{
|
||||
return (char*)self->obj + self->_size * self->_obj_size;
|
||||
}
|
||||
|
||||
bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
|
||||
{
|
||||
assert(list != NULL);
|
||||
@ -194,6 +211,7 @@ bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
|
||||
list->_size = 0;
|
||||
list->_capacity = capacity;
|
||||
list->_ratio = 2;
|
||||
list->_cur = 0;
|
||||
|
||||
// 2. set function
|
||||
// kernel
|
||||
@ -212,6 +230,10 @@ bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
|
||||
list->size = list_size;
|
||||
list->sort = list_sort;
|
||||
|
||||
list->begin = list_begin;
|
||||
list->next = list_next;
|
||||
list->end = list_end;
|
||||
|
||||
// 3. set array
|
||||
list->obj = (void*)calloc(list->_capacity, list->_obj_size);
|
||||
if (list->obj == NULL)
|
||||
@ -223,7 +245,7 @@ bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
|
||||
|
||||
list_t list_new(void)
|
||||
{
|
||||
return (struct _list*)malloc(sizeof(struct _list));
|
||||
return (struct _list*)calloc(1, sizeof(struct _list));
|
||||
}
|
||||
|
||||
void list_free(list_t* list)
|
||||
|
@ -397,6 +397,41 @@ static void test_list2_struct(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
static void test_list_iter(void)
|
||||
{
|
||||
int temp = 0;
|
||||
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||
int data_temp[32];
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
|
||||
uint32_t i = 0;
|
||||
|
||||
list_t list = NULL;
|
||||
|
||||
// ------------------------------
|
||||
list = list_new();
|
||||
list_init2(list, sizeof(int), len);
|
||||
TEST_ASSERT_TRUE(list->clear(list));
|
||||
for(i = 0; i < len; i++)
|
||||
{
|
||||
list->append(list, &data[i]);
|
||||
}
|
||||
|
||||
int * iter = NULL;
|
||||
i = 0;
|
||||
for(iter = list->begin(list); iter != list->end(list); iter = list->next(list))
|
||||
{
|
||||
data_temp[i++] = *iter;
|
||||
}
|
||||
TEST_ASSERT_EQUAL_INT_ARRAY(data, data_temp, i);
|
||||
|
||||
TEST_ASSERT_FALSE(list->empty(list));
|
||||
TEST_ASSERT_TRUE(list->clear(list));
|
||||
TEST_ASSERT_TRUE(list->empty(list));
|
||||
TEST_ASSERT_TRUE(list->clear(list));
|
||||
list_free(&list);
|
||||
}
|
||||
|
||||
void test_list(void)
|
||||
{
|
||||
RUN_TEST(test_list_init2);
|
||||
@ -407,4 +442,6 @@ void test_list(void)
|
||||
|
||||
RUN_TEST(test_list_num);
|
||||
// RUN_TEST(test_list_struct);
|
||||
|
||||
RUN_TEST(test_list_iter);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user