@@ -617,218 +617,4 @@ impl SyncDroplessArena {
617617}
618618
619619#[ cfg( test) ]
620- mod tests {
621- extern crate test;
622- use test:: Bencher ;
623- use super :: TypedArena ;
624- use std:: cell:: Cell ;
625-
626- #[ allow( dead_code) ]
627- #[ derive( Debug , Eq , PartialEq ) ]
628- struct Point {
629- x : i32 ,
630- y : i32 ,
631- z : i32 ,
632- }
633-
634- #[ test]
635- pub fn test_unused ( ) {
636- let arena: TypedArena < Point > = TypedArena :: default ( ) ;
637- assert ! ( arena. chunks. borrow( ) . is_empty( ) ) ;
638- }
639-
640- #[ test]
641- fn test_arena_alloc_nested ( ) {
642- struct Inner {
643- value : u8 ,
644- }
645- struct Outer < ' a > {
646- inner : & ' a Inner ,
647- }
648- enum EI < ' e > {
649- I ( Inner ) ,
650- O ( Outer < ' e > ) ,
651- }
652-
653- struct Wrap < ' a > ( TypedArena < EI < ' a > > ) ;
654-
655- impl < ' a > Wrap < ' a > {
656- fn alloc_inner < F : Fn ( ) -> Inner > ( & self , f : F ) -> & Inner {
657- let r: & EI < ' _ > = self . 0 . alloc ( EI :: I ( f ( ) ) ) ;
658- if let & EI :: I ( ref i) = r {
659- i
660- } else {
661- panic ! ( "mismatch" ) ;
662- }
663- }
664- fn alloc_outer < F : Fn ( ) -> Outer < ' a > > ( & self , f : F ) -> & Outer < ' _ > {
665- let r: & EI < ' _ > = self . 0 . alloc ( EI :: O ( f ( ) ) ) ;
666- if let & EI :: O ( ref o) = r {
667- o
668- } else {
669- panic ! ( "mismatch" ) ;
670- }
671- }
672- }
673-
674- let arena = Wrap ( TypedArena :: default ( ) ) ;
675-
676- let result = arena. alloc_outer ( || Outer {
677- inner : arena. alloc_inner ( || Inner { value : 10 } ) ,
678- } ) ;
679-
680- assert_eq ! ( result. inner. value, 10 ) ;
681- }
682-
683- #[ test]
684- pub fn test_copy ( ) {
685- let arena = TypedArena :: default ( ) ;
686- for _ in 0 ..100000 {
687- arena. alloc ( Point { x : 1 , y : 2 , z : 3 } ) ;
688- }
689- }
690-
691- #[ bench]
692- pub fn bench_copy ( b : & mut Bencher ) {
693- let arena = TypedArena :: default ( ) ;
694- b. iter ( || arena. alloc ( Point { x : 1 , y : 2 , z : 3 } ) )
695- }
696-
697- #[ bench]
698- pub fn bench_copy_nonarena ( b : & mut Bencher ) {
699- b. iter ( || {
700- let _: Box < _ > = Box :: new ( Point { x : 1 , y : 2 , z : 3 } ) ;
701- } )
702- }
703-
704- #[ allow( dead_code) ]
705- struct Noncopy {
706- string : String ,
707- array : Vec < i32 > ,
708- }
709-
710- #[ test]
711- pub fn test_noncopy ( ) {
712- let arena = TypedArena :: default ( ) ;
713- for _ in 0 ..100000 {
714- arena. alloc ( Noncopy {
715- string : "hello world" . to_string ( ) ,
716- array : vec ! [ 1 , 2 , 3 , 4 , 5 ] ,
717- } ) ;
718- }
719- }
720-
721- #[ test]
722- pub fn test_typed_arena_zero_sized ( ) {
723- let arena = TypedArena :: default ( ) ;
724- for _ in 0 ..100000 {
725- arena. alloc ( ( ) ) ;
726- }
727- }
728-
729- #[ test]
730- pub fn test_typed_arena_clear ( ) {
731- let mut arena = TypedArena :: default ( ) ;
732- for _ in 0 ..10 {
733- arena. clear ( ) ;
734- for _ in 0 ..10000 {
735- arena. alloc ( Point { x : 1 , y : 2 , z : 3 } ) ;
736- }
737- }
738- }
739-
740- #[ bench]
741- pub fn bench_typed_arena_clear ( b : & mut Bencher ) {
742- let mut arena = TypedArena :: default ( ) ;
743- b. iter ( || {
744- arena. alloc ( Point { x : 1 , y : 2 , z : 3 } ) ;
745- arena. clear ( ) ;
746- } )
747- }
748-
749- // Drop tests
750-
751- struct DropCounter < ' a > {
752- count : & ' a Cell < u32 > ,
753- }
754-
755- impl Drop for DropCounter < ' _ > {
756- fn drop ( & mut self ) {
757- self . count . set ( self . count . get ( ) + 1 ) ;
758- }
759- }
760-
761- #[ test]
762- fn test_typed_arena_drop_count ( ) {
763- let counter = Cell :: new ( 0 ) ;
764- {
765- let arena: TypedArena < DropCounter < ' _ > > = TypedArena :: default ( ) ;
766- for _ in 0 ..100 {
767- // Allocate something with drop glue to make sure it doesn't leak.
768- arena. alloc ( DropCounter { count : & counter } ) ;
769- }
770- } ;
771- assert_eq ! ( counter. get( ) , 100 ) ;
772- }
773-
774- #[ test]
775- fn test_typed_arena_drop_on_clear ( ) {
776- let counter = Cell :: new ( 0 ) ;
777- let mut arena: TypedArena < DropCounter < ' _ > > = TypedArena :: default ( ) ;
778- for i in 0 ..10 {
779- for _ in 0 ..100 {
780- // Allocate something with drop glue to make sure it doesn't leak.
781- arena. alloc ( DropCounter { count : & counter } ) ;
782- }
783- arena. clear ( ) ;
784- assert_eq ! ( counter. get( ) , i * 100 + 100 ) ;
785- }
786- }
787-
788- thread_local ! {
789- static DROP_COUNTER : Cell <u32 > = Cell :: new( 0 )
790- }
791-
792- struct SmallDroppable ;
793-
794- impl Drop for SmallDroppable {
795- fn drop ( & mut self ) {
796- DROP_COUNTER . with ( |c| c. set ( c. get ( ) + 1 ) ) ;
797- }
798- }
799-
800- #[ test]
801- fn test_typed_arena_drop_small_count ( ) {
802- DROP_COUNTER . with ( |c| c. set ( 0 ) ) ;
803- {
804- let arena: TypedArena < SmallDroppable > = TypedArena :: default ( ) ;
805- for _ in 0 ..100 {
806- // Allocate something with drop glue to make sure it doesn't leak.
807- arena. alloc ( SmallDroppable ) ;
808- }
809- // dropping
810- } ;
811- assert_eq ! ( DROP_COUNTER . with( |c| c. get( ) ) , 100 ) ;
812- }
813-
814- #[ bench]
815- pub fn bench_noncopy ( b : & mut Bencher ) {
816- let arena = TypedArena :: default ( ) ;
817- b. iter ( || {
818- arena. alloc ( Noncopy {
819- string : "hello world" . to_string ( ) ,
820- array : vec ! [ 1 , 2 , 3 , 4 , 5 ] ,
821- } )
822- } )
823- }
824-
825- #[ bench]
826- pub fn bench_noncopy_nonarena ( b : & mut Bencher ) {
827- b. iter ( || {
828- let _: Box < _ > = Box :: new ( Noncopy {
829- string : "hello world" . to_string ( ) ,
830- array : vec ! [ 1 , 2 , 3 , 4 , 5 ] ,
831- } ) ;
832- } )
833- }
834- }
620+ mod tests;
0 commit comments