Skip to content

Commit

Permalink
test opaque array serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeletier committed Nov 18, 2024
1 parent 07a8d95 commit 335bd9d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
2 changes: 2 additions & 0 deletions test/meta/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
84 changes: 84 additions & 0 deletions test/meta/src/OpaqueTypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();

Expand Down Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion test/meta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -5037,7 +5047,7 @@ static bake_test_suite suites[] = {
"OpaqueTypes",
NULL,
NULL,
23,
25,
OpaqueTypes_testcases
},
{
Expand Down

0 comments on commit 335bd9d

Please sign in to comment.