diff --git a/tests/basictests.cpp b/tests/basictests.cpp index 02d66b54..2dd4db05 100644 --- a/tests/basictests.cpp +++ b/tests/basictests.cpp @@ -1338,222 +1338,73 @@ namespace type_tests { } )"_padded; - // 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); - }; - - 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; - } - - 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; - } - } - - template<> - template - bool test_implicit_cast::error_with(A, simdjson::error_code) { - return true; - } - template bool test_cast(simdjson_result result, T expected) { + cast_tester tester; std::cout << " test_cast<" << typeid(T).name() << "> expecting " << expected << std::endl; // Grab the element out and check success dom::element element = result.first; - // 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); - + RUN_TEST( tester.test_get(element, expected ) ); + RUN_TEST( tester.test_get(result, expected) ); + // RUN_TEST( tester.test_named_get(element, expected) ); + // RUN_TEST( tester.test_named_get(result, expected) ); + RUN_TEST( tester.test_is(element, true) ); + RUN_TEST( tester.test_is(result, true) ); + // RUN_TEST( tester.test_named_is(element, true) ); + // RUN_TEST( tester.test_named_is(result, 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; - } - + RUN_TEST( tester.test_implicit_cast(element, expected) ); + RUN_TEST( tester.test_implicit_cast(result, expected) ); #endif return true; } - template bool test_cast(simdjson_result result) { - std::cout << " test_cast<" << typeid(T).name() << "> expecting success" << std::endl; + cast_tester tester; + std::cout << " test_cast<" << typeid(T).name() << ">" << std::endl; // Grab the element out and check success dom::element element = result.first; - // get() == expected - T actual; - simdjson::error_code error; - result.get().tie(actual, error); - ASSERT_SUCCESS(error); - - element.get().tie(actual, error); - ASSERT_SUCCESS(error); - - // 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); - + RUN_TEST( tester.test_get(element) ); + RUN_TEST( tester.test_get(result) ); + // RUN_TEST( tester.test_named_get(element) ); + // RUN_TEST( tester.test_named_get(result) ); + RUN_TEST( tester.test_is(element, true) ); + RUN_TEST( tester.test_is(result, true) ); + // RUN_TEST( tester.test_named_is(element, true) ); + // RUN_TEST( tester.test_named_is(result, 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; - } - + RUN_TEST( tester.test_implicit_cast(element) ); + RUN_TEST( tester.test_implicit_cast(result) ); #endif return true; } + // + // Test that we get errors when we cast to the wrong type + // 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; + bool test_cast_error(simdjson_result result, simdjson::error_code expected_error) { + std::cout << " test_cast_error<" << typeid(T).name() << "> expecting error '" << expected_error << "'" << std::endl; dom::element element = result.first; - // get() == expected - T actual; - simdjson::error_code 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(actual_is, false); - - actual_is = element.is(); - ASSERT_EQUAL(actual_is, false); + cast_tester tester; + RUN_TEST( tester.test_get_error(element, expected_error) ); + RUN_TEST( tester.test_get_error(result, expected_error) ); + // RUN_TEST( tester.test_named_get_error(element, expected_error) ); + // RUN_TEST( tester.test_named_get_error(result, expected_error) ); + RUN_TEST( tester.test_is(element, false) ); + RUN_TEST( tester.test_is(result, false) ); + // RUN_TEST( tester.test_named_is(element, false) ); + // RUN_TEST( tester.test_named_is(result, false) ); #if SIMDJSON_EXCEPTIONS - - // 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; - } - + RUN_TEST( tester.test_implicit_cast_error(element, expected_error) ); + RUN_TEST( tester.test_implicit_cast_error(result, expected_error) ); #endif return true; @@ -1628,13 +1479,13 @@ namespace type_tests { 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_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) && test_is_null(result, false); } @@ -1646,14 +1497,14 @@ namespace type_tests { return true && test_type(result, dom::element_type::OBJECT) - && test_cast(result, INCORRECT_TYPE) + && test_cast_error(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_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) && test_is_null(result, false); } @@ -1665,14 +1516,14 @@ namespace type_tests { return true && test_type(result, dom::element_type::STRING) - && test_cast(result, INCORRECT_TYPE) - && test_cast(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(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_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) && test_is_null(result, false); } @@ -1683,16 +1534,16 @@ namespace type_tests { 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_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(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_error(result, NUMBER_OUT_OF_RANGE)) && test_cast(result, static_cast(expected_value)) - && test_cast(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) && test_is_null(result, false); } @@ -1704,14 +1555,14 @@ namespace type_tests { 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_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, NUMBER_OUT_OF_RANGE) && test_cast(result, expected_value) && test_cast(result, static_cast(expected_value)) - && test_cast(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) && test_is_null(result, false); } @@ -1722,14 +1573,14 @@ namespace type_tests { 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_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) && test_cast(result, expected_value) - && test_cast(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) && test_is_null(result, false); } @@ -1741,13 +1592,13 @@ namespace type_tests { 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_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) && test_cast(result, expected_value) && test_is_null(result, false); } @@ -1759,14 +1610,14 @@ namespace type_tests { simdjson_result result = parser.parse(ALL_TYPES_JSON)["null"]; 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_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) + && test_cast_error(result, INCORRECT_TYPE) && test_is_null(result, true); } diff --git a/tests/cast_tester.h b/tests/cast_tester.h index fae7263e..dd1d89c1 100644 --- a/tests/cast_tester.h +++ b/tests/cast_tester.h @@ -21,14 +21,14 @@ namespace { template class cast_tester { public: - bool test_get(element element, T expected); - bool test_get(simdjson_result element, T expected); + bool test_get(element element, T expected = {}); + bool test_get(simdjson_result element, T expected = {}); bool test_get_error(element element, error_code expected_error); bool test_get_error(simdjson_result element, error_code expected_error); #if SIMDJSON_EXCEPTIONS - bool test_implicit_cast(element element, T expected); - bool test_implicit_cast(simdjson_result element, T expected); + bool test_implicit_cast(element element, T expected = {}); + bool test_implicit_cast(simdjson_result element, T expected = {}); bool test_implicit_cast_error(element element, error_code expected_error); bool test_implicit_cast_error(simdjson_result element, error_code expected_error); #endif // SIMDJSON_EXCEPTIONS @@ -37,8 +37,8 @@ public: bool test_is(simdjson_result element, bool expected); bool test_is_error(simdjson_result element, error_code expected_error); - // bool test_named_get(element element, T expected); - // bool test_named_get(simdjson_result element, T expected); + // bool test_named_get(element element, T expected = {}); + // bool test_named_get(simdjson_result element, T expected = {}); // bool test_named_get_error(element element, error_code expected_error); // bool test_named_get_error(simdjson_result element, error_code expected_error); diff --git a/tests/test_macros.h b/tests/test_macros.h index d5a7a620..909deb10 100644 --- a/tests/test_macros.h +++ b/tests/test_macros.h @@ -28,6 +28,7 @@ bool equals_expected(const char *actual, const char *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 RUN_TEST(RESULT) if (!RESULT) { return false; } #define ASSERT_SUCCESS(ERROR) if (ERROR) { std::cerr << (ERROR) << std::endl; return false; } #endif // TEST_MACROS_H \ No newline at end of file