112 lines
4.1 KiB
Plaintext
112 lines
4.1 KiB
Plaintext
# 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 system;
|
|
import std::testing;
|
|
import std::containers;
|
|
|
|
|
|
# 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; }
|
|
|
|
@test def vectors_grow_by_elements_left_undefined(t: &mut tester): void {
|
|
var v := vector::<u64>::new();
|
|
v.append(1);
|
|
v.extend_undefined(1000);
|
|
t.expects(v.length == 1001 && v[0] == 1);
|
|
for i in 1..v.length {
|
|
v[i] = i;
|
|
}
|
|
t.expects(v[1000] == 1000);
|
|
|
|
var zeroed := vector::<u64>::new();
|
|
zeroed.resize(5000);
|
|
var sum: u64 = 0;
|
|
for &each in zeroed {
|
|
sum += each;
|
|
}
|
|
t.expects(zeroed.length == 5000 && sum == 0);
|
|
}
|
|
|
|
# A set closes a value where it lets it go: one it is given and already holds, one
|
|
# removed, and one another set leaves or takes out, while what it copies in is its
|
|
# own, so the set copied from closes only its own.
|
|
# A vector reads its ends, removes its last, grows by copies, and sorts, closing
|
|
# only what it removes, and moving what it sorts without closing or copying it.
|
|
@test def vectors_read_their_ends_and_sort_what_they_hold(t: &mut tester): void {
|
|
closed = 0;
|
|
var v := vector::<counter>::new();
|
|
for n in 1..4 { v.append(counter { n = n as u64 }); }
|
|
t.expects(v.first().n == 1 && v.last().n == 3);
|
|
v.last().n = 9;
|
|
v.truncate();
|
|
t.expects(closed == 1 && v.length == 2 && v.last().n == 2);
|
|
|
|
# Many are sorted by merging runs, and those an order leaves equal keep theirs.
|
|
closed = 0;
|
|
var many := vector::<counter>::new();
|
|
for i in 0..40 { many.append(counter { n = ((i * 17) % 41 + 1) as u64 }); }
|
|
many.sort();
|
|
var sorted := true;
|
|
for i in 1..many.length { sorted = sorted && many[i - 1].n < many[i].n; }
|
|
t.expects(sorted && closed == 0);
|
|
many.sort_by(def(a: &counter, b: &counter): bool { return a.n % 2 < b.n % 2; });
|
|
t.expects(many[0].n == 2 && many[1].n == 4 && many[20].n == 1 && closed == 0);
|
|
many.clear();
|
|
t.expects(closed == 40);
|
|
|
|
var texts := vector::<string>::new();
|
|
val more: string[2] = [string::from("b", nothing {}), string::from("a", nothing {})];
|
|
texts.extend(more[:]);
|
|
texts.resize(4, string::from("z", nothing {}));
|
|
texts.sort();
|
|
t.expects(texts.length == 4 && texts[0].view() == "a" && texts[3].view() == "z" && more[0].view() == "b");
|
|
|
|
var numbers := vector::<u32>::new();
|
|
numbers.resize(3, 0xFFFFFFFF);
|
|
numbers.extend([4, 2]);
|
|
numbers.sort();
|
|
val view := numbers.view();
|
|
t.expects(numbers.first() == 2 && view.lower_bound(&4) == 1 && view.upper_bound(&4) == 2 && view.lower_bound(&5) == 2);
|
|
t.expects(view.partition_point(def(x: &u32): bool { return x < 0xFFFFFFFF; }) == 2);
|
|
|
|
var d := dict::<u32, string>::new();
|
|
d.set(1, string::from("one", nothing {}));
|
|
t.expects(d.get_or(&1, string::new(nothing {})).view() == "one" && d.get_or(&2, string::from("none", nothing {})).view() == "none");
|
|
}
|
|
|
|
@test def bytes_compare_flags_change_and_digits_read_in_any_base(t: &mut tester): void {
|
|
val a: strview = "abc";
|
|
val b: strview = "abd";
|
|
t.expects(memcmp(a.data, b.data, 3) < 0 && memcmp(b.data, a.data, 3) > 0 && memcmp(a.data, a.data, 3) == 0);
|
|
|
|
var f: flags32<target> = 0;
|
|
f.set(3, true);
|
|
t.expects(f.get(3 as u8) && !f.get(4 as u8));
|
|
f.set(3, false);
|
|
t.expects(!f.get(3 as u8) && f == 0);
|
|
t.expects(f.with(5, true).get(5 as u8) && !f.get(5 as u8));
|
|
f.set(target::fields);
|
|
t.expects(f.get(target::fields) && !f.with(4, false).get(target::fields));
|
|
|
|
t.expects(('2' as u8).is_base_digit(3) && !('3' as u8).is_base_digit(3) && !('9' as u8).is_base_digit(3));
|
|
t.expects(('z' as u8).is_base_digit(36) && ('F' as u8).is_base_digit(16) && !('g' as u8).is_base_digit(16));
|
|
}
|