t27.aiEnglish

Тесты — часть спеки

Вы узнаете

Почему каждая спека t27 несёт свои тесты и что показывает временная диаграмма.

В t27 спека не закончена без своих утверждений: блоки test, инварианты и бенчи живут в том же файле, что и проверяемый ими код, а спеку без них компилятор отвергает. Тест можно запустить и как вентили в симуляторе. Диаграммы ниже — настоящая симуляция Verilog, сгенерированного из спеки троичного умножения с накоплением, на тестовых векторах этой же спеки.

Попробуйте

Приблизьте один такт и прочитайте значения на шинах в отметке pass.

Открыть интерактивный урок →

Test waves: a t27 spec's tests, simulated and drawn as waves
Test waves: a t27 spec's tests, simulated and drawn as waves ↗

Verilog generated from a ternary MAC spec, driven by the spec's own test vectors under Icarus Verilog: clock, buses, pass marks. Drop a .vcd and it draws yours.

specs/tutorial/07_tests_invariants_benches.t27

// SPDX-License-Identifier: Apache-2.0
; 07 — Tests, invariants and benches
; Lesson 7, and the reason this language exists rather than a header file.
; A spec carries its own claims: examples that must hold, properties that must
; always hold, and the operations worth measuring. All three are emitted into
; every target, so the same claim is checked in Zig, C and Rust alike.
; phi^2 + 1/phi^2 = 3 | TRINITY

module tutorial-07-claims;

pub const TRIT_NEG : i8 = -1;
pub const TRIT_ZERO : i8 = 0;
pub const TRIT_POS : i8 = 1;
pub const TRINITY : i8 = 3;

pub fn trit_add(a: i8, b: i8) i8 {
    const sum : i32 = a + b;
    const high = if (sum > 1) 1 else sum;
    const low = if (high < -1) -1 else high;
    return @as(i8, @intCast(low));
}

pub fn trit_mul(a: i8, b: i8) i8 {
    return a * b;
}

// ============================================================================
// test — a concrete example
//
//   test "name" { ... }
//
// Use it for the cases a reader would ask about: the boundaries, the identity,
// the one that used to be wrong.
// ============================================================================

test "trit_add saturates rather than wrapping" {
    try std.testing.expectEqual(@as(i8, TRIT_POS), trit_add(TRIT_POS, TRIT_POS));
    try std.testing.expectEqual(@as(i8, TRIT_NEG), trit_add(TRIT_NEG, TRIT_NEG));
}

test "zero is the additive identity" {
    try std.testing.expectEqual(@as(i8, TRIT_POS), trit_add(TRIT_POS, TRIT_ZERO));
    try std.testing.expectEqual(@as(i8, TRIT_NEG), trit_add(TRIT_ZERO, TRIT_NEG));
}

test "one is the multiplicative identity and zero absorbs" {
    try std.testing.expectEqual(@as(i8, TRIT_NEG), trit_mul(TRIT_NEG, TRIT_POS));
    try std.testing.expectEqual(@as(i8, TRIT_ZERO), trit_mul(TRIT_ZERO, TRIT_NEG));
}

// ============================================================================
// invariant — a property, not an example
//
//   invariant name
//       assert EXPRESSION
//
// This is the part a test cannot express. A test says "for these inputs, this
// output"; an invariant says "this holds, full stop". Reach for it whenever a
// claim has no particular input attached to it.
// ============================================================================

invariant trit_set_has_three_states
    assert TRIT_POS - TRIT_NEG + 1 == TRINITY

invariant trits_are_ordered
    assert TRIT_NEG < TRIT_ZERO

invariant zero_sits_between_the_rails
    assert TRIT_ZERO < TRIT_POS

// ============================================================================
// bench — what is worth measuring
//
//   bench "name" { ... }
//
// A bench names an operation whose cost matters. It is a declaration of intent
// as much as a measurement: it says this is the hot path.
// ============================================================================

bench "trit_add" {
    _ = trit_add(TRIT_POS, TRIT_NEG);
}

bench "trit_mul" {
    _ = trit_mul(TRIT_POS, TRIT_NEG);
}

Открыть спеку урока в плеере ↗

Все уроки