Tests and Benchmarks
Calibre has built-in forms for tests and benchmarks.
A test is declared with test.
test showcase_core_math => { assert(factorial(6) = 720, "factorial(6) should equal 720"); assert(int.gcd(84, 18) = 6, "int.gcd(84, 18) should equal 6");};Tests are ordinary Calibre code blocks, so they can call functions, construct values, match results, and use any other language feature.
Assertions are written with assert(condition, message).
test showcase_conversions => { let ok := parse_and_add("50", 7); assert(ok.is_ok(), "parse_and_add should return ok on valid int text"); assert(("oops" as? int).is_none(), "as? should return none on conversion failure");};Benchmarks use the bench form and return a hyperfine style view of how long it took to run.
This is useful when you want repeatable performance measurements written in the language itself.
bench showcase_generator_throughput => { let gen := even_numbers(0, 20000); let mut count := 0; let mut sum := 0;
for count < 2000 => { let next := gen.next(); assert(next.is_some(), "generator ended too early during benchmark"); sum += next.unwrap_or(0); count += 1; };
assert(sum > 0, "benchmark sum should be positive");};Note that tests can be run using calibre test and benchmarks using calibre bench.