@@ -7,17 +7,31 @@ use std::ops::Bound::{self, Excluded, Included, Unbounded};
7
7
use std:: ops:: RangeBounds ;
8
8
use std:: panic:: catch_unwind;
9
9
use std:: rc:: Rc ;
10
- use std:: sync:: atomic:: { AtomicU32 , Ordering } ;
10
+ use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
11
11
12
12
use super :: DeterministicRng ;
13
13
14
+ // Value of node::CAPACITY, thus capacity of a tree with a single level,
15
+ // i.e. a tree who's root is a leaf node at height 0.
16
+ const NODE_CAPACITY : usize = 11 ;
17
+
18
+ // Minimum number of elements to insert in order to guarantee a tree with 2 levels,
19
+ // i.e. a tree who's root is an internal node at height 1, with edges to leaf nodes.
20
+ // It's not the minimum size: removing an element from such a tree does not always reduce height.
21
+ const MIN_INSERTS_HEIGHT_1 : usize = NODE_CAPACITY + 1 ;
22
+
23
+ // Minimum number of elements to insert in order to guarantee a tree with 3 levels,
24
+ // i.e. a tree who's root is an internal node at height 2, with edges to more internal nodes.
25
+ // It's not the minimum size: removing an element from such a tree does not always reduce height.
26
+ const MIN_INSERTS_HEIGHT_2 : usize = NODE_CAPACITY + ( NODE_CAPACITY + 1 ) * NODE_CAPACITY + 1 ;
27
+
14
28
#[ test]
15
29
fn test_basic_large ( ) {
16
30
let mut map = BTreeMap :: new ( ) ;
17
31
#[ cfg( not( miri) ) ] // Miri is too slow
18
32
let size = 10000 ;
19
33
#[ cfg( miri) ]
20
- let size = 144 ; // to obtain height 3 tree (having edges to both kinds of nodes)
34
+ let size = MIN_INSERTS_HEIGHT_2 ;
21
35
assert_eq ! ( map. len( ) , 0 ) ;
22
36
23
37
for i in 0 ..size {
@@ -237,30 +251,26 @@ impl TryFrom<usize> for Align32 {
237
251
238
252
#[ test]
239
253
fn test_iter_mut_mutation ( ) {
240
- // Check many alignments because various fields precede array in NodeHeader.
241
- // Check with size 0 which should not iterate at all.
242
- // Check with size 1 for a tree with one kind of node (root = leaf).
243
- // Check with size 12 for a tree with two kinds of nodes (root and leaves).
244
- // Check with size 144 for a tree with all kinds of nodes (root, internals and leaves).
254
+ // Check many alignments and trees with roots at various heights.
245
255
do_test_iter_mut_mutation :: < u8 > ( 0 ) ;
246
256
do_test_iter_mut_mutation :: < u8 > ( 1 ) ;
247
- do_test_iter_mut_mutation :: < u8 > ( 12 ) ;
248
- do_test_iter_mut_mutation :: < u8 > ( 127 ) ; // not enough unique values to test 144
257
+ do_test_iter_mut_mutation :: < u8 > ( MIN_INSERTS_HEIGHT_1 ) ;
258
+ do_test_iter_mut_mutation :: < u8 > ( 127 ) ; // not enough unique values to test MIN_INSERTS_HEIGHT_2
249
259
do_test_iter_mut_mutation :: < u16 > ( 1 ) ;
250
- do_test_iter_mut_mutation :: < u16 > ( 12 ) ;
251
- do_test_iter_mut_mutation :: < u16 > ( 144 ) ;
260
+ do_test_iter_mut_mutation :: < u16 > ( MIN_INSERTS_HEIGHT_1 ) ;
261
+ do_test_iter_mut_mutation :: < u16 > ( MIN_INSERTS_HEIGHT_2 ) ;
252
262
do_test_iter_mut_mutation :: < u32 > ( 1 ) ;
253
- do_test_iter_mut_mutation :: < u32 > ( 12 ) ;
254
- do_test_iter_mut_mutation :: < u32 > ( 144 ) ;
263
+ do_test_iter_mut_mutation :: < u32 > ( MIN_INSERTS_HEIGHT_1 ) ;
264
+ do_test_iter_mut_mutation :: < u32 > ( MIN_INSERTS_HEIGHT_2 ) ;
255
265
do_test_iter_mut_mutation :: < u64 > ( 1 ) ;
256
- do_test_iter_mut_mutation :: < u64 > ( 12 ) ;
257
- do_test_iter_mut_mutation :: < u64 > ( 144 ) ;
266
+ do_test_iter_mut_mutation :: < u64 > ( MIN_INSERTS_HEIGHT_1 ) ;
267
+ do_test_iter_mut_mutation :: < u64 > ( MIN_INSERTS_HEIGHT_2 ) ;
258
268
do_test_iter_mut_mutation :: < u128 > ( 1 ) ;
259
- do_test_iter_mut_mutation :: < u128 > ( 12 ) ;
260
- do_test_iter_mut_mutation :: < u128 > ( 144 ) ;
269
+ do_test_iter_mut_mutation :: < u128 > ( MIN_INSERTS_HEIGHT_1 ) ;
270
+ do_test_iter_mut_mutation :: < u128 > ( MIN_INSERTS_HEIGHT_2 ) ;
261
271
do_test_iter_mut_mutation :: < Align32 > ( 1 ) ;
262
- do_test_iter_mut_mutation :: < Align32 > ( 12 ) ;
263
- do_test_iter_mut_mutation :: < Align32 > ( 144 ) ;
272
+ do_test_iter_mut_mutation :: < Align32 > ( MIN_INSERTS_HEIGHT_1 ) ;
273
+ do_test_iter_mut_mutation :: < Align32 > ( MIN_INSERTS_HEIGHT_2 ) ;
264
274
}
265
275
266
276
#[ test]
@@ -376,12 +386,11 @@ fn test_range_small() {
376
386
}
377
387
378
388
#[ test]
379
- fn test_range_height_2 ( ) {
380
- // Assuming that node.CAPACITY is 11, having 12 pairs implies a height 2 tree
381
- // with 2 leaves. Depending on details we don't want or need to rely upon,
382
- // the single key at the root will be 6 or 7.
389
+ fn test_range_height_1 ( ) {
390
+ // Tests tree with a root and 2 leaves. Depending on details we don't want or need
391
+ // to rely upon, the single key at the root will be 6 or 7.
383
392
384
- let map: BTreeMap < _ , _ > = ( 1 ..=12 ) . map ( |i| ( i, i) ) . collect ( ) ;
393
+ let map: BTreeMap < _ , _ > = ( 1 ..=MIN_INSERTS_HEIGHT_1 as i32 ) . map ( |i| ( i, i) ) . collect ( ) ;
385
394
for & root in & [ 6 , 7 ] {
386
395
assert_eq ! ( range_keys( & map, ( Excluded ( root) , Excluded ( root + 1 ) ) ) , vec![ ] ) ;
387
396
assert_eq ! ( range_keys( & map, ( Excluded ( root) , Included ( root + 1 ) ) ) , vec![ root + 1 ] ) ;
@@ -519,7 +528,7 @@ fn test_range_1000() {
519
528
#[ cfg( not( miri) ) ] // Miri is too slow
520
529
let size = 1000 ;
521
530
#[ cfg( miri) ]
522
- let size = 144 ; // to obtain height 3 tree (having edges to both kinds of nodes)
531
+ let size = MIN_INSERTS_HEIGHT_2 ;
523
532
let map: BTreeMap < _ , _ > = ( 0 ..size) . map ( |i| ( i, i) ) . collect ( ) ;
524
533
525
534
fn test ( map : & BTreeMap < u32 , u32 > , size : u32 , min : Bound < & u32 > , max : Bound < & u32 > ) {
@@ -755,7 +764,7 @@ fn test_bad_zst() {
755
764
#[ test]
756
765
fn test_clone ( ) {
757
766
let mut map = BTreeMap :: new ( ) ;
758
- let size = 12 ; // to obtain height 2 tree (having edges to leaf nodes)
767
+ let size = MIN_INSERTS_HEIGHT_1 ;
759
768
assert_eq ! ( map. len( ) , 0 ) ;
760
769
761
770
for i in 0 ..size {
@@ -783,20 +792,19 @@ fn test_clone() {
783
792
assert_eq ! ( map, map. clone( ) ) ;
784
793
}
785
794
786
- // Full 2-level and minimal 3-level tree (sizes 143, 144 -- the only ones we clone for).
787
- for i in 1 ..=144 {
788
- assert_eq ! ( map. insert( i, i) , None ) ;
789
- assert_eq ! ( map. len( ) , i) ;
790
- if i >= 143 {
791
- assert_eq ! ( map, map. clone( ) ) ;
792
- }
793
- }
795
+ // Test a tree with 2 chock-full levels and a tree with 3 levels.
796
+ map = ( 1 ..MIN_INSERTS_HEIGHT_2 ) . map ( |i| ( i, i) ) . collect ( ) ;
797
+ assert_eq ! ( map. len( ) , MIN_INSERTS_HEIGHT_2 - 1 ) ;
798
+ assert_eq ! ( map, map. clone( ) ) ;
799
+ map. insert ( 0 , 0 ) ;
800
+ assert_eq ! ( map. len( ) , MIN_INSERTS_HEIGHT_2 ) ;
801
+ assert_eq ! ( map, map. clone( ) ) ;
794
802
}
795
803
796
804
#[ test]
797
805
fn test_clone_from ( ) {
798
806
let mut map1 = BTreeMap :: new ( ) ;
799
- let max_size = 12 ; // to obtain height 2 tree (having edges to leaf nodes)
807
+ let max_size = MIN_INSERTS_HEIGHT_1 ;
800
808
801
809
// Range to max_size inclusive, because i is the size of map1 being tested.
802
810
for i in 0 ..=max_size {
@@ -1014,8 +1022,8 @@ fn test_split_off_large_random_sorted() {
1014
1022
}
1015
1023
1016
1024
#[ test]
1017
- fn test_into_iter_drop_leak_1 ( ) {
1018
- static DROPS : AtomicU32 = AtomicU32 :: new ( 0 ) ;
1025
+ fn test_into_iter_drop_leak_height_0 ( ) {
1026
+ static DROPS : AtomicUsize = AtomicUsize :: new ( 0 ) ;
1019
1027
1020
1028
struct D ;
1021
1029
@@ -1040,10 +1048,10 @@ fn test_into_iter_drop_leak_1() {
1040
1048
}
1041
1049
1042
1050
#[ test]
1043
- fn test_into_iter_drop_leak_2 ( ) {
1044
- let size = 12 ; // to obtain tree with 2 levels (having edges to leaf nodes)
1045
- static DROPS : AtomicU32 = AtomicU32 :: new ( 0 ) ;
1046
- static PANIC_POINT : AtomicU32 = AtomicU32 :: new ( 0 ) ;
1051
+ fn test_into_iter_drop_leak_height_1 ( ) {
1052
+ let size = MIN_INSERTS_HEIGHT_1 ;
1053
+ static DROPS : AtomicUsize = AtomicUsize :: new ( 0 ) ;
1054
+ static PANIC_POINT : AtomicUsize = AtomicUsize :: new ( 0 ) ;
1047
1055
1048
1056
struct D ;
1049
1057
impl Drop for D {
0 commit comments