Files
sakurac/tests/programs/contracts_3.sakura
T
2026-10-28 22:13:49 -05:00

56 lines
1.0 KiB
Plaintext

# An `ensures` is checked on every way out of its block once it is reached: a
# `return`, a `break` or `continue` leaving the block, and the block's end. Of
# several, the last written is checked first.
import std::io;
var seen: string;
def saw(what: strview): bool {
seen.append(what);
seen.append(" ");
return true;
}
def returns(early: bool): i64 {
ensures(saw("first"));
return 1 if early;
ensures(saw("second"));
return 2;
}
def loops(): void {
for i in 0..4 {
ensures(saw($"pass{i}"));
continue if i == 1;
break if i == 2;
}
var k := 0;
while k < 2 {
k++;
ensures(saw($"turn{k}"));
}
{
ensures(saw("block"));
}
}
def first_even(xs: span<i64>): i64 {
var found: i64 = -1;
for x in xs {
ensures(found == -1 || found % 2 == 0);
if x % 2 == 1 {
found = x;
break;
}
}
return found;
}
def main(): void {
_ = returns(true);
_ = returns(false);
loops();
io::println(seen.view());
io::println($"{first_even([1, 2, 3][:])}");
}