Skip to content

Commit d2aa3de

Browse files
authored
Rollup merge of #135621 - bjorn3:move_tests_to_stdtests, r=Noratrieb
Move some std tests to integration tests Unit tests directly inside of standard library crates require a very fragile way of building that is hard to reproduce outside of bootstrap. Follow up to #133859
2 parents 2a8a191 + cc7e3a6 commit d2aa3de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+635
-640
lines changed

Diff for: library/std/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ license = "MIT OR Apache-2.0"
77
repository = "https://github.com/rust-lang/rust.git"
88
description = "The Rust Standard Library"
99
edition = "2021"
10+
autobenches = false
1011

1112
[lib]
1213
crate-type = ["dylib", "rlib"]
@@ -130,6 +131,18 @@ name = "pipe-subprocess"
130131
path = "tests/pipe_subprocess.rs"
131132
harness = false
132133

134+
[[test]]
135+
name = "sync"
136+
path = "tests/sync/lib.rs"
137+
138+
[[test]]
139+
name = "floats"
140+
path = "tests/floats/lib.rs"
141+
142+
[[test]]
143+
name = "thread_local"
144+
path = "tests/thread_local/lib.rs"
145+
133146
[[bench]]
134147
name = "stdbenches"
135148
path = "benches/lib.rs"

Diff for: library/std/benches/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
extern crate test;
66

77
mod hash;
8+
mod path;
9+
mod time;

Diff for: library/std/benches/path.rs

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
use core::hint::black_box;
2+
use std::collections::{BTreeSet, HashSet};
3+
use std::hash::{DefaultHasher, Hash, Hasher};
4+
use std::path::*;
5+
6+
#[bench]
7+
#[cfg_attr(miri, ignore)] // Miri isn't fast...
8+
fn bench_path_cmp_fast_path_buf_sort(b: &mut test::Bencher) {
9+
let prefix = "my/home";
10+
let mut paths: Vec<_> =
11+
(0..1000).map(|num| PathBuf::from(prefix).join(format!("file {num}.rs"))).collect();
12+
13+
paths.sort();
14+
15+
b.iter(|| {
16+
black_box(paths.as_mut_slice()).sort_unstable();
17+
});
18+
}
19+
20+
#[bench]
21+
#[cfg_attr(miri, ignore)] // Miri isn't fast...
22+
fn bench_path_cmp_fast_path_long(b: &mut test::Bencher) {
23+
let prefix = "/my/home/is/my/castle/and/my/castle/has/a/rusty/workbench/";
24+
let paths: Vec<_> =
25+
(0..1000).map(|num| PathBuf::from(prefix).join(format!("file {num}.rs"))).collect();
26+
27+
let mut set = BTreeSet::new();
28+
29+
paths.iter().for_each(|p| {
30+
set.insert(p.as_path());
31+
});
32+
33+
b.iter(|| {
34+
set.remove(paths[500].as_path());
35+
set.insert(paths[500].as_path());
36+
});
37+
}
38+
39+
#[bench]
40+
#[cfg_attr(miri, ignore)] // Miri isn't fast...
41+
fn bench_path_cmp_fast_path_short(b: &mut test::Bencher) {
42+
let prefix = "my/home";
43+
let paths: Vec<_> =
44+
(0..1000).map(|num| PathBuf::from(prefix).join(format!("file {num}.rs"))).collect();
45+
46+
let mut set = BTreeSet::new();
47+
48+
paths.iter().for_each(|p| {
49+
set.insert(p.as_path());
50+
});
51+
52+
b.iter(|| {
53+
set.remove(paths[500].as_path());
54+
set.insert(paths[500].as_path());
55+
});
56+
}
57+
58+
#[bench]
59+
#[cfg_attr(miri, ignore)] // Miri isn't fast...
60+
fn bench_path_hashset(b: &mut test::Bencher) {
61+
let prefix = "/my/home/is/my/castle/and/my/castle/has/a/rusty/workbench/";
62+
let paths: Vec<_> =
63+
(0..1000).map(|num| PathBuf::from(prefix).join(format!("file {num}.rs"))).collect();
64+
65+
let mut set = HashSet::new();
66+
67+
paths.iter().for_each(|p| {
68+
set.insert(p.as_path());
69+
});
70+
71+
b.iter(|| {
72+
set.remove(paths[500].as_path());
73+
set.insert(black_box(paths[500].as_path()))
74+
});
75+
}
76+
77+
#[bench]
78+
#[cfg_attr(miri, ignore)] // Miri isn't fast...
79+
fn bench_path_hashset_miss(b: &mut test::Bencher) {
80+
let prefix = "/my/home/is/my/castle/and/my/castle/has/a/rusty/workbench/";
81+
let paths: Vec<_> =
82+
(0..1000).map(|num| PathBuf::from(prefix).join(format!("file {num}.rs"))).collect();
83+
84+
let mut set = HashSet::new();
85+
86+
paths.iter().for_each(|p| {
87+
set.insert(p.as_path());
88+
});
89+
90+
let probe = PathBuf::from(prefix).join("other");
91+
92+
b.iter(|| set.remove(black_box(probe.as_path())));
93+
}
94+
95+
#[bench]
96+
fn bench_hash_path_short(b: &mut test::Bencher) {
97+
let mut hasher = DefaultHasher::new();
98+
let path = Path::new("explorer.exe");
99+
100+
b.iter(|| black_box(path).hash(&mut hasher));
101+
102+
black_box(hasher.finish());
103+
}
104+
105+
#[bench]
106+
fn bench_hash_path_long(b: &mut test::Bencher) {
107+
let mut hasher = DefaultHasher::new();
108+
let path =
109+
Path::new("/aaaaa/aaaaaa/./../aaaaaaaa/bbbbbbbbbbbbb/ccccccccccc/ddddddddd/eeeeeee.fff");
110+
111+
b.iter(|| black_box(path).hash(&mut hasher));
112+
113+
black_box(hasher.finish());
114+
}

Diff for: library/std/benches/time.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::time::Instant;
2+
3+
#[cfg(not(target_arch = "wasm32"))]
4+
use test::{Bencher, black_box};
5+
6+
macro_rules! bench_instant_threaded {
7+
($bench_name:ident, $thread_count:expr) => {
8+
#[bench]
9+
#[cfg(not(target_arch = "wasm32"))]
10+
fn $bench_name(b: &mut Bencher) -> std::thread::Result<()> {
11+
use std::sync::Arc;
12+
use std::sync::atomic::{AtomicBool, Ordering};
13+
14+
let running = Arc::new(AtomicBool::new(true));
15+
16+
let threads: Vec<_> = (0..$thread_count)
17+
.map(|_| {
18+
let flag = Arc::clone(&running);
19+
std::thread::spawn(move || {
20+
while flag.load(Ordering::Relaxed) {
21+
black_box(Instant::now());
22+
}
23+
})
24+
})
25+
.collect();
26+
27+
b.iter(|| {
28+
let a = Instant::now();
29+
let b = Instant::now();
30+
assert!(b >= a);
31+
});
32+
33+
running.store(false, Ordering::Relaxed);
34+
35+
for t in threads {
36+
t.join()?;
37+
}
38+
Ok(())
39+
}
40+
};
41+
}
42+
43+
bench_instant_threaded!(instant_contention_01_threads, 0);
44+
bench_instant_threaded!(instant_contention_02_threads, 1);
45+
bench_instant_threaded!(instant_contention_04_threads, 3);
46+
bench_instant_threaded!(instant_contention_08_threads, 7);
47+
bench_instant_threaded!(instant_contention_16_threads, 15);

Diff for: library/std/src/env.rs

-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
1111
#![stable(feature = "env", since = "1.0.0")]
1212

13-
#[cfg(test)]
14-
mod tests;
15-
1613
use crate::error::Error;
1714
use crate::ffi::{OsStr, OsString};
1815
use crate::path::{Path, PathBuf};

Diff for: library/std/src/env/tests.rs

-120
This file was deleted.

Diff for: library/std/src/error.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#![doc = include_str!("../../core/src/error.md")]
22
#![stable(feature = "rust1", since = "1.0.0")]
33

4-
#[cfg(test)]
5-
mod tests;
6-
74
#[stable(feature = "rust1", since = "1.0.0")]
85
pub use core::error::Error;
96
#[unstable(feature = "error_generic_member_access", issue = "99301")]

Diff for: library/std/src/f128.rs

-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
//!
55
//! Mathematically significant numbers are provided in the `consts` sub-module.
66
7-
#[cfg(test)]
8-
mod tests;
9-
107
#[unstable(feature = "f128", issue = "116909")]
118
pub use core::f128::consts;
129

Diff for: library/std/src/f16.rs

-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
//!
55
//! Mathematically significant numbers are provided in the `consts` sub-module.
66
7-
#[cfg(test)]
8-
mod tests;
9-
107
#[unstable(feature = "f16", issue = "116909")]
118
pub use core::f16::consts;
129

Diff for: library/std/src/f32.rs

-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
#![stable(feature = "rust1", since = "1.0.0")]
1313
#![allow(missing_docs)]
1414

15-
#[cfg(test)]
16-
mod tests;
17-
1815
#[stable(feature = "rust1", since = "1.0.0")]
1916
#[allow(deprecated, deprecated_in_future)]
2017
pub use core::f32::{

Diff for: library/std/src/f64.rs

-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
#![stable(feature = "rust1", since = "1.0.0")]
1313
#![allow(missing_docs)]
1414

15-
#[cfg(test)]
16-
mod tests;
17-
1815
#[stable(feature = "rust1", since = "1.0.0")]
1916
#[allow(deprecated, deprecated_in_future)]
2017
pub use core::f64::{

Diff for: library/std/src/macros.rs

-15
Original file line numberDiff line numberDiff line change
@@ -372,18 +372,3 @@ macro_rules! dbg {
372372
($($crate::dbg!($val)),+,)
373373
};
374374
}
375-
376-
/// Verify that floats are within a tolerance of each other, 1.0e-6 by default.
377-
#[cfg(test)]
378-
macro_rules! assert_approx_eq {
379-
($a:expr, $b:expr) => {{ assert_approx_eq!($a, $b, 1.0e-6) }};
380-
($a:expr, $b:expr, $lim:expr) => {{
381-
let (a, b) = (&$a, &$b);
382-
let diff = (*a - *b).abs();
383-
assert!(
384-
diff < $lim,
385-
"{a:?} is not approximately equal to {b:?} (threshold {lim:?}, difference {diff:?})",
386-
lim = $lim
387-
);
388-
}};
389-
}

0 commit comments

Comments
 (0)