Files
Rail_Enterprise/tests/ViaFerrea.Tests/Core/CommonTests.cs
T
Claude 7cb98fa206 F1: economia headless, dati validati, generatore di mappa e salvataggi
- Base deterministica: Money, id tipizzati, DMath, XxHash64, calendario
- 12 file dati con schemi, validatore di schema interno, validazione semantica
  e hash dei dati; viaferrea-tools validate-data
- Generatore di mappa (rilievo, mare, priority-flood, fiumi, laghi, copertura,
  giacimenti, città, hub, industrie) e viaferrea-tools genmap
- Economia: città, industrie, hub, mercato con componente mondiale, prezzi
  locali, tariffe, lavoro in sede e a piedi, trasporto locale, registro a
  partita doppia, prestiti, rating, difficoltà
- Salvataggi Brotli con rimappatura per id e hash di stato giornaliero
- viaferrea-tools simulate con KPI CSV; 45 test verdi; ADR 0002-0006

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
2026-10-01 22:38:17 +00:00

99 lines
3.9 KiB
C#

using ViaFerrea.Core.Common;
using ViaFerrea.Tests.Harness;
namespace ViaFerrea.Tests.Basics;
public static class CommonTests
{
[Test]
public static void MoneyFormatsItalian()
{
Assert.Equal("₤ 1.284.500", Money.FromLire(1_284_500).ToString());
Assert.Equal("₤ 1.284.500,25", Money.FromLire(1_284_500.25m).ToString("it2"));
Assert.Equal("₤ 3,91 mln", Money.FromLire(3_910_000).ToString("short"));
Assert.Equal("₤ 48.200", Money.FromLire(48_200).ToString("short"));
Assert.Equal("−₤ 19.700", Money.FromLire(-19_700).ToString());
Assert.Equal("₤ 0", Money.Zero.ToString());
Assert.Equal("₤ 999", Money.FromLire(999).ToString());
}
[Test]
public static void MoneyArithmeticIsExact()
{
var a = Money.FromLire(10.10m);
var b = Money.FromLire(0.20m);
Assert.Equal(1030L, (a + b).Cents);
Assert.Equal(990L, (a - b).Cents);
Assert.Equal(3030L, (a * 3).Cents);
Assert.Equal(1515L, (a * 1.5).Cents);
Assert.True(a > b);
Assert.Throws<OverflowException>(() => _ = new Money(long.MaxValue) + new Money(1));
}
[Test]
public static void XxHash64MatchesReferenceVectors()
{
Assert.Equal(0xef46db3751d8e999UL, XxHash64.Hash([]));
Assert.Equal(0xd24ec4f1a98c6e5bUL, XxHash64.Hash("a"u8));
Assert.Equal(0x44bc2cf5ad770999UL, XxHash64.Hash("abc"u8));
Assert.Equal(0x7e13e2793f7d6198UL, XxHash64.Hash("Via Ferrea"u8));
Assert.Equal(0x95584af7701f808dUL, XxHash64.Hash([], 12345));
var hundred = new byte[100];
for (int i = 0; i < hundred.Length; i++) hundred[i] = (byte)i;
Assert.Equal(0x6ac1e58032166597UL, XxHash64.Hash(hundred));
Assert.Equal(0x28ba1ae2de4de27UL, XxHash64.Hash(hundred, 12345));
}
[Test(Description = "DMath coincide con Math entro 1e-14 relativi su argomenti casuali")]
public static void DMathMatchesMathClosely()
{
var rng = new Rng(2024);
for (int i = 0; i < 20_000; i++)
{
double x = rng.NextDouble(-50, 50);
Check(Math.Exp(x), DMath.Exp(x), "exp", x);
double p = rng.NextDouble(1e-6, 1e6);
Check(Math.Log(p), DMath.Log(p), "log", p);
double a = rng.NextDouble(-1000, 1000);
CheckAbs(Math.Sin(a), DMath.Sin(a), "sin", a);
CheckAbs(Math.Cos(a), DMath.Cos(a), "cos", a);
double t = rng.NextDouble(-100, 100);
Check(Math.Atan(t), DMath.Atan(t), "atan", t);
double y = rng.NextDouble(-5, 5), xx = rng.NextDouble(-5, 5);
CheckAbs(Math.Atan2(y, xx), DMath.Atan2(y, xx), "atan2", y);
double b = rng.NextDouble(0.01, 300), e = rng.NextDouble(-3, 3);
Check(Math.Pow(b, e), DMath.Pow(b, e), "pow", b);
}
Assert.Equal(1.0, DMath.Exp(0));
Assert.Equal(0.0, DMath.Log(1));
Assert.Equal(8.0, DMath.Pow(2, 3));
Assert.Equal(0.125, DMath.Pow(2, -3));
Assert.True(double.IsNaN(DMath.Log(-1)));
Assert.Equal(double.PositiveInfinity, DMath.Exp(1000));
Assert.Equal(0.0, DMath.Exp(-1000));
Assert.Approx(Math.Exp(-740), DMath.Exp(-740), 1e-320);
Assert.Approx(Math.Log(5e-310), DMath.Log(5e-310), 1e-12);
}
private static void Check(double expected, double actual, string fn, double arg)
{
double tol = Math.Max(1e-300, Math.Abs(expected) * 2e-14);
Assert.Approx(expected, actual, tol, $"{fn}({arg:R})");
}
private static void CheckAbs(double expected, double actual, string fn, double arg)
{
Assert.Approx(expected, actual, 1e-13, $"{fn}({arg:R})");
}
[Test]
public static void TypedIdsAreDistinctAndOrdered()
{
var a = new StationId(3);
var b = new StationId(5);
Assert.True(a.CompareTo(b) < 0);
Assert.False(StationId.None.IsValid);
Assert.Equal("Station#3", a.ToString());
}
}