openac-mosswartmassacre/tests/OpenAC.MosswartMassacre.Tests/Backend/WireNumberTests.cs
Erik 71f14473c6 Review fixes: telemetry session id per login; Framework number text
F1: the telemetry session id is new at every login. The original made one per
load of its assembly and its loader reloaded it at every login; the backend
counts new kills per (session id, character), so reusing one id across a
relog under-counted kills until the reset counter passed the old total.

F5/F6: .NET Framework never wrote a negative zero, and its fixed-point
formats (F7, F2, F0) rounded at fifteen significant digits, half away from
zero. Spawn and portal coordinates, kills_per_hour and the !report line now
format that way; the round-trip converter maps -0 to 0.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 13:04:28 +02:00

71 lines
3 KiB
C#

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 Negative_zero_is_written_as_zero_as_the_framework_did()
{
Assert.Equal("{\"v\":0.0}", Wire.Serialize(new { v = Math.Round(-0.001, 2) }));
Assert.Equal("{\"v\":0.0}", Wire.Serialize(new { v = -0f }));
}
[Theory]
// Rounded half away from zero at fifteen significant digits, as the
// Framework did; the modern exact-binary rounding differs on these.
[InlineData(0.125, "F2", "0.13")]
[InlineData(2.675, "F2", "2.68")]
[InlineData(-0.001, "F2", "0.00")]
[InlineData(-0.0, "F7", "0.0000000")]
[InlineData(12.5, "F0", "13")]
[InlineData(33.650001525878906, "F7", "33.6500015")]
public void Fixed_formats_round_as_the_framework_did(double value, string format, string expected) =>
Assert.Equal(expected, FrameworkNumberConverter.FrameworkFixed(value, format, System.Globalization.CultureInfo.InvariantCulture));
[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 }));
}
}