1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
#include "simdjson.h"
#include "test_ondemand.h"
using namespace simdjson;
namespace parse_api_tests {
using namespace std;
const padded_string BASIC_JSON = "[1,2,3]"_padded;
const padded_string BASIC_NDJSON = "[1,2,3]\n[4,5,6]"_padded;
const padded_string EMPTY_NDJSON = ""_padded;
bool parser_iterate_empty() {
TEST_START();
FILE *p;
// Of course, we could just call iterate on the empty string, but
// we want to test the whole process.
const char *const tmpfilename = "emptyondemand.txt";
if((p = fopen(tmpfilename, "w")) != nullptr) {
fclose(p);
auto json = padded_string::load(tmpfilename);
ondemand::document doc;
ondemand::parser parser;
auto error = parser.iterate(json).get(doc);
remove(tmpfilename);
if(error != simdjson::EMPTY) {
std::cerr << "Was expecting empty but got " << error << std::endl;
return false;
}
} else {
std::cout << "Warning: I could not create temporary file " << tmpfilename << std::endl;
std::cout << "We omit testing the empty file case." << std::endl;
}
TEST_SUCCEED();
}
bool parser_iterate() {
TEST_START();
ondemand::parser parser;
auto doc = parser.iterate(BASIC_JSON);
ASSERT_SUCCESS( doc.get_array() );
return true;
}
bool parser_iterate_padded() {
TEST_START();
ondemand::parser parser;
const char json_str[] = "12\0 ";// 64 bytes of padding
ASSERT_EQUAL(sizeof(json_str), 66);
ASSERT_EQUAL(strlen(json_str), 2);
{
cout << "- char*" << endl;
auto doc = parser.iterate(json_str, strlen(json_str), sizeof(json_str));
ASSERT_SUCCESS( doc.get_double() );
}
{
cout << "- uint8_t*" << endl;
const uint8_t* json = reinterpret_cast<const uint8_t*>(json_str);
auto doc = parser.iterate(json, strlen(json_str), sizeof(json_str));
ASSERT_SUCCESS( doc.get_double() );
}
{
cout << "- string_view" << endl;
std::string_view json(json_str);
auto doc = parser.iterate(json, sizeof(json_str));
ASSERT_SUCCESS( doc.get_double() );
}
{
cout << "- string" << endl;
std::string json = "12";
json.reserve(json.length() + SIMDJSON_PADDING);
auto doc = parser.iterate(json);
ASSERT_SUCCESS( doc.get_double() );
}
TEST_SUCCEED();
}
bool parser_iterate_padded_string_view() {
TEST_START();
ondemand::parser parser;
const char json_str[] = "12\0 "; // 64 bytes of padding
ASSERT_EQUAL(sizeof(json_str), 66);
ASSERT_EQUAL(strlen(json_str), 2);
{
cout << "- padded_string_view(string_view)" << endl;
padded_string_view json(std::string_view(json_str), sizeof(json_str));
auto doc = parser.iterate(json);
ASSERT_SUCCESS( doc.get_double() );
}
{
cout << "- padded_string_view(char*)" << endl;
auto doc = parser.iterate(padded_string_view(json_str, strlen(json_str), sizeof(json_str)));
ASSERT_SUCCESS( doc.get_double() );
}
{
cout << "- padded_string_view(string)" << endl;
std::string json = "12";
json.reserve(json.length() + SIMDJSON_PADDING);
auto doc = parser.iterate(padded_string_view(json));
ASSERT_SUCCESS( doc.get_double() );
}
{
cout << "- padded_string_view(string_view(char*))" << endl;
padded_string_view json(json_str, sizeof(json_str));
auto doc = parser.iterate(json);
ASSERT_SUCCESS( doc.get_double() );
}
TEST_SUCCEED();
}
bool parser_iterate_insufficient_padding() {
TEST_START();
ondemand::parser parser;
constexpr char json_str[] = "12\0 "; // 63 bytes of padding
ASSERT_EQUAL(sizeof(json_str), 65);
ASSERT_EQUAL(strlen(json_str), 2);
ASSERT_EQUAL(padded_string_view(json_str, strlen(json_str), sizeof(json_str)).padding(), 63);
ASSERT_EQUAL(SIMDJSON_PADDING, 64);
{
cout << "- char*, 63 padding" << endl;
ASSERT_ERROR( parser.iterate(json_str, strlen(json_str), sizeof(json_str)), INSUFFICIENT_PADDING );
cout << "- char*, 0 padding" << endl;
ASSERT_ERROR( parser.iterate(json_str, strlen(json_str), strlen(json_str)), INSUFFICIENT_PADDING );
}
{
std::string_view json(json_str);
cout << "- string_view, 63 padding" << endl;
ASSERT_ERROR( parser.iterate(json, sizeof(json_str)), INSUFFICIENT_PADDING );
cout << "- string_view, 0 padding" << endl;
ASSERT_ERROR( parser.iterate(json, strlen(json_str)), INSUFFICIENT_PADDING );
}
{
std::string json = "12345642314123421321321321321321312321321321321312";
json.shrink_to_fit();
cout << "- string, 0 padding" << endl;
ASSERT_SUCCESS( parser.iterate(json) );
}
{
std::string json = "12345642314123421321321321321321312321321321321312";
json.shrink_to_fit();
cout << "- string, 0 padding" << endl;
ASSERT_ERROR( parser.iterate((const std::string&)json), INSUFFICIENT_PADDING );
// It's actually kind of hard to allocate "just enough" capacity, since the string tends
// to grow more than you tell it to.
}
TEST_SUCCEED();
}
#if SIMDJSON_EXCEPTIONS
bool parser_iterate_exception() {
TEST_START();
ondemand::parser parser;
auto doc = parser.iterate(BASIC_JSON);
simdjson_unused ondemand::array array = doc;
TEST_SUCCEED();
}
bool parser_document_reuse() {
TEST_START();
ondemand::document doc;
// A document spans about 40 bytes. Nevertheless, some users
// would rather reuse them.
std::cout << sizeof(doc) << std::endl;
auto json = R"({"key": "value"})"_padded;
auto jsonbad = R"({"key": "value")"_padded; // deliberaty broken
auto jsonunclosedstring = "{\"coordinates:[{\"x\":1.1,\"y\":2.2,\"z\":3.3}]}"_padded;
std::string_view output;
ondemand::parser parser;
std::cout << "correct document (1)" << std::endl;
ASSERT_SUCCESS( parser.iterate(json).get(doc) );
ASSERT_SUCCESS(simdjson::to_json_string(doc).get(output));
std::cout << output << std::endl;
std::cout << "correct document (2)" << std::endl;
ASSERT_SUCCESS( parser.iterate(json).get(doc) );
for(ondemand::field field : doc.get_object() ) {
std::cout << "field: " << field.key() << std::endl;
}
std::cout << "unclosed string document " << std::endl;
simdjson::error_code error;
if((error = parser.iterate(jsonunclosedstring).get(doc)) == SUCCESS) {
// fallback kernel:
ASSERT_EQUAL( doc.get_object().find_field("coordinates").error(), TAPE_ERROR );
} else {
// regular kernels:
ASSERT_EQUAL( error, UNCLOSED_STRING );
}
std::cout << "truncated document " << std::endl;
ASSERT_SUCCESS( parser.iterate(jsonbad).get(doc) );
ASSERT_EQUAL( simdjson::to_json_string(doc).get(output), TAPE_ERROR );
std::cout << "correct document with new doc" << std::endl;
ondemand::document doc2;
ASSERT_SUCCESS( parser.iterate(json).get(doc2) );
for(ondemand::field field : doc2.get_object() ) {
std::cout << "field: " << field.key() << std::endl;
}
std::cout << "correct document (3): " << doc.to_debug_string() << std::endl;
std::cout << "correct document (3)" << std::endl;
ASSERT_SUCCESS( parser.iterate(json).get(doc) );
std::cout << doc.to_debug_string() << std::endl;
for(ondemand::field field : doc.get_object() ) {
std::cout << "field: " << field.key() << std::endl;
}
std::cout << "unclosed string document " << std::endl;
ASSERT_SUCCESS( parser.iterate(jsonbad).get(doc) );
ASSERT_EQUAL( simdjson::to_json_string(doc).get(output), TAPE_ERROR );
// next two lines are terrible code.
doc.~document();
doc = ondemand::document();
//
std::cout << "correct document (4)" << std::endl;
ASSERT_SUCCESS( parser.iterate(json).get(doc) );
ASSERT_SUCCESS( simdjson::to_json_string(doc).get(output) );
std::cout << output << std::endl;
std::cout << "unclosed string document " << std::endl;
if((error = parser.iterate(jsonunclosedstring).get(doc)) == SUCCESS) {
// fallback kernel:
ASSERT_EQUAL( doc.get_object().find_field("coordinates").error(), TAPE_ERROR );
} else {
// regular kernels:
ASSERT_EQUAL( error, UNCLOSED_STRING );
}
// next two lines are terrible code.
doc.~document();
doc = ondemand::document();
//
std::cout << "correct document (5)" << std::endl;
ASSERT_SUCCESS( parser.iterate(json).get(doc) );
ASSERT_SUCCESS( simdjson::to_json_string(doc).get(output) );
std::cout << output << std::endl;
TEST_SUCCEED();
}
#endif // SIMDJSON_EXCEPTIONS
bool run() {
return parser_iterate_empty() &&
parser_iterate() &&
parser_iterate_padded() &&
parser_iterate_padded_string_view() &&
parser_iterate_insufficient_padding() &&
#if SIMDJSON_EXCEPTIONS
parser_document_reuse() &&
parser_iterate_exception() &&
#endif // SIMDJSON_EXCEPTIONS
true;
}
}
int main(int argc, char *argv[]) {
return test_main(argc, argv, parse_api_tests::run);
}
|