add xzarray_t for replacement of zarray_t finally

This commit is contained in:
Rongsong Shen 2026-02-20 11:25:42 +08:00
parent ce69347581
commit f582742e77
5 changed files with 465 additions and 101 deletions

View file

@ -1,3 +1,5 @@
#include <errno.h>
#include <rtti.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -5,117 +7,152 @@
#include "zarray.h"
static void test_zarray_create(void)
static void test_zarray_create()
{
zarray_t array;
var a = makeInstance(&xzarray_type, 4, NULL);
zarray_init(&array, 8, NULL);
TEST_ASSERT_TRUE(a != NULL);
TEST_ASSERT_EQUAL(0, array.count);
TEST_ASSERT_NULL(array.data);
destroy(a);
}
static void test_zarray_store(void)
static void test_zarray_store()
{
zarray_t array;
int idx;
char* s1 = "hello";
char* s2 = NULL;
var a = makeInstance(&xzarray_type, 4, NULL);
int idx;
int rc;
char* p = NULL;
zarray_init(&array, 8, NULL);
idx = zarray_store(&array, (void*)s1);
TEST_ASSERT_TRUE(a != NULL);
idx = vstore(a, (void*)"Hello");
TEST_ASSERT_TRUE(idx >= 0);
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)
{
zarray_t array;
int idx;
char* s1 = "hello";
zarray_init(&array, 8, NULL);
idx = zarray_store(&array, (void*)s1);
TEST_ASSERT_TRUE(idx >= 0);
TEST_ASSERT_TRUE(array.count == 1);
zarray_del(&array, idx);
TEST_ASSERT_TRUE(zarray_get(&array, idx) == NULL);
TEST_ASSERT_TRUE(array.count == 0);
zarray_fini(&array);
}
static void test_zarray_put(void)
{
zarray_t array;
int idx;
char* s1 = "hello";
char* s2 = "test";
zarray_init(&array, 8, NULL);
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(zarray_get(&array, idx) != NULL);
TEST_ASSERT_TRUE(strcmp((char*)zarray_get(&array, idx), s2) == 0);
zarray_fini(&array);
}
static void test_zarray_put_get_ex(void)
{
zarray_t array;
int idxs[2] = { 1, 3 };
char* s1 = "hello";
char* s2 = "test";
void* datas1[2] = { s1, s2 };
void* datas2[2];
int rc;
zarray_init(&array, 8, NULL);
rc = zarray_put_ex(&array, 2, &idxs[0], &datas1[0]);
rc = vget(a, idx, (void**)&p);
TEST_ASSERT_TRUE(rc >= 0);
rc = zarray_get_ex(&array, 2, &idxs[0], &datas2[0]);
TEST_ASSERT_TRUE(p != NULL);
TEST_ASSERT_TRUE(strcmp(p, "Hello") == 0);
destroy(a);
}
static void test_zarray_put()
{
var a = makeInstance(&xzarray_type, 4, NULL);
int rc, idx = 10;
char* p;
TEST_ASSERT_TRUE(a != NULL);
rc = vput(a, idx, (void*)"Hello,world");
TEST_ASSERT_TRUE(rc >= 0);
TEST_ASSERT_TRUE((datas1[0] == datas2[0]) && (datas1[1] == datas2[1]));
zarray_fini(&array);
rc = vget(a, idx, (void**)&p);
TEST_ASSERT_TRUE(rc >= 0);
TEST_ASSERT_TRUE(p != NULL);
TEST_ASSERT_TRUE(strcmp(p, "Hello,world") == 0);
destroy(a);
}
static int sum_(void* arg1, void* arg2)
static void test_zarray_put_get_vec()
{
int* pv = (int*)arg1;
int x = (int)((long)arg2);
var a = makeInstance(&xzarray_type, 4, NULL);
int idxs[5] = { 1, 3, 5, 7, 9 };
void* vals[5] = { (void*)(long)2, (void*)(long)4, (void*)(long)6,
(void*)(long)8, (void*)(long)10 };
void* vals1[5];
int i;
int rc;
(*pv) += x;
return 0;
}
TEST_ASSERT_TRUE(a != NULL);
static void test_zarray_iter(void)
{
zarray_t array;
int i;
int val = 0;
rc = vput_vec(a, 5, &idxs[0], (void**)&vals[0]);
TEST_ASSERT_TRUE(rc >= 0);
zarray_init(&array, 10, NULL);
for (i = 0; i < 10; i++) {
zarray_store(&array, (void*)(long)i);
rc = vget_vec(a, 5, &idxs[0], &vals1[0]);
TEST_ASSERT_TRUE(rc >= 0);
for (i = 0; i < 5; i++) {
TEST_ASSERT_TRUE(vals[i] == vals1[i]);
}
zarray_iter(&array, (zarray_iter_fn_t)sum_, &val);
destroy(a);
}
TEST_ASSERT_TRUE(val == 45);
static void test_zarray_iter()
{
var a = makeInstance(&xzarray_type, 4, NULL);
int idxs[5] = { 1, 3, 5, 7, 9 };
int vals[5] = { 2, 4, 6, 8, 10 };
int i;
iterator_t it;
int rc;
zarray_fini(&(array));
TEST_ASSERT_TRUE(a != NULL);
for (i = 0; i < 5; i++) {
rc = vput(a, idxs[i], (void*)(long)vals[i]);
TEST_ASSERT_TRUE(rc >= 0);
}
rc = iter_make(&it, a);
TEST_ASSERT_TRUE(rc == RTTI_ITER_OK);
i = 0;
while (1) {
void* data;
rc = iter_next(&it, &data);
if (rc == RTTI_ITER_NO_DATA) {
break;
}
TEST_ASSERT_TRUE((int)(long)data == vals[i]);
i++;
}
TEST_ASSERT_TRUE(i == 5);
iter_destroy(&it);
destroy(a);
}
static void my_killer(void* p)
{
if (p == NULL) {
return;
}
free(p);
}
static void test_zarray_clear()
{
var a = makeInstance(&xzarray_type, 4, my_killer);
char* s = strdup("Hello");
char* d = NULL;
int idx = 31;
int rc;
TEST_ASSERT_TRUE(a != NULL);
rc = vput(a, idx, s);
TEST_ASSERT_TRUE(rc >= 0);
rc = vget(a, idx, (void**)&d);
TEST_ASSERT_TRUE(rc >= 0);
TEST_ASSERT_TRUE(d == s);
rc = vclear(a, idx);
TEST_ASSERT_TRUE(rc >= 0);
rc = vget(a, idx, (void**)&d);
TEST_ASSERT_TRUE(rc == -ENOENT);
destroy(a); /* no memory leak */
}
void test_zarray(void)
{
@ -123,8 +160,8 @@ void test_zarray(void)
RUN_TEST(test_zarray_create);
RUN_TEST(test_zarray_store);
RUN_TEST(test_zarray_del);
RUN_TEST(test_zarray_clear);
RUN_TEST(test_zarray_put);
RUN_TEST(test_zarray_put_get_vec);
RUN_TEST(test_zarray_iter);
RUN_TEST(test_zarray_put_get_ex);
}