dymokomi/luce-base
Source of release 0.43.0
0.43.0 / src / std / testing.lucb
#==============================================================================================
#
# testing - What a Base test runs under
#
# DESCRIPTION:
# A fixed-buffer allocator made current for each test, a deterministic seed, and
# assertions that name what differed (ยง16.5).
#
#==============================================================================================
import luce
import memory
from io import Location
from memory import Allocator, FixedBuffer
var arena_bytes: u8[1048576]
var arena: FixedBuffer
var saved: Allocator
var current_seed: u64 = 1
## Called by the runner before each test.
pub func begin(index: u64):
arena = FixedBuffer.over(arena_bytes)
saved = memory.allocator
memory.allocator = &arena
current_seed = index * 2654435761 + 1
## Called by the runner after each test.
pub func end():
memory.allocator = saved
## A seed that is the same on every run of the same test.
pub func seed() -> u64:
return current_seed
## The next pseudo-random value from the seed, and the seed advanced.
pub func random() -> u64:
var x = current_seed
x = x ^ (x << 13)
x = x ^ (x >> 7)
x = x ^ (x << 17)
current_seed = x
return x
## Trap with `message` when `condition` is false, naming the line of the test that asked
## (`at`, filled in where it is called).
pub func expect(condition: bool, message: str, at: Location = luce.location):
if not condition:
trap(f"{at.file}:{at.line}: {message}")
## Trap, naming both values and the line of the test, when `actual` and `expected` differ.
pub func expect_equal(actual: i64, expected: i64, message: str, at: Location = luce.location):
if actual != expected:
trap(f"{at.file}:{at.line}: {message}: expected {expected}, got {actual}")
## Trap, naming both texts and the line of the test, when they differ.
pub func expect_text(actual: str, expected: str, message: str, at: Location = luce.location):
if actual != expected:
trap(f"{at.file}:{at.line}: {message}: expected `{expected}`, got `{actual}`")