Source
tests/purrc/time_sys_test.cpp
1
// Copyright (c) 2026 BigBrain LLC. MIT-licensed (see LICENSE).2
// Original work; see ACKNOWLEDGMENTS.md for the open-source ideas we build upon.3
// System-level test for the cheatah `time` module: a single cohesive .purr4
// program that exercises EVERY public function in stdlib/time/time.hpp5
// (time, time_ns, monotonic, monotonic_ns, perf_counter, perf_counter_ns,6
// process_time, sleep), compiles it with purrc into a loadable module, runs it7
// under the cheatah runtime, and asserts the exact stdout byte-for-byte.8
//9
// Clock readings are non-deterministic, so the program prints DETERMINISTIC10
// boolean PROPERTIES instead of raw values:11
// - wall clock (time/time_ns) is strictly positive;12
// - the monotonic / perf-counter clocks are positive and, after a sleep,13
// read >= their earlier values (they never run backward);14
// - process_time (CPU time) is non-negative.15
// Complements the in-process unit tests (stdlib/tests/time_test.cpp) and the16
// per-function compile-run tests (tests/purrc/time_cr_test.cpp).17
#include "e2e_harness.hpp"19
TEST(StdlibE2E, Time) {20
e2e::expect_e2e("time_sys", R"PURR(import io21
import time23
# Wall-clock readings are positive.24
let w = time.time()25
let wn = time.time_ns()27
# Monotonic / perf clocks are positive and never run backward across a sleep.28
let m0 = time.monotonic()29
let mn0 = time.monotonic_ns()30
let p0 = time.perf_counter()31
let pn0 = time.perf_counter_ns()33
time.sleep(0.01)35
let m1 = time.monotonic()36
let mn1 = time.monotonic_ns()37
let p1 = time.perf_counter()38
let pn1 = time.perf_counter_ns()40
# CPU time consumed by the process is non-negative.41
let cpu = time.process_time()43
io.print(w > 0.0, wn > 0)44
io.print(m0 > 0.0, mn0 > 0, p0 > 0.0, pn0 > 0)45
io.print(m1 >= m0, mn1 >= mn0, p1 >= p0, pn1 >= pn0)46
io.print(cpu >= 0.0)47
)PURR",48
"True True\n"49
"True True True True\n"50
"True True True True\n"51
"True\n");52
}