想统一inset和pop但是编译测试不通

This commit is contained in:
建峰 2025-04-29 16:25:27 +08:00
parent 851f815120
commit c8a48e6c63
3 changed files with 131 additions and 109 deletions

View File

@ -44,7 +44,7 @@ struct _list
bool (*get)(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); 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); // bool (*contains)(struct _list *self, void *obj);
// base // base
@ -59,6 +59,10 @@ struct _list
// config // config
compare_fun_t compare; // !!! you have to implement this function 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 -------------------- // -------------------- debug --------------------
void (*print)(struct _list *self); void (*print)(struct _list *self);
void (*print_obj)(void *obj); void (*print_obj)(void *obj);

View File

@ -10,55 +10,15 @@
*/ */
#include "list.h" #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) 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) 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) static bool list_delete(struct _list* self, int index, void* obj)
{ {
assert(self != NULL); 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)) if (self->empty(self))
{ {
@ -106,9 +70,78 @@ static bool list_delete(struct _list* self, int index, void* obj)
return true; 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) 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) 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) static uint32_t list_size(struct _list* self)
{ {
assert(self != NULL);
return self->_size; return self->_size;
} }

View File

@ -284,51 +284,6 @@ static void test_list_struct(void)
TEST_ASSERT_NULL(list); 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) void test_list_iter(void)
{ {
int temp = 0; int temp = 0;
@ -366,7 +321,34 @@ void test_list_iter(void)
} }
list_free(&list); 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) void test_list(void)
{ {
@ -381,4 +363,6 @@ void test_list(void)
RUN_TEST(test_list_struct); RUN_TEST(test_list_struct);
RUN_TEST(test_list_iter); RUN_TEST(test_list_iter);
RUN_TEST(test_list_index);
} }