t27.aiEnglish

Компилируется — не значит верно

Вы узнаете

Почему зелёные галочки — не доказательство и как поймать код, который компилируется и неверен.

Компилятор, принявший ваш код, говорит только, что код правильно оформлен. Он не говорит, что код значит то, что вы имели в виду. Эта запись показывает инструмент, нашедший спеки, которые компилируются, проходят все проверки и всё равно прочитаны неправильно. Для железа урок ещё жёстче: ошибку в кремнии не исправить патчем. Внесите ошибку сами в записи с мутациями и посмотрите, как тесты её ловят.

Попробуйте

Откройте запись с мутациями в «Ещё попробуйте» и посчитайте, сколько подложенных ошибок поймали тесты.

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

tri misread: code that compiles and is wrong
tri misread: code that compiles and is wrong ↗

1,144 of 1,156 specs generate; 41 spec-shape pairs are read wrongly with every gate green.

specs/tutorial/03_operators.t27

// SPDX-License-Identifier: Apache-2.0
; 03 — Operators
; Lesson 3. Arithmetic, bitwise, shifts, comparison and the logical keywords.
; The bitwise group matters more here than in most languages: a spec that
; describes hardware spends most of its time on masks and shifts.
; phi^2 + 1/phi^2 = 3 | TRINITY

module tutorial-03-operators;

// ============================================================================
// Arithmetic
// ============================================================================

pub fn arithmetic(a: i32, b: i32) i32 {
    const sum = a + b;
    const difference = a - b;
    const product = a * b;
    const quotient = a / b;
    const remainder = a % b;
    return sum + difference + product + quotient + remainder;
}

// ============================================================================
// Bitwise
//
// `&` and, `|` or, `^` xor, `~` not. These are the tools for packing and
// unpacking fields, which is most of what a hardware spec does.
// ============================================================================

pub fn mask_and(value: u16, mask: u16) u16 {
    return value & mask;
}

pub fn mask_or(value: u16, bits: u16) u16 {
    return value | bits;
}

pub fn mask_xor(value: u16, bits: u16) u16 {
    return value ^ bits;
}

// ============================================================================
// Shifts
//
// `<<` and `>>`. Extracting a field is a shift followed by a mask, and that
// pair appears in nearly every numeric spec in this corpus.
// ============================================================================

pub const EXP_SHIFT : u16 = 9;
pub const EXP_MASK : u16 = 0b111111;

pub fn extract_exponent(bits: u16) u16 {
    return (bits >> EXP_SHIFT) & EXP_MASK;
}

pub fn place_exponent(exp: u16) u16 {
    return (exp & EXP_MASK) << EXP_SHIFT;
}

// ============================================================================
// Comparison
// ============================================================================

pub fn is_greater(a: i32, b: i32) bool {
    return a > b;
}

pub fn in_range(value: i32, low: i32, high: i32) bool {
    return value >= low and value <= high;
}

// ============================================================================
// Logical
//
// `and`, `or` and `!` are spelled as words rather than symbols, so they never
// read as their bitwise cousins.
// ============================================================================

pub fn either(a: bool, b: bool) bool {
    return a or b;
}

pub fn both(a: bool, b: bool) bool {
    return a and b;
}

pub fn neither(a: bool, b: bool) bool {
    return !a and !b;
}

// ============================================================================
// Tests
// ============================================================================

test "extract and place are inverse for a value that fits" {
    const exp : u16 = 42;
    try std.testing.expectEqual(exp, extract_exponent(place_exponent(exp)));
}

test "in_range is inclusive at both ends" {
    try std.testing.expect(in_range(0, 0, 10));
    try std.testing.expect(in_range(10, 0, 10));
    try std.testing.expect(!in_range(11, 0, 10));
}

test "neither is true only when both are false" {
    try std.testing.expect(neither(false, false));
    try std.testing.expect(!neither(true, false));
}

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

Все уроки