mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 19:41:36 +08:00
想统一inset和pop但是编译测试不通
This commit is contained in:
parent
851f815120
commit
c8a48e6c63
@ -44,7 +44,7 @@ struct _list
|
||||
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);
|
||||
int (*index)(struct _list *self, void *obj); // retval -1 if not found
|
||||
// bool (*contains)(struct _list *self, void *obj);
|
||||
|
||||
// base
|
||||
@ -58,6 +58,10 @@ struct _list
|
||||
|
||||
// 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);
|
||||
|
130
src/list.c
130
src/list.c
@ -10,55 +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_pop(struct _list* self, void* obj)
|
||||
{
|
||||
assert(self != NULL);
|
||||
|
||||
if (self->empty(self))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t index = self->size(self) - 1;
|
||||
uint32_t offset = index * self->_obj_size;
|
||||
uint32_t offset_next = (index + 1) * self->_obj_size;
|
||||
if (obj != NULL)
|
||||
{
|
||||
memmove(obj, (char*)self->obj + offset, self->_obj_size);
|
||||
}
|
||||
memmove((char*)self->obj + offset, (char*)self->obj + offset_next, 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)
|
||||
{
|
||||
@ -82,7 +42,11 @@ static bool list_insert(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))
|
||||
{
|
||||
@ -106,9 +70,78 @@ static bool list_delete(struct _list* self, int index, void* obj)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool list_append(struct _list* self, void* obj)
|
||||
{
|
||||
#if 1
|
||||
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;
|
||||
#endif
|
||||
return self->insert(self, self->size(self), obj);
|
||||
}
|
||||
|
||||
static bool list_pop(struct _list* self, void* obj)
|
||||
{
|
||||
#if 1
|
||||
assert(self != NULL);
|
||||
|
||||
if (self->empty(self))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t index = self->size(self) - 1;
|
||||
uint32_t offset = index * self->_obj_size;
|
||||
uint32_t offset1 = (index + 1) * self->_obj_size;
|
||||
if (obj != NULL)
|
||||
{
|
||||
memmove(obj, (char*)self->obj + offset, self->_obj_size);
|
||||
}
|
||||
memmove((char*)self->obj + offset, (char*)self->obj + offset1, self->_obj_size);
|
||||
self->_size -= 1;
|
||||
return true;
|
||||
#else
|
||||
return self->delete(self, self->size(self) - 1, obj);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int list_index(struct _list* self, void* obj)
|
||||
{
|
||||
return 0;
|
||||
assert(self != NULL);
|
||||
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)
|
||||
@ -148,6 +181,7 @@ 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;
|
||||
}
|
||||
|
||||
|
104
test/test_list.c
104
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,7 +74,7 @@ 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]);
|
||||
}
|
||||
@ -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]);
|
||||
}
|
||||
@ -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]);
|
||||
|
||||
@ -267,7 +267,7 @@ 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++)
|
||||
@ -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