Skip to content

Commit

Permalink
finish tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeletier committed Nov 29, 2024
1 parent 7abc435 commit c115666
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
9 changes: 7 additions & 2 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18891,11 +18891,16 @@ struct cursor {
/** Get unit of value */
flecs::entity get_unit() const;

/** Get untyped pointer to value */
void* get_ptr() {
/** Get untyped pointer to value for writing */
void* get_write_ptr() {
return ecs_meta_get_write_ptr(&cursor_);
}

/** Get untyped pointer to value for reading */
const void* get_read_ptr() {
return ecs_meta_get_read_ptr(&cursor_);
}

/** Set boolean value */
int set_bool(bool value) {
return ecs_meta_set_bool(&cursor_, value);
Expand Down
57 changes: 53 additions & 4 deletions test/meta/src/Cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -4451,7 +4451,7 @@ void Cursor_struct_w_3_opaque_arrays(void) {
ecs_fini(world);
}

static void* OpaqueStructIntVec_member(void *ptr, const char *member) {
static void* OpaqueStructIntVec_ensure_member(void *ptr, const char *member) {
Struct_w_IntVec *data = ptr;
if (!strcmp(member, "foo")) {
return &data->foo;
Expand All @@ -4462,6 +4462,17 @@ static void* OpaqueStructIntVec_member(void *ptr, const char *member) {
}
}

static const void* OpaqueStructIntVec_get_member(const void *ptr, const char *member) {
const Struct_w_IntVec *data = ptr;
if (!strcmp(member, "foo")) {
return &data->foo;
} else if (!strcmp(member, "bar")) {
return &data->bar;
} else {
return NULL;
}
}

void Cursor_opaque_struct_w_opaque_vec(void) {
ecs_world_t *world = ecs_init();

Expand All @@ -4472,6 +4483,7 @@ void Cursor_opaque_struct_w_opaque_vec(void) {
.entity = ecs_id(IntVec),
.type.as_type = ecs_vector(world, { .type = ecs_id(ecs_i32_t) }),
.type.ensure_element = IntVec_ensure,
.type.get_element = IntVec_get,
.type.count = IntVec_count,
.type.resize = IntVec_resize
});
Expand All @@ -4486,7 +4498,8 @@ void Cursor_opaque_struct_w_opaque_vec(void) {
ecs_opaque(world, {
.entity = ecs_id(Struct_w_IntVec),
.type.as_type = os,
.type.ensure_member = OpaqueStructIntVec_member,
.type.ensure_member = OpaqueStructIntVec_ensure_member,
.type.get_member = OpaqueStructIntVec_get_member
});

Struct_w_IntVec v = {0};
Expand All @@ -4495,19 +4508,37 @@ void Cursor_opaque_struct_w_opaque_vec(void) {
test_int(0, ecs_meta_member(&cur, "foo"));
test_int(0, ecs_meta_push(&cur));
test_int(0, ecs_meta_set_int(&cur, 10));
test_int(ecs_meta_get_int(&cur), 10);
test_int(0, ecs_meta_next(&cur));
test_int(0, ecs_meta_set_int(&cur, 20));
test_int(ecs_meta_get_int(&cur), 20);
test_int(0, ecs_meta_next(&cur));
test_int(0, ecs_meta_set_int(&cur, 30));
test_int(ecs_meta_get_int(&cur), 30);
test_int(0, ecs_meta_pop(&cur));

const IntVec *vec = ecs_meta_get_read_ptr(&cur);
test_int(vec->array[0], 10);
test_int(vec->array[1], 20);
test_int(vec->array[2], 30);

test_int(0, ecs_meta_member(&cur, "bar"));
test_int(0, ecs_meta_push(&cur));
test_int(0, ecs_meta_set_int(&cur, 40));
test_int(ecs_meta_get_int(&cur), 40);
test_int(0, ecs_meta_next(&cur));
test_int(0, ecs_meta_set_int(&cur, 50));
test_int(ecs_meta_get_int(&cur), 50);
test_int(0, ecs_meta_next(&cur));
test_int(0, ecs_meta_set_int(&cur, 60));
test_int(ecs_meta_get_int(&cur), 60);
test_int(0, ecs_meta_pop(&cur));

vec = ecs_meta_get_read_ptr(&cur);
test_int(vec->array[0], 40);
test_int(vec->array[1], 50);
test_int(vec->array[2], 60);

test_int(0, ecs_meta_pop(&cur));

test_int(v.foo.count, 3);
Expand All @@ -4524,7 +4555,15 @@ void Cursor_opaque_struct_w_opaque_vec(void) {
ecs_fini(world);
}

static void* OpaqueStructElem_member(void *ptr, const char *member) {
static void* OpaqueStructElem_ensure_member(void *ptr, const char *member) {
if (!strcmp(member, "i")) {
return ptr;
} else {
return NULL;
}
}

static const void* OpaqueStructElem_get_member(const void *ptr, const char *member) {
if (!strcmp(member, "i")) {
return ptr;
} else {
Expand All @@ -4550,13 +4589,15 @@ void Cursor_opaque_vec_w_opaque_elem(void) {
.type.alignment = ECS_ALIGNOF(ecs_i32_t)
}),
.type.as_type = os,
.type.ensure_member = OpaqueStructElem_member,
.type.ensure_member = OpaqueStructElem_ensure_member,
.type.get_member = OpaqueStructElem_get_member,
});

ecs_opaque(world, {
.entity = ecs_id(IntVec),
.type.as_type = ecs_vector(world, { .type = oelem }),
.type.ensure_element = IntVec_ensure,
.type.get_element = IntVec_get,
.type.count = IntVec_count,
.type.resize = IntVec_resize
});
Expand All @@ -4567,19 +4608,27 @@ void Cursor_opaque_vec_w_opaque_elem(void) {
test_int(0, ecs_meta_push(&cur));
test_int(0, ecs_meta_member(&cur, "i"));
test_int(0, ecs_meta_set_int(&cur, 10));
test_int(ecs_meta_get_int(&cur), 10);
test_int(0, ecs_meta_pop(&cur));
test_int(0, ecs_meta_next(&cur));
test_int(0, ecs_meta_push(&cur));
test_int(0, ecs_meta_member(&cur, "i"));
test_int(0, ecs_meta_set_int(&cur, 20));
test_int(ecs_meta_get_int(&cur), 20);
test_int(0, ecs_meta_pop(&cur));
test_int(0, ecs_meta_next(&cur));
test_int(0, ecs_meta_push(&cur));
test_int(0, ecs_meta_member(&cur, "i"));
test_int(0, ecs_meta_set_int(&cur, 30));
test_int(ecs_meta_get_int(&cur), 30);
test_int(0, ecs_meta_pop(&cur));
test_int(0, ecs_meta_pop(&cur));

const IntVec *vec = ecs_meta_get_read_ptr(&cur);
test_int(vec->array[0], 10);
test_int(vec->array[1], 20);
test_int(vec->array[2], 30);

test_int(v.count, 3);
test_int(v.array[0], 10);
test_int(v.array[1], 20);
Expand Down

0 comments on commit c115666

Please sign in to comment.