Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-12-06 13:52:57 -06:00
commit 528ccf1523
782 files changed
+145156

No files matched your search

+235
View File
@@ -0,0 +1,235 @@
# Tests of `function`, which holds one callable of a signature, whatever its type,
# and owns it: each kind of callable it holds, in its buffer or a box, signatures of
# every shape, closing what it holds exactly once, holding nothing, lending what it
# holds by its type, and being handed to another thread.
import std::testing;
import std::thread;
# How many callables the tests gave functions have been closed, from any thread.
var callables_closed: atomic<u64>;
# A functor of two words counting its calls, closed once where it holds a mark.
struct call_counter : invokable<def(i64): i64>, owning {
var mark: u64;
var calls: u64;
}
def call_counter::invoke(&mut this, x: i64): i64 {
this.calls++;
return x + this.calls as i64;
}
def call_counter::close(&mut this): void {
if this.mark != 0 {
_ = callables_closed.fetch_add(1);
this.mark = 0;
}
}
# Functors of 8, 24 and 25 bytes, the last too large for a function's buffer.
struct one_word : invokable<def(): u64> { var word: u64; }
def one_word::invoke(&mut this): u64 { return this.word; }
struct three_words : invokable<def(): u64> { var bytes: u8[24]; }
def three_words::invoke(&mut this): u64 { return this.bytes[0] as u64 + this.bytes[23] as u64; }
struct past_three_words : invokable<def(): u64> { var bytes: u8[25]; }
def past_three_words::invoke(&mut this): u64 { return this.bytes[0] as u64 + this.bytes[24] as u64; }
# A functor in a box, closed once where it holds a mark, as a large one held is.
struct large_counter : invokable<def(): u64>, owning {
var mark: u64;
var calls: u64;
var pad: u64[4];
}
def large_counter::invoke(&mut this): u64 {
this.calls++;
return this.calls;
}
def large_counter::close(&mut this): void {
if this.mark != 0 {
_ = callables_closed.fetch_add(1);
this.mark = 0;
}
}
struct held_point { var x: f64; var y: f64; }
def held_point::sum(&this): f64 { return this.x + this.y; }
def held_point::origin(): held_point { return held_point {}; }
def twice_given(x: i64): i64 { return x * 2; }
def added_up(xs: i64...): i64 { var total: i64 = 0; for x in xs { total += x; } return total; }
def nothing_given(): u64 { return 7; }
def four_given(a: u8, b: u16, c: u32, d: u64): u64 { return a as u64 + b as u64 + c as u64 + d; }
def larger_of(a: &i64, b: &i64): &i64 {
val x: i64 = a;
val y: i64 = b;
return x > y ? a : b;
}
# A lambda using no locals, which may outlive the function making it.
def lambda_using_none(): function<def(i64): i64> {
return function::new(def(x: i64): i64 => x - 1);
}
@test def functions_are_four_words_holding_any_callable(t: &mut tester): void {
t.expects(size_of::<function<def(i64): i64>>() == 32 && size_of::<function<def(): void>>() == 32);
val named: function<def(i64): i64> = function::new(twice_given);
t.expects(named(21) == 42);
# A method taken through its type takes its receiver first.
val method: function<def(&held_point): f64> = function::new(held_point::sum);
val p := held_point { x = 1.5, y = 2.0 };
t.expects(method(&p) == 3.5);
val made: function<def(): held_point> = function::new(held_point::origin);
t.expects(made().x == 0.0);
# A lambda refers to the locals it uses, which change under it.
var by := 10;
val lambda: function<def(i64): i64> = function::new(def(x: i64): i64 => x + by);
t.expects(lambda(1) == 11);
by = 20;
t.expects(lambda(1) == 21);
val kept := lambda_using_none();
t.expects(kept(5) == 4);
}
@test def functions_hold_small_functors_in_place_and_large_ones_in_a_box(t: &mut tester): void {
var bytes: u8[25];
bytes[0] = 1;
bytes[23] = 2;
bytes[24] = 3;
val one: function<def(): u64> = function::new(one_word { word = 9 });
var three: three_words;
three.bytes[0] = 1;
three.bytes[23] = 2;
val in_place: function<def(): u64> = function::new(three);
val boxed: function<def(): u64> = function::new(past_three_words { bytes = bytes });
t.expects(one() == 9 && in_place() == 3 && boxed() == 4);
# What a functor changes as it is called stays changed, in place or boxed.
val counting: function<def(i64): i64> = function::new(call_counter {});
t.expects(counting(10) == 11 && counting(10) == 12);
val large: function<def(): u64> = function::new(large_counter {});
t.expects(large() == 1 && large() == 2 && large() == 3);
}
@test def functions_call_a_functor_referred_to_where_it_is(t: &mut tester): void {
var counter := call_counter {};
{
val referring: function<def(i64): i64> = function::new(&mut counter);
t.expects(referring(0) == 1 && referring(0) == 2);
}
t.expects(counter.calls == 2);
}
@test def functions_take_signatures_of_every_shape(t: &mut tester): void {
val none: function<def(): u64> = function::new(nothing_given);
val four: function<def(u8, u16, u32, u64): u64> = function::new(four_given);
t.expects(none() == 7 && four(1, 2, 3, 4) == 10);
# References are given and given back.
val larger: function<def(&i64, &i64): &i64> = function::new(larger_of);
val a: i64 = 3;
val b: i64 = 8;
val got: i64 = larger(&a, &b);
t.expects(got == 8);
# What gives nothing, which a lambda appending to a vector does.
var seen := vector::<i64>::new();
val keep: function<def(i64): void> = function::new(def(x: i64) { seen.append(x); });
keep(4);
keep(5);
t.expects(seen.length == 2 && seen[1] == 5);
# A variadic signature takes any number of arguments, or a view of them.
val sum: function<def(i64...): i64> = function::new(added_up);
val more: i64[3] = [4, 5, 6];
t.expects(sum() == 0 && sum(1, 2, 3) == 6 && sum(...more) == 15);
}
@test def functions_close_what_they_hold_once(t: &mut tester): void {
val before := callables_closed.load();
{
val f: function<def(i64): i64> = function::new(call_counter { mark = 1 });
val g: function<def(): u64> = function::new(large_counter { mark = 1 });
}
t.expects(callables_closed.load() == before + 2);
# Written over, what it held closes; moved from, it holds nothing to close.
var f: function<def(i64): i64> = function::new(call_counter { mark = 1 });
f = function::new(call_counter { mark = 2 });
t.expects(callables_closed.load() == before + 3);
var moved := move(&mut f);
f.close();
t.expects(callables_closed.load() == before + 3);
moved.close();
moved.close();
t.expects(callables_closed.load() == before + 4);
# A vector of them grows by moving them, and closes each once.
{
var many := vector::<function<def(): u64>>::new();
for i in 0..40u {
many.append(function::new(large_counter { mark = i + 1 }));
}
t.expects(many[39]() == 1);
}
t.expects(callables_closed.load() == before + 44);
}
@test def empty_functions_hold_nothing_and_stop_where_called(t: &mut tester): void {
var empty: function<def(): void>;
t.expects(!empty.holds());
empty = function::new(def() {});
t.expects(empty.holds());
val moved := move(&mut empty);
t.expects(!empty.holds() && moved.holds());
t.expects(stops(def() { var none: function<def(i64): i64>; _ = none(1); }));
}
@test def functions_lend_what_they_hold_by_its_type(t: &mut tester): void {
val counting: function<def(i64): i64> = function::new(call_counter { calls = 5 });
_ = counting(0);
if counting.get::<call_counter>() is some(&held) {
t.expects(held.calls == 6);
} else {
t.expects(false);
}
t.expects(!(counting.get::<def(i64): i64>() is some));
val large: function<def(): u64> = function::new(large_counter {});
_ = large();
if large.get::<large_counter>() is some(&held) {
t.expects(held.calls == 1);
} else {
t.expects(false);
}
t.expects(!(large.get::<one_word>() is some));
var empty: function<def(): u64>;
t.expects(!(empty.get::<large_counter>() is some));
}
# Calls a function given to a thread, which then owns it.
def call_given(f: function<def(): u64>): void {
_ = f();
_ = f();
}
@test def functions_are_handed_to_other_threads(t: &mut tester): void {
val before := callables_closed.load();
var counting: function<def(): u64> = function::new(large_counter { mark = 1 });
var thread := thread::spawn_with(call_given, move(&mut counting)) else {
t.expects(false);
return;
};
thread.join();
t.expects(!counting.holds() && callables_closed.load() == before + 1);
}