From f512f72d2e511e8dcb46c334517f556d08121773 Mon Sep 17 00:00:00 2001 From: jf-home Date: Thu, 14 May 2026 00:32:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(darray):=20=E4=BC=98=E5=8C=96=E5=86=85?= =?UTF-8?q?=E5=AD=98=E7=AD=96=E7=95=A5=E5=B9=B6=E7=A7=BB=E9=99=A4=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E6=89=A9=E5=AE=B9=E5=BC=80=E5=85=B3=E3=80=82=E5=9B=BA?= =?UTF-8?q?=E5=AE=9A=E5=AE=B9=E9=87=8F=E5=9C=BA=E6=99=AF=E4=BA=A4=E7=94=B1?= =?UTF-8?q?=E4=B8=8A=E5=B1=82=E5=B0=81=E8=A3=85=EF=BC=8Cdarray=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E6=94=AF=E6=8C=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdparty/unicstl-unity/src/unity_config.h | 6 +- 3rdparty/unicstl-unity/src/unity_internals.h | 2 + doc/notes.md | 8 + include/darray.h | 9 +- include/unicstl_internal.h | 2 +- src/darray.c | 93 ++--- src/unicstl_internal.c | 4 +- test/test_darray.c | 383 ++++++++++++++++--- test/test_unicstal.c | 8 +- 9 files changed, 386 insertions(+), 129 deletions(-) create mode 100644 doc/notes.md diff --git a/3rdparty/unicstl-unity/src/unity_config.h b/3rdparty/unicstl-unity/src/unity_config.h index 6d5e8c8..4020395 100644 --- a/3rdparty/unicstl-unity/src/unity_config.h +++ b/3rdparty/unicstl-unity/src/unity_config.h @@ -2,8 +2,8 @@ #ifndef _UNITY_CONFIG_H_ #define _UNITY_CONFIG_H_ -// #define UNITY_OUTPUT_COLOR 1 -#define UNITY_USE_FLUSH_STDOUT 1 -#define UNITY_INCLUDE_PRINT_FORMATTED 1 // support TEST_PRINTF +#define UNITY_OUTPUT_COLOR // enable ANSI color output +#define UNITY_USE_FLUSH_STDOUT +#define UNITY_INCLUDE_PRINT_FORMATTED // support TEST_PRINTF #endif diff --git a/3rdparty/unicstl-unity/src/unity_internals.h b/3rdparty/unicstl-unity/src/unity_internals.h index 562f5b6..104230e 100644 --- a/3rdparty/unicstl-unity/src/unity_internals.h +++ b/3rdparty/unicstl-unity/src/unity_internals.h @@ -8,6 +8,8 @@ #ifndef UNITY_INTERNALS_H #define UNITY_INTERNALS_H +#define UNITY_INCLUDE_CONFIG_H + #ifdef UNITY_INCLUDE_CONFIG_H #include "unity_config.h" #endif diff --git a/doc/notes.md b/doc/notes.md new file mode 100644 index 0000000..3c640be --- /dev/null +++ b/doc/notes.md @@ -0,0 +1,8 @@ + +# 笔记 + +## 单元测试找bug + +`2026-05-14` +1. darray模块,insert传参obj没有判断NULL,导致crash +2. darray模块,reseze没有处理当realloc之后,size > capacity 的情况 diff --git a/include/darray.h b/include/darray.h index e4b47af..fea7371 100644 --- a/include/darray.h +++ b/include/darray.h @@ -22,17 +22,12 @@ struct _darray size_t _obj_size; size_t _size; size_t _capacity; - size_t _ratio; - size_t _cur; struct _iterator _iter; - void (*_destory)(struct _darray *self); // -------------------- public -------------------- // kernel - bool (*resize)(struct _darray *self, size_t capacity); - bool (*append)(struct _darray *self, const void *obj); // O(1) bool (*pop)(struct _darray *self, void *obj); // O(1) @@ -48,15 +43,13 @@ struct _darray bool (*contains)(struct _darray *self, const void *obj); // O(n) // base + bool (*resize)(struct _darray *self, size_t capacity); size_t (*size)(struct _darray *self); size_t (*capacity)(struct _darray *self); bool (*empty)(struct _darray *self); bool (*full)(struct _darray *self); bool (*clear)(struct _darray *self); - void (*set_dynamic)(struct _darray *self, bool enable); - bool (*dynamic)(struct _darray *self); - // iter iterator_t (*iter)(struct _darray *self); diff --git a/include/unicstl_internal.h b/include/unicstl_internal.h index dd9a7be..6780f64 100644 --- a/include/unicstl_internal.h +++ b/include/unicstl_internal.h @@ -34,7 +34,7 @@ * */ #ifndef UNICSTL_CAPACITY_INIT -#define UNICSTL_CAPACITY_INIT 8 // 最大容量 +#define UNICSTL_CAPACITY_INIT 8 // 若capacity参数为0时,自动分配默认初始容量 #endif #ifndef UNICSTL_CAPACITY_MAX diff --git a/src/darray.c b/src/darray.c index 9fe2ea6..b0a110c 100644 --- a/src/darray.c +++ b/src/darray.c @@ -61,9 +61,8 @@ static void darray_print(struct _darray *self) void *obj = NULL; size_t offset = 0; - - // for(int i = self->size(self) - 1; i >= 0; --i) - for(size_t i = 0; i < self->size(self); i++) + + for (size_t i = 0; i < self->size(self); i++) { offset = self->_obj_size * i; obj = (char *)self->obj + offset; @@ -71,36 +70,25 @@ static void darray_print(struct _darray *self) } } -static void darray_set_dynamic(struct _darray *self, bool enable) -{ - unicstl_assert(self != NULL); - if (enable) - { - self->_ratio = 2; - } - else - { - self->_ratio = 1; - } -} - -static bool darray_dynamic(struct _darray *self) -{ - unicstl_assert(self != NULL); - return self->_ratio == 1 ? false : true; -} - static bool darray_resize(struct _darray *self, size_t capacity) { unicstl_assert(self != NULL); + if(capacity == 0 || capacity > UNICSTL_CAPACITY_MAX) + { + return false; + } + void *new_obj = unicstl_realloc(self->obj, capacity * self->_obj_size); if (new_obj == NULL) { return false; } - self->obj = new_obj; self->_capacity = capacity; + if(self->_size > self->_capacity) + { + self->_size = self->_capacity; + } return true; } @@ -111,20 +99,20 @@ static bool darray_insert(struct _darray *self, int index, const void *obj) { return false; } + if(obj == NULL) + { + return false; + } + if (self->full(self)) { - if(self->dynamic(self) != true) - { - return false; - } - int new_capacity = unicstl_new_capacity(self->capacity(self)); if (darray_resize(self, new_capacity) == false) { return false; } } - + size_t offset = index * self->_obj_size; if (index < self->size(self)) { @@ -156,9 +144,9 @@ static bool darray_remove(struct _darray *self, int index, void *obj) size_t count = self->size(self) - 1 - index; if (obj != NULL) { - memmove(obj, (char*)self->obj + offset, self->_obj_size); + memmove(obj, (char *)self->obj + offset, self->_obj_size); } - memmove((char*)self->obj + offset, (char*)self->obj + offset1, count * self->_obj_size); + memmove((char *)self->obj + offset, (char *)self->obj + offset1, count * self->_obj_size); self->_size -= 1; return true; } @@ -176,7 +164,7 @@ static bool darray_pop(struct _darray *self, void *obj) static bool darray_set(struct _darray *self, int index, const void *obj) { unicstl_assert(self != NULL); - if(obj == NULL) + if (obj == NULL) { return false; } @@ -185,14 +173,14 @@ static bool darray_set(struct _darray *self, int index, const void *obj) return false; } size_t offset = index * self->_obj_size; - memmove((char*)self->obj + offset, obj, self->_obj_size); + memmove((char *)self->obj + offset, obj, self->_obj_size); return true; } static bool darray_get(struct _darray *self, int index, void *obj) { unicstl_assert(self != NULL); - if(obj == NULL) + if (obj == NULL) { return false; } @@ -201,11 +189,11 @@ static bool darray_get(struct _darray *self, int index, void *obj) return false; } size_t offset = index * self->_obj_size; - memmove(obj, (char*)self->obj + offset, self->_obj_size); + memmove(obj, (char *)self->obj + offset, self->_obj_size); return true; } -const void* darray_at(struct _darray *self, int index) +const void *darray_at(struct _darray *self, int index) { unicstl_assert(self != NULL); if (index < 0 || index >= self->size(self)) @@ -213,7 +201,7 @@ const void* darray_at(struct _darray *self, int index) return false; } size_t offset = index * self->_obj_size; - return (const char*)self->obj + offset; + return (const char *)self->obj + offset; } static int darray_index(struct _darray *self, const void *obj) @@ -226,7 +214,7 @@ static int darray_index(struct _darray *self, const void *obj) for (int i = 0; i < self->size(self); ++i) { - if (self->compare((const char*)self->obj + i * self->_obj_size, (const char*)obj) == 0) + if (self->compare((const char *)self->obj + i * self->_obj_size, (const char *)obj) == 0) { return i; } @@ -243,19 +231,14 @@ static bool darray_contains(struct _darray *self, const void *obj) static bool darray_init(struct _darray *self, size_t obj_size, size_t capacity) { unicstl_assert(self != NULL); - if (obj_size == 0 || capacity == 0) - { - return false; - } + unicstl_assert(obj_size != 0); // -------------------- private -------------------- self->_obj_size = obj_size; self->_size = 0; self->_capacity = capacity; - // self->_ratio = 2; self->obj = NULL; - self->_destory = darray_destory; // -------------------- public -------------------- @@ -272,13 +255,10 @@ static bool darray_init(struct _darray *self, size_t obj_size, size_t capacity) self->index = darray_index; self->contains = darray_contains; - self->empty = darray_empty; - self->full = darray_full; - self->set_dynamic = darray_set_dynamic; - self->dynamic = darray_dynamic; - self->size = darray_size; self->capacity = darray_capacity; + self->empty = darray_empty; + self->full = darray_full; self->clear = darray_clear; // -------------------- default -------------------- @@ -287,8 +267,11 @@ static bool darray_init(struct _darray *self, size_t obj_size, size_t capacity) // -------------------- debug -------------------- self->print = darray_print; - // -------------------- init -------------------- - self->resize(self, capacity); + // -------------------- malloc -------------------- + if(capacity > 0) + { + self->resize(self, capacity); + } return true; } @@ -296,12 +279,12 @@ darray_t darray_new(size_t obj_size, size_t capacity) { struct _darray *darray = NULL; darray = (struct _darray *)unicstl_malloc(sizeof(struct _darray)); - if(darray == NULL) + if (darray == NULL) { return NULL; } - if(darray_init(darray, obj_size, capacity) != true) + if (darray_init(darray, obj_size, capacity) != true) { free(darray); return NULL; @@ -311,9 +294,9 @@ darray_t darray_new(size_t obj_size, size_t capacity) void darray_free(darray_t *darray) { - if(darray != NULL && *darray != NULL) + if (darray != NULL && *darray != NULL) { - if((*darray)->_destory != NULL) + if ((*darray)->_destory != NULL) { (*darray)->_destory((*darray)); } diff --git a/src/unicstl_internal.c b/src/unicstl_internal.c index 196a863..95e4d9b 100644 --- a/src/unicstl_internal.c +++ b/src/unicstl_internal.c @@ -25,9 +25,9 @@ void default_print_obj(const void* obj) size_t unicstl_new_capacity(size_t capacity) { size_t new_capacity = 0; - if(capacity <= 8) + if(capacity == 0) { - new_capacity = 16; + new_capacity = UNICSTL_CAPACITY_INIT; } else if (capacity < 1024) { diff --git a/test/test_darray.c b/test/test_darray.c index 5553b62..fec5b07 100644 --- a/test/test_darray.c +++ b/test/test_darray.c @@ -10,18 +10,28 @@ */ #include "test.h" + static void test_darray_new(void) { + // [invalid param] if obj_size==0, unit test is not needed. because assert will be triggered. + // TEST_ASSERT_NULL(darray_new(0, 0)); + // TEST_ASSERT_NULL(darray_new(0, 1)); + darray_t darray = darray_new(sizeof(int), 10); TEST_ASSERT_NOT_NULL(darray); darray_free(&darray); - - TEST_ASSERT_NULL(darray_new(0, 0)); - TEST_ASSERT_NULL(darray_new(0, 1)); - TEST_ASSERT_NULL(darray_new(sizeof(int), 0)); - - // ------------------------------ TEST_ASSERT_NULL(darray); +} + +static void test_darray_new_lazy(void) +{ + darray_t darray = darray_new(sizeof(int), 0); + TEST_ASSERT_NOT_NULL(darray); + TEST_ASSERT_EQUAL_size_t(0, darray->capacity(darray)); + + int temp = 0; + TEST_ASSERT_TRUE(darray->insert(darray, 0, &temp)); + TEST_ASSERT_EQUAL_size_t(UNICSTL_CAPACITY_INIT, darray->capacity(darray)); darray_free(&darray); } @@ -49,7 +59,23 @@ static void test_darray_insert(void) } TEST_ASSERT_TRUE(darray->full(darray)); - // darray->print(darray); + darray_free(&darray); +} + +static void test_darray_insert_invalid(void) +{ + int temp = 0; + + darray_t darray = darray_new(sizeof(int), 0); + darray->print_obj = print_num; + + TEST_ASSERT_FALSE(darray->insert(darray, 1, &temp)); + TEST_ASSERT_FALSE(darray->insert(darray, -1, &temp)); + TEST_ASSERT_FALSE(darray->insert(darray, 999, &temp)); + + TEST_ASSERT_FALSE(darray->insert(darray, 0, NULL)); + + TEST_ASSERT_TRUE(darray->insert(darray, 0, &temp)); darray_free(&darray); } @@ -80,6 +106,18 @@ static void test_darray_append(void) darray_free(&darray); } +static void test_darray_append_invalid(void) +{ + int temp = 0; + + darray_t darray = darray_new(sizeof(int), 0); + darray->compare = compare_num; + + TEST_ASSERT_FALSE(darray->append(darray, NULL)); + + darray_free(&darray); +} + static void test_darray_remove(void) { int temp = 0; @@ -92,10 +130,8 @@ static void test_darray_remove(void) for(i = 0; i < len; i++) { - TEST_ASSERT_TRUE(darray->append(darray, &data[i])); - TEST_ASSERT_EQUAL_INT(i + 1, darray->size(darray)); + darray->append(darray, &data[i]); } - TEST_ASSERT_TRUE(darray->remove(darray, len - 1, &temp)); TEST_ASSERT_EQUAL_INT(data[len - 1], temp); TEST_ASSERT_FALSE(darray->full(darray)); @@ -111,6 +147,37 @@ static void test_darray_remove(void) TEST_ASSERT_TRUE(darray->remove(darray, 0, &temp)); TEST_ASSERT_EQUAL_INT(data[0], temp); TEST_ASSERT_TRUE(darray->empty(darray)); + + // ---------- no return ---------- + for(i = 0; i < 2; i++) + { + darray->append(darray, &data[i]); + } + TEST_ASSERT_FALSE(darray->remove(darray, 2, NULL)); + TEST_ASSERT_TRUE(darray->remove(darray, 1, NULL)); + TEST_ASSERT_TRUE(darray->remove(darray, 0, NULL)); + + darray_free(&darray); +} + +static void test_darray_remove_invalid(void) +{ + int temp = 0; + int data[] = { 1,2,3,4,5,6,7,8,9,10 }; + size_t len = sizeof(data) / sizeof(data[0]); + size_t i = 0; + + darray_t darray = darray_new(sizeof(int), len); + darray->compare = compare_num; + + for(i = 0; i < len; i++) + { + darray->append(darray, &data[i]); + } + TEST_ASSERT_FALSE(darray->remove(darray, len, &temp)); + TEST_ASSERT_FALSE(darray->remove(darray, -1, &temp)); + TEST_ASSERT_FALSE(darray->remove(darray, 999, &temp)); + darray_free(&darray); } @@ -126,10 +193,8 @@ static void test_darray_pop(void) for(i = 0; i < len; i++) { - TEST_ASSERT_TRUE(darray->append(darray, &data[i])); - TEST_ASSERT_EQUAL_INT(i + 1, darray->size(darray)); + darray->append(darray, &data[i]); } - TEST_ASSERT_TRUE(darray->full(darray)); for (i = 0; i < len; i++) { @@ -139,6 +204,16 @@ static void test_darray_pop(void) TEST_ASSERT_FALSE(darray->full(darray)); } TEST_ASSERT_TRUE(darray->empty(darray)); + + // ---------- no return ---------- + for(i = 0; i < 2; i++) + { + darray->append(darray, &data[i]); + } + TEST_ASSERT_TRUE(darray->pop(darray, NULL)); + TEST_ASSERT_TRUE(darray->pop(darray, NULL)); + TEST_ASSERT_FALSE(darray->pop(darray, NULL)); + darray_free(&darray); } @@ -179,6 +254,32 @@ static void test_darray_set(void) darray_free(&darray); } +static void test_darray_set_invalid(void) +{ + int temp = 0; + int data[] = { 1,2,3,4,5,6,7,8,9,10 }; + size_t len = sizeof(data) / sizeof(data[0]); + size_t i = 0; + + darray_t darray = darray_new(sizeof(int), len); + darray->compare = compare_num; + + for(i = 0; i < len; i++) + { + darray->append(darray, &data[i]); + } + + // ---------- invalid index ---------- + temp = 0x11; + TEST_ASSERT_FALSE(darray->set(darray, -1, &temp)); + TEST_ASSERT_FALSE(darray->set(darray, len, &temp)); + TEST_ASSERT_FALSE(darray->set(darray, 999, &temp)); + + TEST_ASSERT_FALSE(darray->set(darray, 0, NULL)); + + darray_free(&darray); +} + static void test_darray_resize(void) { int temp = 0; @@ -188,20 +289,39 @@ static void test_darray_resize(void) darray_t darray = darray_new(sizeof(int), 1); darray->compare = compare_num; - - // TEST_ASSERT_EQUAL_INT(8, darray->capacity(darray)); + + TEST_ASSERT_EQUAL_INT(1, darray->capacity(darray)); + darray->resize(darray, 16); + TEST_ASSERT_EQUAL_INT(16, darray->capacity(darray)); + for(i = 0; i < len; i++) { - TEST_ASSERT_TRUE(darray->append(darray, &data[i])); - TEST_ASSERT_EQUAL_INT(i + 1, darray->size(darray)); - - // printf("capacity: %d\n", darray->capacity(darray)); + darray->append(darray, &data[i]); } TEST_ASSERT_EQUAL_INT(16, darray->capacity(darray)); + TEST_ASSERT_EQUAL_INT(10, darray->size(darray)); + + TEST_ASSERT_TRUE(darray->resize(darray, 8)); + TEST_ASSERT_EQUAL_INT(8, darray->capacity(darray)); + for(i = 0; i < len; i++) + { + if(i < 8) + { + TEST_ASSERT_TRUE(darray->get(darray, i, &temp)); + TEST_ASSERT_EQUAL_INT(data[i], temp); + } + else + { + TEST_ASSERT_FALSE(darray->get(darray, i, &temp)); + } + } + TEST_ASSERT_EQUAL_INT(8, darray->capacity(darray)); + TEST_ASSERT_EQUAL_INT(8, darray->size(darray)); + darray_free(&darray); } -static void test_darray_clear(void) +static void test_darray_resize_invalid(void) { int temp = 0; int data[] = { 1,2,3,4,5,6,7,8,9,10 }; @@ -210,40 +330,11 @@ static void test_darray_clear(void) darray_t darray = darray_new(sizeof(int), len); darray->compare = compare_num; - for(i = 0; i < len; i++) - { - TEST_ASSERT_TRUE(darray->append(darray, &data[i])); - TEST_ASSERT_EQUAL_INT(i + 1, darray->size(darray)); - } - TEST_ASSERT_TRUE(darray->full(darray)); - - TEST_ASSERT_TRUE(darray->clear(darray)); - TEST_ASSERT_TRUE(darray->empty(darray)); - - darray_free(&darray); -} - -static void test_darray_dynamic(void) -{ - int temp = 0; - int data[] = { 1,2,3,4,5,6,7,8,9,10 }; - size_t len = sizeof(data) / sizeof(data[0]); - size_t i = 0; - - darray_t darray = darray_new(sizeof(int), 8); - darray->compare = compare_num; - TEST_ASSERT_TRUE(darray->dynamic(darray)); - darray->set_dynamic(darray, false); - TEST_ASSERT_FALSE(darray->dynamic(darray)); + TEST_ASSERT_EQUAL_INT(len, darray->capacity(darray)); + TEST_ASSERT_FALSE(darray->resize(darray, 0)); + TEST_ASSERT_FALSE(darray->resize(darray, -1)); - TEST_ASSERT_EQUAL_INT(8, darray->capacity(darray)); - for(i = 0; i < 8; i++) - { - TEST_ASSERT_TRUE(darray->append(darray, &data[i])); - TEST_ASSERT_EQUAL_INT(i + 1, darray->size(darray)); - } - TEST_ASSERT_FALSE(darray->append(darray, &data[i])); darray_free(&darray); } @@ -277,6 +368,166 @@ static void test_darray_index(void) TEST_ASSERT_EQUAL_INT(-1, darray->index(darray, &temp)); TEST_ASSERT_FALSE(darray->contains(darray, &temp)); + TEST_ASSERT_EQUAL_INT(-1, darray->index(darray, NULL)); + + darray_free(&darray); +} + +static void test_darray_index_invalid(void) +{ + int temp = 0; + int data[] = { 1,2,3,4,5,6,7,8,9,10 }; + size_t len = sizeof(data) / sizeof(data[0]); + size_t i = 0; + + darray_t darray = darray_new(sizeof(int), len); + darray->compare = compare_num; + for(i = 0; i < len; i++) + { + darray->append(darray, &data[i]); + } + + temp = 11; + TEST_ASSERT_EQUAL_INT(-1, darray->index(darray, &temp)); + TEST_ASSERT_FALSE(darray->contains(darray, &temp)); + + TEST_ASSERT_EQUAL_INT(-1, darray->index(darray, NULL)); + TEST_ASSERT_FALSE(darray->contains(darray, NULL)); + + darray_free(&darray); +} + + +static void test_darray_at(void) +{ + int temp = 0; + int data[] = { 1,2,3,4,5,6,7,8,9,10 }; + size_t len = sizeof(data) / sizeof(data[0]); + size_t i = 0; + + darray_t darray = darray_new(sizeof(int), len); + darray->compare = compare_num; + for(i = 0; i < len; i++) + { + darray->append(darray, &data[i]); + } + + const int *p_int = NULL; + p_int = darray->at(darray, 0); + TEST_ASSERT_EQUAL_INT(1, *p_int); + + p_int = darray->at(darray, 4); + TEST_ASSERT_EQUAL_INT(5, *p_int); + + p_int = darray->at(darray, 9); + TEST_ASSERT_EQUAL_INT(10, *p_int); + + TEST_ASSERT_NULL(darray->at(darray, 10)); + TEST_ASSERT_NULL(darray->at(darray, -1)); + + + // warning: initialization discards 'const' qualifier from pointer target type + // int *p_int_warring = darray->at(darray, 0); + + // !!! you should not do this. + int *p_int_warring = (int *)darray->at(darray, 0); + *p_int_warring = 100; + + darray->get(darray, 0, &temp); + TEST_ASSERT_EQUAL_INT(100, temp); + + darray_free(&darray); +} + +static void test_darray_dynamic(void) +{ + int temp = 0; + int data[] = { 1,2,3,4,5,6,7,8,9,10 }; + + size_t i = 0; + size_t len = 2; + darray_t darray = darray_new(sizeof(int), len); + + for(i = 0; i < len; i++) + { + TEST_ASSERT_TRUE(darray->insert(darray, 0, &data[i])); + TEST_ASSERT_EQUAL_INT(i + 1, darray->size(darray)); + + TEST_ASSERT_TRUE(darray->get(darray, 0, &temp)); + TEST_ASSERT_EQUAL_INT(data[i], temp); + + TEST_ASSERT_EQUAL_INT(len, darray->capacity(darray)); + if(i < len - 1) + { + TEST_ASSERT_FALSE(darray->full(darray)); + } + else + { + TEST_ASSERT_TRUE(darray->full(darray)); + } + } + + len *= 2; + for(; i < len; i++) + { + TEST_ASSERT_TRUE(darray->insert(darray, 0, &data[i])); + TEST_ASSERT_EQUAL_INT(i + 1, darray->size(darray)); + + TEST_ASSERT_TRUE(darray->get(darray, 0, &temp)); + TEST_ASSERT_EQUAL_INT(data[i], temp); + + TEST_ASSERT_EQUAL_INT(len, darray->capacity(darray)); + if(i < len - 1) + { + TEST_ASSERT_FALSE(darray->full(darray)); + } + else + { + TEST_ASSERT_TRUE(darray->full(darray)); + } + } + + len *= 2; + for(; i < len; i++) + { + TEST_ASSERT_TRUE(darray->insert(darray, 0, &data[i])); + TEST_ASSERT_EQUAL_INT(i + 1, darray->size(darray)); + + TEST_ASSERT_TRUE(darray->get(darray, 0, &temp)); + TEST_ASSERT_EQUAL_INT(data[i], temp); + + TEST_ASSERT_EQUAL_INT(len, darray->capacity(darray)); + if(i < len - 1) + { + TEST_ASSERT_FALSE(darray->full(darray)); + } + else + { + TEST_ASSERT_TRUE(darray->full(darray)); + } + } + + darray_free(&darray); +} + +static void test_darray_clear(void) +{ + int temp = 0; + int data[] = { 1,2,3,4,5,6,7,8,9,10 }; + size_t len = sizeof(data) / sizeof(data[0]); + size_t i = 0; + + darray_t darray = darray_new(sizeof(int), len); + darray->compare = compare_num; + for(i = 0; i < len; i++) + { + darray->append(darray, &data[i]); + } + TEST_ASSERT_TRUE(darray->full(darray)); + + TEST_ASSERT_TRUE(darray->clear(darray)); + TEST_ASSERT_TRUE(darray->empty(darray)); + darray_free(&darray); } @@ -326,17 +577,37 @@ void test_darray(void) { UnitySetTestFile(__FILE__); + // ---------- kernel ---------- RUN_TEST(test_darray_new); + RUN_TEST(test_darray_new_lazy); + RUN_TEST(test_darray_insert); + RUN_TEST(test_darray_insert_invalid); + RUN_TEST(test_darray_append); + RUN_TEST(test_darray_append_invalid); + RUN_TEST(test_darray_remove); + RUN_TEST(test_darray_remove_invalid); + RUN_TEST(test_darray_pop); RUN_TEST(test_darray_set); - RUN_TEST(test_darray_resize); - RUN_TEST(test_darray_clear); - RUN_TEST(test_darray_dynamic); - RUN_TEST(test_darray_index); + RUN_TEST(test_darray_set_invalid); + RUN_TEST(test_darray_resize); + RUN_TEST(test_darray_resize_invalid); + + RUN_TEST(test_darray_index); + RUN_TEST(test_darray_index_invalid); + + RUN_TEST(test_darray_at); + + RUN_TEST(test_darray_dynamic); + + // ---------- base ---------- + RUN_TEST(test_darray_clear); + + // ---------- ext ---------- RUN_TEST(test_darray_struct); } diff --git a/test/test_unicstal.c b/test/test_unicstal.c index 2b4e13d..577e018 100644 --- a/test/test_unicstal.c +++ b/test/test_unicstal.c @@ -12,11 +12,11 @@ void test_unicstl_capacity(void) { - TEST_ASSERT_EQUAL_UINT32(16, unicstl_new_capacity(0)); - TEST_ASSERT_EQUAL_UINT32(16, unicstl_new_capacity(1)); - TEST_ASSERT_EQUAL_UINT32(16, unicstl_new_capacity(3)); + TEST_ASSERT_EQUAL_UINT32(UNICSTL_CAPACITY_INIT, unicstl_new_capacity(0)); - TEST_ASSERT_EQUAL_UINT32(16, unicstl_new_capacity(4)); + TEST_ASSERT_EQUAL_UINT32(2, unicstl_new_capacity(1)); + TEST_ASSERT_EQUAL_UINT32(6, unicstl_new_capacity(3)); + TEST_ASSERT_EQUAL_UINT32(8, unicstl_new_capacity(4)); TEST_ASSERT_EQUAL_UINT32(16, unicstl_new_capacity(8)); TEST_ASSERT_EQUAL_UINT32(1024, unicstl_new_capacity(512));