ludic/packages/ludic.clock/tests/clock_test.ludic
Orkuncakilkaya 05b09b4c98 feat(ludic.clock): the hours, the day, the moon and a calendar as a mechanic package
A ClockWorld port (owns_world, rest_scale, seed), verbs to set and advance it,
clock_new_day, a DayTurned queue (stayed up past midnight, or slept), a
calendar whose month, year and season are pure functions of the day, and a
System (PH_INPUT) with its own save section. Nine test blocks in
tests/clock_test.ludic with fakes for the port.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 04:20:47 +03:00

129 lines
3.5 KiB
Text

# clock_test.ludic - the hours run at the rate the world's rest allows, only on the machine that
# owns it; a day turns past midnight or by a verb, as a fact; the calendar is a function of the day
import "ludic.clock"
import "ludic.base"
program ClockTest {
numbers float
var owns: bool = true
var rest: float = 1.0
function fake_owns() -> bool { return owns }
function fake_rest() -> float { return rest }
function fake_seed() -> int { return 42 }
function fresh() -> void {
owns = true
rest = 1.0
let w = new ClockWorld
w.owns_world = fn fake_owns
w.rest_scale = fn fake_rest
w.seed = fn fake_seed
clock_bind(w)
clock_config(8.0, 20.0, 6.0)
clock_config_moon(29.5, 0.2, 0.8)
clock_set_scale(60.0)
let seasons = new []int
for m in 0 .. 12 { push(seasons, m / 3) }
clock_calendar(10, seasons)
clock_reset()
}
function turned() -> []DayTurned { return q_drain(clock_days()) }
function waiting() -> int { return q_len(clock_days()) }
function run_hours(h: float) -> void {
let s = clock_system()
s.tick(tick_new(0.016, 0, h))
}
test "a trip starts on day one at the start hour, the moon inside its range" {
fresh()
expect_eq(clock_day(), 1)
expect(clock_hours() == 8.0)
expect(clock_moon() >= 0.2 and clock_moon() < 0.8)
expect(clock_hhmm() == "08:00")
}
test "the same seed gives the same moon" {
fresh()
let a = clock_moon()
clock_reset()
expect(clock_moon() == a)
}
test "a real minute is a game hour at scale 60, and resting speeds it" {
fresh()
expect(clock_hours_in(60.0) == 1.0)
rest = 4.0
expect(clock_rate() == 240.0)
}
test "past midnight the day turns, once, as a fact" {
fresh()
run_hours(15.0)
expect_eq(waiting(), 0)
run_hours(2.5)
expect_eq(clock_day(), 2)
expect(clock_hours() == 1.5)
let t = turned()
expect_eq(len(t), 1)
expect_eq(t[0].day, 2)
expect_eq(t[0].how, DAY_STAYED_UP)
}
test "a machine that does not own the world does not move it" {
fresh()
owns = false
run_hours(20.0)
expect(clock_hours() == 8.0)
expect_eq(clock_day(), 1)
}
test "a night slept turns the day and moves the moon a month's share" {
fresh()
let m0 = clock_moon()
clock_set_hours(6.5)
clock_new_day()
expect_eq(clock_day(), 2)
expect(clock_moon() > m0)
expect_eq(turned()[0].how, DAY_SLEPT)
}
test "night is outside the day's hours, and the hour wraps" {
fresh()
clock_set_hours(21.0)
expect(clock_is_night())
clock_set_hours(12.0)
expect(not clock_is_night())
clock_set_hours(25.0)
expect(clock_hours() == 1.0)
}
test "the calendar is a function of the day" {
fresh()
expect_eq(clock_season_of(1), 0)
expect_eq(clock_month_of(31), 3)
expect_eq(clock_season_of(31), 1)
expect_eq(clock_day_of_month_of(35), 5)
expect_eq(clock_year_of(121), 2)
expect_eq(clock_first_day_of(2), 61)
clock_set_day(28)
expect_eq(clock_days_to_change(), 3)
expect_eq(clock_season(), 0)
}
test "a save section reads back the same clock" {
fresh()
clock_set_day(17)
clock_set_hours(13.25)
clock_set_moon(0.375)
let root = save_tree()
save_section(root, "clock", 1, clock_save())
let text = save_encode(root)
clock_reset()
let n = load_section(save_decode(text), "clock")
clock_load(n.data, n.version)
expect_eq(clock_day(), 17)
expect(clock_hours() == 13.25)
expect(clock_moon() == 0.375)
}
}