Skip to content

Commit

Permalink
add memory arena test (#2298)
Browse files Browse the repository at this point in the history
* add memory arena test

* add assert

* Update stacker/src/memory_arena.rs

Co-authored-by: Paul Masurel <[email protected]>

---------

Co-authored-by: Paul Masurel <[email protected]>
  • Loading branch information
PSeitz and fulmicoton authored Jan 11, 2024
1 parent 014328e commit f95a762
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions stacker/src/memory_arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ struct Page {

impl Page {
fn new(page_id: usize) -> Page {
// We use 32-bits addresses.
// - 20 bits for the in-page addressing
// - 12 bits for the page id.
// This limits us to 2^12 - 1=4095 for the page id.
assert!(page_id < 4096);
Page {
page_id,
len: 0,
Expand Down Expand Up @@ -238,6 +243,7 @@ impl Page {
mod tests {

use super::MemoryArena;
use crate::memory_arena::PAGE_SIZE;

#[test]
fn test_arena_allocate_slice() {
Expand All @@ -255,6 +261,31 @@ mod tests {
assert_eq!(arena.slice(addr_b, b.len()), b);
}

#[test]
fn test_arena_allocate_end_of_page() {
let mut arena = MemoryArena::default();

// A big block
let len_a = PAGE_SIZE - 2;
let addr_a = arena.allocate_space(len_a);
*arena.slice_mut(addr_a, len_a).last_mut().unwrap() = 1;

// Single bytes
let addr_b = arena.allocate_space(1);
arena.slice_mut(addr_b, 1)[0] = 2;

let addr_c = arena.allocate_space(1);
arena.slice_mut(addr_c, 1)[0] = 3;

let addr_d = arena.allocate_space(1);
arena.slice_mut(addr_d, 1)[0] = 4;

assert_eq!(arena.slice(addr_a, len_a)[len_a - 1], 1);
assert_eq!(arena.slice(addr_b, 1)[0], 2);
assert_eq!(arena.slice(addr_c, 1)[0], 3);
assert_eq!(arena.slice(addr_d, 1)[0], 4);
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct MyTest {
pub a: usize,
Expand Down

0 comments on commit f95a762

Please sign in to comment.