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 writes4
// a tiny .purr that calls a single time function, compiles it with purrc, runs it5
// under the cheatah runtime, and asserts the exact stdout. Complements the6
// in-process unit tests (stdlib/tests/time_test.cpp) and the per-module7
// system-level test (StdlibE2E.Time).8
//9
// Clock values are non-deterministic, so every program prints a DETERMINISTIC10
// boolean property (ordering / sign) instead of any raw clock value.11
#include "e2e_harness.hpp"13
TEST(TimeCompileRun, Time) {14
e2e::expect_e2e("time_time", R"PURR(import io15
import time16
io.print(time.time() > 0.0)17
)PURR", "True\n");18
}20
TEST(TimeCompileRun, TimeNs) {21
e2e::expect_e2e("time_time_ns", R"PURR(import io22
import time23
io.print(time.time_ns() > 0)24
)PURR", "True\n");25
}27
TEST(TimeCompileRun, Monotonic) {28
e2e::expect_e2e("time_monotonic", R"PURR(import io29
import time30
io.print(time.monotonic() > 0.0)31
)PURR", "True\n");32
}34
TEST(TimeCompileRun, MonotonicNs) {35
e2e::expect_e2e("time_monotonic_ns", R"PURR(import io36
import time37
io.print(time.monotonic_ns() > 0)38
)PURR", "True\n");39
}41
TEST(TimeCompileRun, PerfCounter) {42
e2e::expect_e2e("time_perf_counter", R"PURR(import io43
import time44
io.print(time.perf_counter() > 0.0)45
)PURR", "True\n");46
}48
TEST(TimeCompileRun, PerfCounterNs) {49
e2e::expect_e2e("time_perf_counter_ns", R"PURR(import io50
import time51
io.print(time.perf_counter_ns() > 0)52
)PURR", "True\n");53
}55
TEST(TimeCompileRun, ProcessTime) {56
e2e::expect_e2e("time_process_time", R"PURR(import io57
import time58
io.print(time.process_time() >= 0.0)59
)PURR", "True\n");60
}62
TEST(TimeCompileRun, Sleep) {63
// sleep blocks at least the requested duration, so the monotonic clock after64
// sleeping is >= the reading before.65
e2e::expect_e2e("time_sleep", R"PURR(import io66
import time67
let t0 = time.monotonic()68
time.sleep(0.01)69
let t1 = time.monotonic()70
io.print(t1 >= t0)71
)PURR", "True\n");72
}