forked from lei.chen/myxce
173 lines
4 KiB
C
173 lines
4 KiB
C
#include <errno.h>
|
|
#include <rtti.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unity/unity.h>
|
|
|
|
#include "zarray.h"
|
|
|
|
static void test_zarray_create()
|
|
{
|
|
xzarray_params_t params = { .delta = 4, .datakiller = NULL };
|
|
var a = makeInstance(&xzarray_type, ¶ms);
|
|
|
|
TEST_ASSERT_TRUE(a != NULL);
|
|
|
|
destroy(a);
|
|
}
|
|
|
|
static void test_zarray_store()
|
|
{
|
|
xzarray_params_t params = { .delta = 4, .datakiller = NULL };
|
|
var a = makeInstance(&xzarray_type, ¶ms);
|
|
int idx;
|
|
int rc;
|
|
char* p = NULL;
|
|
|
|
TEST_ASSERT_TRUE(a != NULL);
|
|
|
|
idx = vstore(a, (void*)"Hello");
|
|
TEST_ASSERT_TRUE(idx >= 0);
|
|
|
|
rc = vget(a, idx, (void**)&p);
|
|
TEST_ASSERT_TRUE(rc >= 0);
|
|
TEST_ASSERT_TRUE(p != NULL);
|
|
|
|
TEST_ASSERT_TRUE(strcmp(p, "Hello") == 0);
|
|
|
|
destroy(a);
|
|
}
|
|
|
|
static void test_zarray_put()
|
|
{
|
|
xzarray_params_t params = { .delta = 4, .datakiller = NULL };
|
|
var a = makeInstance(&xzarray_type, ¶ms);
|
|
int rc, idx = 10;
|
|
char* p;
|
|
|
|
TEST_ASSERT_TRUE(a != NULL);
|
|
|
|
rc = vput(a, idx, (void*)"Hello,world");
|
|
TEST_ASSERT_TRUE(rc >= 0);
|
|
|
|
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 void test_zarray_put_get_vec()
|
|
{
|
|
xzarray_params_t params = { .delta = 4, .datakiller = NULL };
|
|
var a = makeInstance(&xzarray_type, ¶ms);
|
|
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;
|
|
|
|
TEST_ASSERT_TRUE(a != NULL);
|
|
|
|
rc = vput_vec(a, 5, &idxs[0], (void**)&vals[0]);
|
|
TEST_ASSERT_TRUE(rc >= 0);
|
|
|
|
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]);
|
|
}
|
|
|
|
destroy(a);
|
|
}
|
|
|
|
static void test_zarray_iter()
|
|
{
|
|
xzarray_params_t params = { .delta = 4, .datakiller = NULL };
|
|
var a = makeInstance(&xzarray_type, ¶ms);
|
|
int idxs[5] = { 1, 3, 5, 7, 9 };
|
|
int vals[5] = { 2, 4, 6, 8, 10 };
|
|
int i;
|
|
iterator_t it;
|
|
int rc;
|
|
|
|
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()
|
|
{
|
|
xzarray_params_t params = { .delta = 4, .datakiller = my_killer };
|
|
var a = makeInstance(&xzarray_type, ¶ms);
|
|
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)
|
|
{
|
|
printf("\n>> zarray test\n\n");
|
|
|
|
RUN_TEST(test_zarray_create);
|
|
RUN_TEST(test_zarray_store);
|
|
RUN_TEST(test_zarray_clear);
|
|
RUN_TEST(test_zarray_put);
|
|
RUN_TEST(test_zarray_put_get_vec);
|
|
RUN_TEST(test_zarray_iter);
|
|
}
|