cheatah
Source

tests/purrc/time_cr_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// Compile-run unit tests for the `time` module: one test per function. Each writes
4// a tiny .purr that calls a single time function, compiles it with purrc, runs it
5// under the cheatah runtime, and asserts the exact stdout. Complements the
6// in-process unit tests (stdlib/tests/time_test.cpp) and the per-module
7// system-level test (StdlibE2E.Time).
8//
9// Clock values are non-deterministic, so every program prints a DETERMINISTIC
10// boolean property (ordering / sign) instead of any raw clock value.
11#include "e2e_harness.hpp"
13TEST(TimeCompileRun, Time) {
14 e2e::expect_e2e("time_time", R"PURR(import io
15import time
16io.print(time.time() > 0.0)
17)PURR", "True\n");
20TEST(TimeCompileRun, TimeNs) {
21 e2e::expect_e2e("time_time_ns", R"PURR(import io
22import time
23io.print(time.time_ns() > 0)
24)PURR", "True\n");
27TEST(TimeCompileRun, Monotonic) {
28 e2e::expect_e2e("time_monotonic", R"PURR(import io
29import time
30io.print(time.monotonic() > 0.0)
31)PURR", "True\n");
34TEST(TimeCompileRun, MonotonicNs) {
35 e2e::expect_e2e("time_monotonic_ns", R"PURR(import io
36import time
37io.print(time.monotonic_ns() > 0)
38)PURR", "True\n");
41TEST(TimeCompileRun, PerfCounter) {
42 e2e::expect_e2e("time_perf_counter", R"PURR(import io
43import time
44io.print(time.perf_counter() > 0.0)
45)PURR", "True\n");
48TEST(TimeCompileRun, PerfCounterNs) {
49 e2e::expect_e2e("time_perf_counter_ns", R"PURR(import io
50import time
51io.print(time.perf_counter_ns() > 0)
52)PURR", "True\n");
55TEST(TimeCompileRun, ProcessTime) {
56 e2e::expect_e2e("time_process_time", R"PURR(import io
57import time
58io.print(time.process_time() >= 0.0)
59)PURR", "True\n");
62TEST(TimeCompileRun, Sleep) {
63 // sleep blocks at least the requested duration, so the monotonic clock after
64 // sleeping is >= the reading before.
65 e2e::expect_e2e("time_sleep", R"PURR(import io
66import time
67let t0 = time.monotonic()
68time.sleep(0.01)
69let t1 = time.monotonic()
70io.print(t1 >= t0)
71)PURR", "True\n");