Files

372 lines
11 KiB
Plaintext
Raw Permalink Normal View History

2026-10-26 10:14:11 -05:00
# Tests of what references and pointers satisfy. A reference satisfies the traits of
# what it refers to, called through it, so a generic given one borrows what it would
# otherwise own, and it is compared and hashed as what it refers to. A pointer is an
# address: it is compared and hashed as one, and what it points at is reached by
# dereferencing it.
import std::containers;
import std::testing;
# Something written to, a number at a time.
trait sink {
def put(&mut this, n: u64): void;
def put_twice(&mut this, n: u64): void { this.put(n); this.put(n); }
}
# Something whose size is read.
trait sized {
def size(&this): u64;
}
# How many tallies holding a count have been closed.
var tallies_closed: u64 = 0;
struct tally : sink, sized, owning {
var n: u64;
}
def tally::put(&mut this, n: u64): void { this.n += n; }
def tally::size(&this): u64 { return this.n; }
def tally::close(&mut this): void {
if this.n != 0 {
tallies_closed++;
this.n = 0;
}
}
# A sink that passes each number on to what it holds, owned or borrowed, and closes
# it where it owns what closes.
struct relay<S> : sink where S: sink {
var to: S;
}
def relay<S>::put(&mut this, n: u64): void {
this.to.put(n);
}
# Puts a number through a reference to the trait, made from the field.
def relay<S>::put_through_trait(&mut this, n: u64): void {
val to: &mut sink = &mut this.to;
to.put(n);
}
def relay_to<S>(to: S): relay<S> where S: sink {
return relay::<S> { to = move(to) };
}
def size_of_it<T>(it: T): u64 where T: sized {
return it.size();
}
# How many times a mark has been hashed by its own `hash`.
var marks_hashed: u64 = 0;
struct mark : ordered, hashable {
var n: u64;
}
def mark::equals(&this, other: &mark): bool { return this.n == other.n; }
def mark::compare(&this, other: &mark): i64 { return this.n as i64 - other.n as i64; }
def mark::hash(&this): u64 {
marks_hashed++;
return this.n;
}
# Something holding a field named as one of a pointer's own methods.
struct slot {
var hash: u64;
}
def larger<T>(a: T, b: T): T where T: ordered {
if a < b { return b; }
return a;
}
def same<T>(a: T, b: T): bool where T: equatable {
return a == b && a.equals(&b);
}
def total<C>(items: C): u64 where C: iterable<u64> {
var sum: u64 = 0;
for &each in items {
sum += each;
}
return sum;
}
def first_and_last<C>(items: C, count: u64): u64 where C: indexable<u64, u64> {
return items[0] * 10 + items.at(count - 1);
}
# How many elements lie between two pointers, by moving one along, as pointers do.
def elements_between<P>(first: P, last: P): i64 where P: pointer {
var at := first;
at += 1;
return last - at + 1;
}
def no_pointer<P>(): P where P: pointer {
var made: P;
return made;
}
def addressed<T>(): span<u8> {
if const T is pointer { return "pointer"; }
return "not";
}
# Puts a value in a slot, whatever it is: given a reference, the slot refers anew.
def put_in<T>(slot: &mut T, value: T): void {
slot = value;
}
def what<S>(): span<u8> {
if const S is owning { return "owning"; }
if const S is closeable { return "closeable"; }
if const S is sink { return "sink"; }
return "neither";
}
@test def references_satisfy_what_they_refer_to(t: &mut tester): void {
var kept := tally { n = 1 };
var by_reference := relay_to(&mut kept);
by_reference.put(2);
by_reference.put_through_trait(3);
t.expects(kept.n == 6);
# A pointer satisfies nothing of what it points at, but a reference through it does.
var by_pointer := relay_to(&mut through((&mut kept) as *mut tally));
by_pointer.put(4);
by_pointer.put_through_trait(5);
t.expects(kept.n == 15);
val as_trait: &mut sink = &mut kept;
var by_trait := relay_to(as_trait);
by_trait.put(6);
by_trait.put_through_trait(7);
t.expects(kept.n == 28);
# Relays nest, each borrowing the one inside it.
var inner := relay_to(&mut kept);
var outer := relay_to(&mut inner);
outer.put(2);
outer.put_through_trait(1);
t.expects(kept.n == 31);
# Read through a reference that only reads, since `size` takes `&this`.
t.expects(size_of_it(&kept) == 31 && size_of_it(&through((&kept) as *tally)) == 31);
}
@test def references_take_the_types_their_referents_give(t: &mut tester): void {
# A trait asking for types is satisfied through a reference with the referent's, as
# `iterable` asks for an `iterator`, and an `@optional` method writing is not asked
# of a reference that only reads.
var numbers := vector::<u64>::new();
numbers.append(4);
numbers.append(5);
numbers.append(6);
val viewed: &vector<u64> = &numbers;
t.expects(total(&viewed) == 15 && total(&mut numbers) == 15 && total(numbers.span) == 15);
t.expects(first_and_last(&viewed, 3) == 46 && first_and_last(&numbers, 2) == 45);
}
@test def references_close_nothing_they_refer_to(t: &mut tester): void {
t.expects(what::<tally>() == "owning");
t.expects(what::<relay<tally>>() == "closeable");
t.expects(what::<&mut tally>() == "sink" && what::<*mut tally>() == "neither");
t.expects(what::<relay<&mut tally>>() == "sink");
tallies_closed = 0;
var lent := tally { n = 1 };
{
var borrowed := relay_to(&mut lent);
borrowed.put(1);
}
t.expects(tallies_closed == 0 && lent.n == 2);
{
var owned := relay_to(tally { n = 1 });
owned.put(1);
}
t.expects(tallies_closed == 1);
# Never closed itself, a reference still has `close` called through it on what it
# refers to, as a reference to the trait does, and what a pointer points at.
tallies_closed = 0;
var shut := tally { n = 1 };
val by_reference: &mut tally = &mut shut;
by_reference.close();
var pointed := tally { n = 1 };
(*((&mut pointed) as *mut tally)).close();
var traited := tally { n = 1 };
val by_trait: &mut owning = &mut traited;
by_trait.close();
t.expects(tallies_closed == 3 && shut.n == 0 && pointed.n == 0 && traited.n == 0);
}
# A condition is the `bool` a reference refers to, and a ternary's branches are the
# values theirs refer to where only one of them is a reference.
def read_through(flag: &mut bool, by: &u64): u64 {
var total: u64 = 0;
if flag {
total += 1;
}
while flag {
flag = false;
total += 10;
}
total += flag ? 0 : 100;
return total + (flag ? 0 : by);
}
@test def conditions_read_through_references(t: &mut tester): void {
var flag := true;
val by: u64 = 1000;
t.expects(read_through(&mut flag, &by) == 1111 && !flag);
# `_` names nothing, so a test naming it stands anywhere a `bool` does.
val held: maybe<u64> = 4;
val empty: maybe<u64> = nothing {};
t.expects(held is some(_) && !(empty is some(_)) && (empty is some(_) || held.some == 4));
}
@test def references_compare_what_they_refer_to(t: &mut tester): void {
var one := mark { n = 1 };
var two := mark { n = 2 };
var also := mark { n = 2 };
t.expects(larger(&one, &two).n == 2 && larger(&two, &one).n == 2);
t.expects(same(&two, &also) && !same(&one, &two));
var x: i32 = 4;
val by_reference: &i32 = &x;
t.expects(larger(by_reference, &x) == 4 && hash(&by_reference) == hash(&x));
# Keys and elements that are references are found by what they refer to.
marks_hashed = 0;
var counts := dict::<&mark, u64>::new();
counts.set(&two, 5);
t.expects(counts[&also] == 5 && counts.length == 1 && marks_hashed > 0);
var refs := vector::<&mark>::new();
refs.append(&one);
refs.append(&two);
val wanted: &mark = &also;
t.expects(refs.span.contains(&wanted));
}
@test def references_to_what_a_call_gives_are_what_it_gives(t: &mut tester): void {
# `&` of a call giving a reference is that reference, not one to a copy of it,
# so it is the element's address and writes reach the element.
var marks := vector::<mark>::new();
marks.append(mark { n = 1 });
val read := &marks.at(0);
t.expects((read as *mark) == (&marks.at(0)) as *mark && read.n == 1);
val written := &mut marks.at(0);
written.n = 7;
t.expects(marks.at(0).n == 7 && (written as *mark) == marks.span.data);
}
@test def pointers_compare_their_addresses(t: &mut tester): void {
var two := mark { n = 2 };
var also := mark { n = 2 };
val p := (&two) as *mark;
val q := (&also) as *mark;
t.expects(p != q && same(p, p) && !same(p, q));
t.expects(p.equals(&p) && !p.equals(&q) && p.compare(&p) == 0);
# Hashed as addresses, two pointers at equal marks are two keys, and no mark is hashed.
marks_hashed = 0;
var seen := dict::<*mark, u64>::new();
seen.set(p, 1);
seen.set(q, 2);
t.expects(seen.length == 2 && seen[p] == 1 && seen[q] == 2);
t.expects(p.hash() == hash(&p) && marks_hashed == 0);
# What they point at is still compared through them.
t.expects(p[0] == q[0] && (*p).n == (*q).n);
val held: &*mark = &p;
t.expects(held.equals(&p) && held.hash() == p.hash() && (*held).n == 2 && marks_hashed == 0);
# A type parameter bounded by `pointer` moves, subtracts, and starts at `null` as
# pointers do, however deep.
var xs := [1u, 2u, 3u, 4u];
val first := (&mut xs) as *mut u64;
t.expects(elements_between(first, first + 3) == 3 && no_pointer::<*mark>() == null);
t.expects(addressed::<**mark>() == "pointer" && addressed::<&mark>() == "not" && addressed::<u64>() == "not");
# What a pointer points at is reached by dereferencing it, even a field named as one
# of the pointer's own methods.
var s := slot { hash = 9 };
val at := (&mut s) as *mut slot;
t.expects((*at).hash == 9 && through(at).hash == 9 && at.hash() == hash(&at));
}
@test def containers_hold_references_and_pointers(t: &mut tester): void {
# A container moves what it is given, which for a reference is the reference
# itself, and closes none of what its references reach.
tallies_closed = 0;
var a := tally { n = 1 };
var b := tally { n = 2 };
{
var refs := vector::<&mut tally>::new();
refs.append(&mut a);
refs.append(&mut b);
refs[1].put(18);
refs.remove_at(0);
t.expects(refs.length == 1 && refs[0].n == 20);
var pointers := stack::<*mut tally>::new();
pointers.push((&mut a) as *mut tally);
(*pointers.peek()).put(9);
var named := dict::<u64, &tally>::new();
named.set(1, &b);
t.expects(named[1].size() == 20);
}
t.expects(a.n == 10);
t.expects(b.n == 20);
t.expects(tallies_closed == 0);
}
@test def what_a_pointer_points_at_is_reached_by_dereferencing_it(t: &mut tester): void {
var a := tally { n = 1 };
var p := (&mut a) as *mut tally;
(*p).put_twice(1);
through(p).put(1);
# A reference to a pointer reads through to the pointer, which is dereferenced.
val r: &mut *mut tally = &mut p;
(*r).put(5);
through(r).put_twice(1);
p[0].put(1);
t.expects(a.n == 12 && (*r).size() == 12 && r == (&mut a) as *mut tally);
}
@test def generics_assign_references_as_their_templates_say(t: &mut tester): void {
var a: u64 = 1;
var b: u64 = 2;
var r: &mut u64 = &mut a;
# References never stack, so `&mut r` is `r`, and the slot is `a`.
put_in(&mut r, 5);
t.expects(a == 5 && b == 2);
var n: u64 = 0;
put_in(&mut n, 4);
t.expects(n == 4);
# Where `T` is a reference, the template's assignment gives the slot the
# reference, as it gives any other value.
var slots := vector::<&mut u64>::new();
slots.append(&mut a);
put_in(&mut slots[0], &mut b);
b = 9;
t.expects(slots[0] == 9 && a == 5);
# A reference written as one points a variable elsewhere, and a value given to
# a reference is written to what it refers to.
r = &mut b;
r = 7;
t.expects(a == 5 && b == 7);
}