Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,440 @@
|
||||
# Tests of iterating what an iterator gives by value: each element is the loop's own,
|
||||
# closed where each pass of the body ends, however the pass ends.
|
||||
import std::containers;
|
||||
import std::testing;
|
||||
|
||||
|
||||
# How many lines holding a number have been closed.
|
||||
var lines_closed: u64 = 0;
|
||||
|
||||
struct line : owning {
|
||||
var n: u64;
|
||||
}
|
||||
|
||||
def line::close(&mut this): void {
|
||||
if this.n != 0 {
|
||||
lines_closed++;
|
||||
this.n = 0;
|
||||
}
|
||||
}
|
||||
|
||||
# Gives lines numbered from how many are left down to 1, each a new value.
|
||||
struct lines : iterator<line> {
|
||||
var left: u64;
|
||||
}
|
||||
|
||||
def lines::next(&mut this): maybe<line> {
|
||||
if this.left == 0 {
|
||||
return nothing {};
|
||||
}
|
||||
this.left--;
|
||||
return line { n = this.left + 1 };
|
||||
}
|
||||
|
||||
# Gives words numbered down to 0, each a new string, as a reader gives its lines.
|
||||
struct words : iterator<string> {
|
||||
var left: u64;
|
||||
}
|
||||
|
||||
def words::next(&mut this): maybe<string> {
|
||||
if this.left == 0 {
|
||||
return nothing {};
|
||||
}
|
||||
this.left--;
|
||||
return $"word {this.left}";
|
||||
}
|
||||
|
||||
# The first line numbered no more than the limit, the rest skipped by `continue`.
|
||||
def first_within(limit: u64): u64 {
|
||||
for each in (lines { left = 5 }) {
|
||||
if each.n > limit {
|
||||
continue;
|
||||
}
|
||||
return each.n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@test def iterators_give_elements_the_loop_owns(t: &mut tester): void {
|
||||
lines_closed = 0;
|
||||
var total: u64 = 0;
|
||||
for each in (lines { left = 4 }) {
|
||||
total += each.n;
|
||||
}
|
||||
t.expects(total == 10 && lines_closed == 4);
|
||||
|
||||
# 5, then 4 by `continue`, then 3, then 2 by `break`, and 1 never given.
|
||||
lines_closed = 0;
|
||||
for each in (lines { left = 5 }) {
|
||||
if each.n == 4 {
|
||||
continue;
|
||||
}
|
||||
if each.n == 2 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
t.expects(lines_closed == 4);
|
||||
|
||||
# 5 and 4 by `continue`, and 3 by `return`.
|
||||
lines_closed = 0;
|
||||
t.expects(first_within(3) == 3 && lines_closed == 3);
|
||||
|
||||
lines_closed = 0;
|
||||
for _ in (lines { left = 3 }) {}
|
||||
t.expects(lines_closed == 3);
|
||||
|
||||
# An iterator variable is advanced where it is, so a second loop goes on from there.
|
||||
lines_closed = 0;
|
||||
var walk := lines { left = 4 };
|
||||
for each in walk {
|
||||
if each.n == 3 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
var rest: u64 = 0;
|
||||
for each in walk {
|
||||
rest += each.n;
|
||||
}
|
||||
t.expects(rest == 3 && lines_closed == 4);
|
||||
}
|
||||
|
||||
@test def iterators_give_strings_the_loop_owns(t: &mut tester): void {
|
||||
var joined := string::new(nothing {});
|
||||
for word in (words { left = 3 }) {
|
||||
joined.append(word.view());
|
||||
joined.append(";");
|
||||
}
|
||||
t.expects(joined.view() == "word 2;word 1;word 0;");
|
||||
|
||||
var counted := 0u;
|
||||
for word in (words { left = 100 }) {
|
||||
counted += word.length;
|
||||
}
|
||||
t.expects(counted == 690);
|
||||
}
|
||||
|
||||
@test def loops_refer_to_what_they_walk(t: &mut tester): void {
|
||||
# Elements an array holds, or an iterator gives by reference, are referred to by
|
||||
# `for x`, neither copied nor closed by the loop.
|
||||
lines_closed = 0;
|
||||
var kept: line[2];
|
||||
kept[0] = line { n = 1 };
|
||||
kept[1] = line { n = 2 };
|
||||
var sum: u64 = 0;
|
||||
for &each in kept {
|
||||
sum += each.n;
|
||||
}
|
||||
t.expects(sum == 3 && lines_closed == 0);
|
||||
|
||||
var texts := vector::<string>::new();
|
||||
texts.append(string::from("ab", nothing {}));
|
||||
texts.append(string::from("cde", nothing {}));
|
||||
var length: u64 = 0;
|
||||
for &text in texts.view() {
|
||||
length += text.length;
|
||||
}
|
||||
t.expects(length == 5 && texts.length == 2);
|
||||
}
|
||||
|
||||
# The numbers 1 to 6, which the tests of adapters and consumers walk.
|
||||
def six(): vector<u64> {
|
||||
var made := vector::<u64>::new();
|
||||
for i in 1..=6u {
|
||||
made.append(i);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# The text of what an iterator gives, separated by spaces.
|
||||
def spaced<T, I>(it: I): string where I: iterator<T> {
|
||||
return it.to_string(" ");
|
||||
}
|
||||
|
||||
@test def adapters_make_iterators_of_iterators(t: &mut tester): void {
|
||||
val v := six();
|
||||
t.expects(spaced(v.iter().map(def(x): u64 => x * 10)).view() == "10 20 30 40 50 60");
|
||||
t.expects(spaced(v.iter().filter(def(x) => x % 2 == 0)).view() == "2 4 6");
|
||||
t.expects(spaced(v.iter().filter_map(def(x): maybe<u64> => x % 3 == 0 ? something::<u64>(x / 3) : nothing {})).view() == "1 2");
|
||||
t.expects(spaced(v.iter().enumerate().map(def(p): u64 => p.first * 10 + p.second)).view() == "1 12 23 34 45 56");
|
||||
t.expects(spaced(v.iter().zip(v.iter().skip(1)).map(def(p): u64 => p.first * p.second)).view() == "2 6 12 20 30");
|
||||
t.expects(spaced(v.iter().take(2).chain(v.iter().skip(4))).view() == "1 2 5 6");
|
||||
t.expects(spaced(v.iter().take_while(def(x) => x < 4)).view() == "1 2 3");
|
||||
t.expects(spaced(v.iter().skip_while(def(x) => x < 4)).view() == "4 5 6");
|
||||
t.expects(spaced(v.iter().step(2)).view() == "1 3 5");
|
||||
t.expects(spaced(v.iter().rev()).view() == "6 5 4 3 2 1");
|
||||
t.expects(spaced(v.iter().take(3).flat_map(def(x) => 0..x)).view() == "0 0 1 0 1 2");
|
||||
t.expects(spaced(v.iter().copied().map(def(x: u64): u64 => x + 1)).view() == "2 3 4 5 6 7");
|
||||
|
||||
var seen: u64 = 0;
|
||||
t.expects(v.iter().inspect(def(x) { seen += x; }).count() == 6 && seen == 21);
|
||||
|
||||
var looked := v.iter().peekable();
|
||||
t.expects(looked.peek() is some(first) && first == 1);
|
||||
t.expects(spaced(looked).view() == "1 2 3 4 5 6");
|
||||
}
|
||||
|
||||
@test def adapters_take_the_edges(t: &mut tester): void {
|
||||
val v := six();
|
||||
val none := vector::<u64>::new();
|
||||
t.expects(none.iter().map(def(x): u64 => x).count() == 0 && none.iter().rev().count() == 0);
|
||||
t.expects(v.iter().take(0).count() == 0 && v.iter().skip(9).count() == 0);
|
||||
t.expects(spaced(v.iter().step(1)).view() == "1 2 3 4 5 6");
|
||||
t.expects(v.iter().zip(v.iter().take(2)).count() == 2 && v.iter().take(1).zip(v.iter()).count() == 1);
|
||||
|
||||
# Walking from both ends gives each element once, until the ends meet.
|
||||
var both := v.iter();
|
||||
var taken := string::new(nothing {});
|
||||
while true {
|
||||
val front := both.next();
|
||||
if front is some(a) {
|
||||
taken.append($"{a}<");
|
||||
}
|
||||
val back := both.next_back();
|
||||
if back is some(b) {
|
||||
taken.append($">{b}");
|
||||
}
|
||||
break if both.remaining() == 0;
|
||||
}
|
||||
t.expects(taken.view() == "1<>62<>53<>4");
|
||||
t.expects(both.next() is not some(_) && both.next_back() is not some(_));
|
||||
}
|
||||
|
||||
@test def consumers_walk_to_the_end(t: &mut tester): void {
|
||||
val v := six();
|
||||
t.expects(v.iter().count() == 6 && v.iter().copied().sum() == 21 && v.iter().copied().product() == 720);
|
||||
t.expects(v.iter().fold(100u, def(a: u64, x: &u64): u64 => a - x) == 79);
|
||||
t.expects((v.iter().copied().reduce(def(a: u64, b: u64): u64 => a * b) else 0) == 720);
|
||||
t.expects(v.iter().any(def(x) => x > 5) && !v.iter().any(def(x) => x > 6));
|
||||
t.expects(v.iter().all(def(x) => x > 0) && !v.iter().all(def(x) => x > 1));
|
||||
t.expects((v.iter().copied().find(def(x) => x > 3) else 0) == 4);
|
||||
t.expects((v.iter().position(def(x) => x > 3) else 99) == 3 && v.iter().position(def(x) => x > 9) is not some(_));
|
||||
t.expects((v.iter().copied().min() else 0) == 1 && (v.iter().copied().max() else 0) == 6);
|
||||
t.expects((v.iter().copied().min_by(def(a, b) => a > b) else 0) == 6);
|
||||
t.expects((v.iter().copied().max_by_key(def(x): u64 => (x * 7) % 5) else 0) == 2);
|
||||
t.expects((v.iter().copied().min_by_key(def(x): u64 => (x * 7) % 5) else 0) == 5);
|
||||
t.expects((v.iter().copied().last() else 0) == 6 && (v.iter().copied().nth(2) else 0) == 3);
|
||||
t.expects(v.iter().nth(6) is not some(_));
|
||||
|
||||
val none := vector::<u64>::new();
|
||||
t.expects(none.iter().copied().sum() == 0 && none.iter().copied().min() is not some(_));
|
||||
t.expects(none.iter().copied().reduce(def(a: u64, b: u64): u64 => a + b) is not some(_));
|
||||
|
||||
val doubled := v.iter().map(def(x): u64 => x * 2).to_vector();
|
||||
t.expects(doubled.length == 6 && doubled[5] == 12);
|
||||
|
||||
var into := vector::<u64>::new();
|
||||
into.append(0);
|
||||
v.iter().copied().into(&mut into);
|
||||
t.expects(into.length == 7 && into[6] == 6);
|
||||
|
||||
var walked: u64 = 0;
|
||||
v.iter().for_each(def(x) { walked += x; });
|
||||
t.expects(walked == 21);
|
||||
}
|
||||
|
||||
# How many `ticket`s holding a number have been closed.
|
||||
var tickets_closed: u64 = 0;
|
||||
|
||||
struct ticket : owning {
|
||||
var n: u64;
|
||||
}
|
||||
|
||||
def ticket::close(&mut this): void {
|
||||
if this.n != 0 {
|
||||
tickets_closed++;
|
||||
this.n = 0;
|
||||
}
|
||||
}
|
||||
|
||||
# Gives tickets numbered from how many are left down to 1, each a new value, and
|
||||
# counts how many times it is closed itself, which one moved from is not.
|
||||
var ticket_sources_closed: u64 = 0;
|
||||
|
||||
struct tickets : owning, iterator<ticket> {
|
||||
var left: u64;
|
||||
var live: bool;
|
||||
}
|
||||
|
||||
def tickets::next(&mut this): maybe<ticket> {
|
||||
return nothing {} if this.left == 0;
|
||||
this.left--;
|
||||
return ticket { n = this.left + 1 };
|
||||
}
|
||||
|
||||
def tickets::close(&mut this): void {
|
||||
if this.live {
|
||||
ticket_sources_closed++;
|
||||
this.live = false;
|
||||
}
|
||||
}
|
||||
|
||||
@test def adapters_close_what_they_own_once(t: &mut tester): void {
|
||||
tickets_closed = 0;
|
||||
ticket_sources_closed = 0;
|
||||
val total := (tickets { left = 4, live = true }).map(def(x: ticket): u64 => x.n).sum();
|
||||
t.expects(total == 10 && tickets_closed == 4 && ticket_sources_closed == 1);
|
||||
|
||||
tickets_closed = 0;
|
||||
ticket_sources_closed = 0;
|
||||
val kept := (tickets { left = 5, live = true }).filter(def(x) => x.n % 2 == 1).take(2).to_vector();
|
||||
t.expects(kept.length == 2 && kept[0].n == 5 && kept[1].n == 3);
|
||||
t.expects(tickets_closed == 1 && ticket_sources_closed == 1);
|
||||
|
||||
tickets_closed = 0;
|
||||
ticket_sources_closed = 0;
|
||||
var found := (tickets { left = 3, live = true }).enumerate().find(def(p) => p.second.n == 2);
|
||||
t.expects(found is some(&p) && p.first == 1);
|
||||
t.expects(tickets_closed == 1 && ticket_sources_closed == 1);
|
||||
}
|
||||
|
||||
type view_cursor = span<u64>::cursor;
|
||||
type view_filter = filtered<&u64, span<u64>::cursor, def(& &u64): bool>;
|
||||
type list_map = mapped<&u64, u64, list<u64>::cursor, def(&u64): u64>;
|
||||
|
||||
# Whether a view's cursor knows how many it has left, which it does.
|
||||
def view_is_sized(): bool { if const view_cursor is sized_iterator<&u64> { return true; } return false; }
|
||||
|
||||
# Whether filtering it knows, which it does not.
|
||||
def filter_is_sized(): bool { if const view_filter is sized_iterator<&u64> { return true; } return false; }
|
||||
|
||||
# Whether mapping a list's cursor walks backward, which it does not.
|
||||
def list_map_is_backward(): bool { if const list_map is backward_iterator<u64> { return true; } return false; }
|
||||
|
||||
@test def adapters_stay_sized_and_backward_as_what_they_adapt(t: &mut tester): void {
|
||||
var v := six();
|
||||
val view := v.view();
|
||||
t.expects(view.iter().map(def(x): u64 => x).remaining() == 6);
|
||||
t.expects(view.iter().zip(view.iter().skip(2)).remaining() == 4);
|
||||
t.expects(view.iter().enumerate().rev().next() is some(p) && p.first == 5 && p.second == 6);
|
||||
t.expects(view.iter().step(4).remaining() == 2 && view.iter().take(10).remaining() == 6);
|
||||
t.expects(view.iter().chain(view.iter()).remaining() == 12);
|
||||
t.expects(view.iter().filter(def(x) => x > 2).rev().next() is some(last) && last == 6);
|
||||
t.expects(view_is_sized() && !filter_is_sized() && !list_map_is_backward());
|
||||
}
|
||||
|
||||
# An enum with values no member has between its members', and one value two
|
||||
# members share.
|
||||
enum shade: i8 { deep = -3, dim, bright = 2, glare, plain = -2 }
|
||||
|
||||
@test def ranges_are_values(t: &mut tester): void {
|
||||
t.expects(spaced(0..5).view() == "0 1 2 3 4" && spaced(0..=5).view() == "0 1 2 3 4 5");
|
||||
t.expects(spaced((0..=5).rev()).view() == "5 4 3 2 1 0" && spaced((0..20).step(5)).view() == "0 5 10 15");
|
||||
t.expects((1..=100).sum() == 5050 && (3..10).length == 7 && (3..=10).length == 8);
|
||||
t.expects((5..2).count() == 0 && (5..=5).count() == 1 && (5..=4).count() == 0);
|
||||
|
||||
val r: range<u8> = 250..=255;
|
||||
t.expects(r.length == 6 && r.contains(255) && !r.contains(249));
|
||||
val first: u8 = 250;
|
||||
val last: u8 = 255;
|
||||
var top := first..=last;
|
||||
var counted: u64 = 0;
|
||||
while top.next() is some(x) {
|
||||
counted++;
|
||||
}
|
||||
t.expects(counted == 6);
|
||||
val low: i8 = -128;
|
||||
val high: i8 = 127;
|
||||
t.expects((low..=high).count() == 256 && (-3..3).sum() == -3);
|
||||
t.expects((0xFFFFFFFFFFFFFFFDu..=0xFFFFFFFFFFFFFFFFu).count() == 3);
|
||||
|
||||
var walk := 0..10u;
|
||||
t.expects(walk.nth(3) is some(x) && x == 3 && walk.next() is some(y) && y == 4);
|
||||
t.expects(walk.nth(9) is not some(_) && walk.is_empty());
|
||||
|
||||
var sum: u64 = 0;
|
||||
for i in 0..=4u {
|
||||
sum += i;
|
||||
}
|
||||
for i in last..=last {
|
||||
sum += i as u64;
|
||||
}
|
||||
t.expects(sum == 265);
|
||||
}
|
||||
|
||||
@test def ranges_count_through_the_members_of_enums(t: &mut tester): void {
|
||||
# Each value once, in order, passing over those no member has.
|
||||
t.expects(spaced(shade::deep..=shade::glare).view() == "deep dim bright glare");
|
||||
t.expects(spaced((shade::deep..shade::glare).rev()).view() == "bright dim deep");
|
||||
t.expects(spaced((shade::deep..=shade::glare).step(2)).view() == "deep bright");
|
||||
val shades := shade::dim..=shade::glare;
|
||||
t.expects(shades.length == 3 && shades.contains(shade::bright) && !shades.contains(shade::deep));
|
||||
t.expects((shade::glare..shade::deep).count() == 0 && (shade::plain..=shade::dim).count() == 1);
|
||||
|
||||
var along := shade::deep..=shade::glare;
|
||||
t.expects(along.nth(2) is some(s) && s == shade::bright && along.remaining() == 1);
|
||||
t.expects(along.next_back() is some(last) && last == shade::glare && along.is_empty());
|
||||
|
||||
var walked := 0;
|
||||
for s in shade::deep..shade::glare {
|
||||
walked = walked * 10 + (s as i64 + 4);
|
||||
}
|
||||
t.expects(walked == 126);
|
||||
}
|
||||
|
||||
@test def text_is_walked_lazily(t: &mut tester): void {
|
||||
val text: strview = "a,,b,c";
|
||||
t.expects(text.split(',').to_string("|").view() == "a||b|c" && text.split(',').rev().to_string("|").view() == "c|b||a");
|
||||
t.expects("".split(',').count() == 1 && ",".split(',').count() == 2);
|
||||
val parts := text.split(',').to_vector();
|
||||
t.expects(parts.length == 4 && parts[3] == "c");
|
||||
|
||||
val doc: strview = "one\r\ntwo\n\nthree\n";
|
||||
t.expects(doc.lines().to_string("|").view() == "one|two||three");
|
||||
t.expects("".lines().count() == 0 && "x".lines().count() == 1 && "\n".lines().count() == 1);
|
||||
t.expects(" hello wide\tworld \n".words().to_string("|").view() == "hello|wide|world" && " \t".words().count() == 0);
|
||||
|
||||
val utf: strview = "aé€😀";
|
||||
t.expects(utf.chars().map(def(c): u64 => c as u64).to_string(" ").view() == "97 233 8364 128512");
|
||||
var bad: u8[6] = [0x61, 0xC3, 0x28, 0xFF, 0xE2, 0x82];
|
||||
val broken: strview = strview::over(&bad[0] as *u8, 6);
|
||||
t.expects(broken.chars().map(def(c): u64 => c as u64).to_string(" ").view() == "97 65533 40 65533 65533 65533");
|
||||
var overlong: u8[2] = [0xC0, 0x80];
|
||||
val long: strview = strview::over(&overlong[0] as *u8, 2);
|
||||
t.expects(long.chars().count() == 2);
|
||||
}
|
||||
|
||||
@test def containers_are_made_of_iterators(t: &mut tester): void {
|
||||
var d := dict::<u64, u64>::new();
|
||||
d.set(1, 10);
|
||||
d.set(2, 20);
|
||||
d.set(3, 30);
|
||||
t.expects(d.keys().copied().sum() == 6 && d.values().copied().sum() == 60 && d.keys().remaining() == 3);
|
||||
|
||||
val squares := dict::from((0..4u).map(def(i: u64): pair<u64, u64> => pair::<u64, u64> { first = i, second = i * i }));
|
||||
t.expects(squares.length == 4 && squares[3] == 9);
|
||||
(5..7u).map(def(i: u64): pair<u64, u64> => pair::<u64, u64> { first = i, second = i }).into(&mut d);
|
||||
t.expects(d.length == 5 && d[6] == 6);
|
||||
|
||||
var v := vector::from((0..10u).filter(def(x) => x % 3 != 0));
|
||||
t.expects(v.iter().to_string(" ").view() == "1 2 4 5 7 8");
|
||||
val taken := v.drain(2..5).to_vector();
|
||||
t.expects(taken.iter().to_string(" ").view() == "4 5 7" && v.iter().to_string(" ").view() == "1 2 8");
|
||||
|
||||
tickets_closed = 0;
|
||||
var held := vector::<ticket>::new();
|
||||
for i in 1..=5u {
|
||||
held.append(ticket { n = i });
|
||||
}
|
||||
{
|
||||
var some_of := held.drain(1..=3);
|
||||
t.expects(some_of.remaining() == 3);
|
||||
t.expects(some_of.next_back() is some(x) && x.n == 4);
|
||||
}
|
||||
t.expects(tickets_closed == 3 && held.length == 2 && held[1].n == 5);
|
||||
|
||||
var joined := string::new(nothing {});
|
||||
"a b c".split(' ').into(&mut joined);
|
||||
t.expects(joined.view() == "abc");
|
||||
|
||||
var c := chain::<u64>::new();
|
||||
c.push_back(1);
|
||||
c.push_back(2);
|
||||
c.push_back(3);
|
||||
val read := &c;
|
||||
t.expects(read.iter().rev().copied().to_string(" ").view() == "3 2 1" && read.iter().remaining() == 3);
|
||||
var l := list::<u64>::new();
|
||||
l.push_front(5);
|
||||
l.push_front(6);
|
||||
val lread := &l;
|
||||
t.expects(lread.iter().copied().sum() == 11 && lread.iter().remaining() == 2);
|
||||
}
|
||||
Reference in new issue
Block a user