@@ -312,7 +312,93 @@ namespace document_stream_tests {
312
312
return count == 1 ;
313
313
}
314
314
#endif
315
+ bool simple_example () {
316
+ std::cout << " Running " << __func__ << std::endl;
317
+ // The last JSON document is
318
+ // intentionally truncated.
319
+ auto json = R"( [1,2,3] {"1":1,"2":3,"4":4} [1,2,3] )" _padded;
320
+ simdjson::dom::parser parser;
321
+ size_t count = 0 ;
322
+ simdjson::dom::document_stream stream;
323
+ // We use a window of json.size() though any large value would do.
324
+ ASSERT_SUCCESS ( parser.parse_many (json, json.size ()).get (stream) );
325
+ auto i = stream.begin ();
326
+ for (; i != stream.end (); ++i) {
327
+ auto doc = *i;
328
+ if (!doc.error ()) {
329
+ std::cout << " got full document at " << i.current_index () << std::endl;
330
+ count++;
331
+ }
332
+ }
333
+ if (count != 3 ) {
334
+ std::cerr << " Expected to get three full documents " << std::endl;
335
+ return false ;
336
+ }
337
+ size_t index = i.current_index ();
338
+ if (index != 38 ) {
339
+ std::cerr << " Expected to stop after the three full documents " << std::endl;
340
+ std::cerr << " index = " << index << std::endl;
341
+ return false ;
342
+ }
343
+ return true ;
344
+ }
345
+
346
+
347
+ bool truncated_window () {
348
+ std::cout << " Running " << __func__ << std::endl;
349
+ // The last JSON document is
350
+ // intentionally truncated.
351
+ auto json = R"( [1,2,3] {"1":1,"2":3,"4":4} [1,2 )" _padded;
352
+ simdjson::dom::parser parser;
353
+ size_t count = 0 ;
354
+ simdjson::dom::document_stream stream;
355
+ // We use a window of json.size() though any large value would do.
356
+ ASSERT_SUCCESS ( parser.parse_many (json, json.size ()).get (stream) );
357
+ auto i = stream.begin ();
358
+ for (; i != stream.end (); ++i) {
359
+ auto doc = *i;
360
+ if (!doc.error ()) {
361
+ std::cout << " got full document at " << i.current_index () << std::endl;
362
+ count++;
363
+ }
364
+ }
365
+ if (count != 2 ) {
366
+ std::cerr << " Expected to get two full documents " << std::endl;
367
+ return false ;
368
+ }
369
+ size_t index = i.current_index ();
370
+ if (index != 29 ) {
371
+ std::cerr << " Expected to stop after the two full documents " << std::endl;
372
+ std::cerr << " index = " << index << std::endl;
373
+ return false ;
374
+ }
375
+ return true ;
376
+ }
315
377
378
+ bool truncated_window_unclosed_string () {
379
+ std::cout << " Running " << __func__ << std::endl;
380
+ // The last JSON document is intentionally truncated. In this instance, we use
381
+ // a truncated string which will create trouble since stage 1 will recognize the
382
+ // JSON as invalid and refuse to even start parsing.
383
+ auto json = R"( [1,2,3] {"1":1,"2":3,"4":4} "intentionally unclosed string )" _padded;
384
+ simdjson::dom::parser parser;
385
+ simdjson::dom::document_stream stream;
386
+ // We use a window of json.size() though any large value would do.
387
+ ASSERT_SUCCESS ( parser.parse_many (json,json.size ()).get (stream) );
388
+ // Rest is ineffective because stage 1 fails.
389
+ auto i = stream.begin ();
390
+ for (; i != stream.end (); ++i) {
391
+ auto doc = *i;
392
+ if (!doc.error ()) {
393
+ std::cout << " got full document at " << i.current_index () << std::endl;
394
+ return false ;
395
+ } else {
396
+ std::cout << doc.error () << std::endl;
397
+ return (doc.error () == simdjson::UNCLOSED_STRING);
398
+ }
399
+ }
400
+ return false ;
401
+ }
316
402
bool small_window () {
317
403
std::cout << " Running " << __func__ << std::endl;
318
404
char input[2049 ];
@@ -502,7 +588,10 @@ namespace document_stream_tests {
502
588
}
503
589
504
590
bool run () {
505
- return issue1307 () &&
591
+ return simple_example () &&
592
+ truncated_window () &&
593
+ truncated_window_unclosed_string () &&
594
+ issue1307 () &&
506
595
issue1308 () &&
507
596
issue1309 () &&
508
597
issue1310 () &&
0 commit comments