Skip to content

Commit

Permalink
[runtime] Enhancement: Create unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carvalhof authored and iyzhang committed Feb 7, 2025
1 parent 4e435b7 commit a91c4bf
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/rust/runtime/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,56 @@ pub trait MemoryRuntime {
Ok(clone)
}
}

//======================================================================================================================
// Unit Tests
//======================================================================================================================

#[cfg(test)]
mod tests {
use crate::{
expect_ok,
demi_sgarray_t,
runtime::memory::MemoryRuntime,
};
use ::test::{
black_box,
Bencher,
};

// The buffer size.
const BUFSIZE: usize = 1024;

pub struct DummyRuntime { }

impl MemoryRuntime for DummyRuntime {}

#[bench]
fn benchmark_clone_sgarray(b: &mut Bencher) {
let runtime: DummyRuntime = DummyRuntime { };

let sga: demi_sgarray_t = match runtime.sgaalloc(BUFSIZE) {
Ok(sga) => sga,
Err(e) => panic!("failed to allocate sgarray: {:?}", e),
};

b.iter(|| {
black_box(expect_ok!(runtime.clone_sgarray(&sga), "failed to clone sgarray"));
});
}

#[bench]
fn benchmark_into_buf(b: &mut Bencher) {
let runtime: DummyRuntime = DummyRuntime { };

let sga: demi_sgarray_t = match runtime.sgaalloc(BUFSIZE) {
Ok(sga) => sga,
Err(e) => panic!("failed to allocate sgarray: {:?}", e),
};

b.iter(|| {
black_box(expect_ok!(runtime.into_buf(&sga), "failed to convert sgarray"));
});
}

}

0 comments on commit a91c4bf

Please sign in to comment.