From 335bd9dffd7d1c8dd55803caf483ac37df083d24 Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Mon, 21 Oct 2024 13:08:14 +0200 Subject: [PATCH] test opaque array serialization --- test/meta/project.json | 2 + test/meta/src/OpaqueTypes.c | 84 +++++++++++++++++++++++++++++++++++++ test/meta/src/main.c | 12 +++++- 3 files changed, 97 insertions(+), 1 deletion(-) diff --git a/test/meta/project.json b/test/meta/project.json index e1352b379..02e12607d 100644 --- a/test/meta/project.json +++ b/test/meta/project.json @@ -992,6 +992,8 @@ "ser_vec_i32_type_to_json_auto", "ser_vec_string_type_to_json", "ser_vec_string_type_to_json_auto", + "ser_arr_i32_type_to_json", + "ser_arr_i32_type_to_json_auto", "ser_struct_1_member", "ser_struct_2_members", "ser_struct_3_members", diff --git a/test/meta/src/OpaqueTypes.c b/test/meta/src/OpaqueTypes.c index 34ea7f31d..f8cdb7880 100644 --- a/test/meta/src/OpaqueTypes.c +++ b/test/meta/src/OpaqueTypes.c @@ -48,6 +48,7 @@ const char* String_getter(const void *ptr) { return ptr; } +static int IntVec_serialize(const ecs_serializer_t *ser, const void *ptr) { test_assert(ser != NULL); test_assert(ptr != NULL); @@ -62,18 +63,21 @@ int IntVec_serialize(const ecs_serializer_t *ser, const void *ptr) { return 0; } +static const void* IntVec_get_element(const void *ptr, size_t i) { test_assert(ptr != NULL); const IntVec *data = ptr; return &data->elems[i]; } +static size_t IntVec_count(const void *ptr) { test_assert(ptr != NULL); const IntVec *data = ptr; return data->count; } +static int StringVec_serialize(const ecs_serializer_t *ser, const void *ptr) { test_assert(ser != NULL); test_assert(ptr != NULL); @@ -88,18 +92,42 @@ int StringVec_serialize(const ecs_serializer_t *ser, const void *ptr) { return 0; } +static const void* StringVec_get_element(const void *ptr, size_t i) { test_assert(ptr != NULL); const StringVec *data = ptr; return &data->elems[i]; } +static size_t StringVec_count(const void *ptr) { test_assert(ptr != NULL); const StringVec *data = ptr; return data->count; } +static +int IntArr_serialize(const ecs_serializer_t *ser, const void *ptr) { + test_assert(ser != NULL); + test_assert(ptr != NULL); + + const ecs_i32_t *data = ptr; + for (int i = 0; i < 3; i ++) { + int result = ser->value(ser, ecs_id(ecs_i32_t), &data[i]); + test_assert(result == 0); + } + + serialize_invoked ++; + return 0; +} + +static +const void* IntArr_get_element(const void *ptr, size_t i) { + test_assert(ptr != NULL); + const ecs_i32_t *data = ptr; + return &data[i]; +} + void OpaqueTypes_ser_i32_type_to_json(void) { ecs_world_t *world = ecs_init(); @@ -285,6 +313,62 @@ void OpaqueTypes_ser_vec_string_type_to_json_auto(void) { ecs_fini(world); } +void OpaqueTypes_ser_arr_i32_type_to_json(void) { + typedef ecs_i32_t IntArr[3]; + ecs_world_t *world = ecs_init(); + + ECS_COMPONENT(world, IntArr); + + ecs_entity_t int_arr = ecs_array(world, { + .type = ecs_id(ecs_i32_t), + .count = 3 + }); + + ecs_opaque(world, { + .entity = ecs_id(IntArr), + .type.as_type = int_arr, + .type.serialize = IntArr_serialize + }); + + IntArr arr = {1, 2, 3}; + + char *json = ecs_ptr_to_json(world, ecs_id(IntArr), &arr); + test_assert(json != NULL); + test_str(json, "[1, 2, 3]"); + ecs_os_free(json); + + test_int(serialize_invoked, 1); + + ecs_fini(world); +} + +void OpaqueTypes_ser_arr_i32_type_to_json_auto(void) { + typedef ecs_i32_t IntArr[3]; + ecs_world_t *world = ecs_init(); + + ECS_COMPONENT(world, IntArr); + + ecs_entity_t int_arr = ecs_array(world, { + .type = ecs_id(ecs_i32_t), + .count = 3 + }); + + ecs_opaque(world, { + .entity = ecs_id(IntArr), + .type.as_type = int_arr, + .type.get_element = IntArr_get_element + }); + + IntArr arr = {1, 2, 3}; + + char *json = ecs_ptr_to_json(world, ecs_id(IntArr), &arr); + test_assert(json != NULL); + test_str(json, "[1, 2, 3]"); + ecs_os_free(json); + + ecs_fini(world); +} + typedef struct Struct_1_member { int32_t x; } Struct_1_member; diff --git a/test/meta/src/main.c b/test/meta/src/main.c index b70e19d5d..14f983bab 100644 --- a/test/meta/src/main.c +++ b/test/meta/src/main.c @@ -949,6 +949,8 @@ void OpaqueTypes_ser_vec_i32_type_to_json(void); void OpaqueTypes_ser_vec_i32_type_to_json_auto(void); void OpaqueTypes_ser_vec_string_type_to_json(void); void OpaqueTypes_ser_vec_string_type_to_json_auto(void); +void OpaqueTypes_ser_arr_i32_type_to_json(void); +void OpaqueTypes_ser_arr_i32_type_to_json_auto(void); void OpaqueTypes_ser_struct_1_member(void); void OpaqueTypes_ser_struct_2_members(void); void OpaqueTypes_ser_struct_3_members(void); @@ -4673,6 +4675,14 @@ bake_test_case OpaqueTypes_testcases[] = { "ser_vec_string_type_to_json_auto", OpaqueTypes_ser_vec_string_type_to_json_auto }, + { + "ser_arr_i32_type_to_json", + OpaqueTypes_ser_arr_i32_type_to_json + }, + { + "ser_arr_i32_type_to_json_auto", + OpaqueTypes_ser_arr_i32_type_to_json_auto + }, { "ser_struct_1_member", OpaqueTypes_ser_struct_1_member @@ -5037,7 +5047,7 @@ static bake_test_suite suites[] = { "OpaqueTypes", NULL, NULL, - 23, + 25, OpaqueTypes_testcases }, {