From c115666a9ffdeb2f1c84478f082b0442d7db8c1c Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Thu, 17 Oct 2024 14:36:36 +0200 Subject: [PATCH] finish tests --- distr/flecs.h | 9 +++++-- test/meta/src/Cursor.c | 57 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/distr/flecs.h b/distr/flecs.h index 5f3f2daf1..4376e9eef 100644 --- a/distr/flecs.h +++ b/distr/flecs.h @@ -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); diff --git a/test/meta/src/Cursor.c b/test/meta/src/Cursor.c index cbaa3323b..7eb060734 100644 --- a/test/meta/src/Cursor.c +++ b/test/meta/src/Cursor.c @@ -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; @@ -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(); @@ -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 }); @@ -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}; @@ -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); @@ -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 { @@ -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 }); @@ -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);