property_testing
property_testing
Section titled “property_testing”The property_testing module is a small QuickCheck/Hypothesis-style
property-based testing framework. You define a generator that produces random
inputs and a property predicate that should hold for all inputs; the module
runs the predicate against many random inputs and reports any
counterexample.
Import
Section titled “Import”use std::core::utils::property_testingRunning Properties
Section titled “Running Properties”property_testing::property(name: string, n_trials: int, gen_fn, prop_fn)
Section titled “property_testing::property(name: string, n_trials: int, gen_fn, prop_fn)”Run a single property. gen_fn() -> input generates a random input; prop_fn(input) -> bool
checks the property. Stops on the first failure.
Returns { passed: bool, name: string, trials: int, counterexample: _? }.
use std::core::utils::property_testing
let result = property_testing::property( "abs is non-negative", 100, property_testing::gen_int(-1000, 1000), |x| abs(x) >= 0)print(f"{result.name}: {if result.passed { \"PASS\" } else { \"FAIL\" }}")property_testing::run_properties(tests)
Section titled “property_testing::run_properties(tests)”Run multiple properties and return a summary. Each test is
{ name, trials, gen, prop }.
let summary = property_testing::run_properties([ { name: "addition is commutative", trials: 200, gen: property_testing::gen_int(-100, 100), prop: |x| { let y = 7 (x + y) == (y + x) } }])print(f"passed: {summary.passed}/{summary.total}")Built-in Generators
Section titled “Built-in Generators”These return generator closures () -> value.
| Generator | Description |
|---|---|
gen_int(lo, hi) | Random int in [lo, hi] |
gen_float(lo, hi) | Random float in [lo, hi) |
gen_bool() | Random boolean |
gen_string(max_len) | Random ASCII alphanumeric string of length [0, max_len] |
gen_array(max_len, elem_gen) | Random array of length [0, max_len] from elem_gen |
gen_one_of(choices) | Pick one value uniformly at random |
use std::core::utils::property_testing
let int_gen = property_testing::gen_int(0, 100)let arr_gen = property_testing::gen_array(10, int_gen)
let result = property_testing::property( "len is non-negative", 50, arr_gen, |arr| arr.length >= 0)See Also
Section titled “See Also”- testing — single-assertion unit tests