# Tests of the allocators: an arena hands out blocks one after another, grows the # last where it is, and frees them all at once, by reset, rewind, or close; the # containers keep their memory in one; and nodes linked by reference live in one. # A pool hands out blocks of one size, those given back first, makes and unmakes # values in them, and holds a list's nodes. import std::allocators; import std::containers; import std::testing; @test def arenas_hand_out_blocks_one_after_another(t: &mut tester): void { var a := arena::new(1024); val first := a.acquire(10, 1); val second := a.acquire(10, 1); val third := a.acquire(8, 8); t.expects(second == first + 10 && third == first + 24 && (third as u64) % 8 == 0); # A block too large for what is left of a chunk starts another, and one larger # than a chunk has a chunk of its own. val big := a.acquire(5000, 16); t.expects(big != null && (big as u64) % 16 == 0); for i in 0..5000u { big[i] = (i % 251) as u8; } val after := a.acquire(16, 16); t.expects(after != null && big[4999] == (4999 % 251) as u8); } @test def arenas_grow_and_give_back_the_last_block_where_it_is(t: &mut tester): void { var a := arena::new(1024); val block := a.acquire(16, 8); block[0] = 7; val grown := a.resize(block, 16, 64, 8); t.expects(grown == block && grown[0] == 7); # A block that is not the last is copied. val other := a.acquire(16, 8); val moved := a.resize(grown, 64, 128, 8); t.expects(moved != grown && moved[0] == 7); # The last block given back is where the next begins. a.dispose(moved, 128); t.expects(a.acquire(32, 8) == moved); a.dispose(other, 16); t.expects(a.acquire(8, 8) != other); } @test def arenas_free_what_they_handed_out_by_reset_and_rewind(t: &mut tester): void { var a := arena::new(512); val first := a.acquire(100, 16); for _ in 0..20u { a.acquire(100, 16); } a.reset(); t.expects(a.acquire(100, 16) == first); val point := a.mark(); val kept := a.acquire(64, 16); for _ in 0..20u { a.acquire(200, 16); } a.rewind(point); t.expects(a.acquire(64, 16) == kept); } @test def arenas_over_a_buffer_hand_it_out_first(t: &mut tester): void { var bytes: u8[1024]; var a := arena::over(bytes[:]); val low := (&bytes) as *u8 as u64; val made := a.acquire(100, 8) as u64; t.expects(made >= low && made + 100 <= low + 1024); # Once it is full, blocks come from the heap. val more := a.acquire(2000, 8) as u64; t.expects(more + 2000 <= low || more >= low + 1024); } @test def containers_keep_their_memory_in_an_arena(t: &mut tester): void { var a := arena::new(); val alloc: maybe<&mut allocator> = &mut a; var numbers := vector::::new(alloc); for i in 0..1000u { numbers.append(i * 3); } var total: u64 = 0; for &n in numbers { total += n; } t.expects(numbers.length == 1000 && total == 1498500); var names := dict::::new(alloc); for i in 0..500u { names.set(i, i + 1); } t.expects(names.length == 500 && names[499] == 500); var text := string::new(alloc); for _ in 0..100u { text.append("ab"); } t.expects(text.length == 200 && text[199] == 'b' as u8); var nested := vector::>::new(alloc); for i in 0..50u { var inner := vector::::new(alloc); inner.append(i as u32); nested.append(move(inner)); } t.expects(nested.length == 50 && nested[49][0] == 49); } struct arena_node { var value: u64; var next: maybe<&arena_node>; } @test def an_arena_holds_nodes_linked_by_reference(t: &mut tester): void { var a := arena::new(); val alloc: maybe<&mut allocator> = &mut a; # Nodes made in an arena live until it rewinds past them, so they link to one # another by reference, and are given back together. val point := a.mark(); var front: maybe<&arena_node> = nothing {}; for i in 0..500u { val made := acquire::(alloc, 1); made[0] = arena_node { value = i, next = front }; front = through(made); } var sum: u64 = 0; var count: u64 = 0; while front is some(&each) { sum += each.value; count++; front = each.next; } t.expects(count == 500 && sum == 124750); a.rewind(point); } @test def pools_hand_out_the_blocks_given_back_first(t: &mut tester): void { var p := pool::new(24, 8); t.expects(p.block_size() == 24); val a := p.acquire(24, 8); val b := p.acquire(16, 8); val c := p.acquire(24, 8); t.expects(a != null && b == a + 24 && c == b + 24 && p.count() == 3); # A block given back is the next handed out, the last given back first. p.dispose(b, 16); p.dispose(a, 24); t.expects(p.count() == 1 && p.acquire(24, 8) == a && p.acquire(24, 8) == b); # Of a size or alignment larger than its blocks, it hands out nothing. t.expects(p.acquire(25, 8) == null && p.acquire(8, 16) == null && p.count() == 3); val aligned := pool::new(20, 16); t.expects(aligned.block_size() == 32); } @test def pools_hand_out_their_chunks_again_once_reset(t: &mut tester): void { # More blocks than a chunk holds take more chunks, each carved in turn, and a # pool reset carves them again from the first. var p := pool::new(64, 8); val first := p.acquire(64, 8); for _ in 0..3000u { _ = p.acquire(64, 8); } t.expects(p.count() == 3001); p.reset(); t.expects(p.count() == 0 && p.acquire(64, 8) == first); for _ in 0..3000u { _ = p.acquire(64, 8); } t.expects(p.count() == 3001); } # The values of the pooled values closed, from any test. var pooled_closed: u64; struct pooled : owning { var value: u64; } def pooled::close(&mut this): void { pooled_closed += this.value; } @test def pools_make_values_and_close_them_as_they_are_unmade(t: &mut tester): void { var p := pool::of::(); pooled_closed = 0; var made := vector::<*mut pooled>::new(); for i in 1..101u { made.append(p.make(pooled { value = i })); } t.expects(p.count() == 100 && made[41][0].value == 42); # The odd values, 1 to 99, are unmade, then the rest. for i in 0..made.length { if i % 2 == 0 { p.unmake(made[i]); } } t.expects(pooled_closed == 2500 && p.count() == 50); for i in 0..made.length { if i % 2 == 1 { p.unmake(made[i]); } } t.expects(pooled_closed == 5050 && p.count() == 0); } @test def lists_keep_their_nodes_in_a_pool(t: &mut tester): void { # A list asks for one node at a time, each within the pool's blocks, and gives # each back as it is removed. var p := pool::new(64, 8); val alloc: maybe<&mut allocator> = &mut p; { var numbers := list::::new(alloc); for i in 0..1000u { numbers.push_back(i); } t.expects(p.count() == 1000); for _ in 0..400u { numbers.pop_front(); } var total: u64 = 0; for &n in numbers { total += n; } t.expects(p.count() == 600 && total == 419700); } t.expects(p.count() == 0); }