Wee fuzz test and associated bug fixes

This commit is contained in:
2025-05-18 13:46:00 -04:00
parent 9543aba2ad
commit b7f6ed1c9c
5 changed files with 179 additions and 106 deletions

View File

@@ -1,6 +1,5 @@
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cstring>
@@ -10,6 +9,7 @@
#include <nanobench.h>
#include <simdjson.h>
#include "minify.h"
#include "parser3.h"
// This is the JSON grammar in McKeeman Form.
@@ -167,92 +167,21 @@ Callbacks printCallbacks() {
return result;
}
struct MinifyState {
bool isKey = false;
struct Cursor {
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) {
result.append(",");
}
if (back.isObject && back.index % 2 == 1 && back.index > 0) {
result.append(":");
}
if (!back.isObject && back.index > 0) {
result.append(",");
}
++back.index;
}
}
std::vector<Cursor> stack;
};
Callbacks minifyCallbacks() {
Callbacks noopCallbacks() {
Callbacks result;
result.on_begin_object = +[](void *p) {
auto *state = (MinifyState *)p;
state->on_begin_value();
state->stack.push_back({0, true});
state->result.append("{");
};
result.on_end_object = +[](void *p) {
auto *state = (MinifyState *)p;
state->stack.pop_back();
state->result.append("}");
};
result.on_begin_string = +[](void *p) {
auto *state = (MinifyState *)p;
state->on_begin_value();
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_begin_array = +[](void *p) {
auto *state = (MinifyState *)p;
state->on_begin_value();
state->stack.push_back({0, false});
state->result.append("[");
};
result.on_end_array = +[](void *p) {
auto *state = (MinifyState *)p;
state->stack.pop_back();
state->result.append("]");
};
result.on_begin_number = +[](void *p) {
auto *state = (MinifyState *)p;
state->on_begin_value();
};
result.on_number_data = +[](void *p, const char *buf, int len) {
auto *state = (MinifyState *)p;
state->result.append(std::string(buf, len));
};
result.on_begin_object = +[](void *) {};
result.on_end_object = +[](void *) {};
result.on_begin_string = +[](void *) {};
result.on_string_data = +[](void *, const char *buf, int len) {};
result.on_end_string = +[](void *) {};
result.on_begin_array = +[](void *) {};
result.on_end_array = +[](void *) {};
result.on_begin_number = +[](void *) {};
result.on_number_data = +[](void *, const char *buf, int len) {};
result.on_end_number = +[](void *) {};
result.on_true_literal = +[](void *p) {
auto *state = (MinifyState *)p;
state->on_begin_value();
state->result.append("true");
};
result.on_false_literal = +[](void *p) {
auto *state = (MinifyState *)p;
state->on_begin_value();
state->result.append("false");
};
result.on_null_literal = +[](void *p) {
auto *state = (MinifyState *)p;
state->on_begin_value();
state->result.append("null");
};
result.on_true_literal = +[](void *) {};
result.on_false_literal = +[](void *) {};
result.on_null_literal = +[](void *) {};
return result;
}
@@ -277,24 +206,6 @@ void testStreaming(std::string const &json) {
CHECK(streaming.result == batch.result);
}
Callbacks noopCallbacks() {
Callbacks result;
result.on_begin_object = +[](void *) {};
result.on_end_object = +[](void *) {};
result.on_begin_string = +[](void *) {};
result.on_string_data = +[](void *, const char *buf, int len) {};
result.on_end_string = +[](void *) {};
result.on_begin_array = +[](void *) {};
result.on_end_array = +[](void *) {};
result.on_begin_number = +[](void *) {};
result.on_number_data = +[](void *, const char *buf, int len) {};
result.on_end_number = +[](void *) {};
result.on_true_literal = +[](void *) {};
result.on_false_literal = +[](void *) {};
result.on_null_literal = +[](void *) {};
return result;
}
} // namespace
TEST_CASE("parser3") {