mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-05 16:36:54 +08:00
Compare commits
6 Commits
51b41f46da
...
80f8722e2e
Author | SHA1 | Date | |
---|---|---|---|
80f8722e2e | |||
0c545dfca5 | |||
c8a48e6c63 | |||
851f815120 | |||
446ede15db | |||
6fb620850e |
@ -85,19 +85,18 @@ void* iter_next(); // 迭代器下一个元素
|
||||
// -------------------- 扩展功能 --------------------
|
||||
// 元素相关操作
|
||||
bool append(const void* obj); // 追加元素 <push_back> 一般用于list
|
||||
bool delete(const void* obj); // 删除元素
|
||||
// bool remove(const void *obj); // 删除元素 <同delete>
|
||||
// bool remove(const void *obj); // 删除元素 <暂不使用该命名>
|
||||
bool find(const void* obj); // 查找元素 <返回值,bool/uint32_t/void*待定?>
|
||||
bool contains(const void* obj); // 判断元素是否存在 <返回bool>
|
||||
uint32_t count(const void* obj); // 统计元素个数
|
||||
uint32_t count(const void* obj); // 统计元素obj的个数
|
||||
|
||||
// 索引相关操作
|
||||
uint32_t index(void *obj); // 获取元素索引
|
||||
bool insert(uint32_t index, const void* obj); // 插入元素 <非树>
|
||||
bool erase(uint32_t index); // 删除元素
|
||||
bool delete(uint32_t index, void* obj); // 删除元素
|
||||
// bool erase(uint32_t index); // 删除元素<暂时不用该命名>
|
||||
bool set(uint32_t index, const void* obj); // 设置元素
|
||||
bool get(uint32_t index, void* obj); // 获取元素
|
||||
|
||||
```
|
||||
|
||||
## 特点
|
||||
|
@ -140,6 +140,7 @@ static void demo_deque_num(void)
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
printf("----- push_front -----\n");
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
@ -173,6 +174,7 @@ static void demo_deque_num(void)
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
deque_free(&deque);
|
||||
}
|
||||
@ -334,7 +336,6 @@ static void demo_deque_struct(void)
|
||||
printf("----- print -----\n");
|
||||
deque->print(deque);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
printf("----- get -----\n");
|
||||
for (i = 0; i < len; i++)
|
||||
@ -346,6 +347,7 @@ static void demo_deque_struct(void)
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
deque_free(&deque);
|
||||
}
|
||||
|
@ -32,15 +32,15 @@ static void demo_list_num(void)
|
||||
printf("\n");
|
||||
|
||||
printf("----- pop -----\n");
|
||||
list->pop(list, 9, NULL);
|
||||
list->delete(list, 9, NULL);
|
||||
list->print(list);
|
||||
printf("\n");
|
||||
|
||||
list->pop(list, 0, NULL);
|
||||
list->delete(list, 0, NULL);
|
||||
list->print(list);
|
||||
printf("\n");
|
||||
|
||||
list->pop(list, 4, NULL);
|
||||
list->delete(list, 4, NULL);
|
||||
list->print(list);
|
||||
printf("\n");
|
||||
|
||||
@ -129,7 +129,7 @@ static void demo_list_num(void)
|
||||
printf("----- pop -----\n");
|
||||
for (i = 0; i < len + 1; i++)
|
||||
{
|
||||
list->pop(list, 0, &temp);
|
||||
list->pop(list, &temp);
|
||||
|
||||
if (list->empty(list))
|
||||
{
|
||||
@ -167,15 +167,15 @@ static void demo_list_struct(void)
|
||||
printf("\n");
|
||||
|
||||
printf("----- pop -----\n");
|
||||
list->pop(list, 9, NULL);
|
||||
list->delete(list, 9, NULL);
|
||||
list->print(list);
|
||||
printf("\n");
|
||||
|
||||
list->pop(list, 0, NULL);
|
||||
list->delete(list, 0, NULL);
|
||||
list->print(list);
|
||||
printf("\n");
|
||||
|
||||
list->pop(list, 4, NULL);
|
||||
list->delete(list, 4, NULL);
|
||||
list->print(list);
|
||||
printf("\n");
|
||||
|
||||
@ -286,7 +286,7 @@ static void demo_list_struct(void)
|
||||
printf("----- pop -----\n");
|
||||
for (i = 0; i < len + 1; i++)
|
||||
{
|
||||
list->pop(list, 0, &temp);
|
||||
list->pop(list, &temp);
|
||||
|
||||
if (list->empty(list))
|
||||
{
|
||||
|
@ -49,29 +49,15 @@ struct _deque
|
||||
bool (*pop_front)(struct _deque* self, void* obj);
|
||||
bool (*back)(struct _deque* self, void* obj);
|
||||
bool (*front)(struct _deque* self, void* obj);
|
||||
bool (*empty)(struct _deque* self);
|
||||
|
||||
|
||||
// base
|
||||
uint32_t(*size)(struct _deque* self);
|
||||
bool (*clear)(struct _deque* self);
|
||||
bool (*empty)(struct _deque* self);
|
||||
|
||||
// iter
|
||||
iterator_t (*iter)(struct _deque* self, enum _deque_order order);
|
||||
|
||||
// ohters
|
||||
bool (*insert)(struct _deque* self, int index, void* obj);
|
||||
bool (*erase)(struct _deque* self, int index, void* obj);
|
||||
|
||||
int (*index)(struct _deque* self, void* obj);
|
||||
bool (*remove)(struct _deque* self, void* obj);
|
||||
|
||||
bool (*get)(struct _deque* self, int index, void* obj);
|
||||
bool (*set)(struct _deque* self, int index, void* obj);
|
||||
|
||||
// compare
|
||||
// int (*compare)(void* obj, void* obj2);
|
||||
// bool (*sort)(struct _deque* self, uint8_t reserve);
|
||||
|
||||
// -------------------- debug --------------------
|
||||
void (*print)(struct _deque* self);
|
||||
void (*print_obj)(void* obj);
|
||||
|
@ -2,6 +2,10 @@
|
||||
* @file list.h
|
||||
* @author wenjf (Orig5826@163.com)
|
||||
* @brief
|
||||
*
|
||||
* @details dynamic array list.
|
||||
* similar to python list/ java ArrayList / C++ std::vector
|
||||
*
|
||||
* @version 0.1
|
||||
* @date 2024-06-23
|
||||
*
|
||||
@ -31,16 +35,18 @@ struct _list
|
||||
|
||||
// -------------------- public --------------------
|
||||
// kernel
|
||||
bool (*append)(struct _list *self, void *obj); // Append object to the end of the list.
|
||||
bool (*insert)(struct _list *self, int index, void *obj); // Insert object before index.
|
||||
bool (*pop)(struct _list *self, int index, void *obj); // Remove and return item at index.
|
||||
|
||||
int (*index)(struct _list *self, void *obj); // Return first index of obj. Return -1 if the obj is not present.
|
||||
bool (*remove)(struct _list *self, void *obj); // Remove first occurrence of obj.
|
||||
bool (*append)(struct _list *self, void *obj);
|
||||
bool (*pop)(struct _list *self, void *obj);
|
||||
|
||||
bool (*insert)(struct _list *self, int index, void *obj);
|
||||
bool (*delete)(struct _list *self, int index, void *obj);
|
||||
|
||||
bool (*get)(struct _list *self, int index, void *obj);
|
||||
bool (*set)(struct _list *self, int index, void *obj);
|
||||
|
||||
int (*index)(struct _list *self, void *obj); // retval -1 if not found
|
||||
// bool (*contains)(struct _list *self, void *obj);
|
||||
|
||||
// base
|
||||
uint32_t (*size)(struct _list *self);
|
||||
uint32_t (*capacity)(struct _list *self);
|
||||
@ -50,16 +56,13 @@ struct _list
|
||||
// iter
|
||||
iterator_t (*iter)(struct _list *self);
|
||||
|
||||
// sort
|
||||
// bool (*reverse)(struct _list* self); // Reverse *IN PLACE*.
|
||||
/**
|
||||
Sort the list in ascending order and return false.
|
||||
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
|
||||
order of two equal elements is maintained).
|
||||
The reverse flag can be set to sort in descending order.
|
||||
*/
|
||||
// bool (*sort)(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2));
|
||||
// config
|
||||
compare_fun_t compare; // !!! you have to implement this function
|
||||
|
||||
// copy
|
||||
struct _list* (*slice)(struct _list *self, int start, int end, int step);
|
||||
// struct _list* (*copy)(struct _list *self);
|
||||
|
||||
// -------------------- debug --------------------
|
||||
void (*print)(struct _list *self);
|
||||
void (*print_obj)(void *obj);
|
||||
|
66
src/deque.c
66
src/deque.c
@ -198,26 +198,6 @@ static bool deque_front(struct _deque* self, void* obj)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool deque_insert(struct _deque* self, int index, void* obj)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool deque_erase(struct _deque* self, int index, void* obj)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static int deque_index(struct _deque* self, void* obj)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
static bool deque_remove(struct _deque* self, void* obj)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool deque_clear(struct _deque* self)
|
||||
{
|
||||
while (!self->empty(self))
|
||||
@ -227,38 +207,6 @@ static bool deque_clear(struct _deque* self)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool deque_get(struct _deque* self, int index, void* obj)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(obj != NULL);
|
||||
assert(index >= 0 && index < self->size(self));
|
||||
|
||||
struct _deque_node* node = self->_head; // front
|
||||
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
node = node->next;
|
||||
}
|
||||
memmove(obj, node->obj, self->_obj_size);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool deque_set(struct _deque* self, int index, void* obj)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(obj != NULL);
|
||||
assert(index >= 0 && index < self->size(self));
|
||||
|
||||
struct _deque_node* node = self->_head; // front
|
||||
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
node = node->next;
|
||||
}
|
||||
memmove(node->obj, obj, self->_obj_size);
|
||||
return true;
|
||||
}
|
||||
|
||||
static uint32_t deque_size(struct _deque* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
@ -384,25 +332,15 @@ static bool deque_init(struct _deque* self, uint32_t obj_size)
|
||||
self->pop_front = deque_pop_front;
|
||||
self->back = deque_back;
|
||||
self->front = deque_front;
|
||||
self->empty = deque_empty;
|
||||
|
||||
|
||||
// base
|
||||
self->clear = deque_clear;
|
||||
self->size = deque_size;
|
||||
self->empty = deque_empty;
|
||||
|
||||
// iter
|
||||
self->iter = deque_iter;
|
||||
|
||||
// others
|
||||
self->insert = deque_insert;
|
||||
self->erase = deque_erase;
|
||||
|
||||
self->index = deque_index;
|
||||
self->remove = deque_remove;
|
||||
|
||||
self->set = deque_set;
|
||||
self->get = deque_get;
|
||||
|
||||
// -------------------- debug --------------------
|
||||
self->print = deque_print;
|
||||
|
||||
|
117
src/list.c
117
src/list.c
@ -10,34 +10,15 @@
|
||||
*/
|
||||
#include "list.h"
|
||||
|
||||
static bool list_append(struct _list* self, void* obj)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(self->obj != NULL);
|
||||
assert(obj != NULL);
|
||||
|
||||
if (self->size(self) == self->_capacity)
|
||||
{
|
||||
int capacity = self->_capacity * self->_ratio;
|
||||
void * obj_new = (void *)realloc(self->obj, capacity * self->_obj_size);
|
||||
if (obj_new == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
self->obj = obj_new;
|
||||
self->_capacity = capacity;
|
||||
}
|
||||
uint32_t index = self->size(self);
|
||||
uint32_t offset = index * self->_obj_size;
|
||||
memmove((char*)self->obj + offset, obj, self->_obj_size);
|
||||
|
||||
self->_size += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool list_insert(struct _list* self, int index, void* obj)
|
||||
{
|
||||
assert(index >= 0 && index < (int)self->size(self));
|
||||
// assert(index >= 0 && index < (int)self->size(self));
|
||||
// assert(index >= 0 && index <= (int)self->size(self));
|
||||
if(index < 0 || index > (int)self->size(self))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (self->size(self) == self->_capacity)
|
||||
{
|
||||
@ -52,16 +33,24 @@ static bool list_insert(struct _list* self, int index, void* obj)
|
||||
}
|
||||
uint32_t offset = index * self->_obj_size;
|
||||
uint32_t offset1 = (index + 1) * self->_obj_size;
|
||||
uint32_t count = self->size(self) - index;
|
||||
|
||||
memmove((char*)self->obj + offset, (char*)self->obj + offset, self->size(self) * self->_obj_size);
|
||||
// move data to right
|
||||
memmove((char*)self->obj + offset1, (char*)self->obj + offset, count * self->_obj_size);
|
||||
// copy new data
|
||||
memmove((char*)self->obj + offset, obj, self->_obj_size);
|
||||
self->_size += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool list_pop(struct _list* self, int index, void* obj)
|
||||
static bool list_delete(struct _list* self, int index, void* obj)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self));
|
||||
// assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self));
|
||||
if(index < (int)(0 - self->size(self)) || index >= (int)self->size(self))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (self->empty(self))
|
||||
{
|
||||
@ -73,9 +62,9 @@ static bool list_pop(struct _list* self, int index, void* obj)
|
||||
index += self->size(self);
|
||||
}
|
||||
|
||||
uint32_t count = self->size(self) - 1 - index;
|
||||
uint32_t offset = index * self->_obj_size;
|
||||
uint32_t offset1 = (index + 1) * self->_obj_size;
|
||||
uint32_t count = self->size(self) - 1 - index;
|
||||
if (obj != NULL)
|
||||
{
|
||||
memmove(obj, (char*)self->obj + offset, self->_obj_size);
|
||||
@ -85,15 +74,39 @@ static bool list_pop(struct _list* self, int index, void* obj)
|
||||
return true;
|
||||
}
|
||||
|
||||
static int list_index(struct _list* self, void* obj)
|
||||
static bool list_append(struct _list* self, void* obj)
|
||||
{
|
||||
return 0;
|
||||
return self->insert(self, (int)self->size(self), obj);
|
||||
}
|
||||
|
||||
static bool list_remove(struct _list* self, void* obj)
|
||||
static bool list_pop(struct _list* self, void* obj)
|
||||
{
|
||||
if (self->empty(self))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return self->delete(self, (int)self->size(self) - 1, obj);
|
||||
}
|
||||
|
||||
static int list_index(struct _list* self, void* obj)
|
||||
{
|
||||
assert(self != NULL);
|
||||
return true;
|
||||
int index = 0;
|
||||
if(obj == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
while(index < (int)self->size(self))
|
||||
{
|
||||
if(self->compare(self->obj + index * self->_obj_size, obj) == 0)
|
||||
{
|
||||
return index;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static bool list_clear(struct _list* self)
|
||||
@ -133,25 +146,22 @@ static bool list_set(struct _list* self, int index, void* obj)
|
||||
|
||||
static uint32_t list_size(struct _list* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
return self->_size;
|
||||
}
|
||||
|
||||
static uint32_t list_capacity(struct _list* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
return self->_capacity;
|
||||
}
|
||||
|
||||
static bool list_empty(struct _list* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
return !self->size(self);
|
||||
}
|
||||
|
||||
static bool list_reverse(struct _list* self)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool list_sort(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// free
|
||||
static void list_destory(struct _list* self)
|
||||
{
|
||||
@ -240,27 +250,26 @@ static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
|
||||
// -------------------- public --------------------
|
||||
// kernel
|
||||
list->append = list_append;
|
||||
list->insert = list_insert;
|
||||
list->pop = list_pop;
|
||||
|
||||
list->empty = list_empty;
|
||||
list->insert = list_insert;
|
||||
list->delete = list_delete;
|
||||
|
||||
list->get = list_get;
|
||||
list->set = list_set;
|
||||
|
||||
list->index = list_index;
|
||||
// list->contains = list_contains;
|
||||
|
||||
// base
|
||||
list->clear = list_clear;
|
||||
list->size = list_size;
|
||||
list->empty = list_empty;
|
||||
list->capacity = list_capacity;
|
||||
|
||||
// iter
|
||||
list->iter = list_iter;
|
||||
|
||||
// others
|
||||
list->index = list_index;
|
||||
list->remove = list_remove;
|
||||
list->get = list_get;
|
||||
list->set = list_set;
|
||||
|
||||
// list->reverse = list_reverse;
|
||||
// list->sort = list_sort;
|
||||
|
||||
// -------------------- debug --------------------
|
||||
list->print = list_print;
|
||||
|
||||
|
@ -113,6 +113,7 @@ static void test_deque_num(void)
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
TEST_ASSERT_TRUE(deque->push_front(deque, &data[i]));
|
||||
@ -130,6 +131,7 @@ static void test_deque_num(void)
|
||||
{
|
||||
TEST_ASSERT_TRUE(deque->get(deque, i, &temp));
|
||||
}
|
||||
#endif
|
||||
|
||||
deque_free(&deque);
|
||||
TEST_ASSERT_NULL(deque);
|
||||
@ -254,10 +256,12 @@ static void test_deque_struct(void)
|
||||
TEST_ASSERT_TRUE(deque->push_front(deque, &data[i]));
|
||||
}
|
||||
|
||||
#if 0
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
TEST_ASSERT_TRUE(deque->get(deque, i, &temp));
|
||||
}
|
||||
#endif
|
||||
|
||||
deque_free(&deque);
|
||||
TEST_ASSERT_NULL(deque);
|
||||
|
124
test/test_list.c
124
test/test_list.c
@ -1,12 +1,12 @@
|
||||
/**
|
||||
* @file test_list->c
|
||||
* @author wenjf (Orig5826@163.com)
|
||||
* @brief
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2024-08-30
|
||||
*
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*
|
||||
*
|
||||
*/
|
||||
#include "test.h"
|
||||
|
||||
@ -15,7 +15,7 @@ static void test_list_new(void)
|
||||
list_t list = NULL;
|
||||
list = list_new2(sizeof(int), 1);
|
||||
TEST_ASSERT_NOT_NULL(list);
|
||||
list_free(&list);
|
||||
list_free(&list);
|
||||
TEST_ASSERT_NULL(list);
|
||||
|
||||
list_free(&list); // list_free(NULL);
|
||||
@ -39,7 +39,7 @@ static void test_list_append(void)
|
||||
// ------------------------------
|
||||
list = list_new2(sizeof(int), len);
|
||||
TEST_ASSERT_TRUE(list->empty(list));
|
||||
for(i = 0; i < len; i++)
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
TEST_ASSERT_TRUE(list->append(list, &data[i]));
|
||||
TEST_ASSERT_EQUAL_INT(i + 1, list->size(list));
|
||||
@ -54,7 +54,7 @@ static void test_list_append(void)
|
||||
// ------------------------------
|
||||
// if capacity is less than data len
|
||||
list = list_new2(sizeof(int), len - 2);
|
||||
for(i = 0; i < len; i++)
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
TEST_ASSERT_TRUE(list->append(list, &data[i]));
|
||||
TEST_ASSERT_EQUAL_INT(i + 1, list->size(list));
|
||||
@ -74,16 +74,16 @@ static void test_list_pop(void)
|
||||
|
||||
// ------------------------------
|
||||
list = list_new2(sizeof(int), len);
|
||||
for(i = 0; i < len; i++)
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
list->append(list, &data[i]);
|
||||
}
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
TEST_ASSERT_TRUE(list->pop(list, 0, &temp));
|
||||
TEST_ASSERT_TRUE(list->pop(list, &temp));
|
||||
}
|
||||
TEST_ASSERT_TRUE(list->empty(list));
|
||||
// TEST_ASSERT_FALSE(list->pop(list, 1, &temp));
|
||||
TEST_ASSERT_FALSE(list->pop(list, &temp));
|
||||
list_free(&list);
|
||||
}
|
||||
|
||||
@ -100,7 +100,7 @@ static void test_list_clear(void)
|
||||
// ------------------------------
|
||||
list = list_new2(sizeof(int), len);
|
||||
TEST_ASSERT_TRUE(list->clear(list));
|
||||
for(i = 0; i < len; i++)
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
list->append(list, &data[i]);
|
||||
}
|
||||
@ -129,9 +129,9 @@ static void test_list_num(void)
|
||||
{
|
||||
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->delete(list, 9, NULL));
|
||||
TEST_ASSERT_TRUE(list->delete(list, 0, NULL));
|
||||
TEST_ASSERT_TRUE(list->delete(list, 4, NULL));
|
||||
TEST_ASSERT_TRUE(list->clear(list));
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
@ -182,7 +182,7 @@ static void test_list_num(void)
|
||||
|
||||
for (i = 0; i < len + 1; i++)
|
||||
{
|
||||
TEST_ASSERT_TRUE(list->pop(list, 0, &temp));
|
||||
TEST_ASSERT_TRUE(list->pop(list, &temp));
|
||||
|
||||
if (list->empty(list))
|
||||
{
|
||||
@ -199,10 +199,10 @@ static void test_list_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,
|
||||
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
|
||||
"feng", 1009, "cheng",1010,
|
||||
};
|
||||
struct _student temp = {0};
|
||||
struct _student temp = { 0 };
|
||||
int index = 0;
|
||||
int len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
@ -213,9 +213,9 @@ static void test_list_struct(void)
|
||||
{
|
||||
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->delete(list, 9, NULL));
|
||||
TEST_ASSERT_TRUE(list->delete(list, 0, NULL));
|
||||
TEST_ASSERT_TRUE(list->delete(list, 4, NULL));
|
||||
|
||||
TEST_ASSERT_TRUE(list->clear(list));
|
||||
for (i = 0; i < len; i++)
|
||||
@ -267,12 +267,12 @@ static void test_list_struct(void)
|
||||
TEST_ASSERT_TRUE(list->set(list, index, &temp));
|
||||
|
||||
index = -10;
|
||||
temp = (struct _student){"robot", 97};
|
||||
temp = (struct _student){ "robot", 97 };
|
||||
TEST_ASSERT_TRUE(list->set(list, index, &temp));
|
||||
|
||||
for (i = 0; i < len + 1; i++)
|
||||
{
|
||||
TEST_ASSERT_TRUE(list->pop(list, 0, &temp));
|
||||
TEST_ASSERT_TRUE(list->pop(list, &temp));
|
||||
|
||||
if (list->empty(list))
|
||||
{
|
||||
@ -284,51 +284,6 @@ static void test_list_struct(void)
|
||||
TEST_ASSERT_NULL(list);
|
||||
}
|
||||
|
||||
#if 0
|
||||
static void test_list_iter(void)
|
||||
{
|
||||
int temp = 0;
|
||||
int data[32] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||
// uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
uint32_t len = 10;
|
||||
uint32_t i = 0;
|
||||
int buff[32];
|
||||
int count = 0;
|
||||
|
||||
list_t list = NULL;
|
||||
|
||||
// ------------------------------
|
||||
list = list_new2(sizeof(int), len);
|
||||
TEST_ASSERT_TRUE(list->clear(list));
|
||||
for(i = 0; i < len; i++)
|
||||
{
|
||||
list->append(list, &data[i]);
|
||||
}
|
||||
|
||||
int * iter = NULL;
|
||||
|
||||
iter = list->begin(list);
|
||||
for(count = 0, i = 0; i < len + 12; i++)
|
||||
{
|
||||
if(i < len)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_INT(data[i % len], *iter);
|
||||
}
|
||||
iter = list->next(list);
|
||||
}
|
||||
for(count=0, iter = list->begin(list); iter != list->end(list); iter = list->next(list))
|
||||
{
|
||||
buff[count++] = *iter;
|
||||
}
|
||||
TEST_ASSERT_EQUAL_INT_ARRAY(data, buff, count);
|
||||
|
||||
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);
|
||||
}
|
||||
#else
|
||||
void test_list_iter(void)
|
||||
{
|
||||
int temp = 0;
|
||||
@ -343,7 +298,7 @@ void test_list_iter(void)
|
||||
TEST_ASSERT_TRUE(list->empty(list));
|
||||
list->print_obj = print_num;
|
||||
|
||||
for(i = 0; i < len; i++)
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
TEST_ASSERT_TRUE(list->append(list, &data[i]));
|
||||
TEST_ASSERT_EQUAL_INT(i + 1, list->size(list));
|
||||
@ -357,7 +312,7 @@ void test_list_iter(void)
|
||||
iterator_t iter = list->iter(list);
|
||||
int iter_data = 0;
|
||||
int idx = 0;
|
||||
while(iter->hasnext(iter))
|
||||
while (iter->hasnext(iter))
|
||||
{
|
||||
iter_data = *(int*)iter->next(iter);
|
||||
// printf("%d ", iter_data);
|
||||
@ -366,7 +321,34 @@ void test_list_iter(void)
|
||||
}
|
||||
list_free(&list);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
static void test_list_index(void)
|
||||
{
|
||||
int temp = 0;
|
||||
int data[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
uint32_t i = 0;
|
||||
|
||||
list_t list = NULL;
|
||||
|
||||
// ------------------------------
|
||||
list = list_new2(sizeof(int), len);
|
||||
list->compare = compare_num;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
TEST_ASSERT_TRUE(list->append(list, &data[i]));
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(0, list->index(list, &data[0]));
|
||||
TEST_ASSERT_EQUAL_INT(5, list->index(list, &data[5]));
|
||||
TEST_ASSERT_EQUAL_INT(9, list->index(list, &data[9]));
|
||||
temp = 11;
|
||||
TEST_ASSERT_EQUAL_INT(-1, list->index(list, &temp));
|
||||
|
||||
list_free(&list);
|
||||
}
|
||||
|
||||
void test_list(void)
|
||||
{
|
||||
@ -381,4 +363,6 @@ void test_list(void)
|
||||
RUN_TEST(test_list_struct);
|
||||
|
||||
RUN_TEST(test_list_iter);
|
||||
|
||||
RUN_TEST(test_list_index);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user