diff --git a/src/test.cpp b/src/test.cpp index c642cc5..4127be2 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -173,17 +173,18 @@ struct MinifyState { int64_t index; bool isObject; }; + std::string result; void on_begin_value() { if (!stack.empty()) { auto &back = stack.back(); if (back.isObject && back.index % 2 == 0 && back.index > 0) { - printf(","); + result.append(","); } if (back.isObject && back.index % 2 == 1 && back.index > 0) { - printf(":"); + result.append(":"); } if (!back.isObject && back.index > 0) { - printf(","); + result.append(","); } ++back.index; } @@ -197,57 +198,85 @@ Callbacks minifyCallbacks() { auto *state = (MinifyState *)p; state->on_begin_value(); state->stack.push_back({0, true}); - printf("{"); + state->result.append("{"); }; result.on_end_object = +[](void *p) { auto *state = (MinifyState *)p; state->stack.pop_back(); - printf("}"); + state->result.append("}"); }; result.on_begin_string = +[](void *p) { auto *state = (MinifyState *)p; state->on_begin_value(); - printf("\""); + state->result.append("\""); + }; + result.on_string_data = +[](void *p, const char *buf, int len) { + auto *state = (MinifyState *)p; + state->result.append(std::string(buf, len)); + }; + result.on_end_string = +[](void *p) { + auto *state = (MinifyState *)p; + state->result.append("\""); }; - result.on_string_data = - +[](void *, const char *buf, int len) { printf("%.*s", len, buf); }; - result.on_end_string = +[](void *p) { printf("\""); }; result.on_begin_array = +[](void *p) { auto *state = (MinifyState *)p; state->on_begin_value(); state->stack.push_back({0, false}); - printf("["); + state->result.append("["); }; result.on_end_array = +[](void *p) { auto *state = (MinifyState *)p; state->stack.pop_back(); - printf("]"); + state->result.append("]"); }; result.on_begin_number = +[](void *p) { auto *state = (MinifyState *)p; state->on_begin_value(); }; - result.on_number_data = - +[](void *, const char *buf, int len) { printf("%.*s", len, buf); }; + result.on_number_data = +[](void *p, const char *buf, int len) { + auto *state = (MinifyState *)p; + state->result.append(std::string(buf, len)); + }; result.on_end_number = +[](void *) {}; result.on_true_literal = +[](void *p) { auto *state = (MinifyState *)p; state->on_begin_value(); - printf("true"); + state->result.append("true"); }; result.on_false_literal = +[](void *p) { auto *state = (MinifyState *)p; state->on_begin_value(); - printf("false"); + state->result.append("false"); }; result.on_null_literal = +[](void *p) { auto *state = (MinifyState *)p; state->on_begin_value(); - printf("null"); + state->result.append("null"); }; return result; } +void testStreaming(std::string const &json) { + MinifyState streaming; + MinifyState batch; + auto c = minifyCallbacks(); + { + auto copy = json; + parser3::Parser3 parser(&c, &streaming); + for (int i = 0; i < copy.size(); ++i) { + REQUIRE(parser.parse(copy.data() + i, 1) == parser3::S_AGAIN); + } + CHECK(parser.parse(nullptr, 0) == parser3::S_OK); + } + { + auto copy = json; + parser3::Parser3 parser(&c, &batch); + REQUIRE(parser.parse(copy.data(), copy.size()) == parser3::S_AGAIN); + CHECK(parser.parse(nullptr, 0) == parser3::S_OK); + } + CHECK(streaming.result == batch.result); +} + Callbacks noopCallbacks() { Callbacks result; result.on_begin_object = +[](void *) {}; @@ -305,6 +334,8 @@ TEST_CASE("parser3") { } } +TEST_CASE("streaming") { testStreaming(json); } + TEST_CASE("bench3") { auto c = noopCallbacks(); ankerl::nanobench::Bench bench;