@@ -561,9 +561,11 @@ mod tests {
561
561
extern crate test;
562
562
use self :: test:: Bencher ;
563
563
use super :: { Arena , TypedArena } ;
564
+ use std:: cell:: Cell ;
564
565
use std:: rc:: Rc ;
565
566
566
567
#[ allow( dead_code) ]
568
+ #[ derive( Debug , Eq , PartialEq ) ]
567
569
struct Point {
568
570
x : i32 ,
569
571
y : i32 ,
@@ -668,11 +670,16 @@ mod tests {
668
670
#[ test]
669
671
pub fn test_arena_zero_sized ( ) {
670
672
let arena = Arena :: new ( ) ;
673
+ let mut points = vec ! [ ] ;
671
674
for _ in 0 ..1000 {
672
675
for _ in 0 ..100 {
673
676
arena. alloc ( || ( ) ) ;
674
677
}
675
- arena. alloc ( || Point { x : 1 , y : 2 , z : 3 } ) ;
678
+ let point = arena. alloc ( || Point { x : 1 , y : 2 , z : 3 } ) ;
679
+ points. push ( point) ;
680
+ }
681
+ for point in & points {
682
+ assert_eq ! ( * * point, Point { x: 1 , y: 2 , z: 3 } ) ;
676
683
}
677
684
}
678
685
@@ -747,6 +754,124 @@ mod tests {
747
754
} ) ;
748
755
}
749
756
757
+ // Drop tests
758
+
759
+ struct DropCounter < ' a > {
760
+ count : & ' a Cell < u32 > ,
761
+ }
762
+
763
+ impl < ' a > Drop for DropCounter < ' a > {
764
+ fn drop ( & mut self ) {
765
+ self . count . set ( self . count . get ( ) + 1 ) ;
766
+ }
767
+ }
768
+
769
+ #[ test]
770
+ fn test_arena_drop_count ( ) {
771
+ let counter = Cell :: new ( 0 ) ;
772
+ {
773
+ let arena = Arena :: new ( ) ;
774
+ for _ in 0 ..100 {
775
+ // Allocate something with drop glue to make sure it doesn't leak.
776
+ arena. alloc ( || DropCounter { count : & counter } ) ;
777
+ // Allocate something with funny size and alignment, to keep
778
+ // things interesting.
779
+ arena. alloc ( || [ 0u8 , 1u8 , 2u8 ] ) ;
780
+ }
781
+ // dropping
782
+ } ;
783
+ assert_eq ! ( counter. get( ) , 100 ) ;
784
+ }
785
+
786
+ #[ test]
787
+ fn test_arena_drop_on_clear ( ) {
788
+ let counter = Cell :: new ( 0 ) ;
789
+ for i in 0 ..10 {
790
+ let mut arena = Arena :: new ( ) ;
791
+ for _ in 0 ..100 {
792
+ // Allocate something with drop glue to make sure it doesn't leak.
793
+ arena. alloc ( || DropCounter { count : & counter } ) ;
794
+ // Allocate something with funny size and alignment, to keep
795
+ // things interesting.
796
+ arena. alloc ( || [ 0u8 , 1u8 , 2u8 ] ) ;
797
+ }
798
+ arena. clear ( ) ;
799
+ assert_eq ! ( counter. get( ) , i * 100 + 100 ) ;
800
+ }
801
+ }
802
+
803
+ #[ test]
804
+ fn test_typed_arena_drop_count ( ) {
805
+ let counter = Cell :: new ( 0 ) ;
806
+ {
807
+ let arena: TypedArena < DropCounter > = TypedArena :: new ( ) ;
808
+ for _ in 0 ..100 {
809
+ // Allocate something with drop glue to make sure it doesn't leak.
810
+ arena. alloc ( DropCounter { count : & counter } ) ;
811
+ }
812
+ } ;
813
+ assert_eq ! ( counter. get( ) , 100 ) ;
814
+ }
815
+
816
+ #[ test]
817
+ fn test_typed_arena_drop_on_clear ( ) {
818
+ let counter = Cell :: new ( 0 ) ;
819
+ let mut arena: TypedArena < DropCounter > = TypedArena :: new ( ) ;
820
+ for i in 0 ..10 {
821
+ for _ in 0 ..100 {
822
+ // Allocate something with drop glue to make sure it doesn't leak.
823
+ arena. alloc ( DropCounter { count : & counter } ) ;
824
+ }
825
+ arena. clear ( ) ;
826
+ assert_eq ! ( counter. get( ) , i * 100 + 100 ) ;
827
+ }
828
+ }
829
+
830
+ thread_local ! {
831
+ static DROP_COUNTER : Cell <u32 > = Cell :: new( 0 )
832
+ }
833
+
834
+ struct SmallDroppable ;
835
+
836
+ impl Drop for SmallDroppable {
837
+ fn drop ( & mut self ) {
838
+ DROP_COUNTER . with ( |c| c. set ( c. get ( ) + 1 ) ) ;
839
+ }
840
+ }
841
+
842
+ #[ test]
843
+ fn test_arena_drop_small_count ( ) {
844
+ DROP_COUNTER . with ( |c| c. set ( 0 ) ) ;
845
+ {
846
+ let arena = Arena :: new ( ) ;
847
+ for _ in 0 ..10 {
848
+ for _ in 0 ..10 {
849
+ // Allocate something with drop glue to make sure it doesn't leak.
850
+ arena. alloc ( || SmallDroppable ) ;
851
+ }
852
+ // Allocate something with funny size and alignment, to keep
853
+ // things interesting.
854
+ arena. alloc ( || [ 0u8 , 1u8 , 2u8 ] ) ;
855
+ }
856
+ // dropping
857
+ } ;
858
+ assert_eq ! ( DROP_COUNTER . with( |c| c. get( ) ) , 100 ) ;
859
+ }
860
+
861
+ #[ test]
862
+ fn test_typed_arena_drop_small_count ( ) {
863
+ DROP_COUNTER . with ( |c| c. set ( 0 ) ) ;
864
+ {
865
+ let arena: TypedArena < SmallDroppable > = TypedArena :: new ( ) ;
866
+ for _ in 0 ..100 {
867
+ // Allocate something with drop glue to make sure it doesn't leak.
868
+ arena. alloc ( SmallDroppable ) ;
869
+ }
870
+ // dropping
871
+ } ;
872
+ assert_eq ! ( DROP_COUNTER . with( |c| c. get( ) ) , 100 ) ;
873
+ }
874
+
750
875
#[ bench]
751
876
pub fn bench_noncopy ( b : & mut Bencher ) {
752
877
let arena = TypedArena :: new ( ) ;
0 commit comments