Skip to content

Commit 5e5ea84

Browse files
committed
flipperzero: Add some tests
1 parent 6430945 commit 5e5ea84

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

crates/flipperzero/src/dolphin/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,16 @@ impl Dolphin {
5454
unsafe { sys::dolphin_flush(self.data.as_ptr()) };
5555
}
5656
}
57+
58+
#[flipperzero_test::tests]
59+
mod tests {
60+
use super::Dolphin;
61+
62+
#[test]
63+
fn stats() -> Result<(), &'static str> {
64+
let mut dolphin = Dolphin::open();
65+
let stats = dolphin.stats();
66+
assert!(stats.level >= 1);
67+
Ok(())
68+
}
69+
}

crates/flipperzero/src/furi/message_queue.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,47 @@ impl<M: Sized> Drop for MessageQueue<M> {
8282
unsafe { sys::furi_message_queue_free(self.hnd) }
8383
}
8484
}
85+
86+
#[flipperzero_test::tests]
87+
mod tests {
88+
use core::time::Duration;
89+
90+
use flipperzero_sys::furi::Status;
91+
92+
use super::MessageQueue;
93+
94+
#[test]
95+
fn capacity() -> Result<(), &'static str> {
96+
let queue = MessageQueue::new(3);
97+
assert_eq!(queue.len(), 0);
98+
assert_eq!(queue.space(), 3);
99+
assert_eq!(queue.capacity(), 3);
100+
101+
// Adding a message to the queue should consume capacity.
102+
queue.put(2, Duration::from_millis(1)).unwrap();
103+
assert_eq!(queue.len(), 1);
104+
assert_eq!(queue.space(), 2);
105+
assert_eq!(queue.capacity(), 3);
106+
107+
// We should be able to fill the queue to capacity.
108+
queue.put(4, Duration::from_millis(1)).unwrap();
109+
queue.put(6, Duration::from_millis(1)).unwrap();
110+
assert_eq!(queue.len(), 3);
111+
assert_eq!(queue.space(), 0);
112+
assert_eq!(queue.capacity(), 3);
113+
114+
// Attempting to add another message should time out.
115+
assert_eq!(
116+
queue.put(7, Duration::from_millis(1)),
117+
Err(Status::ERR_TIMEOUT),
118+
);
119+
120+
// Removing a message from the queue frees up capacity.
121+
assert_eq!(queue.get(Duration::from_millis(1)), Ok(2));
122+
assert_eq!(queue.len(), 2);
123+
assert_eq!(queue.space(), 1);
124+
assert_eq!(queue.capacity(), 3);
125+
126+
Ok(())
127+
}
128+
}

crates/flipperzero/src/furi/sync.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,26 @@ impl<T: ?Sized> Drop for MutexGuard<'_, T> {
7878
// `UnsendUnsync` is actually a bit too strong.
7979
// As long as `T` implements `Sync`, it's fine to access it from another thread.
8080
unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> {}
81+
82+
#[flipperzero_test::tests]
83+
mod tests {
84+
use super::Mutex;
85+
86+
#[test]
87+
fn unshared_mutex_does_not_block() -> Result<(), &'static str> {
88+
let mutex = Mutex::new(7u64);
89+
90+
{
91+
let mut value = mutex.lock().expect("should not fail");
92+
assert_eq!(*value, 7);
93+
*value = 42;
94+
}
95+
96+
{
97+
let value = mutex.lock().expect("should not fail");
98+
assert_eq!(*value, 42);
99+
}
100+
101+
Ok(())
102+
}
103+
}

crates/flipperzero/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ pub mod __internal {
1818
pub use ufmt;
1919
}
2020

21-
flipperzero_test::tests_runner!([]);
21+
flipperzero_test::tests_runner!([
22+
crate::dolphin::tests,
23+
crate::furi::message_queue::tests,
24+
crate::furi::sync::tests,
25+
]);

0 commit comments

Comments
 (0)