feat(Data): add exact 64-bit signed/unsigned JSON numbers and checked conversion (M-DATA-02)
This commit is contained in:
1 parent
eacb8a4602
commit
692f5ba5b2
6 files changed
+530
-47
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user