# 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::::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::::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::::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::::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::::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].span == "a" && texts[3].span == "z" && more[0].span == "b"); var numbers := vector::::new(); numbers.resize(3, 0xFFFFFFFF); numbers.extend([4, 2]); numbers.sort(); val view := numbers.span; 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::::new(); d.set(1, string::from("one", nothing {})); t.expects(d.get_or(&1, string::new(nothing {})).span == "one" && d.get_or(&2, string::from("none", nothing {})).span == "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 = 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)); } @test def tuples_hold_a_value_of_each_type_and_close_what_closes(t: &mut tester): void { var pair := tuple::of(3, string::from("three", nothing {})); pair.at::<0>() += 1; t.expects(pair.items[0] == 4 && pair.at::<1>().span == "three" && pair.items.length == 2); val before := closed; { val held := tuple::of(counter { n = 1 }, 5 as u8, counter { n = 2 }); t.expects(held.at::<2>().n == 2 && closed == before); } t.expects(closed == before + 2); }