From 04e69f9d4004ed32a7740ee75abab8dffc8b6e63 Mon Sep 17 00:00:00 2001 From: chenzizhan Date: Tue, 5 Sep 2023 16:16:44 +0800 Subject: test json_writer --- src/exporter/cjson_exporter.c | 2 +- src/utils/very_fast_json_writer.h | 3 +++ test/test_exporter_json.cpp | 37 ++++++++++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/exporter/cjson_exporter.c b/src/exporter/cjson_exporter.c index 6639d8f..856c24d 100644 --- a/src/exporter/cjson_exporter.c +++ b/src/exporter/cjson_exporter.c @@ -517,7 +517,7 @@ char *fieldstat_json_exporter_export(const struct fieldstat_json_exporter *expor int buf_len = 4096; char *cjson_str_arr = (char *)malloc(buf_len); - // cjson is so slow, so we construct the json array manually + // cjson is so slow, so we construct the json array manually, the reason why we don't use json_writer is simply because I wrote this before implementing json_writer int used_len = add_object_to_json_array_start(cjson_str_arr, buf_len); for (int i = 0; i < n_pair; i++) { used_len = add_object_to_json_array(&cjson_str_arr, &buf_len, used_len, str_arr[i]); diff --git a/src/utils/very_fast_json_writer.h b/src/utils/very_fast_json_writer.h index deee1a6..ec7b858 100644 --- a/src/utils/very_fast_json_writer.h +++ b/src/utils/very_fast_json_writer.h @@ -12,6 +12,9 @@ struct json_writer; struct json_writer *json_writer_init(); void json_writer_start_map(struct json_writer *writer); // { void json_writer_end_map(struct json_writer *writer); // } +void json_writer_array_start(struct json_writer *writer); // [ +void json_writer_array_end(struct json_writer *writer); // ] + void json_writer_str_field(struct json_writer *writer, const char *key, const char *value, size_t str_len); void json_writer_int_field(struct json_writer *writer, const char *key, int value); void json_writer_double_field(struct json_writer *writer, const char *key, double value); diff --git a/test/test_exporter_json.cpp b/test/test_exporter_json.cpp index fdafacc..2b56868 100644 --- a/test/test_exporter_json.cpp +++ b/test/test_exporter_json.cpp @@ -133,10 +133,8 @@ cJSON *test_exporter_extract_results(const struct fieldstat *instance) { struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance); - printf("going to export, get char\n"); // getchar(); char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter); - printf("json_string: %s\n", json_string); // exit(0); cJSON *root_arr = cJSON_Parse(json_string); free(json_string); @@ -447,18 +445,43 @@ TEST(export_unit_test, test_add_json_length_is_on_margin_4095) free(buf); } -TEST(simple_test_json_writer, test1) +TEST(export_unit_test, json_writer_length_is_on_margin_4095_int) { + char str[4090]; + memset(str, 'a', 4089); // 4089 + ':' + '"'*2 + ‘{’ + ‘}’ = 4095 + str[4089] = '\0'; + + struct json_writer *writer = json_writer_init(); + json_writer_start_map(writer); + json_writer_int_field(writer, str, 6); + json_writer_end_map(writer); + char *result; + size_t len; + json_writer_finish(writer, &result, &len); + EXPECT_EQ(len, 4095); + std::string target = "{\"" + std::string(str) + "\":6}"; + EXPECT_STREQ(result, target.c_str()); + + free(result); +} + +TEST(export_unit_test, json_writer_length_is_on_margin_4096_string) +{ + char str[4090]; + memset(str, 'a', 4087); // 4087 + ':' + '"'*4 + ‘{’ + ‘}’ + int1 = 4096 + str[4087] = '\0'; + struct json_writer *writer = json_writer_init(); json_writer_start_map(writer); - json_writer_str_field(writer, "key", "value", 5); - json_writer_int_field(writer, "key2", 123); - json_writer_double_field(writer, "key3", 1.1); + json_writer_str_field(writer, str, "6", 1); json_writer_end_map(writer); char *result; size_t len; json_writer_finish(writer, &result, &len); - printf("simple_test_json_writer %s\n", result); + EXPECT_EQ(len, 4095); + std::string target = "{\"" + std::string(str) + "\":\"6\"}"; + EXPECT_STREQ(result, target.c_str()); + free(result); } -- cgit v1.2.3