Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -0,0 +1,209 @@
|
||||
# Tests of what is closed where its scope ends: a type naming `owning`, and what holds
|
||||
# one, which is `closeable`. A `close` alone is only a method, and a reference or a
|
||||
# pointer owns nothing.
|
||||
import std::testing;
|
||||
|
||||
|
||||
# How many values holding a number have been closed.
|
||||
var shut: u64 = 0;
|
||||
|
||||
struct named : owning {
|
||||
var n: u64;
|
||||
}
|
||||
|
||||
def named::close(&mut this): void {
|
||||
if this.n != 0 {
|
||||
shut++;
|
||||
this.n = 0;
|
||||
}
|
||||
}
|
||||
|
||||
struct shaped {
|
||||
var n: u64;
|
||||
}
|
||||
|
||||
def shaped::close(&mut this): void {
|
||||
if this.n != 0 {
|
||||
shut++;
|
||||
this.n = 0;
|
||||
}
|
||||
}
|
||||
|
||||
struct wraps {
|
||||
var inner: named;
|
||||
}
|
||||
|
||||
def kind<T>(): span<u8> {
|
||||
if const T is owning { return "owning"; }
|
||||
if const T is closeable { return "closeable"; }
|
||||
return "plain";
|
||||
}
|
||||
|
||||
@test def only_what_names_owning_is_closed(t: &mut tester): void {
|
||||
t.expects(kind::<named>() == "owning" && kind::<wraps>() == "closeable" && kind::<named[2]>() == "closeable");
|
||||
t.expects(kind::<shaped>() == "plain" && kind::<&mut named>() == "plain" && kind::<*mut named>() == "plain");
|
||||
|
||||
shut = 0;
|
||||
{
|
||||
val a := named { n = 1 };
|
||||
val b := shaped { n = 1 };
|
||||
val c := wraps { inner = named { n = 1 } };
|
||||
}
|
||||
t.expects(shut == 2);
|
||||
|
||||
# A `close` is still called by hand, through a reference too.
|
||||
var d := shaped { n = 1 };
|
||||
val through: &mut shaped = &mut d;
|
||||
through.close();
|
||||
t.expects(shut == 3 && d.n == 0);
|
||||
}
|
||||
|
||||
# Gives values counting down to one, then nothing.
|
||||
struct countdown {
|
||||
var left: u64;
|
||||
}
|
||||
|
||||
def countdown::next(&mut this): maybe<named> {
|
||||
if this.left == 0 {
|
||||
return nothing {};
|
||||
}
|
||||
this.left--;
|
||||
return named { n = this.left + 1 };
|
||||
}
|
||||
|
||||
def first_of(from: &mut countdown): named {
|
||||
if from.next() is some(got) {
|
||||
return got;
|
||||
}
|
||||
return named {};
|
||||
}
|
||||
|
||||
@test def union_fields_are_taken_from_what_a_call_made(t: &mut tester): void {
|
||||
# Named by value, each is taken out of what the call gave, and closed once, where the
|
||||
# turn ends, a `continue` goes on, or a `break` leaves.
|
||||
shut = 0;
|
||||
var from := countdown { left = 5 };
|
||||
var seen: u64 = 0;
|
||||
while from.next() is some(got) {
|
||||
if got.n == 4 {
|
||||
continue;
|
||||
}
|
||||
seen += got.n;
|
||||
if got.n == 2 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
t.expects(seen == 10 && shut == 4);
|
||||
|
||||
from.left = 1;
|
||||
if from.next() is some(got) {
|
||||
seen += got.n;
|
||||
}
|
||||
from.left = 1;
|
||||
match from.next() {
|
||||
some(got) => seen += got.n;
|
||||
else => {}
|
||||
}
|
||||
t.expects(seen == 12 && shut == 6);
|
||||
|
||||
# Given away, by `return` or as an arm's value, it is closed by what took it.
|
||||
from.left = 2;
|
||||
{
|
||||
val kept := first_of(&mut from);
|
||||
val also: named = match from.next() {
|
||||
some(got) => got,
|
||||
else => named {},
|
||||
};
|
||||
t.expects(kept.n == 2 && also.n == 1 && shut == 6);
|
||||
}
|
||||
t.expects(shut == 8);
|
||||
|
||||
# `is not` gives it to the rest of the block.
|
||||
from.left = 1;
|
||||
{
|
||||
if from.next() is not some(got) {
|
||||
return;
|
||||
}
|
||||
t.expects(got.n == 1 && shut == 8);
|
||||
}
|
||||
t.expects(shut == 9);
|
||||
}
|
||||
|
||||
@test def a_while_closes_what_its_condition_made_each_turn(t: &mut tester): void {
|
||||
# Named in place, each value is still the one the call gave, closed before the
|
||||
# condition makes the next.
|
||||
shut = 0;
|
||||
var from := countdown { left = 4 };
|
||||
var seen: u64 = 0;
|
||||
while from.next() is some(&got) {
|
||||
if got.n == 3 {
|
||||
continue;
|
||||
}
|
||||
seen += got.n;
|
||||
}
|
||||
t.expects(seen == 7 && shut == 4);
|
||||
}
|
||||
|
||||
@test def names_are_given_where_their_test_holds(t: &mut tester): void {
|
||||
# What the left of `&&` names is the right's to use, and of the branch, and each value
|
||||
# a test takes is closed once, whichever tests a short circuit leaves unrun.
|
||||
shut = 0;
|
||||
var from := countdown { left = 3 };
|
||||
var seen: u64 = 0;
|
||||
if from.next() is some(a) && from.next() is some(b) && a.n > b.n {
|
||||
seen = a.n * 10 + b.n;
|
||||
}
|
||||
t.expects(seen == 32 && shut == 2);
|
||||
if from.next() is some(a) && from.next() is some(b) {
|
||||
seen = 0;
|
||||
}
|
||||
t.expects(seen == 32 && shut == 3);
|
||||
|
||||
# `||` gives its right what an `is not` test names, and `!` turns a test around.
|
||||
from.left = 2;
|
||||
val small := from.next() is not some(c) || c.n < 3;
|
||||
t.expects(small && shut == 4);
|
||||
from.left = 1;
|
||||
{
|
||||
if !(from.next() is some(d)) {
|
||||
seen = 0;
|
||||
} else {
|
||||
seen = d.n;
|
||||
}
|
||||
}
|
||||
t.expects(seen == 1 && shut == 5);
|
||||
|
||||
# A guard gives the rest of the block what it names.
|
||||
from.left = 2;
|
||||
{
|
||||
if from.next() is not some(e) || from.next() is not some(f) {
|
||||
return;
|
||||
}
|
||||
seen = e.n * 10 + f.n;
|
||||
}
|
||||
t.expects(seen == 21 && shut == 7);
|
||||
}
|
||||
|
||||
@test def bindings_holding_values_are_moved_from(t: &mut tester): void {
|
||||
# A value a binding took is its own, so it is moved on, and the binding then closes
|
||||
# nothing, as a `var` moved from does.
|
||||
shut = 0;
|
||||
var from := countdown { left = 2 };
|
||||
{
|
||||
var kept := vector::<named>::new();
|
||||
while from.next() is some(got) {
|
||||
kept.append(move(got));
|
||||
}
|
||||
from.left = 1;
|
||||
match from.next() {
|
||||
some(got) => kept.append(move(got));
|
||||
else => {}
|
||||
}
|
||||
from.left = 2;
|
||||
for each in from {
|
||||
kept.append(move(each));
|
||||
}
|
||||
t.expects(kept.length == 5 && shut == 0);
|
||||
}
|
||||
t.expects(shut == 5);
|
||||
}
|
||||
Reference in new issue
Block a user