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 26, 2023
1 parent f61ccf9 commit 754d827
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/flipperzero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ flipperzero-rt.workspace = true
# enables features requiring an allocator
alloc = []

[[test]]
name = "dolphin"
harness = false

[[example]]
name = "dialog"
required-features = ["alloc"]
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);
}
}
}
5 changes: 4 additions & 1 deletion crates/flipperzero/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ pub mod __internal {
pub use ufmt;
}

flipperzero_test::tests_runner!([]);
flipperzero_test::tests_runner!(
name = "flipperzero-rs Unit Tests",
[crate::furi::message_queue::tests, crate::furi::sync::tests]
);
16 changes: 16 additions & 0 deletions crates/flipperzero/tests/dolphin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![no_std]
#![no_main]

#[flipperzero_test::tests]
mod tests {
use flipperzero::dolphin::Dolphin;

#[test]
fn stats() {
let mut dolphin = Dolphin::open();
let stats = dolphin.stats();
assert!(stats.level >= 1);
}
}

flipperzero_test::tests_runner!(name = "Dolphin Integration Test", [crate::tests]);

0 comments on commit 754d827

Please sign in to comment.