feat(Data): add exact 64-bit signed/unsigned JSON numbers and checked conversion (M-DATA-02)

This commit is contained in:
faumaray committed 2026-11-24 03:25:17 +03:00
1 parent eacb8a4602
commit 692f5ba5b2
6 files changed
+530 -47

No files matched your search

+115
View File
@@ -2,6 +2,9 @@
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cmath>
#include <cstdint>
#include <limits>
#include <string>
using namespace Void::Json;
@@ -235,3 +238,115 @@ Void::Testing::Test json_copy_deep()
Value v2 = *v1;
Void::Testing::expect_eq(v2.size(), std::size_t{3});
}
// ── exact 64-bit integers & checked conversions (M-DATA-02) ───────────────────
Void::Testing::Test json_exact_64_bit_integers_and_negative_zero_round_trip()
{
constexpr std::uint64_t p53 = std::uint64_t{1} << 53;
const std::string doc =
R"({"min":-9223372036854775808,"max":9223372036854775807,)"
R"("umax":18446744073709551615,"p53_m1":)" +
std::to_string(p53 - 1ULL) + R"(,"p53":)" + std::to_string(p53) +
R"(,"p53_p1":)" + std::to_string(p53 + 1ULL) +
R"(,"neg_zero_int":-0,"neg_zero_real":-0.0})";
const auto v = parse(doc);
Void::Testing::require(v.has_value());
Void::Testing::expect(v->find("min")->number_kind() == NumberKind::signed_integer);
Void::Testing::expect_eq(v->find("min")->as_int(), std::numeric_limits<std::int64_t>::min());
Void::Testing::expect(v->find("max")->number_kind() == NumberKind::signed_integer);
Void::Testing::expect_eq(v->find("max")->as_int(), std::numeric_limits<std::int64_t>::max());
Void::Testing::expect(v->find("umax")->number_kind() == NumberKind::unsigned_integer);
Void::Testing::expect_eq(v->find("umax")->as_uint(), std::numeric_limits<std::uint64_t>::max());
Void::Testing::expect_eq(v->find("p53_p1")->as_int(), static_cast<std::int64_t>(p53 + 1ULL));
Void::Testing::expect(v->find("neg_zero_int")->is_int());
Void::Testing::expect_eq(v->find("neg_zero_int")->as_int(), std::int64_t{0});
const auto* nzr = v->find("neg_zero_real");
Void::Testing::expect(nzr->is_double());
Void::Testing::expect(std::signbit(nzr->as_double()));
const auto dumped = serialize(*v);
Void::Testing::expect(dumped.find("-9223372036854775808") != std::string::npos);
Void::Testing::expect(dumped.find("18446744073709551615") != std::string::npos);
Void::Testing::expect(dumped.find("\"neg_zero_real\":-0.0") != std::string::npos);
const auto reparsed = parse(dumped);
Void::Testing::require(reparsed.has_value());
Void::Testing::expect(*reparsed == *v);
}
Void::Testing::Test json_rejects_oversized_integers_and_bad_number_grammar()
{
Void::Testing::expect(!parse("-9223372036854775809").has_value());
Void::Testing::expect(!parse("18446744073709551616").has_value());
Void::Testing::expect(!parse("01").has_value());
Void::Testing::expect(!parse("-").has_value());
Void::Testing::expect(!parse("1.").has_value());
Void::Testing::expect(!parse("1e").has_value());
const std::string huge(257, '9');
Void::Testing::expect(!parse(huge).has_value());
}
Void::Testing::Test json_exact_equality_and_number_as_checked_conversion()
{
constexpr std::uint64_t p53_p1 = (std::uint64_t{1} << 53) + 1ULL;
const Value exact_i{p53_p1};
const Value rounded_d{static_cast<double>(std::uint64_t{1} << 53)};
Void::Testing::expect(!(exact_i == rounded_d));
Void::Testing::expect(Value{std::numeric_limits<std::uint64_t>::max()} !=
Value{static_cast<double>(std::numeric_limits<std::uint64_t>::max())});
Void::Testing::expect(Value{std::int64_t{42}} == Value{std::uint64_t{42}});
Void::Testing::expect(Value{std::int64_t{42}} == Value{42.0});
// number_as defaults to Rounding::exact.
const auto exact_fail = number_as<double>(exact_i, Void::Numeric::Rounding::exact, "p53_p1");
Void::Testing::expect(!exact_fail.has_value());
Void::Testing::expect(exact_fail.error().numeric_error == Void::Numeric::Error::Inexact);
Void::Testing::expect_eq(exact_fail.error().path, std::string{"p53_p1"});
const auto lossy_ok = number_as<double>(exact_i, Void::Numeric::Rounding::nearest_even);
Void::Testing::expect(lossy_ok.has_value());
const auto umax_to_i64 = number_as<std::int64_t>(Value{std::numeric_limits<std::uint64_t>::max()});
Void::Testing::expect(!umax_to_i64.has_value());
Void::Testing::expect(umax_to_i64.error().numeric_error == Void::Numeric::Error::Overflow);
const auto neg_to_u64 = number_as<std::uint64_t>(Value{std::int64_t{-1}});
Void::Testing::expect(!neg_to_u64.has_value());
Void::Testing::expect(neg_to_u64.error().numeric_error == Void::Numeric::Error::Underflow);
// try_serialize rejects non-finite floats with path diagnostics.
Value::Object_t obj;
obj.emplace_back("ok", Value{1});
obj.emplace_back("bad", Value{std::numeric_limits<double>::quiet_NaN()});
const auto ser_err = try_serialize(Value{std::move(obj)});
Void::Testing::expect(!ser_err.has_value());
Void::Testing::expect(ser_err.error().numeric_error == Void::Numeric::Error::NonFinite);
Void::Testing::expect_eq(ser_err.error().path, std::string{"bad"});
}
Void::Testing::Test json_sax_typed_integer_callbacks_preserve_64_bit_endpoints()
{
std::int64_t seen_i{0};
std::uint64_t seen_u{0};
double seen_d{0.0};
SaxHandler h;
h.on_int = [&](std::int64_t v) { seen_i = v; };
h.on_uint = [&](std::uint64_t v) { seen_u = v; };
h.on_number = [&](double v) { seen_d = v; };
const auto err = parse_sax(
R"([-9223372036854775808,18446744073709551615,2.5])", h);
Void::Testing::expect(!err.has_value());
Void::Testing::expect_eq(seen_i, std::numeric_limits<std::int64_t>::min());
Void::Testing::expect_eq(seen_u, std::numeric_limits<std::uint64_t>::max());
Void::Testing::expect_eq(seen_d, 2.5);
}
+49
View File
@@ -2,6 +2,8 @@
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cstdint>
#include <limits>
#include <map>
#include <optional>
#include <string>
@@ -242,4 +244,51 @@ Void::Testing::Test reflect_member_customization_wins()
Void::Testing::expect_eq(back->doubled, 10);
}
namespace
{
struct WideNumbers
{
std::int64_t smin = 0;
std::int64_t smax = 0;
std::uint64_t umax = 0;
std::uint64_t p53_p1 = 0;
float ratio = 0.0F;
double dval = 0.0;
};
} // namespace
Void::Testing::Test reflect_exact_64_bit_integers_and_rejects_inexact()
{
constexpr std::uint64_t p53_p1 = (std::uint64_t{1} << 53) + 1ULL;
const WideNumbers orig{
.smin = std::numeric_limits<std::int64_t>::min(),
.smax = std::numeric_limits<std::int64_t>::max(),
.umax = std::numeric_limits<std::uint64_t>::max(),
.p53_p1 = p53_p1,
.ratio = 0.1F,
.dval = 1.25};
const auto json_str = Void::Json::serialize(Void::Json::to_json(orig));
const auto parsed = Void::Json::parse(json_str);
Void::Testing::require(parsed.has_value());
const auto round = Void::Json::from_json<WideNumbers>(*parsed);
Void::Testing::require(round.has_value());
Void::Testing::expect_eq(round->smin, orig.smin);
Void::Testing::expect_eq(round->smax, orig.smax);
Void::Testing::expect_eq(round->umax, orig.umax);
Void::Testing::expect_eq(round->p53_p1, orig.p53_p1);
Void::Testing::expect_eq(round->dval, 1.25);
// Reject fractional value into integer field & inexact 2^53+1 into double field.
const auto frac_err = Void::Json::parse(R"({"smin": 1.5})");
Void::Testing::require(frac_err.has_value());
Void::Testing::expect(!Void::Json::from_json<WideNumbers>(*frac_err).has_value());
const auto inexact_dbl = Void::Json::parse(
std::string{R"({"dval": )"} + std::to_string(p53_p1) + "}");
Void::Testing::require(inexact_dbl.has_value());
Void::Testing::expect(!Void::Json::from_json<WideNumbers>(*inexact_dbl).has_value());
}
#endif // VOID_HAS_JSON_REFLECTION