@@ -544,3 +544,87 @@ impl<const PAGE_SIZE: usize> PageAllocator for BuddyPageAllocator<PAGE_SIZE> {
544544 return 0 ;
545545 }
546546}
547+
548+ #[ cfg( test) ]
549+ mod tests {
550+ use super :: * ;
551+ use alloc:: alloc:: { alloc, dealloc} ;
552+ use core:: alloc:: Layout ;
553+
554+ const TEST_HEAP_SIZE : usize = 16 * 1024 * 1024 ;
555+ const TEST_PAGE_SIZE : usize = 0x1000 ;
556+
557+ fn alloc_test_heap ( size : usize ) -> ( * mut u8 , Layout ) {
558+ let layout = Layout :: from_size_align ( size, TEST_PAGE_SIZE ) . unwrap ( ) ;
559+ let ptr = unsafe { alloc ( layout) } ;
560+ assert ! ( !ptr. is_null( ) ) ;
561+ ( ptr, layout)
562+ }
563+
564+ fn dealloc_test_heap ( ptr : * mut u8 , layout : Layout ) {
565+ unsafe { dealloc ( ptr, layout) } ;
566+ }
567+
568+ #[ test]
569+ fn test_buddy_allocator_init ( ) {
570+ let ( heap_ptr, heap_layout) = alloc_test_heap ( TEST_HEAP_SIZE ) ;
571+ let heap_addr = heap_ptr as usize ;
572+
573+ let mut allocator = BuddyPageAllocator :: < TEST_PAGE_SIZE > :: new ( ) ;
574+ allocator. init ( heap_addr, TEST_HEAP_SIZE ) ;
575+
576+ let addr = allocator. alloc_pages ( 1 , TEST_PAGE_SIZE ) ;
577+ assert ! ( addr. is_ok( ) ) ;
578+ if let Ok ( a) = addr {
579+ allocator. dealloc_pages ( a, 1 ) ;
580+ }
581+
582+ dealloc_test_heap ( heap_ptr, heap_layout) ;
583+ }
584+
585+ #[ test]
586+ fn test_buddy_allocator_multi_pages ( ) {
587+ let ( heap_ptr, heap_layout) = alloc_test_heap ( TEST_HEAP_SIZE ) ;
588+ let heap_addr = heap_ptr as usize ;
589+
590+ let mut allocator = BuddyPageAllocator :: < TEST_PAGE_SIZE > :: new ( ) ;
591+ allocator. init ( heap_addr, TEST_HEAP_SIZE ) ;
592+
593+ let addr1 = allocator. alloc_pages ( 4 , TEST_PAGE_SIZE ) . unwrap ( ) ;
594+ let addr2 = allocator. alloc_pages ( 8 , TEST_PAGE_SIZE ) . unwrap ( ) ;
595+ assert_ne ! ( addr1, addr2) ;
596+
597+ allocator. dealloc_pages ( addr1, 4 ) ;
598+ allocator. dealloc_pages ( addr2, 8 ) ;
599+
600+ dealloc_test_heap ( heap_ptr, heap_layout) ;
601+ }
602+
603+ #[ test]
604+ fn test_buddy_allocator_alignment ( ) {
605+ let ( heap_ptr, heap_layout) = alloc_test_heap ( TEST_HEAP_SIZE ) ;
606+ let heap_addr = heap_ptr as usize ;
607+
608+ let mut allocator = BuddyPageAllocator :: < TEST_PAGE_SIZE > :: new ( ) ;
609+ allocator. init ( heap_addr, TEST_HEAP_SIZE ) ;
610+
611+ let addr = allocator. alloc_pages ( 1 , TEST_PAGE_SIZE * 4 ) . unwrap ( ) ;
612+ assert_eq ! ( addr & ( TEST_PAGE_SIZE * 4 - 1 ) , 0 ) ;
613+
614+ allocator. dealloc_pages ( addr, 1 ) ;
615+ dealloc_test_heap ( heap_ptr, heap_layout) ;
616+ }
617+
618+ #[ test]
619+ fn test_buddy_allocator_zone_count ( ) {
620+ let ( heap_ptr, heap_layout) = alloc_test_heap ( TEST_HEAP_SIZE ) ;
621+ let heap_addr = heap_ptr as usize ;
622+
623+ let mut allocator = BuddyPageAllocator :: < TEST_PAGE_SIZE > :: new ( ) ;
624+ allocator. init ( heap_addr, TEST_HEAP_SIZE ) ;
625+
626+ assert_eq ! ( allocator. get_zone_count( ) , 1 ) ;
627+
628+ dealloc_test_heap ( heap_ptr, heap_layout) ;
629+ }
630+ }
0 commit comments