From c484829272cd13a738e35412498e12f2c9a194ac Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 07:48:59 +0200 Subject: Adding upstream version 0.19.2. Signed-off-by: Daniel Baumann --- src/parser/parser_test_numeric.cpp | 105 +++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/parser/parser_test_numeric.cpp (limited to 'src/parser/parser_test_numeric.cpp') diff --git a/src/parser/parser_test_numeric.cpp b/src/parser/parser_test_numeric.cpp new file mode 100644 index 0000000..b16ed9a --- /dev/null +++ b/src/parser/parser_test_numeric.cpp @@ -0,0 +1,105 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "test_global.hpp" +#include "numeric_parser.hpp" + +#include +#include +#include +#include + +using namespace orcus; +using std::cout; +using std::endl; + +namespace { + +struct check +{ + std::string_view str; + double expected; +}; + +const double invalid = std::numeric_limits::quiet_NaN(); + +template +bool run_checks(const std::vector& checks) +{ + for (const check& c : checks) + { + ParserT parser(c.str.data(), c.str.data() + c.str.size()); + double v = parser.parse(); + + if (std::isnan(c.expected)) + { + if (!std::isnan(v)) + { + cout << "'" << c.str << "' was expected to be invalid, but parser parsed as if it was valid." << endl; + return false; + } + } + else + { + if (v != c.expected) + { + cout << "'" << c.str << "' was expected to be parsed as (" << c.expected << "), but the parser parsed it as (" << v << ")" << endl; + return false; + } + } + } + + return true; +} + +} + +void test_generic_number_parsing() +{ + using parser_type = detail::numeric_parser; + + std::vector checks = { + { "-6.e3", -6e3 }, + { "true", invalid }, + { "1", 1.0 }, + { "1.0", 1.0 }, + { "-1.0", -1.0 }, + { "-01", -1.0 }, + { "2e2", 200.0 }, + { "1.2", 1.2 }, + { "-0.0001", -0.0001 }, + { "-0.0", 0.0 }, + { "+.", invalid }, + { "+e", invalid }, + { "+e1", invalid }, + { "+ ", invalid }, + { "- ", invalid } + }; + + assert(run_checks(checks)); +} + +void test_json_number_parsing() +{ + using parser_type = detail::numeric_parser; + + std::vector checks = { + { "-01", invalid }, // Leading zeros are invalid. + }; + + assert(run_checks(checks)); +} + +int main() +{ + test_generic_number_parsing(); + test_json_number_parsing(); + + return EXIT_SUCCESS; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3