Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

No files matched your search

+244
View File
@@ -0,0 +1,244 @@
# Tests of generics given constants, and of what calls infer. A constant parameter is
# given a value of its type, worked out while checking, and instances given equal values
# are one instance. Calls infer constants from the lengths of fixed arrays and the
# arguments of other instances, and infer type arguments from bounds, the type expected
# of their result, array literals, functions named as values, and the arguments around
# one that needs another's type first.
import std::testing;
import std::containers;
# A buffer of a number of elements its type gives.
struct buffer<T, const N: u64> where T: zeroable {
var items: T[N];
}
def buffer<T, N>::capacity(&this): u64 { return N; }
def buffer<T, const N: u64>::filled(value: T): buffer<T, N> {
var made: buffer<T, N>;
for i in 0..N { made.items[i] = value; }
return made;
}
const four_of: u64 = 4;
@test def constants_tell_instances_apart(t: &mut tester): void {
var b: buffer<i64, 4>;
val c: buffer<i64, four_of> = buffer::<i64, 2 + 2>::filled(7);
b = c;
t.expects(b.capacity() == 4 && b.items.length == 4 && b.items[3] == 7);
t.expects(name_of::<buffer<u8, 16>>() == "buffer<u8, 16>");
t.expects(name_of::<buffer<u16, four_of * 2>>() == "buffer<u16, 8>");
}
enum color { red, green, blue }
struct spot { var x: i64; var y: i64 = 7; }
union figure { var circle: f64; var square: f64; }
const origin: spot = spot { x = 1 };
const table: i64[3] = [10, 20, 30];
struct labelled<const NAME: strview> {}
def labelled<NAME>::name(&this): strview { return NAME; }
struct painted<const C: color> {}
def painted<C>::color(&this): color { return C; }
struct placed<const P: spot> {}
def placed<P>::sum(&this): i64 { return P.x + P.y; }
struct flagged<const B: bool> {}
def flagged<B>::on(&this): bool {
if const B {
return true;
}
return false;
}
struct scaled<const F: f64> {}
def scaled<F>::times(&this, x: f64): f64 { return x * F; }
struct drawn<const S: figure> {}
def drawn<S>::size(&this): f64 {
if S is circle(r) {
return r;
}
return 0.0;
}
def double(x: i64): i64 { return x * 2; }
def triple(x: i64): i64 { return x * 3; }
struct applied<const F: def(i64): i64> {}
def applied<F>::run(&this, x: i64): i64 { return F(x); }
struct indexed<const XS: i64[3]> {}
def indexed<XS>::second(&this): i64 { return XS[1]; }
struct aimed<const R: &i64[3]> {}
def aimed<R>::last(&this): i64 { return R[2]; }
struct keeper<T, const V: T> {}
def keeper<T, V>::get(&this): T { return V; }
@test def constants_are_values_of_every_kind(t: &mut tester): void {
t.expects(labelled::<"hello"> {}.name() == "hello");
t.expects(painted::<color::green> {}.color() == color::green);
# A struct is the same constant as another holding the same fields, its defaults
# included, however each is written.
val here: placed<spot { x = 1, y = 7 }> = placed::<origin> {};
t.expects(here.sum() == 8);
t.expects(flagged::<true> {}.on() && !flagged::<(1 > 2)> {}.on());
t.expects(scaled::<1.5> {}.times(2.0) == 3.0);
t.expects(drawn::<figure { circle = 2.5 }> {}.size() == 2.5);
t.expects(applied::<double> {}.run(5) == 10 && applied::<triple> {}.run(5) == 15);
t.expects(indexed::<table> {}.second() == 20 && indexed::<[4, 5, 6]> {}.second() == 5);
t.expects(aimed::<&table> {}.last() == 30);
t.expects(keeper::<u8, 200> {}.get() == 200 && keeper::<strview, "x"> {}.get() == "x");
}
def summed<const N: u64>(xs: &i64[N]): i64 {
var sum: i64 = 0;
for x in xs { sum += x; }
return sum;
}
def grown<T, const N: u64>(xs: &T[N], last: T): T[N + 1] where T: zeroable {
var out: T[N + 1];
for i, x in xs { out[i] = x; }
out[N] = last;
return out;
}
def capacity_of<T, const N: u64>(b: &buffer<T, N>): u64 where T: zeroable { return N; }
def widened<T, const N: u64>(b: &buffer<T, N>): buffer<T, N * 2> where T: zeroable {
var out: buffer<T, N * 2>;
for i, x in b.items {
out.items[i] = x;
out.items[i + N] = x;
}
return out;
}
type grid_of<const N: u64> = i64[N][N];
@test def constants_are_inferred_from_what_is_given(t: &mut tester): void {
val xs: i64[3] = [1, 2, 3];
t.expects(summed(&xs) == 6 && summed::<2>(&[5, 6]) == 11);
val ys := grown(&xs, 4);
t.expects(ys.length == 4 && ys[3] == 4);
val b := buffer::<i64, 3>::filled(9);
val w := widened(&b);
t.expects(capacity_of(&b) == 3 && capacity_of(&w) == 6 && w.items[5] == 9);
val sq: grid_of<2> = [[1, 2], [3, 4]];
t.expects(sq[1][0] == 3);
}
def doubled<const N: u64>(): u64 { return N * 2; }
const twelve: u64 = doubled::<6>();
def described<const N: u64>(): strview {
if const N > 2 {
return "many";
}
return "few";
}
@test def constants_are_known_while_compiling(t: &mut tester): void {
t.expects(twelve == 12);
t.expects(described::<1>() == "few" && described::<5>() == "many");
}
# What a container's elements are, as a bound gives it.
def leading<C, T>(c: &C): maybe<T> where C: iterable<T> {
for x in c {
return x;
}
return nothing {};
}
struct wrapped<T> { var value: T; }
def wrapped<T>::new(value: T): wrapped<T> { return wrapped::<T> { value = value }; }
def itself<T>(x: T): T { return x; }
def apply<T, U>(f: def(T): U, x: T): U { return f(x); }
def sum_of<T>(xs: span<T>): T where T: numeric, zeroable {
var all: T;
for x in xs { all += x; }
return all;
}
def made_empty<T>(): vector<T> { return vector::<T>::new(); }
def pick<T>(a: T, b: T): T { return b; }
def latter<T, U>(a: T, b: U): U { return b; }
@test def calls_infer_what_their_arguments_do_not_give(t: &mut tester): void {
var xs := vector::<i64>::new();
xs.append(4);
t.expects(leading(&xs) is some(got) && got == 4);
# A generic type's own static method infers the type's arguments too.
val b := wrapped::new(5);
val c: wrapped<u8> = wrapped::new(5);
var v: vector<i64> = vector::new();
v.append(1);
t.expects(b.value == 5 && c.value == 5 && v.length == 1);
# A generic function labelled as a value is the instance the type expected gives.
val f: def(i64): i64 = itself;
t.expects(f(4) == 4 && apply(itself, 3) == 3 && apply(double, 3) == 6);
t.expects(sum_of([1, 2, 3]) == 6 && sum_of([1.5, 2.5]) == 4.0);
# An argument needing another's type is checked once the other gives it.
var given := vector::<u8>::new();
given.append(1);
val picked := pick(made_empty(), move(given));
t.expects(picked.length == 1);
# Type arguments written are the first ones, and the call infers the rest.
t.expects(latter::<i64>(1, true));
}
# Says only whether a number is even, failing with the number otherwise.
def even_only(n: u64): result<void, u64> {
if n % 2 != 0 { return n; }
return;
}
@test def void_is_given_to_std_generics_as_any_type(t: &mut tester): void {
# Owners, containers, and results hold `void`, its one value taking no bytes.
val held := box::<void>::new(void {});
t.expects(held.holds());
var shared := rc::<void>::new(void {});
val also := copy(&shared);
t.expects(shared.count() == 2);
var marks := vector::<void>::new();
for _ in 0..5u { marks.append(void {}); }
t.expects(marks.length == 5 && size_of::<void>() == 0);
# A `maybe<void>` tells a value missing from the value of `void`.
var seen: maybe<void> = nothing {};
t.expects(!(seen is some));
seen = void {};
t.expects(seen is some);
# A result of `void` says only whether something failed.
t.expects(even_only(4) is value && even_only(3) is error);
var failed: u64 = 0;
even_only(7) else (n) { failed = n; };
t.expects(failed == 7);
# A function giving nothing is held, and called, as one giving any type.
var count := 0;
val f: function<def(): void> = function::new(def() { count++; });
f();
f();
t.expects(count == 2);
}