135 lines
5.1 KiB
C#
135 lines
5.1 KiB
C#
using TheLivingWorld.Core.Simulation;
|
|
|
|
namespace TheLivingWorld.Tests;
|
|
|
|
public sealed class GameTimeTests
|
|
{
|
|
[Fact]
|
|
public void Advance_at_x1_adds_five_game_minutes_per_real_second()
|
|
{
|
|
var start = GameTime.DefaultStart;
|
|
var next = GameTime.Advance(start, TimeSpan.FromSeconds(1), timeScale: 1, paused: false);
|
|
|
|
Assert.Equal(start.AddMinutes(5), next);
|
|
}
|
|
|
|
[Fact]
|
|
public void Advance_scales_with_time_scale()
|
|
{
|
|
var start = GameTime.DefaultStart;
|
|
|
|
Assert.Equal(start.AddMinutes(10), GameTime.Advance(start, TimeSpan.FromSeconds(1), 2, false));
|
|
Assert.Equal(start.AddMinutes(20), GameTime.Advance(start, TimeSpan.FromSeconds(1), 4, false));
|
|
}
|
|
|
|
[Fact]
|
|
public void Advance_does_nothing_when_paused_or_non_positive()
|
|
{
|
|
var start = GameTime.DefaultStart;
|
|
|
|
Assert.Equal(start, GameTime.Advance(start, TimeSpan.FromSeconds(10), 4, paused: true));
|
|
Assert.Equal(start, GameTime.Advance(start, TimeSpan.Zero, 1, paused: false));
|
|
Assert.Equal(start, GameTime.Advance(start, TimeSpan.FromSeconds(-1), 1, paused: false));
|
|
}
|
|
|
|
[Fact]
|
|
public void Advance_handles_a_large_wall_clock_gap()
|
|
{
|
|
var start = GameTime.DefaultStart;
|
|
// One real hour at x1 → 5 * 3600 = 18_000 game minutes = 12.5 game days.
|
|
var next = GameTime.Advance(start, TimeSpan.FromHours(1), timeScale: 1, paused: false);
|
|
|
|
Assert.Equal(start.AddMinutes(5 * 3600), next);
|
|
Assert.Equal(new DateTime(2012, 4, 24, 18, 0, 0, DateTimeKind.Unspecified), next);
|
|
}
|
|
|
|
[Fact]
|
|
public void LimitCatchUp_trims_a_step_to_the_game_time_it_is_allowed_to_bank()
|
|
{
|
|
var cap = TimeSpan.FromHours(24);
|
|
|
|
// At x1 a day of game time is 288 real seconds, so a week of downtime comes back trimmed to that.
|
|
var trimmed = GameTime.LimitCatchUp(TimeSpan.FromDays(7), timeScale: 1, cap);
|
|
Assert.Equal(288, trimmed.TotalSeconds, 1);
|
|
Assert.Equal(cap, GameTime.Advance(GameTime.DefaultStart, trimmed, 1, false) - GameTime.DefaultStart);
|
|
|
|
// Four times the speed banks the same day of game time in a quarter of the wall clock.
|
|
Assert.Equal(72, GameTime.LimitCatchUp(TimeSpan.FromDays(7), timeScale: 4, cap).TotalSeconds, 1);
|
|
}
|
|
|
|
[Fact]
|
|
public void LimitCatchUp_leaves_an_ordinary_tick_alone()
|
|
{
|
|
var tick = TimeSpan.FromMilliseconds(100);
|
|
Assert.Equal(tick, GameTime.LimitCatchUp(tick, 1, TimeSpan.FromHours(24)));
|
|
|
|
// Five seconds is what an idle world banks between lazy ticks; it must survive untouched too.
|
|
var lazy = TimeSpan.FromSeconds(5);
|
|
Assert.Equal(lazy, GameTime.LimitCatchUp(lazy, 4, TimeSpan.FromHours(24)));
|
|
}
|
|
|
|
[Fact]
|
|
public void LimitCatchUp_treats_a_non_positive_cap_as_no_cap()
|
|
{
|
|
var week = TimeSpan.FromDays(7);
|
|
Assert.Equal(week, GameTime.LimitCatchUp(week, 1, TimeSpan.Zero));
|
|
Assert.Equal(week, GameTime.LimitCatchUp(week, 1, TimeSpan.FromHours(-1)));
|
|
}
|
|
|
|
[Fact]
|
|
public void DefaultStart_is_morning_of_12_April_2012()
|
|
{
|
|
Assert.Equal(new DateTime(2012, 4, 12, 6, 0, 0, DateTimeKind.Unspecified), GameTime.DefaultStart);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(1, true)]
|
|
[InlineData(4, true)]
|
|
[InlineData(0, false)]
|
|
[InlineData(5, false)]
|
|
public void IsValidTimeScale_accepts_1_through_4(int scale, bool expected)
|
|
{
|
|
Assert.Equal(expected, GameTime.IsValidTimeScale(scale));
|
|
}
|
|
|
|
[Fact]
|
|
public void ResolveStart_defaults_and_normalises()
|
|
{
|
|
Assert.Equal(GameTime.DefaultStart, GameTime.ResolveStart(null));
|
|
Assert.Equal(
|
|
new DateTime(1999, 12, 31, 23, 45, 0, DateTimeKind.Unspecified),
|
|
GameTime.ResolveStart(new DateTime(1999, 12, 31, 23, 45, 59, DateTimeKind.Utc)));
|
|
}
|
|
|
|
[Fact]
|
|
public void ResolveStart_rejects_out_of_range_years()
|
|
{
|
|
// Year 10000 cannot even be constructed, so this has to sit just past the accepted range to
|
|
// actually exercise ResolveStart.
|
|
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
|
GameTime.ResolveStart(new DateTime(9999, 1, 1, 0, 0, 0, DateTimeKind.Unspecified)));
|
|
}
|
|
|
|
[Fact]
|
|
public void Advance_saturates_instead_of_running_off_the_end_of_the_calendar()
|
|
{
|
|
var nearTheEnd = new DateTime(9998, 12, 31, 23, 0, 0, DateTimeKind.Unspecified);
|
|
|
|
// A day of real time at x4 is roughly 16 game years - far past DateTime.MaxValue from here.
|
|
var next = GameTime.Advance(nearTheEnd, TimeSpan.FromDays(1), timeScale: 4, paused: false);
|
|
|
|
Assert.Equal(DateTime.MaxValue, next);
|
|
// Already saturated: the next tick must stay put rather than throw.
|
|
Assert.Equal(DateTime.MaxValue, GameTime.Advance(next, TimeSpan.FromSeconds(1), 1, paused: false));
|
|
}
|
|
|
|
[Fact]
|
|
public void Advance_survives_an_absurd_wall_clock_gap()
|
|
{
|
|
// The naive product realElapsed.Ticks * 1200 overflows a long well before TimeSpan.MaxValue.
|
|
var next = GameTime.Advance(GameTime.DefaultStart, TimeSpan.MaxValue, timeScale: 4, paused: false);
|
|
|
|
Assert.Equal(DateTime.MaxValue, next);
|
|
}
|
|
}
|