Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
test(dev): add file test
Browse files Browse the repository at this point in the history
  • Loading branch information
Rat Cornu committed Nov 6, 2023
1 parent 8acb718 commit d5406da
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
41 changes: 39 additions & 2 deletions src/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use alloc::borrow::{Cow, ToOwned};
use alloc::boxed::Box;
use alloc::vec;
use alloc::vec::Vec;
use core::cell::RefCell;
use core::mem;
Expand Down Expand Up @@ -227,7 +228,7 @@ macro_rules! impl_device {
let range = unsafe { usize::try_from(addr_range.start.index()).unwrap_unchecked() }..unsafe {
usize::try_from(addr_range.end.index()).unwrap_unchecked()
};
// SAFETY:
// SAFETY: it is checked above that the wanted elements exist
Ok(Slice::new(unsafe { <Self as AsRef<[T]>>::as_ref(self).get_unchecked(range) }, addr_start))
} else {
Err(Error::Device(DevError::OutOfBounds(
Expand Down Expand Up @@ -292,11 +293,12 @@ impl<S: Sector> Device<u8, S, std::io::Error> for RefCell<File> {
(0, usize::MAX as i128),
))
})?;
let mut slice = Vec::<u8>::with_capacity(len);
let mut slice = vec![0; len];
let mut file = self.borrow_mut();
file.seek(std::io::SeekFrom::Start(starting_addr.index()))
.and_then(|_| file.read_exact(&mut slice))
.map_err(Error::Other)?;

Ok(Slice::new_owned(slice, starting_addr))
}

Expand All @@ -311,6 +313,10 @@ impl<S: Sector> Device<u8, S, std::io::Error> for RefCell<File> {

#[cfg(test)]
mod test {
use core::cell::RefCell;
use std::fs::{self, OpenOptions};

use super::sector::Size4096;
use crate::dev::sector::{Address, Size512};
use crate::dev::Device;

Expand All @@ -332,4 +338,35 @@ mod test {
assert_eq!(x, usize::from((256..512).contains(&idx)));
}
}

#[allow(clippy::missing_asserts_for_indexing)]
#[test]
fn device_file() {
fs::copy("./tests/device_file_1.txt", "./tests/device_file_1_copy.txt").unwrap();

let mut file_1 = RefCell::new(OpenOptions::new().read(true).write(true).open("./tests/device_file_1_copy.txt").unwrap());

let mut slice = file_1
.slice(Address::<Size4096>::new(0, 0).unwrap()..Address::<Size4096>::new(0, 13).unwrap())
.unwrap();

let word = slice.get_mut(6..=10).unwrap();
word[0] = b'e';
word[1] = b'a';
word[2] = b'r';
word[3] = b't';
word[4] = b'h';

let commit = slice.flush();
file_1.commit(commit).unwrap();

drop(file_1);

let file_1 = String::from_utf8(fs::read("./tests/device_file_1_copy.txt").unwrap()).unwrap();
let file_2 = String::from_utf8(fs::read("./tests/device_file_2.txt").unwrap()).unwrap();

assert_eq!(file_1, file_2);

fs::remove_file("./tests/device_file_1_copy.txt").unwrap();
}
}
11 changes: 4 additions & 7 deletions src/dev/sector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,21 +227,18 @@ impl<S: Sector> Sub for Address<S> {
impl<S: Sector> Step for Address<S> {
#[inline]
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
(start.sector() <= end.sector()).then_some((end.sector() - start.sector()) as usize)
// SAFETY: it is not possible to manipulate addresses with a higher bit number than the device's
(start.sector() <= end.sector()).then_some(unsafe { (end.index() - start.index()).try_into().unwrap_unchecked() })
}

#[inline]
fn forward_checked(start: Self, count: usize) -> Option<Self> {
((start.sector() as usize) < (S::SIZE as usize - count))
.then(|| Self::new(start.sector() + TryInto::<u32>::try_into(count).ok()?, 0).ok())
.flatten()
Some(start + Self::try_from(count).ok()?)
}

#[inline]
fn backward_checked(start: Self, count: usize) -> Option<Self> {
((start.sector() as usize) >= count)
.then(|| Self::new(start.sector() - TryInto::<u32>::try_into(count).ok()?, 0).ok())
.flatten()
Some(start - Self::try_from(count).ok()?)
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/device_file_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello earth!
1 change: 1 addition & 0 deletions tests/device_file_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello earth!

0 comments on commit d5406da

Please sign in to comment.