Skip to content

Commit

Permalink
test: Add unused members to test structs in JSON test suite
Browse files Browse the repository at this point in the history
This commit updates various structs in the JSON test suite to include
unused members. These additions aim to test the improved tolerance of
JSON encoding for C struct members that may not be fully described by
JSON descriptors.

As a consequence of the addition of unused fields,
prefer designated initializers instead of postionnal ones.

Add a note about a test which constantly fails with a unused field.

Signed-off-by: Lucas Dietrich <[email protected]>
  • Loading branch information
lucasdietrich committed Jul 26, 2023
1 parent 59eefd2 commit cf38367
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions tests/lib/json/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

struct test_nested {
int nested_int;
uint32_t _unused_member;
bool nested_bool;
const char *nested_string;
};
Expand All @@ -19,6 +20,7 @@ struct test_struct {
const char *some_string;
int some_int;
bool some_bool;
uint32_t _unused_member;
struct test_nested some_nested_struct;
int some_array[16];
size_t some_array_len;
Expand All @@ -32,17 +34,21 @@ struct test_struct {
};

struct elt {
uint32_t _unused_member;
const char *name;
int height;
};

struct obj_array {
uint32_t _unused_member1;
struct elt elements[10];
uint32_t _unused_member2;
size_t num_elements;
};

struct test_int_limits {
int int_max;
uint32_t _unused_member;
int int_cero;
int int_min;
};
Expand Down Expand Up @@ -92,11 +98,15 @@ static const struct json_obj_descr obj_limits_descr[] = {
};

struct array {
/* uint32_t _unused_member1; // TODO test fails if this is uncommented */
struct elt objects;
uint32_t _unused_member2;
};

struct obj_array_array {
uint32_t _unused_member1;
struct array objects_array[4];
uint32_t _unused_member2;
size_t objects_array_len;
};

Expand All @@ -111,7 +121,9 @@ static const struct json_obj_descr array_array_descr[] = {
};

struct obj_array_2dim {
uint32_t _unused_member1;
struct obj_array objects_array_array[3];
uint32_t _unused_member2;
size_t objects_array_array_len;
};

Expand Down Expand Up @@ -173,8 +185,8 @@ ZTEST(lib_json_test, test_json_encoding)
.nested_string = "no escape necessary",
},
.nested_obj_array = {
{1, true, "true"},
{0, false, "false"}
{.nested_int = 1, .nested_bool = true, .nested_string = "true"},
{.nested_int = 0, .nested_bool = false, .nested_string = "false"}
},
.obj_array_len = 2
};
Expand Down Expand Up @@ -324,10 +336,11 @@ ZTEST(lib_json_test, test_json_encoding_array_array)
{
struct obj_array_array obj_array_array_ts = {
.objects_array = {
[0] = { { .name = "Sim\303\263n Bol\303\255var", .height = 168 } },
[1] = { { .name = "Pel\303\251", .height = 173 } },
[2] = { { .name = "Usain Bolt", .height = 195 } },
},
[0] = {.objects = {.name = "Sim\303\263n Bol\303\255var",
.height = 168}},
[1] = {.objects = {.name = "Pel\303\251", .height = 173}},
[2] = {.objects = {.name = "Usain Bolt", .height = 195}},
},
.objects_array_len = 3,
};
char encoded[] = "{\"objects_array\":["
Expand Down

0 comments on commit cf38367

Please sign in to comment.