summaryrefslogtreecommitdiffstats
path: root/third_party/rust/rayon-core/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/rayon-core/tests
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/rayon-core/tests')
-rw-r--r--third_party/rust/rayon-core/tests/double_init_fail.rs14
-rw-r--r--third_party/rust/rayon-core/tests/init_zero_threads.rs9
-rw-r--r--third_party/rust/rayon-core/tests/scope_join.rs45
-rw-r--r--third_party/rust/rayon-core/tests/scoped_threadpool.rs96
-rw-r--r--third_party/rust/rayon-core/tests/simple_panic.rs7
-rw-r--r--third_party/rust/rayon-core/tests/stack_overflow_crash.rs95
6 files changed, 266 insertions, 0 deletions
diff --git a/third_party/rust/rayon-core/tests/double_init_fail.rs b/third_party/rust/rayon-core/tests/double_init_fail.rs
new file mode 100644
index 0000000000..b3ddbeb888
--- /dev/null
+++ b/third_party/rust/rayon-core/tests/double_init_fail.rs
@@ -0,0 +1,14 @@
+use rayon_core::ThreadPoolBuilder;
+use std::error::Error;
+
+#[test]
+fn double_init_fail() {
+ let result1 = ThreadPoolBuilder::new().build_global();
+ assert!(result1.is_ok());
+ let err = ThreadPoolBuilder::new().build_global().unwrap_err();
+ assert!(err.source().is_none());
+ assert_eq!(
+ err.to_string(),
+ "The global thread pool has already been initialized.",
+ );
+}
diff --git a/third_party/rust/rayon-core/tests/init_zero_threads.rs b/third_party/rust/rayon-core/tests/init_zero_threads.rs
new file mode 100644
index 0000000000..ebd73c585d
--- /dev/null
+++ b/third_party/rust/rayon-core/tests/init_zero_threads.rs
@@ -0,0 +1,9 @@
+use rayon_core::ThreadPoolBuilder;
+
+#[test]
+fn init_zero_threads() {
+ ThreadPoolBuilder::new()
+ .num_threads(0)
+ .build_global()
+ .unwrap();
+}
diff --git a/third_party/rust/rayon-core/tests/scope_join.rs b/third_party/rust/rayon-core/tests/scope_join.rs
new file mode 100644
index 0000000000..9d88133bc5
--- /dev/null
+++ b/third_party/rust/rayon-core/tests/scope_join.rs
@@ -0,0 +1,45 @@
+/// Test that one can emulate join with `scope`:
+fn pseudo_join<F, G>(f: F, g: G)
+where
+ F: FnOnce() + Send,
+ G: FnOnce() + Send,
+{
+ rayon_core::scope(|s| {
+ s.spawn(|_| g());
+ f();
+ });
+}
+
+fn quick_sort<T: PartialOrd + Send>(v: &mut [T]) {
+ if v.len() <= 1 {
+ return;
+ }
+
+ let mid = partition(v);
+ let (lo, hi) = v.split_at_mut(mid);
+ pseudo_join(|| quick_sort(lo), || quick_sort(hi));
+}
+
+fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
+ let pivot = v.len() - 1;
+ let mut i = 0;
+ for j in 0..pivot {
+ if v[j] <= v[pivot] {
+ v.swap(i, j);
+ i += 1;
+ }
+ }
+ v.swap(i, pivot);
+ i
+}
+
+fn is_sorted<T: Send + Ord>(v: &[T]) -> bool {
+ (1..v.len()).all(|i| v[i - 1] <= v[i])
+}
+
+#[test]
+fn scope_join() {
+ let mut v: Vec<i32> = (0..256).rev().collect();
+ quick_sort(&mut v);
+ assert!(is_sorted(&v));
+}
diff --git a/third_party/rust/rayon-core/tests/scoped_threadpool.rs b/third_party/rust/rayon-core/tests/scoped_threadpool.rs
new file mode 100644
index 0000000000..db3d0b8743
--- /dev/null
+++ b/third_party/rust/rayon-core/tests/scoped_threadpool.rs
@@ -0,0 +1,96 @@
+use crossbeam_utils::thread;
+use rayon_core::ThreadPoolBuilder;
+
+#[derive(PartialEq, Eq, Debug)]
+struct Local(i32);
+
+scoped_tls::scoped_thread_local!(static LOCAL: Local);
+
+#[test]
+fn missing_scoped_tls() {
+ LOCAL.set(&Local(42), || {
+ let pool = ThreadPoolBuilder::new()
+ .build()
+ .expect("thread pool created");
+
+ // `LOCAL` is not set in the pool.
+ pool.install(|| {
+ assert!(!LOCAL.is_set());
+ });
+ });
+}
+
+#[test]
+fn spawn_scoped_tls_threadpool() {
+ LOCAL.set(&Local(42), || {
+ LOCAL.with(|x| {
+ thread::scope(|scope| {
+ let pool = ThreadPoolBuilder::new()
+ .spawn_handler(move |thread| {
+ scope
+ .builder()
+ .spawn(move |_| {
+ // Borrow the same local value in the thread pool.
+ LOCAL.set(x, || thread.run())
+ })
+ .map(|_| ())
+ })
+ .build()
+ .expect("thread pool created");
+
+ // The pool matches our local value.
+ pool.install(|| {
+ assert!(LOCAL.is_set());
+ LOCAL.with(|y| {
+ assert_eq!(x, y);
+ });
+ });
+
+ // If we change our local value, the pool is not affected.
+ LOCAL.set(&Local(-1), || {
+ pool.install(|| {
+ assert!(LOCAL.is_set());
+ LOCAL.with(|y| {
+ assert_eq!(x, y);
+ });
+ });
+ });
+ })
+ .expect("scope threads ok");
+ // `thread::scope` will wait for the threads to exit before returning.
+ });
+ });
+}
+
+#[test]
+fn build_scoped_tls_threadpool() {
+ LOCAL.set(&Local(42), || {
+ LOCAL.with(|x| {
+ ThreadPoolBuilder::new()
+ .build_scoped(
+ move |thread| LOCAL.set(x, || thread.run()),
+ |pool| {
+ // The pool matches our local value.
+ pool.install(|| {
+ assert!(LOCAL.is_set());
+ LOCAL.with(|y| {
+ assert_eq!(x, y);
+ });
+ });
+
+ // If we change our local value, the pool is not affected.
+ LOCAL.set(&Local(-1), || {
+ pool.install(|| {
+ assert!(LOCAL.is_set());
+ LOCAL.with(|y| {
+ assert_eq!(x, y);
+ });
+ });
+ });
+ },
+ )
+ .expect("thread pool created");
+ // Internally, `crossbeam::scope` will wait for the threads to exit before returning.
+ });
+ });
+}
diff --git a/third_party/rust/rayon-core/tests/simple_panic.rs b/third_party/rust/rayon-core/tests/simple_panic.rs
new file mode 100644
index 0000000000..2564482a47
--- /dev/null
+++ b/third_party/rust/rayon-core/tests/simple_panic.rs
@@ -0,0 +1,7 @@
+use rayon_core::join;
+
+#[test]
+#[should_panic(expected = "should panic")]
+fn simple_panic() {
+ join(|| {}, || panic!("should panic"));
+}
diff --git a/third_party/rust/rayon-core/tests/stack_overflow_crash.rs b/third_party/rust/rayon-core/tests/stack_overflow_crash.rs
new file mode 100644
index 0000000000..e87e151a92
--- /dev/null
+++ b/third_party/rust/rayon-core/tests/stack_overflow_crash.rs
@@ -0,0 +1,95 @@
+use rayon_core::ThreadPoolBuilder;
+
+use std::env;
+use std::process::{Command, ExitStatus, Stdio};
+
+#[cfg(target_os = "linux")]
+use std::os::unix::process::ExitStatusExt;
+
+fn force_stack_overflow(depth: u32) {
+ let mut buffer = [0u8; 1024 * 1024];
+ std::hint::black_box(&mut buffer);
+ if depth > 0 {
+ force_stack_overflow(depth - 1);
+ }
+}
+
+#[cfg(unix)]
+fn disable_core() {
+ unsafe {
+ libc::setrlimit(
+ libc::RLIMIT_CORE,
+ &libc::rlimit {
+ rlim_cur: 0,
+ rlim_max: 0,
+ },
+ );
+ }
+}
+
+#[cfg(unix)]
+fn overflow_code() -> Option<i32> {
+ None
+}
+
+#[cfg(windows)]
+fn overflow_code() -> Option<i32> {
+ use std::os::windows::process::ExitStatusExt;
+
+ ExitStatus::from_raw(0xc00000fd /*STATUS_STACK_OVERFLOW*/).code()
+}
+
+#[test]
+fn stack_overflow_crash() {
+ // First check that the recursive call actually causes a stack overflow,
+ // and does not get optimized away.
+ let status = run_ignored("run_with_small_stack");
+ #[cfg(any(unix, windows))]
+ assert_eq!(status.code(), overflow_code());
+ #[cfg(target_os = "linux")]
+ assert!(matches!(
+ status.signal(),
+ Some(libc::SIGABRT | libc::SIGSEGV)
+ ));
+
+ // Now run with a larger stack and verify correct operation.
+ let status = run_ignored("run_with_large_stack");
+ assert_eq!(status.code(), Some(0));
+ #[cfg(target_os = "linux")]
+ assert_eq!(status.signal(), None);
+}
+
+fn run_ignored(test: &str) -> ExitStatus {
+ Command::new(env::current_exe().unwrap())
+ .arg("--ignored")
+ .arg("--exact")
+ .arg(test)
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
+ .status()
+ .unwrap()
+}
+
+#[test]
+#[ignore]
+fn run_with_small_stack() {
+ run_with_stack(8);
+}
+
+#[test]
+#[ignore]
+fn run_with_large_stack() {
+ run_with_stack(48);
+}
+
+fn run_with_stack(stack_size_in_mb: usize) {
+ let pool = ThreadPoolBuilder::new()
+ .stack_size(stack_size_in_mb * 1024 * 1024)
+ .build()
+ .unwrap();
+ pool.install(|| {
+ #[cfg(unix)]
+ disable_core();
+ force_stack_overflow(32);
+ });
+}