# 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 where T: zeroable { var items: T[N]; } def buffer::capacity(&this): u64 { return N; } def buffer::filled(value: T): buffer { var made: buffer; 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; val c: buffer = buffer::::filled(7); b = c; t.expects(b.capacity() == 4 && b.items.length == 4 && b.items[3] == 7); t.expects(name_of::>() == "buffer"); t.expects(name_of::>() == "buffer"); } 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 {} def labelled::name(&this): strview { return NAME; } struct painted {} def painted::color(&this): color { return C; } struct placed {} def placed

::sum(&this): i64 { return P.x + P.y; } struct flagged {} def flagged::on(&this): bool { if const B { return true; } return false; } struct scaled {} def scaled::times(&this, x: f64): f64 { return x * F; } struct drawn {} def drawn::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 {} def applied::run(&this, x: i64): i64 { return F(x); } struct indexed {} def indexed::second(&this): i64 { return XS[1]; } struct aimed {} def aimed::last(&this): i64 { return R[2]; } struct keeper {} def keeper::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() == color::green); # A struct is the same constant as another holding the same fields, its defaults # included, however each is written. val here: placed = placed:: {}; t.expects(here.sum() == 8); t.expects(flagged:: {}.on() && !flagged::<(1 > 2)> {}.on()); t.expects(scaled::<1.5> {}.times(2.0) == 3.0); t.expects(drawn::

{}.size() == 2.5); t.expects(applied:: {}.run(5) == 10 && applied:: {}.run(5) == 15); t.expects(indexed:: {}.second() == 20 && indexed::<[4, 5, 6]> {}.second() == 5); t.expects(aimed::<&table> {}.last() == 30); t.expects(keeper:: {}.get() == 200 && keeper:: {}.get() == "x"); } def summed(xs: &i64[N]): i64 { var sum: i64 = 0; for x in xs { sum += x; } return sum; } def grown(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(b: &buffer): u64 where T: zeroable { return N; } def widened(b: &buffer): buffer where T: zeroable { var out: buffer; for i, x in b.items { out.items[i] = x; out.items[i + N] = x; } return out; } type grid_of = 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::::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(): u64 { return N * 2; } const twelve: u64 = doubled::<6>(); def described(): 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: &C): maybe where C: iterable { for x in c { return x; } return nothing {}; } struct wrapped { var value: T; } def wrapped::new(value: T): wrapped { return wrapped:: { value = value }; } def itself(x: T): T { return x; } def apply(f: def(T): U, x: T): U { return f(x); } def sum_of(xs: span): T where T: numeric, zeroable { var all: T; for x in xs { all += x; } return all; } def made_empty(): vector { return vector::::new(); } def pick(a: T, b: T): T { return b; } def latter(a: T, b: U): U { return b; } @test def calls_infer_what_their_arguments_do_not_give(t: &mut tester): void { var xs := vector::::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 = wrapped::new(5); var v: vector = 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::::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::(1, true)); } # Says only whether a number is even, failing with the number otherwise. def even_only(n: u64): result { 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::::new(void {}); t.expects(held.holds()); var shared := rc::::new(void {}); val also := copy(&shared); t.expects(shared.count() == 2); var marks := vector::::new(); for _ in 0..5u { marks.append(void {}); } t.expects(marks.length == 5 && size_of::() == 0); # A `maybe` tells a value missing from the value of `void`. var seen: maybe = 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 = function::new(def() { count++; }); f(); f(); t.expects(count == 2); }