Tests y Benchmarks con el Runner
En esta página
@test marca una función para que sea descubierta por zolo test. La función
no recibe argumentos ni devuelve valor; usa assert_eq para verificar
resultados. Varios tests en el mismo archivo se ejecutan de forma independiente
— un fallo no interrumpe los demás:
Tests de suma, multiplicación y división; assert con mensaje opcional.
// Feature: `@test` — function recognized by the test runner
// Syntax: `@test` before the `fn` (no args, no return). Use `assert_eq`
// inside to validate.
// When to use: unit tests in the same file as the code; discovered by
// `zolo test`.
fn add(a: int, b: int) -> int {
return a + b
}
fn mul(a: int, b: int) -> int {
return a * b
}
fn div(a: int, b: int) -> int {
if b == 0 { panic("div by zero") }
return a / b
}
@test
fn test_add() {
assert_eq(add(2, 3), 5)
}
@test
fn test_add_negative() {
assert_eq(add(-1, 1), 0)
}
@test
fn test_mul() {
assert_eq(mul(3, 4), 12)
}
@test
fn test_div_ok() {
assert_eq(div(10, 2), 5)
}
// Optional message shown if the assert fails.
@test
fn test_with_message() {
assert_eq(add(10, 20), 30, "10 + 20 should be 30")
}
// `main` still runs when you use `zolo run` instead of `zolo test`.
print("module loaded")
// expected: module loaded
@bench registra la función en el runner de benchmarks (zolo bench). A
diferencia de @benchmark — que imprime el tiempo de cada llamada individual —,
@bench se integra en el pipeline de CI para detectar regresiones de rendimiento
entre revisiones. Los dos pueden combinarse:
Bench de dos algoritmos de suma y composición @bench + @benchmark.
// Feature: `@bench` — function recognized by the benchmark runner
// Syntax: `@bench` before the `fn`. Unlike `@benchmark` (which measures
// any single call), `@bench` is discovered by `zolo bench`.
// When to use: performance regressions in CI, comparing variants.
fn slow_sum(n: int) -> int {
var total = 0
for i in 1..=n {
total += i
}
return total
}
fn fast_sum(n: int) -> int {
return n * (n + 1) / 2
}
@bench
fn bench_slow_sum() {
let _ = slow_sum(10000)
}
@bench
fn bench_fast_sum() {
let _ = fast_sum(10000)
}
// A @bench can also mix with @benchmark to print every call.
@bench
@benchmark
fn bench_combined() {
let _ = slow_sum(1000)
}
print("benches registered")
// expected: benches registered
Nota:
@benchy@testregistran la función en el runner; en algunos runtimes queda fuera del ámbito global. Si necesitas reutilizarla en código común, extrae la lógica en una función auxiliar separada y llámala desde ambos lados.
Consulta también