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