Make out of order iteration tests actually test errors in the loop

This commit is contained in:
John Keiser 2021-02-21 11:39:27 -08:00
parent 3076de0405
commit 74d6658f39
4 changed files with 86 additions and 17 deletions

View File

@ -106,18 +106,18 @@ namespace array_error_tests {
TEST_START();
auto json = R"([ [ 1, 2 ] ])"_padded;
SUBTEST("simdjson_result<value>", test_ondemand_doc(json, [&](auto doc) {
for (auto element : doc) {
for (auto subelement : element) { ASSERT_SUCCESS(subelement); }
ASSERT_ERROR( element.begin(), OUT_OF_ORDER_ITERATION );
for (auto arr : doc) {
for (auto subelement : arr) { ASSERT_SUCCESS(subelement); }
ASSERT_ITERATE_ERROR( arr, OUT_OF_ORDER_ITERATION );
}
return true;
}));
SUBTEST("value", test_ondemand_doc(json, [&](auto doc) {
for (auto element : doc) {
ondemand::value val;
ASSERT_SUCCESS( element.get(val) );
for (auto subelement : val) { ASSERT_SUCCESS(subelement); }
ASSERT_ERROR( val.begin(), OUT_OF_ORDER_ITERATION );
ondemand::value arr;
ASSERT_SUCCESS( element.get(arr) );
for (auto subelement : arr) { ASSERT_SUCCESS(subelement); }
ASSERT_ITERATE_ERROR( arr, OUT_OF_ORDER_ITERATION );
}
return true;
}));
@ -125,7 +125,7 @@ namespace array_error_tests {
for (auto element : doc) {
auto arr = element.get_array();
for (auto subelement : arr) { ASSERT_SUCCESS(subelement); }
ASSERT_ERROR( arr.begin(), OUT_OF_ORDER_ITERATION );
ASSERT_ITERATE_ERROR( arr, OUT_OF_ORDER_ITERATION );
}
return true;
}));
@ -134,12 +134,43 @@ namespace array_error_tests {
ondemand::array arr;
ASSERT_SUCCESS( element.get(arr) );
for (auto subelement : arr) { ASSERT_SUCCESS(subelement); }
ASSERT_ERROR( arr.begin(), OUT_OF_ORDER_ITERATION );
ASSERT_ITERATE_ERROR( arr, OUT_OF_ORDER_ITERATION );
}
return true;
}));
TEST_SUCCEED();
}
bool out_of_order_top_level_array_iteration_error() {
TEST_START();
auto json = R"([ 1, 2 ])"_padded;
SUBTEST("simdjson_result<document>", test_ondemand_doc(json, [&](auto arr) {
for (auto element : arr) { ASSERT_SUCCESS(element); }
ASSERT_ITERATE_ERROR( arr, OUT_OF_ORDER_ITERATION );
return true;
}));
SUBTEST("document", test_ondemand_doc(json, [&](auto doc) {
ondemand::document arr;
ASSERT_SUCCESS( std::move(doc).get(arr) );
for (auto element : arr) { ASSERT_SUCCESS(element); }
ASSERT_ITERATE_ERROR( arr, OUT_OF_ORDER_ITERATION );
return true;
}));
SUBTEST("simdjson_result<array>", test_ondemand_doc(json, [&](auto doc) {
simdjson_result<ondemand::array> arr = doc.get_array();
for (auto element : arr) { ASSERT_SUCCESS(element); }
ASSERT_ITERATE_ERROR( arr, OUT_OF_ORDER_ITERATION );
return true;
}));
SUBTEST("array", test_ondemand_doc(json, [&](auto doc) {
ondemand::array arr;
ASSERT_SUCCESS( doc.get(arr) );
for (auto element : arr) { ASSERT_SUCCESS(element); }
ASSERT_ITERATE_ERROR( arr, OUT_OF_ORDER_ITERATION );
return true;
}));
TEST_SUCCEED();
}
#endif // SIMDJSON_DEVELOPMENT_CHECKS
bool run() {
@ -150,6 +181,7 @@ namespace array_error_tests {
array_iterate_unclosed_error() &&
#ifdef SIMDJSON_DEVELOPMENT_CHECKS
out_of_order_array_iteration_error() &&
out_of_order_top_level_array_iteration_error() &&
#endif
true;
}

View File

@ -135,7 +135,7 @@ namespace object_error_tests {
for (auto element : doc) {
auto obj = element.get_object();
for (auto field : obj) { ASSERT_SUCCESS(field); }
ASSERT_ERROR( obj.begin(), OUT_OF_ORDER_ITERATION );
ASSERT_ITERATE_ERROR( obj, OUT_OF_ORDER_ITERATION );
}
return true;
}));
@ -144,13 +144,32 @@ namespace object_error_tests {
ondemand::object obj;
ASSERT_SUCCESS( element.get(obj) );
for (auto field : obj) { ASSERT_SUCCESS(field); }
ASSERT_ERROR( obj.begin(), OUT_OF_ORDER_ITERATION );
ASSERT_ITERATE_ERROR( obj, OUT_OF_ORDER_ITERATION );
}
return true;
}));
TEST_SUCCEED();
}
bool out_of_order_top_level_object_iteration_error() {
TEST_START();
auto json = R"({ "x": 1, "y": 2 })"_padded;
SUBTEST("simdjson_result<object>", test_ondemand_doc(json, [&](auto doc) {
auto obj = doc.get_object();
for (auto field : obj) { ASSERT_SUCCESS(field); }
ASSERT_ITERATE_ERROR( obj, OUT_OF_ORDER_ITERATION );
return true;
}));
SUBTEST("object", test_ondemand_doc(json, [&](auto doc) {
ondemand::object obj;
ASSERT_SUCCESS( doc.get(obj) );
for (auto field : obj) { ASSERT_SUCCESS(field); }
ASSERT_ITERATE_ERROR( obj, OUT_OF_ORDER_ITERATION );
return true;
}));
TEST_SUCCEED();
}
bool out_of_order_object_index_child_error() {
TEST_START();
auto json = R"([ { "x": 1, "y": 2 } ])"_padded;
@ -528,6 +547,7 @@ namespace object_error_tests {
object_lookup_miss_next_error() &&
#ifdef SIMDJSON_DEVELOPMENT_CHECKS
out_of_order_object_iteration_error() &&
out_of_order_top_level_object_iteration_error() &&
out_of_order_object_index_child_error() &&
out_of_order_object_index_sibling_error() &&
out_of_order_object_find_field_child_error() &&

View File

@ -21,12 +21,14 @@ namespace scalar_tests {
return true;
}));
SUBTEST( "document", test_ondemand_doc(json, [&](auto doc_result) {
ondemand::document doc;
ASSERT_SUCCESS( std::move(doc_result).get(doc) );
T actual;
ASSERT_SUCCESS( doc_result.get(actual) );
ASSERT_SUCCESS( doc.get(actual) );
ASSERT_EQUAL( expected, actual );
// Test it twice (scalars can be retrieved more than once)
if (test_twice) {
ASSERT_SUCCESS( doc_result.get(actual) );
ASSERT_SUCCESS( doc.get(actual) );
ASSERT_EQUAL( expected, actual );
}
return true;
@ -47,12 +49,14 @@ namespace scalar_tests {
return true;
}));
SUBTEST( "document", test_ondemand_doc(whitespace_json, [&](auto doc_result) {
ondemand::document doc;
ASSERT_SUCCESS( std::move(doc_result).get(doc) );
T actual;
ASSERT_SUCCESS( doc_result.get(actual) );
ASSERT_SUCCESS( doc.get(actual) );
ASSERT_EQUAL( expected, actual );
// Test it twice (scalars can be retrieved more than once)
if (test_twice) {
ASSERT_SUCCESS( doc_result.get(actual) );
ASSERT_SUCCESS( doc.get(actual) );
ASSERT_EQUAL( expected, actual );
}
return true;
@ -213,7 +217,9 @@ namespace scalar_tests {
bool test_scalar_value_exception(const padded_string &json, const T &expected) {
std::cout << "- JSON: " << json << endl;
SUBTEST( "document", test_ondemand_doc(json, [&](auto doc_result) {
ASSERT_EQUAL( expected, T(doc_result) );
ondemand::document doc;
ASSERT_SUCCESS( std::move(doc_result).get(doc) );
ASSERT_EQUAL( expected, T(doc) );
return true;
}));
padded_string array_json = std::string("[") + std::string(json) + "]";

View File

@ -88,7 +88,17 @@ simdjson_really_inline bool assert_true(bool value, const char *operation = "res
}
return true;
}
template<typename T>
simdjson_really_inline bool assert_iterate_error(T &arr, simdjson::error_code expected, const char *operation = "result") {
int count = 0;
for (auto element : arr) {
count++;
if (!assert_error( element, expected, operation )) {
return false;
}
}
return assert_equal( count, 1, operation );
}
#define TEST_START() do { std::cout << "Running " << __func__ << " ..." << std::endl; } while(0);
#define SUBTEST(NAME, TEST) do { std::cout << "- Subtest " << (NAME) << " ..." << std::endl; if (!(TEST)) { return false; } } while (0);
#define ASSERT_EQUAL(ACTUAL, EXPECTED) do { if (!::assert_equal ((ACTUAL), (EXPECTED), #ACTUAL)) { return false; } } while (0);
@ -97,6 +107,7 @@ simdjson_really_inline bool assert_true(bool value, const char *operation = "res
#define ASSERT_ERROR(ACTUAL, EXPECTED) do { if (!::assert_error ((ACTUAL), (EXPECTED), #ACTUAL)) { return false; } } while (0);
#define ASSERT_TRUE(ACTUAL) do { if (!::assert_true ((ACTUAL), #ACTUAL)) { return false; } } while (0);
#define ASSERT(ACTUAL, MESSAGE) do { if (!::assert_true ((ACTUAL), (MESSAGE))) { return false; } } while (0);
#define ASSERT_ITERATE_ERROR(ACTUAL, EXPECTED) do { if (!::assert_iterate_error((ACTUAL), (EXPECTED), #ACTUAL)) { return false; } } while (0);
#define RUN_TEST(ACTUAL) do { if (!(ACTUAL)) { return false; } } while (0);
#define TEST_FAIL(MESSAGE) do { std::cerr << "FAIL: " << (MESSAGE) << std::endl; return false; } while (0);
#define TEST_SUCCEED() do { return true; } while (0);