168 lines
7.2 KiB
Plaintext
168 lines
7.2 KiB
Plaintext
# Tests of the properties and methods of `maybe` and `result`: reading what they
|
|||
|
|
# hold, by a copy where they are only read and by a move where they can be
|
||
|
|
# written, making others of them, and comparing, hashing, and copying them. A
|
||
|
|
# value that closes is closed once, whichever way it leaves.
|
||
|
|
|
||
|
|
import std::testing;
|
||
|
|
|
||
|
|
|
||
|
|
# How many tokens holding a mark have been closed.
|
||
|
|
var tokens_closed: u64 = 0;
|
||
|
|
|
||
|
|
struct token : owning, copyable {
|
||
|
|
var mark: u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
def token::close(&mut this): void {
|
||
|
|
if this.mark != 0 {
|
||
|
|
tokens_closed++;
|
||
|
|
this.mark = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
def token::copy_of(other: &token): token {
|
||
|
|
return token { mark = other.mark };
|
||
|
|
}
|
||
|
|
|
||
|
|
# Half a number, where it is even.
|
||
|
|
def halved(n: i64): maybe<i64> {
|
||
|
|
return n % 2 == 0 ? something(n / 2) : nothing {};
|
||
|
|
}
|
||
|
|
|
||
|
|
# How long text is, or that it is empty.
|
||
|
|
def measured(text: strview): result<u64, strview> {
|
||
|
|
return text.length == 0 ? result::<u64, strview>::err("empty") : result::<u64, strview>::ok(text.length);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def a_maybe_reads_what_it_holds(t: &mut tester): void {
|
||
|
|
val held: maybe<i64> = 8;
|
||
|
|
val empty: maybe<i64> = nothing {};
|
||
|
|
t.expects(held.is_some && !held.is_none && empty.is_none && !empty.is_some);
|
||
|
|
t.expects(held.unwrap() == 8 && held.expect("held") == 8 && empty.unwrap_or(3) == 3 && held.unwrap_or(3) == 8);
|
||
|
|
t.expects(held.is_some_and(def(x: &i64) => x > 5) && !empty.is_some_and(def(x: &i64) => true));
|
||
|
|
|
||
|
|
# Plain data is copied out of what can be written, as `move` copies it.
|
||
|
|
var kept: maybe<i64> = 6;
|
||
|
|
t.expects(kept.unwrap() == 6 && kept.is_some && kept.unwrap_or(1) == 6);
|
||
|
|
var unset: maybe<i64> = nothing {};
|
||
|
|
t.expects(unset.unwrap_or_else(def(): i64 => 11) == 11);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def a_maybe_makes_others(t: &mut tester): void {
|
||
|
|
var six: maybe<i64> = 6;
|
||
|
|
t.expects(six.map(def(x: i64) => x * 10) == 60 && six.and_then(halved) == 3);
|
||
|
|
t.expects(six.and_then(halved).and_then(halved).is_none);
|
||
|
|
t.expects(six.filter(def(x: &i64) => x > 10).is_none && six.filter(def(x: &i64) => x > 1) == 6);
|
||
|
|
var empty: maybe<i64> = nothing {};
|
||
|
|
t.expects(empty.or(5) == 5 && six.or(5) == 6 && empty.or_else(def(): maybe<i64> => 7) == 7);
|
||
|
|
t.expects(six.ok_or::<strview>("none").unwrap() == 6 && empty.ok_or::<strview>("none").unwrap_err() == "none");
|
||
|
|
t.expects(six.as_ref().map(def(x: &i64) => x + 1) == 7 && empty.as_ref().is_none);
|
||
|
|
if six.as_mut() is some(&mut got) {
|
||
|
|
got += 1;
|
||
|
|
}
|
||
|
|
t.expects(six == 7);
|
||
|
|
|
||
|
|
# `take` and `replace` leave it holding nothing, or the new value.
|
||
|
|
t.expects(six.take() == 7 && six.is_none && six.replace(4).is_none && six == 4);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def a_maybe_compares_hashes_and_copies(t: &mut tester): void {
|
||
|
|
val a: maybe<i64> = 5;
|
||
|
|
val b: maybe<i64> = 5;
|
||
|
|
val c: maybe<i64> = 6;
|
||
|
|
val none: maybe<i64> = nothing {};
|
||
|
|
t.expects(a == b && a != c && a != none && none == maybe::<i64> { none = nothing {} } && a == 5);
|
||
|
|
t.expects(hash(&a) == hash(&b) && hash(&a) != hash(&none));
|
||
|
|
val text: maybe<string> = string::from("held", nothing {});
|
||
|
|
val copied := copy(&text);
|
||
|
|
t.expects(copied == text && copied.as_ref().map(def(s: &string) => s.length) == 4);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def a_maybe_moves_out_what_closes(t: &mut tester): void {
|
||
|
|
val before := tokens_closed;
|
||
|
|
{
|
||
|
|
# Moved out of what can be written, leaving it holding nothing.
|
||
|
|
var held: maybe<token> = token { mark = 1 };
|
||
|
|
var taken := held.unwrap();
|
||
|
|
t.expects(taken.mark == 1 && held.is_none && tokens_closed == before);
|
||
|
|
|
||
|
|
# Moved into what a function makes, which closes it there.
|
||
|
|
var given: maybe<token> = token { mark = 2 };
|
||
|
|
t.expects(given.map(def(x: token) => x.mark) == 2 && given.is_none && tokens_closed == before + 1);
|
||
|
|
|
||
|
|
# Left behind by `filter`, `or`, and `replace`, each closed once.
|
||
|
|
var failed: maybe<token> = token { mark = 3 };
|
||
|
|
t.expects(failed.filter(def(x: &token) => false).is_none && tokens_closed == before + 2);
|
||
|
|
var first: maybe<token> = token { mark = 4 };
|
||
|
|
var other := first.or(token { mark = 5 });
|
||
|
|
t.expects(other.as_ref().map(def(x: &token) => x.mark) == 4 && tokens_closed == before + 3);
|
||
|
|
var swapped: maybe<token> = token { mark = 6 };
|
||
|
|
var old := swapped.replace(token { mark = 7 });
|
||
|
|
t.expects(old.is_some && swapped.as_ref().is_some_and(def(x) => x.mark == 7));
|
||
|
|
|
||
|
|
# A box held by a `maybe` in one word is taken whole.
|
||
|
|
var boxed: maybe<box<token>> = box::<token>::new(token { mark = 8 });
|
||
|
|
var out := boxed.take();
|
||
|
|
t.expects(boxed.is_none && out.is_some && size_of::<maybe<box<token>>>() == 8);
|
||
|
|
}
|
||
|
|
# taken, other, old, swapped, and out's are closed at the end of the block.
|
||
|
|
t.expects(tokens_closed == before + 8);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def a_result_reads_what_it_holds(t: &mut tester): void {
|
||
|
|
val good := measured("four");
|
||
|
|
val bad := measured("");
|
||
|
|
t.expects(good.is_ok && !good.is_err && bad.is_err && !bad.is_ok);
|
||
|
|
t.expects(good.unwrap() == 4 && good.expect("good") == 4 && bad.unwrap_err() == "empty" && bad.expect_err("bad") == "empty");
|
||
|
|
t.expects(bad.unwrap_or(9) == 9 && good.unwrap_or(9) == 4);
|
||
|
|
t.expects(good.is_ok_and(def(x: &u64) => x == 4) && bad.is_err_and(def(e: &strview) => e.length == 5));
|
||
|
|
var failed := measured("");
|
||
|
|
t.expects(failed.unwrap_or_else(def(e: strview) => e.length) == 5);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def a_result_makes_others(t: &mut tester): void {
|
||
|
|
var two := measured("ab");
|
||
|
|
var none := measured("");
|
||
|
|
t.expects(two.map(def(x: u64) => x + 1).unwrap() == 3 && none.map(def(x: u64) => x + 1).unwrap_err() == "empty");
|
||
|
|
t.expects(none.map_err(def(e: strview) => e.length).unwrap_err() == 5 && two.map_err(def(e: strview) => e.length).unwrap() == 2);
|
||
|
|
t.expects(two.and_then(def(x: u64): result<u64, strview> => result::<u64, strview>::err("no")).unwrap_err() == "no");
|
||
|
|
t.expects(none.or_else(def(e: strview): result<u64, i64> => result::<u64, i64>::ok(7)).unwrap() == 7);
|
||
|
|
t.expects(two.to_maybe() == 2 && none.to_maybe().is_none && none.err_to_maybe() == "empty" && two.err_to_maybe().is_none);
|
||
|
|
t.expects(two.as_ref().is_ok_and(def(x) => x == 2) && none.as_ref().is_err);
|
||
|
|
|
||
|
|
# A value and an error of one type are told apart.
|
||
|
|
var same := result::<i64, i64>::err(5);
|
||
|
|
t.expects(same.is_err && same.map(def(x: i64) => x + 100).unwrap_err() == 5);
|
||
|
|
t.expects(result::<i64, i64>::ok(5) != result::<i64, i64>::err(5));
|
||
|
|
|
||
|
|
# A result of `void` says only whether it failed.
|
||
|
|
val done := result::<void, i64>::ok(void {});
|
||
|
|
done.unwrap();
|
||
|
|
t.expects(done.is_ok);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def a_result_compares_hashes_and_copies(t: &mut tester): void {
|
||
|
|
val a := measured("abc");
|
||
|
|
val b := measured("xyz");
|
||
|
|
val c := measured("");
|
||
|
|
t.expects(a == b && a != c && c == measured(""));
|
||
|
|
t.expects(hash(&a) == hash(&b) && hash(&a) != hash(&c));
|
||
|
|
val text := result::<string, u64>::ok(string::from("held", nothing {}));
|
||
|
|
val copied := copy(&text);
|
||
|
|
t.expects(copied == text && copied.as_ref().is_ok_and(def(s) => s.length == 4));
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def a_result_moves_out_what_closes(t: &mut tester): void {
|
||
|
|
val before := tokens_closed;
|
||
|
|
{
|
||
|
|
var held := result::<token, u64>::ok(token { mark = 1 });
|
||
|
|
var taken := held.unwrap();
|
||
|
|
t.expects(taken.mark == 1 && tokens_closed == before);
|
||
|
|
|
||
|
|
var failed := result::<u64, token>::err(token { mark = 2 });
|
||
|
|
t.expects(failed.map_err(def(x: token) => x.mark).unwrap_err() == 2 && tokens_closed == before + 1);
|
||
|
|
var lost := result::<token, u64>::ok(token { mark = 3 });
|
||
|
|
t.expects(lost.err_to_maybe().is_none && tokens_closed == before + 2);
|
||
|
|
}
|
||
|
|
t.expects(tokens_closed == before + 3);
|
||
|
|
}
|