Skip to content

Commit

Permalink
Add some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
qwandor committed Jul 22, 2024
1 parent 3bd1674 commit 5067e1b
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod chrono;

use core::ptr::{addr_of, addr_of_mut};

#[derive(Default)]
#[repr(C, align(4))]
struct Registers {
/// Data register
Expand Down Expand Up @@ -120,3 +121,33 @@ unsafe impl Send for Rtc {}
// SAFETY: An `&Rtc` only allows reading device registers, which can safety be done from multiple
// places at once.
unsafe impl Sync for Rtc {}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_get_timestamp() {
let mut registers = Registers {
dr: 12345678,
..Registers::default()
};

// SAFETY: The pointer is constructed from a reference so it must be valid
let rtc = unsafe { Rtc::new(&mut registers as *mut Registers as _) };

assert_eq!(rtc.get_unix_timestamp(), 12345678);
}

#[test]
fn test_set_timestamp() {
let mut registers = Registers::default();

// SAFETY: The pointer is constructed from a reference so it must be valid
let mut rtc = unsafe { Rtc::new(&mut registers as *mut Registers as _) };

rtc.set_unix_timestamp(424242);
drop(rtc);
assert_eq!(registers.lr, 424242);
}
}

0 comments on commit 5067e1b

Please sign in to comment.