cheatah
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 .purr
4// program that exercises EVERY public function in stdlib/time/time.hpp
5// (time, time_ns, monotonic, monotonic_ns, perf_counter, perf_counter_ns,
6// process_time, sleep), compiles it with purrc into a loadable module, runs it
7// under the cheatah runtime, and asserts the exact stdout byte-for-byte.
8//
9// Clock readings are non-deterministic, so the program prints DETERMINISTIC
10// 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 the
16// per-function compile-run tests (tests/purrc/time_cr_test.cpp).
17#include "e2e_harness.hpp"
19TEST(StdlibE2E, Time) {
20 e2e::expect_e2e("time_sys", R"PURR(import io
21import time
23# Wall-clock readings are positive.
24let w = time.time()
25let wn = time.time_ns()
27# Monotonic / perf clocks are positive and never run backward across a sleep.
28let m0 = time.monotonic()
29let mn0 = time.monotonic_ns()
30let p0 = time.perf_counter()
31let pn0 = time.perf_counter_ns()
33time.sleep(0.01)
35let m1 = time.monotonic()
36let mn1 = time.monotonic_ns()
37let p1 = time.perf_counter()
38let pn1 = time.perf_counter_ns()
40# CPU time consumed by the process is non-negative.
41let cpu = time.process_time()
43io.print(w > 0.0, wn > 0)
44io.print(m0 > 0.0, mn0 > 0, p0 > 0.0, pn0 > 0)
45io.print(m1 >= m0, mn1 >= mn0, p1 >= p0, pn1 >= pn0)
46io.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");