Wire: write doubles and floats with .NET Framework's round-trip digits

The original ran Newtonsoft.Json on .NET Framework, whose "R" format writes
15 significant digits when they read back exactly and 17 otherwise (floats: 7,
else 9). Modern .NET writes the shortest round trip instead, so a value like
-0.066666670143604279 came out as -0.06666667014360428: equal when parsed,
different bytes. Every frame now goes through a converter that repeats the
Framework rule, with Newtonsoft's '.0' suffix and non-finite handling kept.
Goldens that encoded the modern digits are corrected.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-09-25 12:44:34 +02:00
parent 82a518dcfb
commit 5a94e1cf0b
7 changed files with 148 additions and 7 deletions

View file

@ -0,0 +1,52 @@
using OpenAC.MosswartMassacre.Backend;
namespace OpenAC.MosswartMassacre.Tests.Backend;
/// <summary>
/// Numbers written the way the original plugin (Newtonsoft.Json on .NET
/// Framework) wrote them.
/// </summary>
public sealed class WireNumberTests
{
[Theory]
// 15 digits read back: the short form.
[InlineData(0.1, "0.1")]
[InlineData(75.0, "75.0")]
[InlineData(42.5, "42.5")]
[InlineData(-12.0, "-12.0")]
[InlineData(1e-05, "1E-05")]
// 15 digits do not read back: seventeen, not the shortest round trip.
[InlineData(-0.066666670143604279, "-0.066666670143604279")]
[InlineData((double)33.65f, "33.650001525878906")]
[InlineData(0.30000000000000004, "0.30000000000000004")]
public void Doubles_use_the_frameworks_round_trip_digits(double value, string expected)
{
Assert.Equal(expected, Wire.Serialize(value));
Assert.Equal("{\"v\":" + expected + "}", Wire.Serialize(new { v = value }));
Assert.Equal("{\"v\":" + expected + "}", Wire.Serialize(new { v = (double?)value }));
Assert.Equal("{\"1\":" + expected + "}", Wire.Serialize(new Dictionary<int, double> { [1] = value }));
}
[Fact]
public void The_shortest_form_and_the_framework_form_can_differ()
{
// The case the converter exists for.
Assert.Equal("-0.06666667014360428", (-0.066666670143604279).ToString("R", System.Globalization.CultureInfo.InvariantCulture));
Assert.Equal("-0.066666670143604279", Wire.Serialize(-0.066666670143604279));
}
[Theory]
[InlineData(2.7115397f, "2.71153975")]
[InlineData(0.1f, "0.1")]
[InlineData(3f, "3.0")]
[InlineData(1.0000001f, "1.00000012")]
public void Floats_use_the_frameworks_round_trip_digits(float value, string expected) =>
Assert.Equal("{\"v\":" + expected + "}", Wire.Serialize(new { v = value }));
[Fact]
public void Nulls_and_non_finite_values_keep_newtonsofts_handling()
{
Assert.Equal("{\"v\":null}", Wire.Serialize(new { v = (double?)null }));
Assert.Equal("{\"v\":\"NaN\"}", Wire.Serialize(new { v = double.NaN }));
}
}