@@ -441,3 +441,85 @@ impl IndexMut<usize> for MemoryMapOwned {
441441 self . get_mut ( index) . unwrap ( )
442442 }
443443}
444+
445+ #[ cfg( test) ]
446+ mod tests {
447+ use super :: * ;
448+ use alloc:: vec:: Vec ;
449+ use core:: mem:: size_of;
450+
451+ const BASE_MMAP_UNSORTED : [ MemoryDescriptor ; 3 ] = [
452+ MemoryDescriptor {
453+ ty : MemoryType :: CONVENTIONAL ,
454+ phys_start : 0x3000 ,
455+ virt_start : 0x3000 ,
456+ page_count : 1 ,
457+ att : MemoryAttribute :: WRITE_BACK ,
458+ } ,
459+ MemoryDescriptor {
460+ ty : MemoryType :: CONVENTIONAL ,
461+ phys_start : 0x2000 ,
462+ virt_start : 0x2000 ,
463+ page_count : 1 ,
464+ att : MemoryAttribute :: WRITE_BACK ,
465+ } ,
466+ MemoryDescriptor {
467+ ty : MemoryType :: CONVENTIONAL ,
468+ phys_start : 0x1000 ,
469+ virt_start : 0x1000 ,
470+ page_count : 1 ,
471+ att : MemoryAttribute :: WRITE_BACK ,
472+ } ,
473+ ] ;
474+
475+ /// Returns a copy of [`BASE_MMAP_UNSORTED`] owned on the stack.
476+ fn new_mmap_memory ( ) -> [ MemoryDescriptor ; 3 ] {
477+ BASE_MMAP_UNSORTED
478+ }
479+
480+ fn mmap_raw < ' a > ( memory : & mut [ MemoryDescriptor ] ) -> ( & ' a mut [ u8 ] , MemoryMapMeta ) {
481+ let desc_size = size_of :: < MemoryDescriptor > ( ) ;
482+ let len = memory. len ( ) * desc_size;
483+ let ptr = memory. as_mut_ptr ( ) . cast :: < u8 > ( ) ;
484+ let slice = unsafe { core:: slice:: from_raw_parts_mut ( ptr, len) } ;
485+ let meta = MemoryMapMeta {
486+ map_size : len,
487+ desc_size,
488+ map_key : Default :: default ( ) ,
489+ desc_version : MemoryDescriptor :: VERSION ,
490+ } ;
491+ ( slice, meta)
492+ }
493+
494+ /// Basic sanity checks for the type [`MemoryMapRef`].
495+ #[ test]
496+ fn memory_map_ref ( ) {
497+ let mut memory = new_mmap_memory ( ) ;
498+ let ( mmap, meta) = mmap_raw ( & mut memory) ;
499+ let mmap = MemoryMapRef :: new ( mmap, meta, None ) . unwrap ( ) ;
500+
501+ assert_eq ! ( mmap. entries( ) . count( ) , 3 ) ;
502+ assert_eq ! (
503+ mmap. entries( ) . copied( ) . collect:: <Vec <_>>( ) . as_slice( ) ,
504+ & BASE_MMAP_UNSORTED
505+ ) ;
506+ assert ! ( !mmap. is_sorted( ) ) ;
507+ }
508+
509+ /// Basic sanity checks for the type [`MemoryMapRefMut`].
510+ #[ test]
511+ fn memory_map_ref_mut ( ) {
512+ let mut memory = new_mmap_memory ( ) ;
513+ let ( mmap, meta) = mmap_raw ( & mut memory) ;
514+ let mut mmap = MemoryMapRefMut :: new ( mmap, meta, None ) . unwrap ( ) ;
515+
516+ assert_eq ! ( mmap. entries( ) . count( ) , 3 ) ;
517+ assert_eq ! (
518+ mmap. entries( ) . copied( ) . collect:: <Vec <_>>( ) . as_slice( ) ,
519+ & BASE_MMAP_UNSORTED
520+ ) ;
521+ assert ! ( !mmap. is_sorted( ) ) ;
522+ mmap. sort ( ) ;
523+ assert ! ( mmap. is_sorted( ) ) ;
524+ }
525+ }
0 commit comments