Skip to content

Commit 8ab82b8

Browse files
committed
Auto merge of rust-lang#70525 - Centril:rollup-vj3esv3, r=Centril
Rollup of 3 pull requests Successful merges: - rust-lang#68692 (impl From<[T; N]> for Vec<T>) - rust-lang#70101 (Add copy bound to atomic & numeric intrinsics) - rust-lang#70506 (BTreeMap testing: introduce symbolic constants and use height consistently) Failed merges: r? @ghost
2 parents 8045865 + f31e563 commit 8ab82b8

File tree

6 files changed

+202
-160
lines changed

6 files changed

+202
-160
lines changed

src/liballoc/tests/btree/map.rs

+49-41
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,31 @@ use std::ops::Bound::{self, Excluded, Included, Unbounded};
77
use std::ops::RangeBounds;
88
use std::panic::catch_unwind;
99
use std::rc::Rc;
10-
use std::sync::atomic::{AtomicU32, Ordering};
10+
use std::sync::atomic::{AtomicUsize, Ordering};
1111

1212
use super::DeterministicRng;
1313

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+
1428
#[test]
1529
fn test_basic_large() {
1630
let mut map = BTreeMap::new();
1731
#[cfg(not(miri))] // Miri is too slow
1832
let size = 10000;
1933
#[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;
2135
assert_eq!(map.len(), 0);
2236

2337
for i in 0..size {
@@ -237,30 +251,26 @@ impl TryFrom<usize> for Align32 {
237251

238252
#[test]
239253
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.
245255
do_test_iter_mut_mutation::<u8>(0);
246256
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
249259
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);
252262
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);
255265
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);
258268
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);
261271
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);
264274
}
265275

266276
#[test]
@@ -376,12 +386,11 @@ fn test_range_small() {
376386
}
377387

378388
#[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.
383392

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();
385394
for &root in &[6, 7] {
386395
assert_eq!(range_keys(&map, (Excluded(root), Excluded(root + 1))), vec![]);
387396
assert_eq!(range_keys(&map, (Excluded(root), Included(root + 1))), vec![root + 1]);
@@ -519,7 +528,7 @@ fn test_range_1000() {
519528
#[cfg(not(miri))] // Miri is too slow
520529
let size = 1000;
521530
#[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;
523532
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
524533

525534
fn test(map: &BTreeMap<u32, u32>, size: u32, min: Bound<&u32>, max: Bound<&u32>) {
@@ -755,7 +764,7 @@ fn test_bad_zst() {
755764
#[test]
756765
fn test_clone() {
757766
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;
759768
assert_eq!(map.len(), 0);
760769

761770
for i in 0..size {
@@ -783,20 +792,19 @@ fn test_clone() {
783792
assert_eq!(map, map.clone());
784793
}
785794

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());
794802
}
795803

796804
#[test]
797805
fn test_clone_from() {
798806
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;
800808

801809
// Range to max_size inclusive, because i is the size of map1 being tested.
802810
for i in 0..=max_size {
@@ -1014,8 +1022,8 @@ fn test_split_off_large_random_sorted() {
10141022
}
10151023

10161024
#[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);
10191027

10201028
struct D;
10211029

@@ -1040,10 +1048,10 @@ fn test_into_iter_drop_leak_1() {
10401048
}
10411049

10421050
#[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);
10471055

10481056
struct D;
10491057
impl Drop for D {

src/liballoc/vec.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// ignore-tidy-filelength
12
//! A contiguous growable array type with heap-allocated contents, written
23
//! `Vec<T>`.
34
//!
@@ -2398,6 +2399,21 @@ impl<T: Clone> From<&mut [T]> for Vec<T> {
23982399
}
23992400
}
24002401

2402+
#[stable(feature = "vec_from_array", since = "1.44.0")]
2403+
impl<T, const N: usize> From<[T; N]> for Vec<T>
2404+
where
2405+
[T; N]: LengthAtMost32,
2406+
{
2407+
#[cfg(not(test))]
2408+
fn from(s: [T; N]) -> Vec<T> {
2409+
<[T]>::into_vec(box s)
2410+
}
2411+
#[cfg(test)]
2412+
fn from(s: [T; N]) -> Vec<T> {
2413+
crate::slice::into_vec(box s)
2414+
}
2415+
}
2416+
24012417
#[stable(feature = "vec_from_cow_slice", since = "1.14.0")]
24022418
impl<'a, T> From<Cow<'a, [T]>> for Vec<T>
24032419
where

0 commit comments

Comments
 (0)