Skip to content

Commit

Permalink
flipperzero: Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Mar 19, 2023
1 parent 9d77b53 commit a62620e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
12 changes: 12 additions & 0 deletions crates/flipperzero/src/dolphin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
42 changes: 42 additions & 0 deletions crates/flipperzero/src/furi/message_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,45 @@ impl<M: Sized> Drop for MessageQueue<M> {
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);
}
}
21 changes: 21 additions & 0 deletions crates/flipperzero/src/furi/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,24 @@ impl<T: ?Sized> 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<T: ?Sized + Sync> 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);
}
}
}
9 changes: 8 additions & 1 deletion crates/flipperzero/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);

0 comments on commit a62620e

Please sign in to comment.