# Tests of the containers: what they check of what they are asked for, what they # close of their elements, and what they do where an allocator gives no memory, # with the helpers of std they lean on. import std::containers; import std::io; import system; import std::testing; # How many counters have been closed, which only those holding a count are. var closed: u64 = 0; struct counter : owning, hashable, ordered { var n: u64; } def counter::close(&mut this): void { if this.n != 0 { closed++; this.n = 0; } } def counter::hash(&this): u64 { return this.n; } def counter::equals(&this, other: &counter): bool { return this.n == other.n; } def counter::compare(&this, other: &counter): i64 { return this.n as i64 - other.n as i64; } # A value that is not closeable itself, but holds one that is. struct holder { var c: counter; } def held(n: u64): holder { return holder { c = counter { n = n } }; } @test def containers_stop_at_an_index_past_their_end(t: &mut tester): void { t.expects(!stops(def() { var v := vector::::new(); v.append(1); val x := v[0]; v.set(0, 2); v.insert_at(1, 3); v.remove_at(1); })); t.expects(!stops(def() { var i := interner::::new(); i.intern(1); val x := i[0]; })); # Only a program that checks stops at these, which one that does not reads # past what the container holds. if const contracts() { t.expects(stops(def() { var v := vector::::new(); v.append(1); val x := v[1]; })); t.expects(stops(def() { var v := vector::::new(); v.append(1); v[3] = 2; })); t.expects(stops(def() { var v := vector::::new(); v.set(0, 1); })); t.expects(stops(def() { var v := vector::::new(); v.insert_at(1, 1); })); t.expects(stops(def() { var v := vector::::new(); v.append(1); v.remove_at(1); })); t.expects(stops(def() { val s := string::from("ab", nothing {}); val b := s[2]; })); t.expects(stops(def() { val xs: i64[2] = [1, 2]; val view: span = xs[0:2]; val x := view.at(2); })); t.expects(stops(def() { var xs: i64[2] = [1, 2]; var view: slice = xs[0:2]; view.at(2) = 3; })); t.expects(stops(def() { var d := deque::::new(); d.push_back(1); val x := d[1]; })); t.expects(stops(def() { var l := list::::new(); l.push_back(1); val x := l[1]; })); t.expects(stops(def() { var l := list::::new(); l.insert_at(1, 1); })); t.expects(stops(def() { var l := list::::new(); l.push_back(1); l.remove_at(1); })); t.expects(stops(def() { var c := chain::::new(); c.push_back(1); val x := c[1]; })); t.expects(stops(def() { var c := chain::::new(); c.insert_at(1, 1); })); t.expects(stops(def() { var c := chain::::new(); c.push_back(1); c.remove_at(1); })); t.expects(stops(def() { var r := runs::::new(); val x := r.length(1); })); t.expects(stops(def() { var r := runs::::new(); val id := r.add([1, 2]); val x := r.at(id, 2); })); t.expects(stops(def() { var g := groups::::new(2); g.count(2); })); t.expects(stops(def() { var w := byte_writer::new(); w.u16(1); w.set_u32(0, 5); })); t.expects(stops(def() { var i := interner::::new(); i.intern(1); val x := i[1]; })); } } @test def containers_stop_where_they_hold_nothing_asked_for(t: &mut tester): void { t.expects(!stops(def() { var s := stack::::new(); s.push(1); val x := s.peek(); s.pop(); })); t.expects(stops(def() { var g := groups::::new(1); g.seal(); g.count(0); })); t.expects(!stops(def() { var g := groups::::new(1); val x := g.of(0); })); t.expects(!stops(def() { var d := dict::::new(); d.set(1, 2); val x := d[1]; })); # Only a program that checks stops at these, which one that does not reads # past what the container holds. if const contracts() { t.expects(stops(def() { var s := stack::::new(); s.pop(); })); t.expects(stops(def() { val s := stack::::new(); val x := s.peek(); })); t.expects(stops(def() { var q := queue::::new(); q.pop(); })); t.expects(stops(def() { val q := queue::::new(); val x := q.peek(); })); t.expects(stops(def() { var d := deque::::new(); d.pop_front(); })); t.expects(stops(def() { var d := deque::::new(); d.pop_back(); })); t.expects(stops(def() { val d := deque::::new(); val x := d.first; })); t.expects(stops(def() { val d := deque::::new(); val x := d.last; })); t.expects(stops(def() { var l := list::::new(); l.pop_front(); })); t.expects(stops(def() { val l := list::::new(); val x := l.first; })); t.expects(stops(def() { val l := list::::new(); val x := l.last; })); t.expects(stops(def() { var c := chain::::new(); c.pop_front(); })); t.expects(stops(def() { var c := chain::::new(); c.pop_back(); })); t.expects(stops(def() { val c := chain::::new(); val x := c.first; })); t.expects(stops(def() { val c := chain::::new(); val x := c.last; })); t.expects(stops(def() { val v := vector::::new(); val x := v.first; })); t.expects(stops(def() { val v := vector::::new(); val x := v.last; })); t.expects(stops(def() { var v := vector::::new(); v.truncate(); })); t.expects(stops(def() { val h := heap::::new(); val x := h.peek(); })); t.expects(stops(def() { var h := heap::::new(); h.pop(); })); t.expects(stops(def() { var g := groups::::new(1); g.count(0); g.seal(); g.put(0, 1); g.put(0, 2); })); t.expects(stops(def() { var g := groups::::new(1); g.count(0); g.put(0, 1); })); t.expects(stops(def() { var w := byte_writer::new(); w.byte(1); w.rewind(2); })); t.expects(stops(def() { var d := dict::::new(); d.set(1, 2); val x := d[5]; })); } } # The element `get` gives, or -1 where it gives none. def got(found: maybe<&T>): i64 where T: integer { if found is some(element) { return element as i64; } return -1; } @test def containers_give_an_element_or_nothing_by_get(t: &mut tester): void { var v := vector::::new(); v.append(4); v.append(5); val read := &v; t.expects(got(read.get(1)) == 5 && got(read.get(2)) == -1); if v.get(0) is some(&mut first) { first = 6; } t.expects(v[0] == 6 && v.get(2) is none); val s := string::from("ab", nothing {}); t.expects(got(s.get(1)) == 'b' as i64 && got(s.get(2)) == -1); val xs: i64[3] = [1, 2, 3]; val part: span = xs[1:2]; t.expects(got(part.get(1)) == 3 && got(part.get(2)) == -1); var ys: i64[3] = [1, 2, 3]; var place: slice = ys[1:2]; if place.get(1) is some(&mut last) { last = 9; } t.expects(ys[2] == 9 && place.get(2) is none); var d := deque::::new(); d.push_back(7); var l := list::::new(); l.push_back(8); var c := chain::::new(); c.push_back(9); val dr := &d; val lr := &l; val cr := &c; t.expects(got(dr.get(0)) == 7 && got(dr.get(1)) == -1); t.expects(got(lr.get(0)) == 8 && got(lr.get(1)) == -1); t.expects(got(cr.get(0)) == 9 && got(cr.get(1)) == -1); } @test def containers_close_what_their_elements_hold(t: &mut tester): void { # A value holding what closes is closed as a local holding it is. var lone := held(1); closed = 0; close(&mut lone); t.expects(closed == 1 && lone.c.n == 0); closed = 0; var v := vector::::new(); v.append(held(1)); v.append(held(2)); v.append(held(3)); v.append(held(4)); v.remove_at(0); t.expects(closed == 1); v.resize(2); t.expects(closed == 2); v.clear(); t.expects(closed == 4); v.append(held(5)); v.close(); t.expects(closed == 5); closed = 0; var s := stack::::new(); s.push(held(1)); s.push(held(2)); s.push(held(3)); s.pop(); s.resize(1); s.clear(); t.expects(closed == 3); closed = 0; var q := queue::::new(); q.push(held(1)); q.push(held(2)); q.pop(); q.clear(); var d := deque::::new(); d.push_back(held(1)); d.push_front(held(2)); d.push_back(held(3)); d.pop_front(); d.pop_back(); d.clear(); t.expects(closed == 5); closed = 0; var l := list::::new(); l.push_back(held(1)); l.push_back(held(2)); l.push_back(held(3)); l.pop_front(); l.remove_at(1); l.clear(); var c := chain::::new(); c.push_back(held(1)); c.push_back(held(2)); c.push_back(held(3)); c.pop_back(); c.remove_at(0); c.clear(); t.expects(closed == 6); closed = 0; var m := dict::::new(); m.set(1, held(1)); m.set(2, held(2)); m.set(3, held(3)); m.set(1, held(4)); t.expects(closed == 1); t.expects(m.remove(&2)); m.clear(); t.expects(closed == 4); } # An interner closes a value it is given that equals one it holds, keeping the # first, and gives that one's ID; what it holds it closes where it is cleared, and # it numbers from 0 again after. @test def sets_close_the_values_they_let_go(t: &mut tester): void { closed = 0; var s := hashset::::new(); for n in 1..7 { t.expects(s.insert(counter { n = n as u64 })); } t.expects(!s.insert(counter { n = 1 }) && s.length == 6 && closed == 1); val two := counter { n = 2 }; t.expects(s.remove(&two) && !s.remove(&two) && closed == 2); var odd := hashset::::new(); for n in 0..4 { odd.insert(counter { n = (2 * n + 1) as u64 }); } s.intersect_with(&odd); t.expects(s.length == 3 && closed == 4); s.subtract(&odd); t.expects(s.empty && closed == 7); for n in 10..13 { s.insert(counter { n = n as u64 }); } s.clear(); t.expects(closed == 10 && odd.length == 4); var words := hashset::::new(); words.insert(string::from("one", nothing {})); words.insert(string::from("two", nothing {})); var more := hashset::::new(); more.insert(string::from("two", nothing {})); more.insert(string::from("three", nothing {})); var copied := hashset::::copy_of(&words); copied.union_with(&more); more.close(); words.close(); val three := string::from("three", nothing {}); t.expects(copied.length == 3 && copied.contains(&three) && copied.get(&three).some.span == "three"); } @test def interners_keep_the_first_of_equal_values(t: &mut tester): void { closed = 0; var i := interner::::new(); t.expects(i.intern(counter { n = 5 }) == 0 && i.intern(counter { n = 6 }) == 1); t.expects(i.intern(counter { n = 5 }) == 0 && closed == 1 && i.length == 2); val six := counter { n = 6 }; t.expects(i.find(&six).some == 1 && i[1].n == 6 && i.contains(&six)); i.clear(); t.expects(closed == 3 && i.empty && i.find(&six) is none); t.expects(i.intern(counter { n = 7 }) == 0); # Text is found, and interned, by its view, and copied only where it is new. var words := interner::::new(); var ids := vector::::new(); for w in "a b a c b".split(' ' as u8) { ids.append(words.intern_view(w)); } val b: strview = "b"; val d: strview = "d"; t.expects(ids.length == 5 && ids[0] == 0 && ids[1] == 1 && ids[2] == 0 && ids[3] == 2 && ids[4] == 1); t.expects(words.length == 3 && words.find_view(b).some == 1 && words.find_view(d) is none && words[2].span == "c"); var copied := interner::::copy_of(&words); words.close(); t.expects(copied.length == 3 && copied.find_view(b).some == 1 && copied[0].span == "a"); } # A heap gives its least value first, and closes each value it removes. @test def heaps_give_their_least_value_first(t: &mut tester): void { closed = 0; var h := heap::::new(); val given: u64[6] = [5, 2, 8, 1, 9, 3]; for i in 0..6 { h.push(counter { n = given[i] }); } var order := vector::::new(); while !h.empty { order.append(h.peek().n); h.pop(); } t.expects(order.length == 6 && order[0] == 1 && order[1] == 2 && order[2] == 3 && order[5] == 9 && closed == 6); var words := heap::::new(4, nothing {}); for i in 0..100 { words.push(((i * 37) % 101) as u64); } var last: u64 = 0; var rising := true; while !words.empty { rising = rising && words.peek() >= last; last = words.peek(); words.pop(); } t.expects(rising); } # Runs are named by IDs from 1, the empty one 0, and rewound to drop the newest. @test def runs_hold_lists_named_by_ids(t: &mut tester): void { var r := runs::::new(); val a := r.add([1, 2, 3]); r.push(7); r.push(8); val b := r.end(); t.expects(a == 1 && b == 2 && r.end() == 0 && r.add(span::::blank()) == 0 && r.count == 3); t.expects(r.length(a) == 3 && r.at(b, 0) == 7 && r.view(a)[2] == 3 && r.length(0) == 0 && r.size == 5); r.rewind(2); t.expects(r.count == 2 && r.add([4]) == 2 && r.at(2, 0) == 4); val copied := runs::::copy_of(&r); r.clear(); t.expects(r.count == 1 && copied.count == 3 && copied.view(1)[1] == 2); closed = 0; var held := runs::::new(); held.push(counter { n = 1 }); held.push(counter { n = 2 }); val id := held.end(); held.push(counter { n = 3 }); held.rewind(1); t.expects(id == 1 && closed == 3 && held.count == 1); } # Groups put each element after those of its key put before it, once counted. @test def groups_gather_elements_by_key(t: &mut tester): void { var g := groups::::new(4); val keys: u32[6] = [3, 0, 3, 1, 3, 0]; for i in 0..6 { g.count(keys[i]); } g.seal(); for i in 0..6 { g.put(keys[i], i as u32); } t.expects(g.keys == 4 && g.size == 6 && g.length(3) == 3 && g.length(2) == 0); t.expects(g.of(3)[0] == 0 && g.of(3)[1] == 2 && g.of(3)[2] == 4 && g.of(0)[1] == 5 && g.of(1)[0] == 3); g.reset(2); g.count(1, 2); t.expects(!g.is_sealed && g.length(1) == 2 && g.keys == 2 && g.of(1).length == 0); g.seal(); g.put(1, 9); t.expects(g.of(1)[0] == 9 && g.of(1).length == 2); } # An allocator that never has memory to give. struct starved : allocator {} def starved::acquire(&mut this, size: u64, align: u64): *mut u8 { return null; } def starved::dispose(&mut this, addr: *mut u8, size: u64): void {} def starved::resize(&mut this, addr: *mut u8, old: u64, size: u64, align: u64): *mut u8 { return null; } # Each container is given an allocator with nothing to give, named as the # `maybe<&mut allocator>` the overloads of `new` take. @test def an_allocator_that_gives_no_memory_stops_the_program(t: &mut tester): void { t.expects(stops(def() { var none := starved {}; val alloc: maybe<&mut allocator> = &mut none; var v := vector::::new(alloc); v.append(1); })); t.expects(stops(def() { var none := starved {}; val alloc: maybe<&mut allocator> = &mut none; var s := string::new(alloc); s.push('x' as u8); })); t.expects(stops(def() { var none := starved {}; val alloc: maybe<&mut allocator> = &mut none; var d := dict::::new(alloc); d.set(1, 2); })); t.expects(stops(def() { var none := starved {}; val alloc: maybe<&mut allocator> = &mut none; var h := hashset::::new(alloc); h.insert(1); })); t.expects(stops(def() { var none := starved {}; val alloc: maybe<&mut allocator> = &mut none; var i := interner::::new(alloc); i.intern(1); })); t.expects(stops(def() { var none := starved {}; val alloc: maybe<&mut allocator> = &mut none; var h := heap::::new(alloc); h.push(1); })); t.expects(stops(def() { var none := starved {}; val alloc: maybe<&mut allocator> = &mut none; var r := runs::::new(alloc); })); t.expects(stops(def() { var none := starved {}; val alloc: maybe<&mut allocator> = &mut none; var g := groups::::new(4, alloc); })); t.expects(stops(def() { var none := starved {}; val alloc: maybe<&mut allocator> = &mut none; var w := byte_writer::new(alloc); w.byte(1); })); t.expects(stops(def() { var none := starved {}; val alloc: maybe<&mut allocator> = &mut none; var l := list::::new(alloc); l.push_back(1); })); t.expects(stops(def() { var none := starved {}; val alloc: maybe<&mut allocator> = &mut none; var q := queue::::new(alloc); q.push(1); })); }