|
|
@ -17,7 +17,7 @@ pub struct MockSwapper {
|
|
|
|
impl Swapper for MockSwapper {
|
|
|
|
impl Swapper for MockSwapper {
|
|
|
|
fn swap_out(&mut self, data: &[u8]) -> Result<usize, ()> {
|
|
|
|
fn swap_out(&mut self, data: &[u8]) -> Result<usize, ()> {
|
|
|
|
let id = self.alloc_id();
|
|
|
|
let id = self.alloc_id();
|
|
|
|
let mut slice: [u8; PAGE_SIZE] = unsafe { MaybeUninit::uninitialized().into_initialized() };
|
|
|
|
let mut slice: [u8; PAGE_SIZE] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
slice.copy_from_slice(data);
|
|
|
|
slice.copy_from_slice(data);
|
|
|
|
self.map.insert(id, slice);
|
|
|
|
self.map.insert(id, slice);
|
|
|
|
Ok(id)
|
|
|
|
Ok(id)
|
|
|
@ -27,7 +27,7 @@ impl Swapper for MockSwapper {
|
|
|
|
if !self.map.contains_key(&token) {
|
|
|
|
if !self.map.contains_key(&token) {
|
|
|
|
return Err(());
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut slice: [u8; PAGE_SIZE] = unsafe { MaybeUninit::uninitialized().into_initialized() };
|
|
|
|
let mut slice: [u8; PAGE_SIZE] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
slice.copy_from_slice(data);
|
|
|
|
slice.copy_from_slice(data);
|
|
|
|
self.map.insert(token, slice);
|
|
|
|
self.map.insert(token, slice);
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
@ -64,8 +64,8 @@ mod test {
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn swap_out_in() {
|
|
|
|
fn swap_out_in() {
|
|
|
|
let mut swapper = MockSwapper::default();
|
|
|
|
let mut swapper = MockSwapper::default();
|
|
|
|
let mut data: [u8; 4096] = unsafe { MaybeUninit::uninitialized().into_initialized() };
|
|
|
|
let mut data: [u8; 4096] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
let data1: [u8; 4096] = unsafe { MaybeUninit::uninitialized().into_initialized() };
|
|
|
|
let data1: [u8; 4096] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
let token = swapper.swap_out(&data1).unwrap();
|
|
|
|
let token = swapper.swap_out(&data1).unwrap();
|
|
|
|
swapper.swap_in(token, &mut data).unwrap();
|
|
|
|
swapper.swap_in(token, &mut data).unwrap();
|
|
|
|
assert_data_eq(&data, &data1);
|
|
|
|
assert_data_eq(&data, &data1);
|
|
|
@ -74,9 +74,9 @@ mod test {
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn swap_update() {
|
|
|
|
fn swap_update() {
|
|
|
|
let mut swapper = MockSwapper::default();
|
|
|
|
let mut swapper = MockSwapper::default();
|
|
|
|
let mut data: [u8; 4096] = unsafe { MaybeUninit::uninitialized().into_initialized() };
|
|
|
|
let mut data: [u8; 4096] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
let data1: [u8; 4096] = unsafe { MaybeUninit::uninitialized().into_initialized() };
|
|
|
|
let data1: [u8; 4096] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
let data2: [u8; 4096] = unsafe { MaybeUninit::uninitialized().into_initialized() };
|
|
|
|
let data2: [u8; 4096] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
let token = swapper.swap_out(&data1).unwrap();
|
|
|
|
let token = swapper.swap_out(&data1).unwrap();
|
|
|
|
swapper.swap_update(token, &data2).unwrap();
|
|
|
|
swapper.swap_update(token, &data2).unwrap();
|
|
|
|
swapper.swap_in(token, &mut data).unwrap();
|
|
|
|
swapper.swap_in(token, &mut data).unwrap();
|
|
|
@ -86,7 +86,7 @@ mod test {
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn invalid_token() {
|
|
|
|
fn invalid_token() {
|
|
|
|
let mut swapper = MockSwapper::default();
|
|
|
|
let mut swapper = MockSwapper::default();
|
|
|
|
let mut data: [u8; 4096] = unsafe { MaybeUninit::uninitialized().into_initialized() };
|
|
|
|
let mut data: [u8; 4096] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
assert_eq!(swapper.swap_in(0, &mut data), Err(()));
|
|
|
|
assert_eq!(swapper.swap_in(0, &mut data), Err(()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|