# Tests of operators as calls of the methods that give them: each alone, between # operands of different types, through compound assignment, and in generic code by # the traits of std and those a program composes of them. import std::testing; # A length of time, which adds to another, scales by a number, and divides into a ratio. struct stretch : additive, ordered { var ns: i64; } def stretch::equals(&this, other: &stretch): bool { return this.ns == other.ns; } def stretch::compare(&this, other: &stretch): i64 { return this.ns - other.ns; } def stretch::add(&this, other: &stretch): stretch { return stretch { ns = this.ns + other.ns }; } def stretch::sub(&this, other: &stretch): stretch { return stretch { ns = this.ns - other.ns }; } def stretch::negate(&this): stretch { return stretch { ns = -this.ns }; } def stretch::mul(&this, by: &i64): stretch { return stretch { ns = this.ns * by }; } def stretch::mul(&this, by: &f64): stretch { return stretch { ns = (this.ns as f64 * by) as i64 }; } def stretch::div(&this, by: &i64): stretch { return stretch { ns = this.ns / by }; } def stretch::div(&this, by: &stretch): f64 { return this.ns as f64 / by.ns as f64; } # A moment, which moves by a length of time, and is a length of time from another. struct moment : ordered { var ns: i64; } def moment::equals(&this, other: &moment): bool { return this.ns == other.ns; } def moment::compare(&this, other: &moment): i64 { return this.ns - other.ns; } def moment::add(&this, by: &stretch): moment { return moment { ns = this.ns + by.ns }; } def moment::sub(&this, by: &stretch): moment { return moment { ns = this.ns - by.ns }; } def moment::sub(&this, since: &moment): stretch { return stretch { ns = this.ns - since.ns }; } # A vector, which a number scales from either side. struct arrow : additive { var x: f64; var y: f64; } def arrow::equals(&this, other: &arrow): bool { return this.x == other.x && this.y == other.y; } def arrow::add(&this, other: &arrow): arrow { return arrow { x = this.x + other.x, y = this.y + other.y }; } def arrow::sub(&this, other: &arrow): arrow { return arrow { x = this.x - other.x, y = this.y - other.y }; } def arrow::negate(&this): arrow { return arrow { x = -this.x, y = -this.y }; } def arrow::mul(&this, by: &f64): arrow { return arrow { x = this.x * by, y = this.y * by }; } def arrow::mul(&this, with: &arrow): f64 { return this.x * with.x + this.y * with.y; } def f64::mul(&this, of: &arrow): arrow { return of * this; } # How often a ledger was added to, which only adds. var ledgers_added: u64 = 0; struct ledger { var sum: i64; } def ledger::add(&this, other: &ledger): ledger { ledgers_added++; return ledger { sum = this.sum + other.sum }; } # What holds a ledger behind a property. struct purse { var kept: ledger; } var purse::all: ledger { get => this.kept; set => this.kept = value; } # What scales by an `S`, beside adding: a trait of a program's own, composing std's. trait stretches : additive { def mul(&this, by: &S): self; } def blend(a: V, b: V, t: S): V where V: stretches { return a + (b - a) * t; } def gathered(items: span, from: T): T where T: addable { var all := from; for &each in items { all += each; } return all; } def mirrored(a: T, b: T): T where T: additive { return a == b ? a : -(a - b); } def product_of(a: T, b: T): T where T: multiplicative { return a * b; } def shared_out(whole: T, by: T): T where T: divisible, modular, addable { return whole / by + whole % by; } def second(count: i64): stretch { return stretch { ns = count * 1000000000 }; } const long_pause: stretch = second(1) + second(2) * 3 - -second(1); @test def an_operator_is_a_call_of_its_method(t: &mut tester): void { val a := stretch { ns = 250 }; val b := stretch { ns = 100 }; t.expects((a + b).ns == 350 && (a - b).ns == 150 && (-a).ns == -250); t.expects(a + b == b + a && a - b > b - a && a + b - b == a); # One with only `add` has `+`, and nothing else. ledgers_added = 0; val one := ledger { sum = 1 }; t.expects((one + one + one).sum == 3 && ledgers_added == 2); } @test def operands_and_results_are_what_the_method_declares(t: &mut tester): void { val began := moment { ns = 100 }; val now := moment { ns = 350 }; val waited: stretch = now - began; val due: moment = began + waited * 2; t.expects(waited.ns == 250 && due.ns == 600 && (due - waited).ns == 350); # A constant takes the type of the parameter it is given to, and of two # overloads the one it is alone. t.expects((waited * 2).ns == 500 && (waited * 0.5).ns == 125 && (waited / 5).ns == 50); val count: u8 = 3; t.expects((waited * count as i64).ns == 750); # A result of a third type. val ratio: f64 = (waited * 3) / waited; t.expects(ratio == 3.0 && arrow { x = 1.0, y = 2.0 } * arrow { x = 3.0, y = 4.0 } == 11.0); # References are read through, on either side. val later: &moment = &due; val by: &stretch = &waited; t.expects((later - by).ns == 350 && (later - began).ns == 500 && (by + by).ns == 500); } @test def a_number_on_the_left_asks_its_own_type(t: &mut tester): void { val v := arrow { x = 1.5, y = 2.0 }; val a := v * 2.0; val b := 2.0 * v; val scale := 4.0; t.expects(a == b && a.x == 3.0 && (scale * v).y == 8.0 && (0.5 * v * 2.0) == v); # The number is still the number it was: its own methods are beside the program's. t.expects(2.5.mul(&2.0) == 5.0 && 2.5 * 2.0 == 5.0 && scale.mul(&scale) == 16.0); t.expects(2.5 is numeric && 2.5 is multiplicative && product_of(1.5, 4.0) == 6.0); } @test def compound_assignment_writes_what_the_method_gives(t: &mut tester): void { var due := moment { ns = 100 }; due += stretch { ns = 50 }; due -= stretch { ns = 20 }; t.expects(due.ns == 130); var waited := stretch { ns = 10 }; waited += waited; waited *= 3; waited /= 4; t.expects(waited.ns == 15); # Through a property, an element, and a reference. ledgers_added = 0; var held := purse { kept = ledger { sum = 2 } }; held.all += ledger { sum = 5 }; var many := [ledger { sum = 1 }, ledger { sum = 10 }]; many[1] += many[0]; val through: &mut ledger = &mut many[0]; through += held.kept; t.expects(held.kept.sum == 7 && many[1].sum == 11 && many[0].sum == 8 && ledgers_added == 3); } @test def generic_code_has_operators_through_its_bounds(t: &mut tester): void { val low := arrow { x = 0.0, y = 0.0 }; val high := arrow { x = 2.0, y = 4.0 }; t.expects(blend(1.0, 3.0, 0.5) == 2.0 && blend(low, high, 0.5) == arrow { x = 1.0, y = 2.0 }); t.expects(gathered([1, 2, 3][0:3], 0) == 6 && gathered([1.5, 2.5][0:2], 0.0) == 4.0); t.expects(gathered([second(1), second(2)][0:2], second(0)) == second(3)); t.expects(mirrored(7, 3) == -4 && mirrored(second(5), second(2)) == second(-3) && mirrored(high, high) == high); t.expects(product_of(3, 4) == 12 && shared_out(17, 5) == 5 && shared_out(7.5, 2.0) == 5.25); # What satisfies which: numbers all of them, and a type what it declares. t.expects(1 is additive && 1 is multiplicative && 1 is numeric && 1.5 is divisible && 1 is modular); t.expects(second(1) is additive && second(1) is addable && !(second(1) is numeric)); t.expects(high is additive && !(high is multiplicative) && !(true is addable) && !(moment { ns = 0 } is addable)); } @test def operators_of_methods_make_constants(t: &mut tester): void { t.expects(long_pause == second(8)); }