diff options
author | Craig Tiller <ctiller@google.com> | 2015-09-22 12:33:20 -0700 |
---|---|---|
committer | Craig Tiller <ctiller@google.com> | 2015-09-22 12:33:20 -0700 |
commit | a82950e68318a6aab6fe894fa39f7fa616c4647b (patch) | |
tree | 7d02bd1e9e1cbae1f14ad4ad1e06d3ae81a96dfe /test/core/json | |
parent | 8af4c337181322cc4fb396199c90f574cfb4163f (diff) |
clang-format all core files
Diffstat (limited to 'test/core/json')
-rw-r--r-- | test/core/json/json_rewrite.c | 247 | ||||
-rw-r--r-- | test/core/json/json_rewrite_test.c | 320 | ||||
-rw-r--r-- | test/core/json/json_test.c | 246 |
3 files changed, 346 insertions, 467 deletions
diff --git a/test/core/json/json_rewrite.c b/test/core/json/json_rewrite.c index 920c03164a..42fdbd6d54 100644 --- a/test/core/json/json_rewrite.c +++ b/test/core/json/json_rewrite.c @@ -41,19 +41,14 @@ #include "src/core/json/json_reader.h" #include "src/core/json/json_writer.h" -typedef struct json_writer_userdata -{ - FILE *out; -} json_writer_userdata; +typedef struct json_writer_userdata { FILE *out; } json_writer_userdata; -typedef struct stacked_container -{ +typedef struct stacked_container { grpc_json_type type; struct stacked_container *next; } stacked_container; -typedef struct json_reader_userdata -{ +typedef struct json_reader_userdata { FILE *in; grpc_json_writer *writer; char *scratchpad; @@ -64,198 +59,155 @@ typedef struct json_reader_userdata stacked_container *top; } json_reader_userdata; -static void -json_writer_output_char (void *userdata, char c) -{ +static void json_writer_output_char(void *userdata, char c) { json_writer_userdata *state = userdata; - fputc (c, state->out); + fputc(c, state->out); } -static void -json_writer_output_string (void *userdata, const char *str) -{ +static void json_writer_output_string(void *userdata, const char *str) { json_writer_userdata *state = userdata; - fputs (str, state->out); + fputs(str, state->out); } -static void -json_writer_output_string_with_len (void *userdata, const char *str, size_t len) -{ +static void json_writer_output_string_with_len(void *userdata, const char *str, + size_t len) { json_writer_userdata *state = userdata; - fwrite (str, len, 1, state->out); + fwrite(str, len, 1, state->out); } -grpc_json_writer_vtable writer_vtable = { json_writer_output_char, - json_writer_output_string, - json_writer_output_string_with_len -}; +grpc_json_writer_vtable writer_vtable = {json_writer_output_char, + json_writer_output_string, + json_writer_output_string_with_len}; -static void -check_string (json_reader_userdata * state, size_t needed) -{ - if (state->free_space >= needed) - return; +static void check_string(json_reader_userdata *state, size_t needed) { + if (state->free_space >= needed) return; needed -= state->free_space; needed = (needed + 0xffu) & ~0xffu; - state->scratchpad = gpr_realloc (state->scratchpad, state->allocated + needed); + state->scratchpad = gpr_realloc(state->scratchpad, state->allocated + needed); state->free_space += needed; state->allocated += needed; } -static void -json_reader_string_clear (void *userdata) -{ +static void json_reader_string_clear(void *userdata) { json_reader_userdata *state = userdata; state->free_space = state->allocated; state->string_len = 0; } -static void -json_reader_string_add_char (void *userdata, gpr_uint32 c) -{ +static void json_reader_string_add_char(void *userdata, gpr_uint32 c) { json_reader_userdata *state = userdata; - check_string (state, 1); - GPR_ASSERT (c < 256); - state->scratchpad[state->string_len++] = (char) c; -} - -static void -json_reader_string_add_utf32 (void *userdata, gpr_uint32 c) -{ - if (c <= 0x7f) - { - json_reader_string_add_char (userdata, c); - } - else if (c <= 0x7ff) - { - gpr_uint32 b1 = 0xc0u | ((c >> 6u) & 0x1fu); - gpr_uint32 b2 = 0x80u | (c & 0x3fu); - json_reader_string_add_char (userdata, b1); - json_reader_string_add_char (userdata, b2); - } - else if (c <= 0xffffu) - { - gpr_uint32 b1 = 0xe0u | ((c >> 12u) & 0x0fu); - gpr_uint32 b2 = 0x80u | ((c >> 6u) & 0x3fu); - gpr_uint32 b3 = 0x80u | (c & 0x3fu); - json_reader_string_add_char (userdata, b1); - json_reader_string_add_char (userdata, b2); - json_reader_string_add_char (userdata, b3); - } - else if (c <= 0x1fffffu) - { - gpr_uint32 b1 = 0xf0u | ((c >> 18u) & 0x07u); - gpr_uint32 b2 = 0x80u | ((c >> 12u) & 0x3fu); - gpr_uint32 b3 = 0x80u | ((c >> 6u) & 0x3fu); - gpr_uint32 b4 = 0x80u | (c & 0x3fu); - json_reader_string_add_char (userdata, b1); - json_reader_string_add_char (userdata, b2); - json_reader_string_add_char (userdata, b3); - json_reader_string_add_char (userdata, b4); - } -} - -static gpr_uint32 -json_reader_read_char (void *userdata) -{ + check_string(state, 1); + GPR_ASSERT(c < 256); + state->scratchpad[state->string_len++] = (char)c; +} + +static void json_reader_string_add_utf32(void *userdata, gpr_uint32 c) { + if (c <= 0x7f) { + json_reader_string_add_char(userdata, c); + } else if (c <= 0x7ff) { + gpr_uint32 b1 = 0xc0u | ((c >> 6u) & 0x1fu); + gpr_uint32 b2 = 0x80u | (c & 0x3fu); + json_reader_string_add_char(userdata, b1); + json_reader_string_add_char(userdata, b2); + } else if (c <= 0xffffu) { + gpr_uint32 b1 = 0xe0u | ((c >> 12u) & 0x0fu); + gpr_uint32 b2 = 0x80u | ((c >> 6u) & 0x3fu); + gpr_uint32 b3 = 0x80u | (c & 0x3fu); + json_reader_string_add_char(userdata, b1); + json_reader_string_add_char(userdata, b2); + json_reader_string_add_char(userdata, b3); + } else if (c <= 0x1fffffu) { + gpr_uint32 b1 = 0xf0u | ((c >> 18u) & 0x07u); + gpr_uint32 b2 = 0x80u | ((c >> 12u) & 0x3fu); + gpr_uint32 b3 = 0x80u | ((c >> 6u) & 0x3fu); + gpr_uint32 b4 = 0x80u | (c & 0x3fu); + json_reader_string_add_char(userdata, b1); + json_reader_string_add_char(userdata, b2); + json_reader_string_add_char(userdata, b3); + json_reader_string_add_char(userdata, b4); + } +} + +static gpr_uint32 json_reader_read_char(void *userdata) { int r; json_reader_userdata *state = userdata; - r = fgetc (state->in); - if (r == EOF) - r = GRPC_JSON_READ_CHAR_EOF; - return (gpr_uint32) r; + r = fgetc(state->in); + if (r == EOF) r = GRPC_JSON_READ_CHAR_EOF; + return (gpr_uint32)r; } -static void -json_reader_container_begins (void *userdata, grpc_json_type type) -{ +static void json_reader_container_begins(void *userdata, grpc_json_type type) { json_reader_userdata *state = userdata; - stacked_container *container = gpr_malloc (sizeof (stacked_container)); + stacked_container *container = gpr_malloc(sizeof(stacked_container)); container->type = type; container->next = state->top; state->top = container; - grpc_json_writer_container_begins (state->writer, type); + grpc_json_writer_container_begins(state->writer, type); } -static grpc_json_type -json_reader_container_ends (void *userdata) -{ +static grpc_json_type json_reader_container_ends(void *userdata) { json_reader_userdata *state = userdata; stacked_container *container = state->top; - grpc_json_writer_container_ends (state->writer, container->type); + grpc_json_writer_container_ends(state->writer, container->type); state->top = container->next; - gpr_free (container); + gpr_free(container); return state->top ? state->top->type : GRPC_JSON_TOP_LEVEL; } -static void -json_reader_set_key (void *userdata) -{ +static void json_reader_set_key(void *userdata) { json_reader_userdata *state = userdata; - json_reader_string_add_char (userdata, 0); + json_reader_string_add_char(userdata, 0); - grpc_json_writer_object_key (state->writer, state->scratchpad); + grpc_json_writer_object_key(state->writer, state->scratchpad); } -static void -json_reader_set_string (void *userdata) -{ +static void json_reader_set_string(void *userdata) { json_reader_userdata *state = userdata; - json_reader_string_add_char (userdata, 0); + json_reader_string_add_char(userdata, 0); - grpc_json_writer_value_string (state->writer, state->scratchpad); + grpc_json_writer_value_string(state->writer, state->scratchpad); } -static int -json_reader_set_number (void *userdata) -{ +static int json_reader_set_number(void *userdata) { json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len (state->writer, state->scratchpad, state->string_len); + grpc_json_writer_value_raw_with_len(state->writer, state->scratchpad, + state->string_len); return 1; } -static void -json_reader_set_true (void *userdata) -{ +static void json_reader_set_true(void *userdata) { json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len (state->writer, "true", 4); + grpc_json_writer_value_raw_with_len(state->writer, "true", 4); } -static void -json_reader_set_false (void *userdata) -{ +static void json_reader_set_false(void *userdata) { json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len (state->writer, "false", 5); + grpc_json_writer_value_raw_with_len(state->writer, "false", 5); } -static void -json_reader_set_null (void *userdata) -{ +static void json_reader_set_null(void *userdata) { json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len (state->writer, "null", 4); + grpc_json_writer_value_raw_with_len(state->writer, "null", 4); } static grpc_json_reader_vtable reader_vtable = { - json_reader_string_clear, json_reader_string_add_char, - json_reader_string_add_utf32, json_reader_read_char, - json_reader_container_begins, json_reader_container_ends, - json_reader_set_key, json_reader_set_string, - json_reader_set_number, json_reader_set_true, - json_reader_set_false, json_reader_set_null -}; - -int -rewrite (FILE * in, FILE * out, int indent) -{ + json_reader_string_clear, json_reader_string_add_char, + json_reader_string_add_utf32, json_reader_read_char, + json_reader_container_begins, json_reader_container_ends, + json_reader_set_key, json_reader_set_string, + json_reader_set_number, json_reader_set_true, + json_reader_set_false, json_reader_set_null}; + +int rewrite(FILE *in, FILE *out, int indent) { grpc_json_writer writer; grpc_json_reader reader; grpc_json_reader_status status; @@ -272,32 +224,29 @@ rewrite (FILE * in, FILE * out, int indent) writer_user.out = out; - grpc_json_writer_init (&writer, indent, &writer_vtable, &writer_user); - grpc_json_reader_init (&reader, &reader_vtable, &reader_user); + grpc_json_writer_init(&writer, indent, &writer_vtable, &writer_user); + grpc_json_reader_init(&reader, &reader_vtable, &reader_user); - status = grpc_json_reader_run (&reader); + status = grpc_json_reader_run(&reader); - free (reader_user.scratchpad); - while (reader_user.top) - { - stacked_container *container = reader_user.top; - reader_user.top = container->next; - free (container); - } + free(reader_user.scratchpad); + while (reader_user.top) { + stacked_container *container = reader_user.top; + reader_user.top = container->next; + free(container); + } return status == GRPC_JSON_DONE; } -int -main (int argc, char **argv) -{ +int main(int argc, char **argv) { int indent = 2; gpr_cmdline *cl; - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_int (cl, "indent", NULL, &indent); - gpr_cmdline_parse (cl, argc, argv); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_int(cl, "indent", NULL, &indent); + gpr_cmdline_parse(cl, argc, argv); + gpr_cmdline_destroy(cl); - return rewrite (stdin, stdout, indent) ? 0 : 1; + return rewrite(stdin, stdout, indent) ? 0 : 1; } diff --git a/test/core/json/json_rewrite_test.c b/test/core/json/json_rewrite_test.c index 470c0218c5..644a1426e9 100644 --- a/test/core/json/json_rewrite_test.c +++ b/test/core/json/json_rewrite_test.c @@ -42,19 +42,14 @@ #include "src/core/json/json_reader.h" #include "src/core/json/json_writer.h" -typedef struct json_writer_userdata -{ - FILE *cmp; -} json_writer_userdata; +typedef struct json_writer_userdata { FILE *cmp; } json_writer_userdata; -typedef struct stacked_container -{ +typedef struct stacked_container { grpc_json_type type; struct stacked_container *next; } stacked_container; -typedef struct json_reader_userdata -{ +typedef struct json_reader_userdata { FILE *in; grpc_json_writer *writer; char *scratchpad; @@ -66,226 +61,179 @@ typedef struct json_reader_userdata int did_eagain; } json_reader_userdata; -static void -json_writer_output_char (void *userdata, char c) -{ +static void json_writer_output_char(void *userdata, char c) { json_writer_userdata *state = userdata; - int cmp = fgetc (state->cmp); + int cmp = fgetc(state->cmp); /* treat CRLF as LF */ - if (cmp == '\r' && c == '\n') - { - cmp = fgetc (state->cmp); - } - GPR_ASSERT (cmp == c); + if (cmp == '\r' && c == '\n') { + cmp = fgetc(state->cmp); + } + GPR_ASSERT(cmp == c); } -static void -json_writer_output_string (void *userdata, const char *str) -{ - while (*str) - { - json_writer_output_char (userdata, *str++); - } +static void json_writer_output_string(void *userdata, const char *str) { + while (*str) { + json_writer_output_char(userdata, *str++); + } } -static void -json_writer_output_string_with_len (void *userdata, const char *str, size_t len) -{ +static void json_writer_output_string_with_len(void *userdata, const char *str, + size_t len) { size_t i; - for (i = 0; i < len; i++) - { - json_writer_output_char (userdata, str[i]); - } + for (i = 0; i < len; i++) { + json_writer_output_char(userdata, str[i]); + } } -grpc_json_writer_vtable writer_vtable = { json_writer_output_char, - json_writer_output_string, - json_writer_output_string_with_len -}; +grpc_json_writer_vtable writer_vtable = {json_writer_output_char, + json_writer_output_string, + json_writer_output_string_with_len}; -static void -check_string (json_reader_userdata * state, size_t needed) -{ - if (state->free_space >= needed) - return; +static void check_string(json_reader_userdata *state, size_t needed) { + if (state->free_space >= needed) return; needed -= state->free_space; needed = (needed + 0xffu) & ~0xffu; - state->scratchpad = gpr_realloc (state->scratchpad, state->allocated + needed); + state->scratchpad = gpr_realloc(state->scratchpad, state->allocated + needed); state->free_space += needed; state->allocated += needed; } -static void -json_reader_string_clear (void *userdata) -{ +static void json_reader_string_clear(void *userdata) { json_reader_userdata *state = userdata; state->free_space = state->allocated; state->string_len = 0; } -static void -json_reader_string_add_char (void *userdata, gpr_uint32 c) -{ +static void json_reader_string_add_char(void *userdata, gpr_uint32 c) { json_reader_userdata *state = userdata; - check_string (state, 1); - GPR_ASSERT (c <= 256); - state->scratchpad[state->string_len++] = (char) c; + check_string(state, 1); + GPR_ASSERT(c <= 256); + state->scratchpad[state->string_len++] = (char)c; } -static void -json_reader_string_add_utf32 (void *userdata, gpr_uint32 c) -{ - if (c <= 0x7f) - { - json_reader_string_add_char (userdata, c); - } - else if (c <= 0x7ffu) - { - gpr_uint32 b1 = 0xc0u | ((c >> 6u) & 0x1fu); - gpr_uint32 b2 = 0x80u | (c & 0x3fu); - json_reader_string_add_char (userdata, b1); - json_reader_string_add_char (userdata, b2); - } - else if (c <= 0xffffu) - { - gpr_uint32 b1 = 0xe0u | ((c >> 12u) & 0x0fu); - gpr_uint32 b2 = 0x80u | ((c >> 6u) & 0x3fu); - gpr_uint32 b3 = 0x80u | (c & 0x3fu); - json_reader_string_add_char (userdata, b1); - json_reader_string_add_char (userdata, b2); - json_reader_string_add_char (userdata, b3); - } - else if (c <= 0x1fffffu) - { - gpr_uint32 b1 = 0xf0u | ((c >> 18u) & 0x07u); - gpr_uint32 b2 = 0x80u | ((c >> 12u) & 0x3fu); - gpr_uint32 b3 = 0x80u | ((c >> 6u) & 0x3fu); - gpr_uint32 b4 = 0x80u | (c & 0x3fu); - json_reader_string_add_char (userdata, b1); - json_reader_string_add_char (userdata, b2); - json_reader_string_add_char (userdata, b3); - json_reader_string_add_char (userdata, b4); - } +static void json_reader_string_add_utf32(void *userdata, gpr_uint32 c) { + if (c <= 0x7f) { + json_reader_string_add_char(userdata, c); + } else if (c <= 0x7ffu) { + gpr_uint32 b1 = 0xc0u | ((c >> 6u) & 0x1fu); + gpr_uint32 b2 = 0x80u | (c & 0x3fu); + json_reader_string_add_char(userdata, b1); + json_reader_string_add_char(userdata, b2); + } else if (c <= 0xffffu) { + gpr_uint32 b1 = 0xe0u | ((c >> 12u) & 0x0fu); + gpr_uint32 b2 = 0x80u | ((c >> 6u) & 0x3fu); + gpr_uint32 b3 = 0x80u | (c & 0x3fu); + json_reader_string_add_char(userdata, b1); + json_reader_string_add_char(userdata, b2); + json_reader_string_add_char(userdata, b3); + } else if (c <= 0x1fffffu) { + gpr_uint32 b1 = 0xf0u | ((c >> 18u) & 0x07u); + gpr_uint32 b2 = 0x80u | ((c >> 12u) & 0x3fu); + gpr_uint32 b3 = 0x80u | ((c >> 6u) & 0x3fu); + gpr_uint32 b4 = 0x80u | (c & 0x3fu); + json_reader_string_add_char(userdata, b1); + json_reader_string_add_char(userdata, b2); + json_reader_string_add_char(userdata, b3); + json_reader_string_add_char(userdata, b4); + } } -static gpr_uint32 -json_reader_read_char (void *userdata) -{ +static gpr_uint32 json_reader_read_char(void *userdata) { int r; json_reader_userdata *state = userdata; - if (!state->did_eagain) - { - state->did_eagain = 1; - return GRPC_JSON_READ_CHAR_EAGAIN; - } + if (!state->did_eagain) { + state->did_eagain = 1; + return GRPC_JSON_READ_CHAR_EAGAIN; + } state->did_eagain = 0; - r = fgetc (state->in); - if (r == EOF) - r = GRPC_JSON_READ_CHAR_EOF; - return (gpr_uint32) r; + r = fgetc(state->in); + if (r == EOF) r = GRPC_JSON_READ_CHAR_EOF; + return (gpr_uint32)r; } -static void -json_reader_container_begins (void *userdata, grpc_json_type type) -{ +static void json_reader_container_begins(void *userdata, grpc_json_type type) { json_reader_userdata *state = userdata; - stacked_container *container = gpr_malloc (sizeof (stacked_container)); + stacked_container *container = gpr_malloc(sizeof(stacked_container)); container->type = type; container->next = state->top; state->top = container; - grpc_json_writer_container_begins (state->writer, type); + grpc_json_writer_container_begins(state->writer, type); } -static grpc_json_type -json_reader_container_ends (void *userdata) -{ +static grpc_json_type json_reader_container_ends(void *userdata) { json_reader_userdata *state = userdata; stacked_container *container = state->top; - grpc_json_writer_container_ends (state->writer, container->type); + grpc_json_writer_container_ends(state->writer, container->type); state->top = container->next; - gpr_free (container); + gpr_free(container); return state->top ? state->top->type : GRPC_JSON_TOP_LEVEL; } -static void -json_reader_set_key (void *userdata) -{ +static void json_reader_set_key(void *userdata) { json_reader_userdata *state = userdata; - json_reader_string_add_char (userdata, 0); + json_reader_string_add_char(userdata, 0); - grpc_json_writer_object_key (state->writer, state->scratchpad); + grpc_json_writer_object_key(state->writer, state->scratchpad); } -static void -json_reader_set_string (void *userdata) -{ +static void json_reader_set_string(void *userdata) { json_reader_userdata *state = userdata; - json_reader_string_add_char (userdata, 0); + json_reader_string_add_char(userdata, 0); - grpc_json_writer_value_string (state->writer, state->scratchpad); + grpc_json_writer_value_string(state->writer, state->scratchpad); } -static int -json_reader_set_number (void *userdata) -{ +static int json_reader_set_number(void *userdata) { json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len (state->writer, state->scratchpad, state->string_len); + grpc_json_writer_value_raw_with_len(state->writer, state->scratchpad, + state->string_len); return 1; } -static void -json_reader_set_true (void *userdata) -{ +static void json_reader_set_true(void *userdata) { json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len (state->writer, "true", 4); + grpc_json_writer_value_raw_with_len(state->writer, "true", 4); } -static void -json_reader_set_false (void *userdata) -{ +static void json_reader_set_false(void *userdata) { json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len (state->writer, "false", 5); + grpc_json_writer_value_raw_with_len(state->writer, "false", 5); } -static void -json_reader_set_null (void *userdata) -{ +static void json_reader_set_null(void *userdata) { json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len (state->writer, "null", 4); + grpc_json_writer_value_raw_with_len(state->writer, "null", 4); } static grpc_json_reader_vtable reader_vtable = { - json_reader_string_clear, json_reader_string_add_char, - json_reader_string_add_utf32, json_reader_read_char, - json_reader_container_begins, json_reader_container_ends, - json_reader_set_key, json_reader_set_string, - json_reader_set_number, json_reader_set_true, - json_reader_set_false, json_reader_set_null -}; - -int -rewrite_and_compare (FILE * in, FILE * cmp, int indent) -{ + json_reader_string_clear, json_reader_string_add_char, + json_reader_string_add_utf32, json_reader_read_char, + json_reader_container_begins, json_reader_container_ends, + json_reader_set_key, json_reader_set_string, + json_reader_set_number, json_reader_set_true, + json_reader_set_false, json_reader_set_null}; + +int rewrite_and_compare(FILE *in, FILE *cmp, int indent) { grpc_json_writer writer; grpc_json_reader reader; grpc_json_reader_status status; json_writer_userdata writer_user; json_reader_userdata reader_user; - GPR_ASSERT (in); - GPR_ASSERT (cmp); + GPR_ASSERT(in); + GPR_ASSERT(cmp); reader_user.writer = &writer; reader_user.in = in; @@ -298,68 +246,60 @@ rewrite_and_compare (FILE * in, FILE * cmp, int indent) writer_user.cmp = cmp; - grpc_json_writer_init (&writer, indent, &writer_vtable, &writer_user); - grpc_json_reader_init (&reader, &reader_vtable, &reader_user); + grpc_json_writer_init(&writer, indent, &writer_vtable, &writer_user); + grpc_json_reader_init(&reader, &reader_vtable, &reader_user); - do - { - status = grpc_json_reader_run (&reader); - } - while (status == GRPC_JSON_EAGAIN); + do { + status = grpc_json_reader_run(&reader); + } while (status == GRPC_JSON_EAGAIN); - free (reader_user.scratchpad); - while (reader_user.top) - { - stacked_container *container = reader_user.top; - reader_user.top = container->next; - free (container); - } + free(reader_user.scratchpad); + while (reader_user.top) { + stacked_container *container = reader_user.top; + reader_user.top = container->next; + free(container); + } return status == GRPC_JSON_DONE; } -typedef struct test_file -{ +typedef struct test_file { const char *input; const char *cmp; int indent; } test_file; static test_file test_files[] = { - {"test/core/json/rewrite_test_input.json", - "test/core/json/rewrite_test_output_condensed.json", 0}, - {"test/core/json/rewrite_test_input.json", - "test/core/json/rewrite_test_output_indented.json", 2}, - {"test/core/json/rewrite_test_output_indented.json", - "test/core/json/rewrite_test_output_condensed.json", 0}, - {"test/core/json/rewrite_test_output_condensed.json", - "test/core/json/rewrite_test_output_indented.json", 2}, + {"test/core/json/rewrite_test_input.json", + "test/core/json/rewrite_test_output_condensed.json", 0}, + {"test/core/json/rewrite_test_input.json", + "test/core/json/rewrite_test_output_indented.json", 2}, + {"test/core/json/rewrite_test_output_indented.json", + "test/core/json/rewrite_test_output_condensed.json", 0}, + {"test/core/json/rewrite_test_output_condensed.json", + "test/core/json/rewrite_test_output_indented.json", 2}, }; -void -test_rewrites () -{ +void test_rewrites() { unsigned i; - for (i = 0; i < GPR_ARRAY_SIZE (test_files); i++) - { - test_file *test = test_files + i; - FILE *input = fopen (test->input, "rb"); - FILE *cmp = fopen (test->cmp, "rb"); - int status; - gpr_log (GPR_INFO, "Testing file %s against %s using indent=%i", test->input, test->cmp, test->indent); - status = rewrite_and_compare (input, cmp, test->indent); - GPR_ASSERT (status); - fclose (input); - fclose (cmp); - } + for (i = 0; i < GPR_ARRAY_SIZE(test_files); i++) { + test_file *test = test_files + i; + FILE *input = fopen(test->input, "rb"); + FILE *cmp = fopen(test->cmp, "rb"); + int status; + gpr_log(GPR_INFO, "Testing file %s against %s using indent=%i", test->input, + test->cmp, test->indent); + status = rewrite_and_compare(input, cmp, test->indent); + GPR_ASSERT(status); + fclose(input); + fclose(cmp); + } } -int -main (int argc, char **argv) -{ - grpc_test_init (argc, argv); - test_rewrites (); - gpr_log (GPR_INFO, "json_rewrite_test success"); +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + test_rewrites(); + gpr_log(GPR_INFO, "json_rewrite_test success"); return 0; } diff --git a/test/core/json/json_test.c b/test/core/json/json_test.c index 2f4e2921eb..8575331cab 100644 --- a/test/core/json/json_test.c +++ b/test/core/json/json_test.c @@ -42,151 +42,141 @@ #include "test/core/util/test_config.h" -typedef struct testing_pair -{ +typedef struct testing_pair { const char *input; const char *output; } testing_pair; static testing_pair testing_pairs[] = { - /* Testing valid parsing. */ - - /* Testing trivial parses, with de-indentation. */ - {" 0 ", "0"}, - {" 1 ", "1"}, - {" \"a\" ", "\"a\""}, - {" true ", "true"}, - /* Testing the parser's ability to decode trivial UTF-16. */ - {"\"\\u0020\\\\\\u0010\\u000a\\u000D\"", "\" \\\\\\u0010\\n\\r\""}, - /* Testing various UTF-8 sequences. */ - {"\"ßâñć௵⇒\"", "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\""}, - {"\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"", - "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\""}, - /* Testing UTF-8 character "𝄞", U+11D1E. */ - {"\"\xf0\x9d\x84\x9e\"", "\"\\ud834\\udd1e\""}, - {"\"\\ud834\\udd1e\"", "\"\\ud834\\udd1e\""}, - /* Testing nested empty containers. */ - { - " [ [ ] , { } , [ ] ] ", "[[],{},[]]", - }, - /* Testing escapes and control chars in key strings. */ - {" { \"\x7f\\n\\\\a , b\": 1, \"\": 0 } ", - "{\"\\u007f\\n\\\\a , b\":1,\"\":0}"}, - /* Testing the writer's ability to cut off invalid UTF-8 sequences. */ - {"\"abc\xf0\x9d\x24\"", "\"abc\""}, - {"\"\xff\"", "\"\""}, - /* Testing valid number parsing. */ - {"[0, 42 , 0.0123, 123.456]", "[0,42,0.0123,123.456]"}, - {"[1e4,-53.235e-31, 0.3e+3]", "[1e4,-53.235e-31,0.3e+3]"}, - /* Testing keywords parsing. */ - {"[true, false, null]", "[true,false,null]"}, - - /* Testing invalid parsing. */ - - /* Testing plain invalid things, exercising the state machine. */ - {"\\", NULL}, - {"nu ll", NULL}, - {"fals", NULL}, - /* Testing unterminated string. */ - {"\"\\x", NULL}, - /* Testing invalid UTF-16 number. */ - {"\"\\u123x", NULL}, - /* Testing imbalanced surrogate pairs. */ - {"\"\\ud834f", NULL}, - {"\"\\ud834\\n", NULL}, - {"\"\\udd1ef", NULL}, - {"\"\\ud834\\ud834\"", NULL}, - {"\"\\ud834\\u1234\"", NULL}, - /* Testing embedded invalid whitechars. */ - {"\"\n\"", NULL}, - {"\"\t\"", NULL}, - /* Testing empty json data. */ - {"", NULL}, - /* Testing extra characters after end of parsing. */ - {"{},", NULL}, - /* Testing imbalanced containers. */ - {"{}}", NULL}, - {"[]]", NULL}, - {"{{}", NULL}, - {"[[]", NULL}, - {"[}", NULL}, - {"{]", NULL}, - /*Testing trailing comma. */ - {"{,}", NULL}, - {"[1,2,3,4,]", NULL}, - /* Testing having a key syntax in an array. */ - {"[\"x\":0]", NULL}, - /* Testing invalid numbers. */ - {"1.", NULL}, - {"1e", NULL}, - {".12", NULL}, - {"1.x", NULL}, - {"1.12x", NULL}, - {"1ex", NULL}, - {"1e12x", NULL}, - {".12x", NULL}, - {"000", NULL}, + /* Testing valid parsing. */ + + /* Testing trivial parses, with de-indentation. */ + {" 0 ", "0"}, + {" 1 ", "1"}, + {" \"a\" ", "\"a\""}, + {" true ", "true"}, + /* Testing the parser's ability to decode trivial UTF-16. */ + {"\"\\u0020\\\\\\u0010\\u000a\\u000D\"", "\" \\\\\\u0010\\n\\r\""}, + /* Testing various UTF-8 sequences. */ + {"\"ßâñć௵⇒\"", "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\""}, + {"\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"", + "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\""}, + /* Testing UTF-8 character "𝄞", U+11D1E. */ + {"\"\xf0\x9d\x84\x9e\"", "\"\\ud834\\udd1e\""}, + {"\"\\ud834\\udd1e\"", "\"\\ud834\\udd1e\""}, + /* Testing nested empty containers. */ + { + " [ [ ] , { } , [ ] ] ", "[[],{},[]]", + }, + /* Testing escapes and control chars in key strings. */ + {" { \"\x7f\\n\\\\a , b\": 1, \"\": 0 } ", + "{\"\\u007f\\n\\\\a , b\":1,\"\":0}"}, + /* Testing the writer's ability to cut off invalid UTF-8 sequences. */ + {"\"abc\xf0\x9d\x24\"", "\"abc\""}, + {"\"\xff\"", "\"\""}, + /* Testing valid number parsing. */ + {"[0, 42 , 0.0123, 123.456]", "[0,42,0.0123,123.456]"}, + {"[1e4,-53.235e-31, 0.3e+3]", "[1e4,-53.235e-31,0.3e+3]"}, + /* Testing keywords parsing. */ + {"[true, false, null]", "[true,false,null]"}, + + /* Testing invalid parsing. */ + + /* Testing plain invalid things, exercising the state machine. */ + {"\\", NULL}, + {"nu ll", NULL}, + {"fals", NULL}, + /* Testing unterminated string. */ + {"\"\\x", NULL}, + /* Testing invalid UTF-16 number. */ + {"\"\\u123x", NULL}, + /* Testing imbalanced surrogate pairs. */ + {"\"\\ud834f", NULL}, + {"\"\\ud834\\n", NULL}, + {"\"\\udd1ef", NULL}, + {"\"\\ud834\\ud834\"", NULL}, + {"\"\\ud834\\u1234\"", NULL}, + /* Testing embedded invalid whitechars. */ + {"\"\n\"", NULL}, + {"\"\t\"", NULL}, + /* Testing empty json data. */ + {"", NULL}, + /* Testing extra characters after end of parsing. */ + {"{},", NULL}, + /* Testing imbalanced containers. */ + {"{}}", NULL}, + {"[]]", NULL}, + {"{{}", NULL}, + {"[[]", NULL}, + {"[}", NULL}, + {"{]", NULL}, + /*Testing trailing comma. */ + {"{,}", NULL}, + {"[1,2,3,4,]", NULL}, + /* Testing having a key syntax in an array. */ + {"[\"x\":0]", NULL}, + /* Testing invalid numbers. */ + {"1.", NULL}, + {"1e", NULL}, + {".12", NULL}, + {"1.x", NULL}, + {"1.12x", NULL}, + {"1ex", NULL}, + {"1e12x", NULL}, + {".12x", NULL}, + {"000", NULL}, }; -static void -test_pairs () -{ +static void test_pairs() { unsigned i; - for (i = 0; i < GPR_ARRAY_SIZE (testing_pairs); i++) - { - testing_pair *pair = testing_pairs + i; - char *scratchpad = gpr_strdup (pair->input); - grpc_json *json; - - gpr_log (GPR_INFO, "parsing string %i - should %s", i, pair->output ? "succeed" : "fail"); - json = grpc_json_parse_string (scratchpad); - - if (pair->output) - { - char *output; - - GPR_ASSERT (json); - output = grpc_json_dump_to_string (json, 0); - GPR_ASSERT (output); - gpr_log (GPR_INFO, "succeeded with output = %s", output); - GPR_ASSERT (strcmp (output, pair->output) == 0); - - grpc_json_destroy (json); - gpr_free (output); - } - else - { - gpr_log (GPR_INFO, "failed"); - GPR_ASSERT (!json); - } - - gpr_free (scratchpad); + for (i = 0; i < GPR_ARRAY_SIZE(testing_pairs); i++) { + testing_pair *pair = testing_pairs + i; + char *scratchpad = gpr_strdup(pair->input); + grpc_json *json; + + gpr_log(GPR_INFO, "parsing string %i - should %s", i, + pair->output ? "succeed" : "fail"); + json = grpc_json_parse_string(scratchpad); + + if (pair->output) { + char *output; + + GPR_ASSERT(json); + output = grpc_json_dump_to_string(json, 0); + GPR_ASSERT(output); + gpr_log(GPR_INFO, "succeeded with output = %s", output); + GPR_ASSERT(strcmp(output, pair->output) == 0); + + grpc_json_destroy(json); + gpr_free(output); + } else { + gpr_log(GPR_INFO, "failed"); + GPR_ASSERT(!json); } + + gpr_free(scratchpad); + } } -static void -test_atypical () -{ - char *scratchpad = gpr_strdup ("[[],[]]"); - grpc_json *json = grpc_json_parse_string (scratchpad); +static void test_atypical() { + char *scratchpad = gpr_strdup("[[],[]]"); + grpc_json *json = grpc_json_parse_string(scratchpad); grpc_json *brother; - GPR_ASSERT (json); - GPR_ASSERT (json->child); + GPR_ASSERT(json); + GPR_ASSERT(json->child); brother = json->child->next; - grpc_json_destroy (json->child); + grpc_json_destroy(json->child); json->child = brother; - grpc_json_destroy (json); - gpr_free (scratchpad); + grpc_json_destroy(json); + gpr_free(scratchpad); } -int -main (int argc, char **argv) -{ - grpc_test_init (argc, argv); - test_pairs (); - test_atypical (); - gpr_log (GPR_INFO, "json_test success"); +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + test_pairs(); + test_atypical(); + gpr_log(GPR_INFO, "json_test success"); return 0; } |