mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-18 03:51:35 +08:00
队列的迭代器实现并测试通过
This commit is contained in:
parent
07bb44a4d8
commit
aac984ee08
21
src/queue.c
21
src/queue.c
@ -338,8 +338,6 @@ static const void* queue_iter_next(struct _iterator* iter)
|
|||||||
queue_t self = (queue_t)iter->_parent;
|
queue_t self = (queue_t)iter->_parent;
|
||||||
void *obj = NULL;
|
void *obj = NULL;
|
||||||
|
|
||||||
if(self->_front->obj == NULL)
|
|
||||||
{
|
|
||||||
// base on linklist
|
// base on linklist
|
||||||
struct _queue_node * node = (struct _queue_node *)iter->_cur_node;
|
struct _queue_node * node = (struct _queue_node *)iter->_cur_node;
|
||||||
if(node != NULL)
|
if(node != NULL)
|
||||||
@ -347,13 +345,21 @@ static const void* queue_iter_next(struct _iterator* iter)
|
|||||||
obj = node->obj;
|
obj = node->obj;
|
||||||
iter->_cur_node = node->next;
|
iter->_cur_node = node->next;
|
||||||
}
|
}
|
||||||
|
self->_iter._cur += 1;
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
static const void* queue2_iter_next(struct _iterator* iter)
|
||||||
{
|
{
|
||||||
|
assert(iter != NULL);
|
||||||
|
assert(iter->parent != NULL);
|
||||||
|
|
||||||
|
queue_t self = (queue_t)iter->_parent;
|
||||||
|
void *obj = NULL;
|
||||||
|
|
||||||
// base on array
|
// base on array
|
||||||
uint32_t index = self->size(self) - 1 - self->_iter._cur;
|
uint32_t index = self->_iter._cur;
|
||||||
obj = self->_front->obj + self->_obj_size * index;
|
obj = self->_front->obj + self->_obj_size * index;
|
||||||
}
|
|
||||||
|
|
||||||
self->_iter._cur += 1;
|
self->_iter._cur += 1;
|
||||||
return obj;
|
return obj;
|
||||||
@ -399,6 +405,9 @@ static bool queue_init(struct _queue * self, uint32_t obj_size)
|
|||||||
self->capacity = queue_capacity;
|
self->capacity = queue_capacity;
|
||||||
self->clear = queue_clear;
|
self->clear = queue_clear;
|
||||||
|
|
||||||
|
// iter
|
||||||
|
self->iter = queue_iter;
|
||||||
|
|
||||||
// -------------------- debug --------------------
|
// -------------------- debug --------------------
|
||||||
self->print = queue_print;
|
self->print = queue_print;
|
||||||
|
|
||||||
@ -445,7 +454,7 @@ static bool queue_init2(struct _queue * self, uint32_t obj_size, uint32_t capaci
|
|||||||
|
|
||||||
// iter
|
// iter
|
||||||
self->_iter.hasnext = queue_iter_hasnext;
|
self->_iter.hasnext = queue_iter_hasnext;
|
||||||
self->_iter.next = queue_iter_next;
|
self->_iter.next = queue2_iter_next;
|
||||||
|
|
||||||
// -------------------- public --------------------
|
// -------------------- public --------------------
|
||||||
// kernel
|
// kernel
|
||||||
|
@ -586,6 +586,84 @@ static void test_queue2_struct(void)
|
|||||||
TEST_ASSERT_NULL(queue);
|
TEST_ASSERT_NULL(queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_queue_iter(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]);
|
||||||
|
|
||||||
|
queue_t queue = NULL;
|
||||||
|
queue = queue_new(sizeof(int));
|
||||||
|
TEST_ASSERT_NOT_NULL(queue);
|
||||||
|
queue->print_obj = print_num;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->push(queue, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, queue->size(queue));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[0], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator_t iter = queue->iter(queue);
|
||||||
|
i = 0;
|
||||||
|
while(iter->hasnext(iter))
|
||||||
|
{
|
||||||
|
temp = *(int *)iter->next(iter);
|
||||||
|
// printf("%d ", temp);
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
queue_free(&queue);
|
||||||
|
TEST_ASSERT_NULL(queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_queue2_iter(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;
|
||||||
|
|
||||||
|
queue_t queue = NULL;
|
||||||
|
queue = queue_new2(sizeof(int), capacity);
|
||||||
|
TEST_ASSERT_NOT_NULL(queue);
|
||||||
|
queue->print_obj = print_num;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->push(queue, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, queue->size(queue));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[0], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator_t iter = queue->iter(queue);
|
||||||
|
i = 0;
|
||||||
|
while(iter->hasnext(iter))
|
||||||
|
{
|
||||||
|
temp = *(int *)iter->next(iter);
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
queue_free(&queue);
|
||||||
|
TEST_ASSERT_NULL(queue);
|
||||||
|
}
|
||||||
|
|
||||||
void test_queue(void)
|
void test_queue(void)
|
||||||
{
|
{
|
||||||
UnitySetTestFile(__FILE__);
|
UnitySetTestFile(__FILE__);
|
||||||
@ -600,4 +678,7 @@ void test_queue(void)
|
|||||||
|
|
||||||
RUN_TEST(test_queue2_num);
|
RUN_TEST(test_queue2_num);
|
||||||
RUN_TEST(test_queue2_struct);
|
RUN_TEST(test_queue2_struct);
|
||||||
|
|
||||||
|
RUN_TEST(test_queue_iter);
|
||||||
|
RUN_TEST(test_queue2_iter);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user