testing
A lightweight test framework for writing and running tests. Provides named test blocks, assertion functions for common comparisons, and a summary report with pass/fail counts. All output is written to stderr.
Usage
import testing
fn main() {
// Test basic arithmetic
test_begin("addition", 8)
assert_eq(2 + 2, 4)
assert_ne(2 + 2, 5)
test_end()
// Test comparisons
test_begin("comparisons", 11)
assert_gt(10, 5)
assert_lt(3, 7)
assert_ge(5, 5)
assert_le(4, 4)
test_end()
// Test booleans
test_begin("booleans", 8)
assert_true(1)
assert_false(0)
test_end()
// Print summary and get failure count
let failures = test_summary()
// Output:
// 3 tests, 0 failures
//
// Exit with failure count as exit code
syscall(60, failures, 0, 0)
}Function Reference
| Function | Signature | Description |
|---|---|---|
test_begin | (name: &i8, len: i64) | Begin a named test |
test_end | () | End the current test |
assert_eq | (a: i64, b: i64) | Assert a == b |
assert_ne | (a: i64, b: i64) | Assert a != b |
assert_true | (val: i64) | Assert non-zero (truthy) |
assert_false | (val: i64) | Assert zero (falsy) |
assert_gt | (a: i64, b: i64) | Assert a > b |
assert_lt | (a: i64, b: i64) | Assert a < b |
assert_ge | (a: i64, b: i64) | Assert a >= b |
assert_le | (a: i64, b: i64) | Assert a <= b |
test_summary | () -> i64 | Print summary, return failure count |
Detailed API
test_begin
fn test_begin(name: &i8, len: i64)Begin a named test. The name is printed to stderr to identify the test being run. Must be paired with test_end.
test_begin("vec_push", 8)
// ... assertions ...
test_end()test_end
fn test_end()End the current test. Marks the test as complete and updates internal counters. If any assertions failed within this test, it is counted as a failed test.
test_begin("my_test", 7)
assert_eq(1, 1)
test_end() // test passesassert_eq
fn assert_eq(a: i64, b: i64)Assert that a equals b. If the assertion fails, prints the expected and actual values to stderr.
test_begin("equality", 8)
assert_eq(2 + 2, 4) // passes
assert_eq(vec_len(v), 3) // check vec length
test_end()assert_ne
fn assert_ne(a: i64, b: i64)Assert that a is not equal to b.
test_begin("inequality", 10)
assert_ne(1, 2) // passes
assert_ne(0, 0) // FAILS
test_end()assert_true
fn assert_true(val: i64)Assert that val is truthy (non-zero).
test_begin("truthy", 6)
assert_true(1) // passes
assert_true(vec_contains(v, 42)) // passes if 42 is in v
assert_true(fs_exists("/tmp")) // passes if /tmp exists
test_end()assert_false
fn assert_false(val: i64)Assert that val is falsy (zero).
test_begin("falsy", 5)
assert_false(0) // passes
assert_false(map_has(m, 999)) // passes if key not in map
test_end()assert_gt
fn assert_gt(a: i64, b: i64)Assert that a is strictly greater than b.
test_begin("greater_than", 12)
assert_gt(10, 5) // passes
assert_gt(vec_len(v), 0) // vec is non-empty
test_end()assert_lt
fn assert_lt(a: i64, b: i64)Assert that a is strictly less than b.
test_begin("less_than", 9)
assert_lt(3, 7) // passes
test_end()assert_ge
fn assert_ge(a: i64, b: i64)Assert that a is greater than or equal to b.
test_begin("greater_or_equal", 16)
assert_ge(5, 5) // passes (equal)
assert_ge(10, 5) // passes (greater)
test_end()assert_le
fn assert_le(a: i64, b: i64)Assert that a is less than or equal to b.
test_begin("less_or_equal", 13)
assert_le(4, 4) // passes (equal)
assert_le(3, 7) // passes (less)
test_end()test_summary
fn test_summary() -> i64Print the test summary to stderr showing total tests and number of failures. Returns the failure count, which can be used as a process exit code.
let failures = test_summary()
// Output to stderr: "5 tests, 0 failures" (or similar)
// Use as exit code (0 = all passed)
syscall(60, failures, 0, 0)Returns: Number of failed tests. 0 means all tests passed.
Internal Functions
| Function | Signature | Description |
|---|---|---|
test_stderr | (msg: &i8, len: i64) | Write a message to stderr |
test_stderr_int | (val: i64) | Write an integer to stderr |
test_assert_fail | (op: &i8, oplen: i64, a: i64, b: i64) | Report an assertion failure with operator and values |
Example: Testing a Module
import vec
import sort
import testing
fn main() {
test_begin("sort_vec", 8)
let v = vec_new(16)
vec_push(v, 30)
vec_push(v, 10)
vec_push(v, 20)
sort_vec(v)
assert_eq(vec_get(v, 0), 10)
assert_eq(vec_get(v, 1), 20)
assert_eq(vec_get(v, 2), 30)
assert_true(sort_is_sorted(v))
test_end()
test_begin("sort_binary_search", 19)
let idx = sort_binary_search(v, 20)
assert_eq(idx, 1)
let missing = sort_binary_search(v, 99)
assert_eq(missing, -1)
test_end()
let failures = test_summary()
syscall(60, failures, 0, 0)
}