mirror of
https://github.com/hexagonal-sun/moss-kernel.git
synced 2026-04-18 06:08:09 -04:00
async unit testing
This commit is contained in:
@@ -626,3 +626,15 @@ impl VFS {
|
||||
fs.sync().await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::fs::VFS;
|
||||
use crate::ktest;
|
||||
|
||||
ktest! {
|
||||
async fn test_sync_all() {
|
||||
VFS.sync_all().await.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,3 +96,24 @@ impl KPipe {
|
||||
self.inner.splice_from(&source.inner, count).await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::kernel::kpipe::KPipe;
|
||||
use crate::ktest;
|
||||
|
||||
ktest! {
|
||||
async fn kpipe_basic() {
|
||||
let pipe = KPipe::new().unwrap();
|
||||
pipe.push(1).await;
|
||||
pipe.push(2).await;
|
||||
pipe.push(3).await;
|
||||
let val1 = pipe.pop().await;
|
||||
let val2 = pipe.pop().await;
|
||||
let val3 = pipe.pop().await;
|
||||
assert_eq!(val1, 1);
|
||||
assert_eq!(val2, 2);
|
||||
assert_eq!(val3, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,9 +64,9 @@ pub fn panic_noop(_: *mut u8, _: *mut u8) {}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! ktest {
|
||||
(fn $name:ident() $body:block) => {
|
||||
($name:ident, fn $fn_name:ident() $body:block) => {
|
||||
#[cfg(test)]
|
||||
fn $name(_: *mut u8) {
|
||||
fn $fn_name(_: *mut u8) {
|
||||
$body
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ macro_rules! ktest {
|
||||
test_fn: || {
|
||||
let result = unsafe {
|
||||
core::intrinsics::catch_unwind(
|
||||
$name as fn(*mut u8),
|
||||
$fn_name as fn(*mut u8),
|
||||
core::ptr::null_mut(),
|
||||
crate::testing::panic_noop,
|
||||
)
|
||||
@@ -92,4 +92,31 @@ macro_rules! ktest {
|
||||
};
|
||||
}
|
||||
};
|
||||
(fn $name:ident() $body:block) => {
|
||||
crate::ktest!($name, fn $name() $body);
|
||||
};
|
||||
(async fn $name:ident() $body:block) => {
|
||||
async fn $name() {
|
||||
$body
|
||||
}
|
||||
|
||||
paste::paste! {
|
||||
crate::ktest! {
|
||||
$name,
|
||||
fn [<__sync_ $name>]() {
|
||||
let mut fut = alloc::boxed::Box::pin($name());
|
||||
let desc = crate::process::TaskDescriptor::from_tgid_tid(crate::process::thread_group::Tgid(0), crate::process::Tid(0));
|
||||
let waker = crate::sched::waker::create_waker(desc);
|
||||
let mut ctx = core::task::Context::from_waker(&waker);
|
||||
loop {
|
||||
match fut.as_mut().poll(&mut ctx) {
|
||||
core::task::Poll::Ready(()) => break,
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user