From a3b508ceffafef929945eb6819aaf83e6d0e939c Mon Sep 17 00:00:00 2001 From: John Keiser Date: Tue, 14 Apr 2020 12:01:26 -0700 Subject: [PATCH] Test get<>(), exception vs. no exception, explicit vs. implicit cast --- tests/basictests.cpp | 819 ++++++++++++++++++++++--------------------- 1 file changed, 423 insertions(+), 396 deletions(-) diff --git a/tests/basictests.cpp b/tests/basictests.cpp index 1fbf0e48..993b39e0 100644 --- a/tests/basictests.cpp +++ b/tests/basictests.cpp @@ -23,9 +23,17 @@ const char *TWITTER_JSON = SIMDJSON_BENCHMARK_DATA_DIR "twitter.json"; const char *AMAZON_CELLPHONES_NDJSON = SIMDJSON_BENCHMARK_DATA_DIR "amazon_cellphones.ndjson"; -#define ASSERT_EQUAL(ACTUAL, EXPECTED) if ((ACTUAL) != (EXPECTED)) { std::cerr << "Expected " << #ACTUAL << " to be " << (EXPECTED) << ", got " << (ACTUAL) << " instead!" << std::endl; return false; } -#define ASSERT_TRUE(ACTUAL) ASSERT_EQUAL(ACTUAL, true) -#define ASSERT_FALSE(ACTUAL) ASSERT_EQUAL(ACTUAL, false) +template +bool equals_expected(T actual, T expected) { + return actual == expected; +} +template<> +bool equals_expected(const char *actual, const char *expected) { + return !strcmp(actual, expected); +} + +#define ASSERT_EQUAL(ACTUAL, EXPECTED) if (!equals_expected(ACTUAL, EXPECTED)) { std::cerr << "Expected " << #ACTUAL << " to be " << (EXPECTED) << ", got " << (ACTUAL) << " instead!" << std::endl; return false; } +#define ASSERT(RESULT, MESSAGE) if (!(RESULT)) { std::cerr << MESSAGE << std::endl; return false; } #define ASSERT_SUCCESS(ERROR) if (ERROR) { std::cerr << (ERROR) << std::endl; return false; } namespace number_tests { @@ -1150,449 +1158,468 @@ namespace type_tests { } )"_padded; - bool test_array() { - std::cout << "Running " << __func__ << std::endl; + // test_implicit_cast::with(value, [](T value) { return true; }) + // Makes it so we test implicit casts for anything that supports them, but *don't* test them + // for const char * + template + class test_implicit_cast { + public: + template + static bool with(A input, F const & test); + template + static bool error_with(A input, simdjson::error_code expected_error); + }; - const auto key = "array"; - const auto expected_type = dom::element_type::ARRAY; - - dom::parser parser; - simdjson_result result = parser.parse(ALL_TYPES_JSON)[key]; - ASSERT_SUCCESS(result.error()); - - // Test simdjson_result.is() (error chain) - ASSERT_TRUE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is_null()); - - // Test simdjson_result.type() (error chain) - simdjson::error_code error; - dom::element_type type; - result.type().tie(type, error); - ASSERT_SUCCESS(error); - ASSERT_EQUAL(type, expected_type); - - // Test element.is() - dom::element element = result.first; - ASSERT_TRUE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is_null()); - - // Test element.type() - ASSERT_EQUAL(element.type(), expected_type); - - // Test element.get() - dom::array value; - result.get().tie(value, error); - ASSERT_SUCCESS(error); + template + template + bool test_implicit_cast::with(A input, F const & test) { + T actual; + actual = input; + return test(actual); + } + template<> + template + bool test_implicit_cast::with(A, F const &) { return true; } - bool test_object() { - std::cout << "Running " << __func__ << std::endl; - - const auto key = "object"; - const auto expected_type = dom::element_type::OBJECT; - - dom::parser parser; - simdjson_result result = parser.parse(ALL_TYPES_JSON)[key]; - ASSERT_SUCCESS(result.error()); - - // Test simdjson_result.is() (error chain) - ASSERT_FALSE(result.is()); - ASSERT_TRUE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is_null()); - - // Test simdjson_result.type() (error chain) - simdjson::error_code error; - dom::element_type type; - result.type().tie(type, error); - ASSERT_SUCCESS(error); - ASSERT_EQUAL(type, expected_type); - - // Test element.is() - dom::element element = result.first; - ASSERT_FALSE(element.is()); - ASSERT_TRUE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is_null()); - - // Test element.type() - ASSERT_EQUAL(element.type(), expected_type); - - // Test element.get() - dom::object value; - result.get().tie(value, error); - ASSERT_SUCCESS(error); - - return true; - } - - bool test_string() { - std::cout << "Running " << __func__ << std::endl; - - const auto key = "string"; - const auto expected_type = dom::element_type::STRING; - - dom::parser parser; - simdjson_result result = parser.parse(ALL_TYPES_JSON)[key]; - ASSERT_SUCCESS(result.error()); - - // Test simdjson_result.is() (error chain) - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_TRUE(result.is()); - ASSERT_TRUE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is_null()); - - // Test simdjson_result.type() (error chain) - simdjson::error_code error; - dom::element_type type; - result.type().tie(type, error); - ASSERT_SUCCESS(error); - ASSERT_EQUAL(type, expected_type); - - // Test element.is() - dom::element element = result.first; - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_TRUE(element.is()); - ASSERT_TRUE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is_null()); - - // Test element.type() - ASSERT_EQUAL(element.type(), expected_type); - - // Test element.get() - std::string_view value; - result.get().tie(value, error); - ASSERT_SUCCESS(error); - ASSERT_EQUAL(value, string_view("foo")); - - const char *value2; - result.get().tie(value2, error); - ASSERT_SUCCESS(error); - ASSERT_EQUAL(string_view(value2), string_view("foo")); - - return true; - } - - bool test_int64(const char *key, int64_t expected_value) { - std::cout << "Running " << __func__ << "(" << key << ")" << std::endl; - - const auto expected_type = dom::element_type::INT64; - - dom::parser parser; - simdjson_result result = parser.parse(ALL_TYPES_JSON)[key]; - ASSERT_SUCCESS(result.error()); - - // Test simdjson_result.is() (error chain) - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_TRUE(result.is()); - if (expected_value < 0) { - ASSERT_FALSE(result.is()); - } else { - ASSERT_TRUE(result.is()); + template + template + bool test_implicit_cast::error_with(A input, simdjson::error_code expected_error) { + try { + UNUSED T actual; + actual = input; + return false; + } catch(simdjson_error &e) { + ASSERT_EQUAL(e.error(), expected_error); + return true; } - ASSERT_TRUE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is_null()); + } - // Test simdjson_result.type() (error chain) - simdjson::error_code error; - dom::element_type type; - result.type().tie(type, error); - ASSERT_SUCCESS(error); - ASSERT_EQUAL(type, expected_type); + template<> + template + bool test_implicit_cast::error_with(A, simdjson::error_code) { + return true; + } - // Test element.is() + template + bool test_cast(simdjson_result result, T expected) { + std::cout << " test_cast<" << typeid(T).name() << "> expecting " << expected << std::endl; + // Grab the element out and check success dom::element element = result.first; - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_TRUE(element.is()); - if (expected_value < 0) { - ASSERT_FALSE(element.is()); - } else { - ASSERT_TRUE(element.is()); + + // get() == expected + T actual; + simdjson::error_code error; + result.get().tie(actual, error); + ASSERT_SUCCESS(error); + ASSERT_EQUAL(actual, expected); + + element.get().tie(actual, error); + ASSERT_SUCCESS(error); + ASSERT_EQUAL(actual, expected); + + // is() + bool actual_is; + result.is().tie(actual_is, error); + ASSERT_SUCCESS(error); + ASSERT_EQUAL(actual_is, true); + + actual_is = element.is(); + ASSERT_EQUAL(actual_is, true); + +#if SIMDJSON_EXCEPTIONS + try { + + // T() == expected + actual = T(result); + ASSERT_EQUAL(actual, expected); + + actual = T(element); + ASSERT_EQUAL(actual, expected); + + test_implicit_cast::with(result, [&](T a) { ASSERT_EQUAL(a, expected); return false; }); + + test_implicit_cast::with(element, [&](T a) { ASSERT_EQUAL(a, expected); return false; }); + + // get() == expected + actual = result.get(); + ASSERT_EQUAL(actual, expected); + + actual = element.get(); + ASSERT_EQUAL(actual, expected); + + // is() + actual_is = result.is(); + ASSERT_EQUAL(actual_is, true); + + } catch(simdjson_error &e) { + std::cerr << e.error() << std::endl; + return false; } - ASSERT_TRUE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is_null()); - // Test element.type() - ASSERT_EQUAL(element.type(), expected_type); - - // Test element.get() - int64_t value; - result.get().tie(value, error); - ASSERT_SUCCESS(error); - ASSERT_EQUAL(value, expected_value); +#endif return true; } - bool test_uint64(const char *key, uint64_t expected_value) { - std::cout << "Running " << __func__ << "(" << key << ")" << std::endl; - - const auto expected_type = dom::element_type::UINT64; - - dom::parser parser; - simdjson_result result = parser.parse(ALL_TYPES_JSON)[key]; - ASSERT_SUCCESS(result.error()); - - // Test simdjson_result.is() (error chain) - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_TRUE(result.is()); - ASSERT_TRUE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is_null()); - - // Test simdjson_result.type() (error chain) - simdjson::error_code error; - dom::element_type type; - result.type().tie(type, error); - ASSERT_SUCCESS(error); - ASSERT_EQUAL(type, expected_type); - - // Test element.is() + template + bool test_cast(simdjson_result result) { + std::cout << " test_cast<" << typeid(T).name() << "> expecting success" << std::endl; + // Grab the element out and check success dom::element element = result.first; - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_TRUE(element.is()); - ASSERT_TRUE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is_null()); - // Test element.type() - ASSERT_EQUAL(element.type(), expected_type); - - // Test element.get() - uint64_t value; - result.get().tie(value, error); - ASSERT_SUCCESS(error); - ASSERT_EQUAL(value, expected_value); - - return true; - } - - bool test_double(const char *key, double expected_value) { - std::cout << "Running " << __func__ << "(" << key << ")" << std::endl; - - const auto expected_type = dom::element_type::DOUBLE; - - dom::parser parser; - simdjson_result result = parser.parse(ALL_TYPES_JSON)[key]; - ASSERT_SUCCESS(result.error()); - - // Test simdjson_result.is() (error chain) - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_TRUE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is_null()); - - // Test simdjson_result.type() (error chain) + // get() == expected + T actual; simdjson::error_code error; - dom::element_type type; - result.type().tie(type, error); + result.get().tie(actual, error); ASSERT_SUCCESS(error); - ASSERT_EQUAL(type, expected_type); - // Test element.is() - dom::element element = result.first; - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_TRUE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is_null()); - - // Test element.type() - ASSERT_EQUAL(element.type(), expected_type); - - // Test element.get() - double value; - result.get().tie(value, error); + element.get().tie(actual, error); ASSERT_SUCCESS(error); - ASSERT_EQUAL(value, expected_value); + + // is() + bool actual_is; + result.is().tie(actual_is, error); + ASSERT_SUCCESS(error); + ASSERT_EQUAL(actual_is, true); + + actual_is = element.is(); + ASSERT_EQUAL(actual_is, true); + +#if SIMDJSON_EXCEPTIONS + + try { + + // T() + actual = T(result); + + actual = T(element); + + test_implicit_cast::with(result, [&](T) { return true; }); + + test_implicit_cast::with(element, [&](T) { return true; }); + + // get() == expected + actual = result.get(); + + actual = element.get(); + + // is() + actual_is = result.is(); + ASSERT_EQUAL(actual_is, true); + + } catch(simdjson_error &e) { + std::cerr << e.error() << std::endl; + return false; + } + +#endif return true; } - bool test_bool(const char *key, bool expected_value) { - std::cout << "Running " << __func__ << "(" << key << ")" << std::endl; + template + bool test_cast(simdjson_result result, simdjson::error_code expected_error) { + std::cout << " test_cast<" << typeid(T).name() << "> expecting error '" << expected_error << "'" << std::endl; + dom::element element = result.first; - const auto expected_type = dom::element_type::BOOL; - - dom::parser parser; - simdjson_result result = parser.parse(ALL_TYPES_JSON)[key]; - ASSERT_SUCCESS(result.error()); - - // Test simdjson_result.is() (error chain) - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_TRUE(result.is()); - ASSERT_FALSE(result.is_null()); - - // Test simdjson_result.type() (error chain) + // get() == expected + T actual; simdjson::error_code error; - dom::element_type type; - result.type().tie(type, error); + result.get().tie(actual, error); + ASSERT_EQUAL(error, expected_error); + + element.get().tie(actual, error); + ASSERT_EQUAL(error, expected_error); + + // is() + bool actual_is; + result.is().tie(actual_is, error); ASSERT_SUCCESS(error); - ASSERT_EQUAL(type, expected_type); + ASSERT_EQUAL(actual_is, false); - // Test element.is() - dom::element element = result.first; - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_TRUE(element.is()); - ASSERT_FALSE(element.is_null()); + actual_is = element.is(); + ASSERT_EQUAL(actual_is, false); - // Test element.type() - ASSERT_EQUAL(element.type(), expected_type); +#if SIMDJSON_EXCEPTIONS - // Test element.get() - bool value; - result.get().tie(value, error); - ASSERT_SUCCESS(error); - ASSERT_EQUAL(value, expected_value); + // T() + try { + actual = T(result); + return false; + } catch(simdjson_error &e) { + ASSERT_EQUAL(e.error(), expected_error); + } + + try { + actual = T(element); + return false; + } catch(simdjson_error &e) { + ASSERT_EQUAL(e.error(), expected_error); + } + + if (!test_implicit_cast::error_with(result, expected_error)) { return false; } + + if (!test_implicit_cast::error_with(result, expected_error)) { return true; } + + try { + + // is() + actual_is = result.is(); + ASSERT_EQUAL(actual_is, false); + + } catch(simdjson_error &e) { + std::cerr << e.error() << std::endl; + return false; + } + +#endif return true; } - bool test_null() { + bool test_type(simdjson_result result, dom::element_type expected_type) { + std::cout << " test_type() expecting " << expected_type << std::endl; + dom::element element = result.first; + + dom::element_type actual_type; + simdjson::error_code error; + result.type().tie(actual_type, error); + ASSERT_SUCCESS(error); + ASSERT_EQUAL(actual_type, expected_type); + + actual_type = element.type(); + ASSERT_SUCCESS(error); + ASSERT_EQUAL(actual_type, expected_type); + +#if SIMDJSON_EXCEPTIONS + + try { + + actual_type = result.type(); + ASSERT_EQUAL(actual_type, expected_type); + + } catch(simdjson_error &e) { + std::cerr << e.error() << std::endl; + return false; + } + +#endif // SIMDJSON_EXCEPTIONS + + return true; + } + + bool test_is_null(simdjson_result result, bool expected_is_null) { + std::cout << " test_is_null() expecting " << expected_is_null << std::endl; + // Grab the element out and check success + dom::element element = result.first; + + bool actual_is_null; + simdjson::error_code error; + result.is_null().tie(actual_is_null, error); + ASSERT_SUCCESS(error); + ASSERT_EQUAL(actual_is_null, expected_is_null); + + actual_is_null = element.is_null(); + ASSERT_EQUAL(actual_is_null, expected_is_null); + +#if SIMDJSON_EXCEPTIONS + + try { + + actual_is_null = result.is_null(); + ASSERT_EQUAL(actual_is_null, expected_is_null); + + } catch(simdjson_error &e) { + std::cerr << e.error() << std::endl; + return false; + } + +#endif // SIMDJSON_EXCEPTIONS + + return true; + } + + bool cast_array() { std::cout << "Running " << __func__ << std::endl; - const auto expected_type = dom::element_type::NULL_VALUE; + dom::parser parser; + simdjson_result result = parser.parse(ALL_TYPES_JSON)["array"]; + + return true + && test_type(result, dom::element_type::ARRAY) + && test_cast(result) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_is_null(result, false); + } + + bool cast_object() { + std::cout << "Running " << __func__ << std::endl; + + dom::parser parser; + simdjson_result result = parser.parse(ALL_TYPES_JSON)["object"]; + + return true + && test_type(result, dom::element_type::OBJECT) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_is_null(result, false); + } + + bool cast_string() { + std::cout << "Running " << __func__ << std::endl; + + dom::parser parser; + simdjson_result result = parser.parse(ALL_TYPES_JSON)["string"]; + + return true + && test_type(result, dom::element_type::STRING) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, "foo") + && test_cast(result, "foo") + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_is_null(result, false); + } + + bool cast_int64(const char *key, int64_t expected_value) { + std::cout << "Running " << __func__ << "(" << key << ")" << std::endl; + + dom::parser parser; + simdjson_result result = parser.parse(ALL_TYPES_JSON)[key]; + + return true + && test_type(result, dom::element_type::INT64) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, expected_value) + && (expected_value >= 0 ? + test_cast(result, expected_value) : + test_cast(result, NUMBER_OUT_OF_RANGE)) + && test_cast(result, expected_value) + && test_cast(result, INCORRECT_TYPE) + && test_is_null(result, false); + } + + bool cast_uint64(const char *key, uint64_t expected_value) { + std::cout << "Running " << __func__ << "(" << key << ")" << std::endl; + + dom::parser parser; + simdjson_result result = parser.parse(ALL_TYPES_JSON)[key]; + + return true + && test_type(result, dom::element_type::UINT64) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, NUMBER_OUT_OF_RANGE) + && test_cast(result, expected_value) + && test_cast(result, expected_value) + && test_cast(result, INCORRECT_TYPE) + && test_is_null(result, false); + } + + bool cast_double(const char *key, double expected_value) { + std::cout << "Running " << __func__ << "(" << key << ")" << std::endl; + + dom::parser parser; + simdjson_result result = parser.parse(ALL_TYPES_JSON)[key]; + + return true + && test_type(result, dom::element_type::DOUBLE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, expected_value) + && test_cast(result, INCORRECT_TYPE) + && test_is_null(result, false); + } + + bool cast_bool(const char *key, bool expected_value) { + std::cout << "Running " << __func__ << "(" << key << ")" << std::endl; + + dom::parser parser; + simdjson_result result = parser.parse(ALL_TYPES_JSON)[key]; + + return true + && test_type(result, dom::element_type::BOOL) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, expected_value) + && test_is_null(result, false); + } + + bool cast_null() { + std::cout << "Running " << __func__ << std::endl; dom::parser parser; simdjson_result result = parser.parse(ALL_TYPES_JSON)["null"]; - ASSERT_SUCCESS(result.error()); - - // Test simdjson_result.is() (error chain) - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_FALSE(result.is()); - ASSERT_TRUE(result.is_null()); - - // Test simdjson_result.type() (error chain) - simdjson::error_code error; - dom::element_type type; - result.type().tie(type, error); - ASSERT_SUCCESS(error); - ASSERT_EQUAL(type, expected_type); - - // Test element.is() - dom::element element = result.first; - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_FALSE(element.is()); - ASSERT_TRUE(element.is_null()); - - // Test element.type() - ASSERT_EQUAL(element.type(), expected_type); - - // Test element.get() - - return true; + return true + && test_type(result, dom::element_type::NULL_VALUE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_cast(result, INCORRECT_TYPE) + && test_is_null(result, true); } bool run() { - return test_array() && + return cast_array() && - test_object() && + cast_object() && - test_string() && + cast_string() && - test_int64("0", 0) && - test_int64("1", 1) && - test_int64("-1", -1) && - test_int64("9223372036854775807", 9223372036854775807LL) && - test_int64("-9223372036854775808", -1 - 9223372036854775807LL) && + cast_int64("0", 0) && + cast_int64("1", 1) && + cast_int64("-1", -1) && + cast_int64("9223372036854775807", 9223372036854775807LL) && + cast_int64("-9223372036854775808", -1 - 9223372036854775807LL) && - test_uint64("9223372036854775808", 9223372036854775808ULL) && - test_uint64("18446744073709551615", 18446744073709551615ULL) && + cast_uint64("9223372036854775808", 9223372036854775808ULL) && + cast_uint64("18446744073709551615", 18446744073709551615ULL) && - test_double("0.0", 0.0) && - test_double("0.1", 0.1) && - test_double("1e0", 1e0) && - test_double("1e100", 1e100) && + cast_double("0.0", 0.0) && + cast_double("0.1", 0.1) && + cast_double("1e0", 1e0) && + cast_double("1e100", 1e100) && - test_bool("true", true) && - test_bool("false", false) && + cast_bool("true", true) && + cast_bool("false", false) && - test_null() && + cast_null() && true; }