t27.aiРусский

Invariants and benches

You will learn

How invariant and bench blocks are written and who evaluates them.

invariant name followed by an assert states a rule of the spec that must hold for every value, not one example. bench name marks a block to time. Both compile. The test runner on this site evaluates test blocks only: it skips invariants and benches, so a false invariant passes here silently. The lesson spec repeats its invariant as a test for that reason.

Try it

Make the invariant false in the player and check that nothing on this site complains, then make the test false.

Open the interactive lesson →

t27 basics 20: test, invariant and bench
t27 basics 20: test, invariant and bench ↗

The three kinds of checking block in t27 and which ones the site test runner evaluates. Lesson 20 of the t27 basics course.

specs/basics/20_invariants.t27

// SPDX-License-Identifier: Apache-2.0
; t27 basics, lesson 20: Invariants and benches.
; An invariant states a rule that must always hold; a bench names a measurement.

module basics_20_invariants;

pub const SIDES : u8 = 3;

invariant a_triangle_has_three_sides
    assert SIDES == 3

test the_invariant_as_a_test {
    assert SIDES == 3;
}

Open the lesson's spec in the player ↗

All lessons