cheatah
Source

tests/purrc/builtins_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 "real program" test for the `builtins` module: a single cohesive
4// program that exercises EVERY public, purr-callable built-in declared in
5// stdlib/builtins/builtins.hpp in one run, then asserts its stdout byte-for-byte.
6//
7// Unlike the per-function compile-run suite (builtins_cr_test.cpp, one built-in
8// each), this is one small but genuine program: it inspects a string with
9// len/ord/chr, renders a number in every base (hex/oct/bin), shows its printable
10// repr with ascii, and runs the int()/float()/bool() conversions (the Python
11// spellings map to builtins::to_int/to_float/to_bool). The program is fully
12// DETERMINISTIC, so its output is asserted exactly.
13//
14// Coverage of stdlib/builtins/builtins.hpp (every purr-callable built-in):
15// len, ord, chr, hex, oct, bin, ascii,
16// int() -> to_int (from string and from float),
17// float()-> to_float(from string and from int),
18// bool() -> to_bool (from string and from number),
19// hash -> value is implementation-defined, so we assert only the
20// deterministic property hash("a") == hash("a") (not the value).
21//
22// Note: purrc treats a newline as a statement terminator, so each call stays on
23// its own source line.
24#include "e2e_harness.hpp"
26TEST(StdlibE2E, Builtins) {
27 e2e::expect_e2e("builtins_sys", R"PURR(import io
28import builtins
30# Inspect a string byte-by-byte using len/ord/chr (+ ascii for its repr).
31let s = "Cat"
32io.print(io.format("len({}) = {}", ascii(s), len(s)))
34let i = 0
35while i < len(s) {
36 let ch = chr(ord(s) + i)
37 io.print(io.format("ord/chr step {}: {}", i, ch))
38 i = i + 1
41# Base representations of a number: hex / oct / bin.
42let n = 255
43io.print(io.format("{} -> hex={} oct={} bin={}", n, hex(n), oct(n), bin(n)))
45# Conversions: int()/float()/bool() map to to_int/to_float/to_bool.
46io.print(io.format("int(\"42\")={} int(3.9)={}", int("42"), int(3.9)))
47io.print(io.format("float(\"2.5\")={} float(7)={}", float("2.5"), float(7)))
48io.print(io.format("bool(\"x\")={} bool(0)={} bool(7)={}", bool("x"), bool(0), bool(7)))
50# hash: value is implementation-defined; assert only a deterministic property.
51io.print(io.format("hash stable: {}", bool(hash("a") == hash("a"))))
52)PURR",
53 "len('Cat') = 3\n"
54 "ord/chr step 0: C\n"
55 "ord/chr step 1: D\n"
56 "ord/chr step 2: E\n"
57 "255 -> hex=0xff oct=0o377 bin=0b11111111\n"
58 "int(\"42\")=42 int(3.9)=3\n"
59 "float(\"2.5\")=2.5 float(7)=7\n"
60 "bool(\"x\")=1 bool(0)=0 bool(7)=1\n"
61 "hash stable: 1\n");