update controller code

This commit is contained in:
Rongsong Shen 2025-11-05 16:58:57 +08:00
parent 2836d196da
commit 41f56fa6ed
54 changed files with 9688 additions and 214 deletions

View file

@ -5,38 +5,41 @@
#include "zarray.h"
static void test_zarray_create(void) {
static void test_zarray_create(void)
{
zarray_t array;
zarray_init(&array, 8, NULL);
TEST_ASSERT_EQUAL(0,array.count);
TEST_ASSERT_EQUAL(0, array.count);
TEST_ASSERT_NULL(array.data);
}
static void test_zarray_store(void) {
static void test_zarray_store(void)
{
zarray_t array;
int idx;
char *s1 = "hello";
char *s2 = NULL;
int idx;
char* s1 = "hello";
char* s2 = NULL;
zarray_init(&array, 8, NULL);
idx = zarray_store(&array, (void *)s1);
idx = zarray_store(&array, (void*)s1);
TEST_ASSERT_TRUE(idx >= 0);
s2 = (char *)zarray_get(&array, idx);
s2 = (char*)zarray_get(&array, idx);
TEST_ASSERT_NOT_NULL(s2);
TEST_ASSERT_EQUAL(0, strcmp(s1, s2));
zarray_fini(&(array));
}
static void test_zarray_del(void) {
static void test_zarray_del(void)
{
zarray_t array;
int idx;
char *s1 = "hello";
int idx;
char* s1 = "hello";
zarray_init(&array, 8, NULL);
idx = zarray_store(&array, (void *)s1);
idx = zarray_store(&array, (void*)s1);
TEST_ASSERT_TRUE(idx >= 0);
TEST_ASSERT_TRUE(array.count == 1);
zarray_del(&array, idx);
@ -46,52 +49,56 @@ static void test_zarray_del(void) {
zarray_fini(&array);
}
static void test_zarray_put(void) {
static void test_zarray_put(void)
{
zarray_t array;
int idx;
char *s1 = "hello";
char *s2 = "test";
int idx;
char* s1 = "hello";
char* s2 = "test";
zarray_init(&array, 8, NULL);
idx = zarray_store(&array, (void *)s1);
idx = zarray_store(&array, (void*)s1);
TEST_ASSERT_TRUE(idx >= 0);
TEST_ASSERT_TRUE(array.count == 1);
TEST_ASSERT_TRUE(zarray_get(&array, idx) != NULL);
TEST_ASSERT_TRUE(strcmp((char *)zarray_get(&array, idx), s1) == 0);
zarray_put(&array, idx, (void *)s2);
TEST_ASSERT_TRUE(strcmp((char*)zarray_get(&array, idx), s1) == 0);
zarray_put(&array, idx, (void*)s2);
TEST_ASSERT_TRUE(zarray_get(&array, idx) != NULL);
TEST_ASSERT_TRUE(strcmp((char *)zarray_get(&array, idx), s2) == 0);
TEST_ASSERT_TRUE(strcmp((char*)zarray_get(&array, idx), s2) == 0);
zarray_fini(&array);
}
static int sum_(void *arg1, void *arg2) {
int *pv = (int *)arg1;
int x = (int)((long)arg2);
static int sum_(void* arg1, void* arg2)
{
int* pv = (int*)arg1;
int x = (int)((long)arg2);
(*pv) += x;
return 0;
}
static void test_zarray_iter(void) {
static void test_zarray_iter(void)
{
zarray_t array;
int i;
int val = 0;
int i;
int val = 0;
zarray_init(&array, 10, NULL);
for(i = 0; i < 10; i ++) {
zarray_store(&array, (void *)(long)i);
for (i = 0; i < 10; i++) {
zarray_store(&array, (void*)(long)i);
}
zarray_iter(&array, (zarray_iter_fn_t)sum_, &val);
TEST_ASSERT_TRUE(val == 45);
zarray_fini(&(array));
}
void test_zarray(void) {
void test_zarray(void)
{
printf("\n>> zarray test\n\n");
RUN_TEST(test_zarray_create);