diff --git a/crates/flipperzero/src/dolphin/mod.rs b/crates/flipperzero/src/dolphin/mod.rs index 625c79cb..4758a8cb 100644 --- a/crates/flipperzero/src/dolphin/mod.rs +++ b/crates/flipperzero/src/dolphin/mod.rs @@ -54,3 +54,15 @@ impl Dolphin { unsafe { sys::dolphin_flush(self.data.as_ptr()) }; } } + +#[flipperzero_test::tests] +mod tests { + use super::Dolphin; + + #[test] + fn stats() { + let mut dolphin = Dolphin::open(); + let stats = dolphin.stats(); + assert!(stats.level >= 1); + } +} diff --git a/crates/flipperzero/src/furi/message_queue.rs b/crates/flipperzero/src/furi/message_queue.rs index 3134533e..13b4611d 100644 --- a/crates/flipperzero/src/furi/message_queue.rs +++ b/crates/flipperzero/src/furi/message_queue.rs @@ -82,3 +82,45 @@ impl Drop for MessageQueue { unsafe { sys::furi_message_queue_free(self.hnd) } } } + +#[flipperzero_test::tests] +mod tests { + use core::time::Duration; + + use flipperzero_sys::furi::Status; + + use super::MessageQueue; + + #[test] + fn capacity() { + let queue = MessageQueue::new(3); + assert_eq!(queue.len(), 0); + assert_eq!(queue.space(), 3); + assert_eq!(queue.capacity(), 3); + + // Adding a message to the queue should consume capacity. + queue.put(2, Duration::from_millis(1)).unwrap(); + assert_eq!(queue.len(), 1); + assert_eq!(queue.space(), 2); + assert_eq!(queue.capacity(), 3); + + // We should be able to fill the queue to capacity. + queue.put(4, Duration::from_millis(1)).unwrap(); + queue.put(6, Duration::from_millis(1)).unwrap(); + assert_eq!(queue.len(), 3); + assert_eq!(queue.space(), 0); + assert_eq!(queue.capacity(), 3); + + // Attempting to add another message should time out. + assert_eq!( + queue.put(7, Duration::from_millis(1)), + Err(Status::ERR_TIMEOUT), + ); + + // Removing a message from the queue frees up capacity. + assert_eq!(queue.get(Duration::from_millis(1)), Ok(2)); + assert_eq!(queue.len(), 2); + assert_eq!(queue.space(), 1); + assert_eq!(queue.capacity(), 3); + } +} diff --git a/crates/flipperzero/src/furi/sync.rs b/crates/flipperzero/src/furi/sync.rs index f26c2b39..069a856c 100644 --- a/crates/flipperzero/src/furi/sync.rs +++ b/crates/flipperzero/src/furi/sync.rs @@ -78,3 +78,24 @@ impl Drop for MutexGuard<'_, T> { // `UnsendUnsync` is actually a bit too strong. // As long as `T` implements `Sync`, it's fine to access it from another thread. unsafe impl Sync for MutexGuard<'_, T> {} + +#[flipperzero_test::tests] +mod tests { + use super::Mutex; + + #[test] + fn unshared_mutex_does_not_block() { + let mutex = Mutex::new(7u64); + + { + let mut value = mutex.lock().expect("should not fail"); + assert_eq!(*value, 7); + *value = 42; + } + + { + let value = mutex.lock().expect("should not fail"); + assert_eq!(*value, 42); + } + } +} diff --git a/crates/flipperzero/src/lib.rs b/crates/flipperzero/src/lib.rs index d09cd064..90b9cfc8 100644 --- a/crates/flipperzero/src/lib.rs +++ b/crates/flipperzero/src/lib.rs @@ -18,4 +18,11 @@ pub mod __internal { pub use ufmt; } -flipperzero_test::tests_runner!([]); +flipperzero_test::tests_runner!( + name = "flipperzero-rs Unit Tests", + [ + crate::dolphin::tests, + crate::furi::message_queue::tests, + crate::furi::sync::tests, + ] +);