summaryrefslogtreecommitdiffstats
path: root/vendor/rayon-core/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/rayon-core/tests
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/rayon-core/tests')
-rw-r--r--vendor/rayon-core/tests/double_init_fail.rs14
-rw-r--r--vendor/rayon-core/tests/init_zero_threads.rs9
-rw-r--r--vendor/rayon-core/tests/scope_join.rs45
-rw-r--r--vendor/rayon-core/tests/scoped_threadpool.rs96
-rw-r--r--vendor/rayon-core/tests/simple_panic.rs7
-rw-r--r--vendor/rayon-core/tests/stack_overflow_crash.rs82
6 files changed, 253 insertions, 0 deletions
diff --git a/vendor/rayon-core/tests/double_init_fail.rs b/vendor/rayon-core/tests/double_init_fail.rs
new file mode 100644
index 000000000..ea06bf0e1
--- /dev/null
+++ b/vendor/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_eq!(result1.unwrap(), ());
+ 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/vendor/rayon-core/tests/init_zero_threads.rs b/vendor/rayon-core/tests/init_zero_threads.rs
new file mode 100644
index 000000000..ebd73c585
--- /dev/null
+++ b/vendor/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/vendor/rayon-core/tests/scope_join.rs b/vendor/rayon-core/tests/scope_join.rs
new file mode 100644
index 000000000..9d88133bc
--- /dev/null
+++ b/vendor/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/vendor/rayon-core/tests/scoped_threadpool.rs b/vendor/rayon-core/tests/scoped_threadpool.rs
new file mode 100644
index 000000000..db3d0b874
--- /dev/null
+++ b/vendor/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/vendor/rayon-core/tests/simple_panic.rs b/vendor/rayon-core/tests/simple_panic.rs
new file mode 100644
index 000000000..2564482a4
--- /dev/null
+++ b/vendor/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/vendor/rayon-core/tests/stack_overflow_crash.rs b/vendor/rayon-core/tests/stack_overflow_crash.rs
new file mode 100644
index 000000000..61288982c
--- /dev/null
+++ b/vendor/rayon-core/tests/stack_overflow_crash.rs
@@ -0,0 +1,82 @@
+use rayon_core::ThreadPoolBuilder;
+
+use std::env;
+use std::process::Command;
+
+#[cfg(target_os = "linux")]
+use std::os::unix::process::ExitStatusExt;
+
+fn force_stack_overflow(depth: u32) {
+ let _buffer = [0u8; 1024 * 1024];
+ 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;
+ use std::process::ExitStatus;
+
+ ExitStatus::from_raw(0xc00000fd /*STATUS_STACK_OVERFLOW*/).code()
+}
+
+fn main() {
+ if env::args().len() == 1 {
+ // first check that the recursivecall actually causes a stack overflow, and does not get optimized away
+ {
+ let status = Command::new(env::current_exe().unwrap())
+ .arg("8")
+ .status()
+ .unwrap();
+
+ #[cfg(any(unix, windows))]
+ assert_eq!(status.code(), overflow_code());
+
+ #[cfg(target_os = "linux")]
+ assert!(
+ status.signal() == Some(11 /*SIGABRT*/) || status.signal() == Some(6 /*SIGSEGV*/)
+ );
+ }
+
+ // now run with a larger stack and verify correct operation
+ {
+ let status = Command::new(env::current_exe().unwrap())
+ .arg("48")
+ .status()
+ .unwrap();
+ assert_eq!(status.code(), Some(0));
+ #[cfg(target_os = "linux")]
+ assert_eq!(status.signal(), None);
+ }
+ } else {
+ let stack_size_in_mb: usize = env::args().nth(1).unwrap().parse().unwrap();
+ let pool = ThreadPoolBuilder::new()
+ .stack_size(stack_size_in_mb * 1024 * 1024)
+ .build()
+ .unwrap();
+ pool.install(|| {
+ #[cfg(unix)]
+ disable_core();
+ force_stack_overflow(32);
+ });
+ }
+}