#[cfg(test)] mod cpu_tests { use super::*; use std::{boxed::Box,cell::Cell,rc::Rc}; struct Memory { pages:[Box<[u8;4096]>;7], live:[bool;7], allocations:Rc> } impl Memory { fn new(a:Rc>)->Self{Self{pages:core::array::from_fn(|_|Box::new([0xa5;4096])),live:[false;7],allocations:a}} fn slot(a:u64)->usize{((a-4096)/4096)as usize} } impl Backend for Memory { fn allocate(&mut self)->Result{let i=self.live.iter().position(|v|!*v).ok_or(MetadataError::NoMemory)?;self.live[i]=true;self.allocations.set(self.allocations.get()+1);Ok((i as u64+1)*4096)} fn release(&mut self,a:u64)->Result<(),MetadataError>{let i=Self::slot(a);assert!(self.live[i]);assert!(self.pages[i].iter().all(|v|*v==0));self.live[i]=false;Ok(())} fn zero_page(&mut self,a:u64)->Result<(),MetadataError>{let i=Self::slot(a);assert!(self.live[i]);self.pages[i].fill(0);Ok(())} fn read_byte(&self,a:u64,o:usize)->Result{let i=Self::slot(a);assert!(self.live[i]);Ok(self.pages[i][o])} fn available_pages(&self)->u64{self.live.iter().filter(|v|!**v).count()as u64} } struct D { binding:Binding, reset:bool, barrier:bool } impl Device for D { fn binding(&self)->Binding{self.binding} fn write_reset(&mut self){self.reset=true} fn reset_barrier(&mut self){assert!(self.reset);self.barrier=true} fn read_status(&mut self)->u32{assert!(self.barrier);0} fn read_ready(&mut self)->u32{assert!(self.barrier);0} } fn hardware(cpu:u64,mask:u64,el:u64){crate::set_context((mask,cpu,el));} fn binding()->Binding{Binding{device:0x10000000,queue:__QUEUE__,epoch:1}} fn empty(w:&Workspace){assert_eq!(w.state,State::Empty);assert_eq!(w.identity,0);assert_eq!(w.owner,0);assert!(w.handles.iter().all(Option::is_none));assert!(w.pins.iter().all(Option::is_none));} #[test] fn all_cpu_dma_cycles_and_foreign_completion(){ let mut previous:Option<__RECEIPT__>=None;let mut last_id=0; for cpu in 0..4 { hardware(cpu,0x3c0,4);let allocations=Rc::new(Cell::new(0));let mut r=Registry::<_,7>::new(100+cpu,Memory::new(allocations.clone())).unwrap();let before=r.snapshot();let mut w=Workspace::new();let b=binding(); let mut pool=unsafe{Pool::initialize_on_cpu(&mut w,&mut r,7,b__LENGTHS__,cpu)}.unwrap();assert!(pool.snapshot().identity>last_id);last_id=pool.snapshot().identity;assert_eq!(allocations.get(),PAGES as u64);assert_eq!(pool.snapshot().registry.pins,PAGES);pool.activate().unwrap();let active=pool.audit().unwrap(); assert_eq!(pool.ordinary_release(3),Err(Error::Metadata(MetadataError::Pinned)));assert_eq!(pool.retire(),Err(Error::Active));assert_eq!(pool.audit(),Ok(active)); if let Some(old)=previous.take(){assert_eq!(pool.__VALIDATE__(&old),Err(Error::Foreign));assert_eq!(pool.audit(),Ok(active));} let receipt=pool.complete(b,Used{id:0,length:__USED__}).unwrap();assert_eq!(receipt.owner(),7);assert_eq!(receipt.slot(),0);pool.__VALIDATE__(&receipt).unwrap(); let mut device=D{binding:b,reset:false,barrier:false};assert_eq!(pool.reset(&mut device),Ok(Reset{status:0,ready:0}));pool.retire().unwrap();assert_eq!(pool.snapshot().registry.pins,0);assert_eq!(pool.snapshot().registry.live_pages,0);assert_eq!(pool.snapshot().registry.backend_free_pages,before.backend_free_pages);previous=Some(receipt); } } #[test] fn wrong_cpu_and_context_leave_workspace_and_registry_untouched(){ let mut cases=std::vec::Vec::new();for expected in 0..4{for actual in 0..4{if expected!=actual{cases.push((expected,actual,0x3c0,4));}}} cases.extend([(4,4,0x3c0,4),(255,255,0x3c0,4),(0,0x100,0x3c0,4),(0,1<<32,0x3c0,4),(0,0,0,4),(1,1,0x340,4),(2,2,0x3c1,4),(0,0,0x3c0,0),(1,1,0x3c0,8),(2,2,0x3c0,12)]); for(expected,actual,mask,el)in cases{ hardware(actual,mask,el);let allocations=Rc::new(Cell::new(0));let mut r=Registry::<_,7>::new(1,Memory::new(allocations.clone())).unwrap();let before=r.snapshot();let mut w=Workspace::new();let id=unsafe{NEXT_ID}; assert_eq!(unsafe{Pool::initialize_on_cpu(&mut w,&mut r,7,binding()__LENGTHS__,expected)}.err(),Some(Error::Active));assert_eq!(r.snapshot(),before);assert_eq!(allocations.get(),0);assert_eq!(unsafe{NEXT_ID},id);empty(&w); } } #[test] fn legacy_initializer_stays_on_cpu_zero(){ for cpu in 1..4{hardware(cpu,0x3c0,4);let allocations=Rc::new(Cell::new(0));let mut r=Registry::<_,7>::new(1,Memory::new(allocations.clone())).unwrap();let before=r.snapshot();let mut w=Workspace::new();assert_eq!(Pool::initialize(&mut w,&mut r,7,binding()__LENGTHS__).err(),Some(Error::Active));assert_eq!(r.snapshot(),before);assert_eq!(allocations.get(),0);empty(&w);} hardware(0,0x3c0,4);let mut r=Registry::<_,7>::new(1,Memory::new(Rc::new(Cell::new(0)))).unwrap();let mut w=Workspace::new();let mut pool=Pool::initialize(&mut w,&mut r,7,binding()__LENGTHS__).unwrap();pool.retire().unwrap();assert_eq!(pool.snapshot().registry.live_pages,0); } #[test] fn exhausted_identity_never_wraps_or_allocates(){ hardware(3,0x3c0,4);let old=unsafe{NEXT_ID};unsafe{NEXT_ID=u64::MAX};let allocations=Rc::new(Cell::new(0));let mut r=Registry::<_,7>::new(1,Memory::new(allocations.clone())).unwrap();let before=r.snapshot();let mut w=Workspace::new(); assert_eq!(unsafe{Pool::initialize_on_cpu(&mut w,&mut r,7,binding()__LENGTHS__,3)}.err(),Some(Error::Identity));assert_eq!(unsafe{NEXT_ID},u64::MAX);assert_eq!(r.snapshot(),before);assert_eq!(allocations.get(),0);empty(&w);unsafe{NEXT_ID=old}; } }