Crafter.Debug/tests/ShouldRoundTripAggregates/main.cpp
Jorijn van der Graaf 6987033d6b
fix(core): encode a float as its shortest decimal
A float was widened to double before encoding, so every float watch,
argument default and result read as the double nearest it: gravity
-9.81 showed as -9.8100004196167, a radius of 0.1 as 0.10000000149011612,
in `get`, `help` and call results alike. Agents read these, and copy
them back into `set` and `call`.

Codec<float> now encodes the shortest decimal that reads back as the
same float (std::to_chars), as a double. Decoding is unchanged, and so
is every value: 0.1 decodes to the float 0.1f came from. A double is
encoded as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 03:21:41 +01:00

103 lines
4.4 KiB
C++

// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Codec round-trips an aggregate built from every supported field type
// (strings, nested aggregates in a vector, optional, std::array, string-keyed
// maps, a scoped enum, Required) through Value and its text, with no
// hand-written serialization. Fields the input leaves out keep their default
// member initializers, null clears an optional, and an enum also accepts its
// integer. A float encodes as its shortest decimal, 0.1 rather than the
// 0.10000000149011612 it widens to, which every float watch used to show.
// Guards the reflection that command arguments and results rely on.
import std;
import Crafter.Debug.Core;
using namespace Crafter::Debug;
namespace Game {
enum class Mode : std::uint8_t { Idle, Build, Fight = 5 };
struct Vec3 {
float x = 0;
float y = 0;
float z = 0;
};
struct Loadout {
std::string name = "default";
std::vector<Vec3> points;
std::optional<std::int32_t> target;
std::array<bool, 3> flags{};
std::map<std::string, double> weights;
Mode mode = Mode::Idle;
Required<std::uint16_t> slot;
};
}
namespace {
std::int32_t failures = 0;
void Check(bool ok, std::string_view what) {
std::println("{} {}", ok ? "PASS" : "FAIL", what);
if (!ok) ++failures;
}
}
int main() {
Game::Loadout in;
in.name = "fort \"one\"";
in.points = { { 1, 2, 3 }, { -0.5F, 0, 1e10F } };
in.target = -7;
in.flags = { true, false, true };
in.weights = { { "cannon", 0.25 }, { "laser", 1 } };
in.mode = Game::Mode::Fight;
*in.slot = 3;
const Value encoded = Encode(in);
Check(encoded.IsObject() && encoded.Size() == 7, "an aggregate encodes as an object with one member per field");
Check(encoded.Find("mode")->AsString() == "Fight", "a scoped enum encodes by name");
Check(encoded.Find("slot")->AsInteger() == 3, "Required encodes as its value");
Check(encoded.Find("target")->AsInteger() == -7, "an optional with a value encodes as the value");
Check(encoded.Find("points")->Items()[1].Find("z")->AsNumber() == 1e10F, "nested aggregates encode inside a vector");
Game::Loadout out;
DecodeContext context;
Decode(Value::Parse(encoded.Dump()), out, context);
Check(context.Ok(), std::format("decodes what it encoded ({})", context.Report()));
Check(out.name == in.name && out.points.size() == 2 && out.points[1].z == 1e10F && out.target == -7 && out.flags[2]
&& out.weights.at("laser") == 1 && out.mode == Game::Mode::Fight && *out.slot == 3,
"every field survives the round trip");
Game::Loadout partial;
partial.target = 9;
DecodeContext partialContext;
Decode(Value::Parse(R"({"slot": 1, "target": null, "mode": 1})"), partial, partialContext);
Check(partialContext.Ok(), std::format("a partial object decodes ({})", partialContext.Report()));
Check(partial.name == "default" && partial.points.empty(), "fields left out keep their defaults");
Check(!partial.target.has_value(), "null clears an optional");
Check(partial.mode == Game::Mode::Build, "an enum accepts its integer");
Check(Encode(0.1F).Dump() == "0.1" && Encode(-9.81F).Dump() == "-9.81" && Encode(4.0F).Dump() == "4.0",
std::format("a float reads as its shortest decimal ({}, {})", Encode(0.1F).Dump(), Encode(-9.81F).Dump()));
float tenth = 0;
DecodeContext tenthContext;
Decode(Value::Parse(Encode(0.1F).Dump()), tenth, tenthContext);
Check(tenthContext.Ok() && tenth == 0.1F && Encode(0.1).Dump() == "0.1", "and decodes to the same float; a double is unchanged");
float nan = 0;
DecodeContext nanContext;
Decode(Encode(std::numeric_limits<float>::quiet_NaN()), nan, nanContext);
Check(nanContext.Ok() && std::isnan(nan), "NaN survives the round trip");
std::vector<bool> bits;
DecodeContext bitsContext;
Decode(Value::Parse("[true,false,true]"), bits, bitsContext);
Check(bitsContext.Ok() && bits.size() == 3 && bits[2] && !bits[1], "std::vector<bool> decodes");
const std::uint64_t large = (std::uint64_t{ 1 } << 63) + 2048;
std::uint64_t decoded = 0;
DecodeContext largeContext;
Decode(Value::Parse(Encode(large).Dump()), decoded, largeContext);
Check(largeContext.Ok() && decoded == large, "a uint64 above the int64 range survives");
return failures == 0 ? 0 : 1;
}