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
This commit is contained in:
Claude committed 2026-10-01 22:38:17 +00:00
1 parent 290a1d886c
commit 7cb98fa206
113 files changed
+12553 -23

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
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());
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
using ViaFerrea.Core.Common;
using ViaFerrea.Tests.Harness;
namespace ViaFerrea.Tests.Core;
namespace ViaFerrea.Tests.Basics;
public static class RngTests
{
+46
View File
@@ -0,0 +1,46 @@
using ViaFerrea.Core.Time;
using ViaFerrea.Tests.Harness;
namespace ViaFerrea.Tests.Basics;
public static class TimeTests
{
private static Calendar Cal() => new(TestData.Data.Balance.Time, 1850);
[Test]
public static void CalendarConstantsMatchGuide()
{
var c = Cal();
Assert.Equal(240, c.TicksPerDay);
Assert.Equal(360, c.DaysPerYear);
Assert.Approx(3.0, c.TrainSecondsPerTick, 1e-12, "un tick = 3 s di orologio dei treni");
Assert.Approx(2.5, c.TrainMinutesToDays(30), 1e-12, "30 minuti di orologio = 2,5 giorni");
}
[Test]
public static void DatesRoundTripAndFormatInItalian()
{
var c = Cal();
var d = c.DateOfDay(22 * 360 + 2 * 30 + 13);
Assert.Equal(new GameDate(1872, 3, 14), d);
Assert.Equal("14 marzo 1872", d.ToString());
Assert.Equal(22L * 360 + 2 * 30 + 13, c.DayOf(d));
}
[Test]
public static void ClockFlagsFireAtBoundaries()
{
var clock = new GameClock(Cal());
Assert.True(clock.IsNewDay && clock.IsNewMonth && clock.IsNewYear);
clock.Advance();
Assert.False(clock.IsNewDay);
clock.Restore(240L * 30);
Assert.True(clock.IsNewMonth);
Assert.False(clock.IsNewQuarter);
clock.Restore(240L * 90);
Assert.True(clock.IsNewQuarter);
clock.Restore(240L * 360);
Assert.True(clock.IsNewYear);
Assert.Equal(1851, clock.Year);
}
}