lib misc+ww tests: hand-main plumbing -> assert (@test conversion B7)

toktest documents the per-row signalled pinpoint loss.
This commit is contained in:
2026-06-10 18:52:53 +09:00
parent eb891f4007
commit 3e2bf0612e
8 changed files with 797 additions and 858 deletions

View File

@@ -1,16 +1,12 @@
// timetest — exercises lib/time. Run with `out/bin/ww run
// lib/time/timetest.ww`. Same `signalled`-then-fail()-with-+10
// pattern as fmttest / logtest / stattest so a non-zero exit
// pinpoints the offending scenario.
// lib/time/timetest.ww`. A failing row aborts via the assert/abort
// builtin (task #5 @test conversion).
package time_test;
import os;
import time;
let signalled: i32 = 0;
fn fail() void = { os.exit(signalled + 10); };
// ---- constants: load-bearing names + values ----------------------------
//
@@ -19,16 +15,16 @@ fn fail() void = { os.exit(signalled + 10); };
// ref/hare/time/duration.ha:9-18.
@test fn constants() void = {
if ((time.nanosecond: i64) != 1i64) { fail(); };
if ((time.microsecond: i64) != 1000i64) { fail(); };
if ((time.millisecond: i64) != 1000000i64) { fail(); };
if ((time.second: i64) != 1000000000i64) { fail(); };
assert(!((time.nanosecond: i64) != 1i64));
assert(!((time.microsecond: i64) != 1000i64));
assert(!((time.millisecond: i64) != 1000000i64));
assert(!((time.second: i64) != 1000000000i64));
// "Roundtrip" check: derived multiples land on the canonical
// nanosecond count. Catches a future re-derivation drift.
if (5i64 * (time.second: i64) != 5000000000i64) { fail(); };
if (3i64 * (time.millisecond: i64) != 3000000i64) { fail(); };
if (7i64 * (time.microsecond: i64) != 7000i64) { fail(); };
assert(!(5i64 * (time.second: i64) != 5000000000i64));
assert(!(3i64 * (time.millisecond: i64) != 3000000i64));
assert(!(7i64 * (time.microsecond: i64) != 7000i64));
};
// ---- now(monotonic) is monotonic across two calls ----------------------
@@ -38,7 +34,7 @@ fn fail() void = { os.exit(signalled + 10); };
let b = time.now(time.clock.monotonic);
// Two back-to-back calls — b can equal a (same ns tick) but
// must never precede it.
if (time.compare(a, b) > 0i8) { fail(); };
assert(!(time.compare(a, b) > 0i8));
};
// ---- now(realtime) returns a post-2020 epoch ---------------------------
@@ -51,9 +47,9 @@ fn fail() void = { os.exit(signalled + 10); };
@test fn nowrealtime() void = {
let t = time.now(time.clock.realtime);
if (t.sec < 1577836800i64) { fail(); };
if (t.nsec < 0i64) { fail(); };
if (t.nsec >= 1000000000i64) { fail(); };
assert(!(t.sec < 1577836800i64));
assert(!(t.nsec < 0i64));
assert(!(t.nsec >= 1000000000i64));
};
// ---- add: zero duration is identity ------------------------------------
@@ -62,8 +58,8 @@ fn fail() void = { os.exit(signalled + 10); };
let a: time.instant;
a.sec = 100i64; a.nsec = 500i64;
let r = time.add(a, 0i64);
if (r.sec != 100i64) { fail(); };
if (r.nsec != 500i64) { fail(); };
assert(!(r.sec != 100i64));
assert(!(r.nsec != 500i64));
};
// ---- add: positive duration, no carry ----------------------------------
@@ -72,8 +68,8 @@ fn fail() void = { os.exit(signalled + 10); };
let a: time.instant;
a.sec = 10i64; a.nsec = 100i64;
let r = time.add(a, 200i64);
if (r.sec != 10i64) { fail(); };
if (r.nsec != 300i64) { fail(); };
assert(!(r.sec != 10i64));
assert(!(r.nsec != 300i64));
};
// ---- add: positive duration, carries into next second ------------------
@@ -82,8 +78,8 @@ fn fail() void = { os.exit(signalled + 10); };
let a: time.instant;
a.sec = 10i64; a.nsec = 999999900i64;
let r = time.add(a, 200i64);
if (r.sec != 11i64) { fail(); };
if (r.nsec != 100i64) { fail(); };
assert(!(r.sec != 11i64));
assert(!(r.nsec != 100i64));
};
// ---- add: full-second duration -----------------------------------------
@@ -92,8 +88,8 @@ fn fail() void = { os.exit(signalled + 10); };
let a: time.instant;
a.sec = 5i64; a.nsec = 0i64;
let r = time.add(a, (time.second: i64) * 3i64);
if (r.sec != 8i64) { fail(); };
if (r.nsec != 0i64) { fail(); };
assert(!(r.sec != 8i64));
assert(!(r.nsec != 0i64));
};
// ---- add: negative duration, no borrow ---------------------------------
@@ -102,8 +98,8 @@ fn fail() void = { os.exit(signalled + 10); };
let a: time.instant;
a.sec = 10i64; a.nsec = 500i64;
let r = time.add(a, -100i64);
if (r.sec != 10i64) { fail(); };
if (r.nsec != 400i64) { fail(); };
assert(!(r.sec != 10i64));
assert(!(r.nsec != 400i64));
};
// ---- add: negative duration, borrows from previous second --------------
@@ -112,8 +108,8 @@ fn fail() void = { os.exit(signalled + 10); };
let a: time.instant;
a.sec = 10i64; a.nsec = 100i64;
let r = time.add(a, -200i64);
if (r.sec != 9i64) { fail(); };
if (r.nsec != 999999900i64) { fail(); };
assert(!(r.sec != 9i64));
assert(!(r.nsec != 999999900i64));
};
// ---- diff: simple subtraction ------------------------------------------
@@ -123,7 +119,7 @@ fn fail() void = { os.exit(signalled + 10); };
let b: time.instant; b.sec = 12i64; b.nsec = 300i64;
let d: i64 = time.diff(a, b): i64;
// b - a = 2.0000002 sec = 2_000_000_200 ns
if (d != 2000000200i64) { fail(); };
assert(!(d != 2000000200i64));
};
// ---- diff: reversed sign -----------------------------------------------
@@ -132,7 +128,7 @@ fn fail() void = { os.exit(signalled + 10); };
let a: time.instant; a.sec = 12i64; a.nsec = 300i64;
let b: time.instant; b.sec = 10i64; b.nsec = 100i64;
let d: i64 = time.diff(a, b): i64;
if (d != -2000000200i64) { fail(); };
assert(!(d != -2000000200i64));
};
// ---- compare: -1 / 0 / +1 ladder ---------------------------------------
@@ -140,43 +136,43 @@ fn fail() void = { os.exit(signalled + 10); };
@test fn comparelt() void = {
let a: time.instant; a.sec = 1i64; a.nsec = 0i64;
let b: time.instant; b.sec = 2i64; b.nsec = 0i64;
if (time.compare(a, b) != -1i8) { fail(); };
assert(!(time.compare(a, b) != -1i8));
};
@test fn comparegt() void = {
let a: time.instant; a.sec = 2i64; a.nsec = 0i64;
let b: time.instant; b.sec = 1i64; b.nsec = 0i64;
if (time.compare(a, b) != 1i8) { fail(); };
assert(!(time.compare(a, b) != 1i8));
};
@test fn compareeq() void = {
let a: time.instant; a.sec = 5i64; a.nsec = 42i64;
let b: time.instant; b.sec = 5i64; b.nsec = 42i64;
if (time.compare(a, b) != 0i8) { fail(); };
assert(!(time.compare(a, b) != 0i8));
};
@test fn comparensec_only() void = {
let a: time.instant; a.sec = 5i64; a.nsec = 100i64;
let b: time.instant; b.sec = 5i64; b.nsec = 200i64;
if (time.compare(a, b) != -1i8) { fail(); };
if (time.compare(b, a) != 1i8) { fail(); };
assert(!(time.compare(a, b) != -1i8));
assert(!(time.compare(b, a) != 1i8));
};
export fn main() i32 = {
signalled = 1; constants();
signalled = 2; nowmonotonic();
signalled = 3; nowrealtime();
signalled = 4; addzero();
signalled = 5; addpos_nocarry();
signalled = 6; addpos_carry();
signalled = 7; addpos_fullsecond();
signalled = 8; addneg_noborrow();
signalled = 9; addneg_borrow();
signalled = 10; diffsimple();
signalled = 11; diffneg();
signalled = 12; comparelt();
signalled = 13; comparegt();
signalled = 14; compareeq();
signalled = 15; comparensec_only();
constants();
nowmonotonic();
nowrealtime();
addzero();
addpos_nocarry();
addpos_carry();
addpos_fullsecond();
addneg_noborrow();
addneg_borrow();
diffsimple();
diffneg();
comparelt();
comparegt();
compareeq();
comparensec_only();
return 0;
};