summaryrefslogtreecommitdiffstats
path: root/vendor/once_cell/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:25 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:25 +0000
commit5363f350887b1e5b5dd21a86f88c8af9d7fea6da (patch)
tree35ca005eb6e0e9a1ba3bb5dbc033209ad445dc17 /vendor/once_cell/tests
parentAdding debian version 1.66.0+dfsg1-1. (diff)
downloadrustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.tar.xz
rustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.zip
Merging upstream version 1.67.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/once_cell/tests')
-rw-r--r--vendor/once_cell/tests/it.rs45
1 files changed, 36 insertions, 9 deletions
diff --git a/vendor/once_cell/tests/it.rs b/vendor/once_cell/tests/it.rs
index 410b93b64..d18f0a165 100644
--- a/vendor/once_cell/tests/it.rs
+++ b/vendor/once_cell/tests/it.rs
@@ -249,10 +249,16 @@ mod unsync {
}
}
-#[cfg(feature = "std")]
+#[cfg(any(feature = "std", feature = "critical-section"))]
mod sync {
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
+ #[cfg(feature = "std")]
+ use std::sync::Barrier;
+
+ #[cfg(not(feature = "std"))]
+ use core::cell::Cell;
+
use crossbeam_utils::thread::scope;
use once_cell::sync::{Lazy, OnceCell};
@@ -354,6 +360,7 @@ mod sync {
assert_eq!(cell.get(), Some(&"hello".to_string()));
}
+ #[cfg(feature = "std")]
#[test]
fn wait() {
let cell: OnceCell<String> = OnceCell::new();
@@ -365,9 +372,9 @@ mod sync {
.unwrap();
}
+ #[cfg(feature = "std")]
#[test]
fn get_or_init_stress() {
- use std::sync::Barrier;
let n_threads = if cfg!(miri) { 30 } else { 1_000 };
let n_cells = if cfg!(miri) { 30 } else { 1_000 };
let cells: Vec<_> = std::iter::repeat_with(|| (Barrier::new(n_threads), OnceCell::new()))
@@ -430,6 +437,7 @@ mod sync {
#[test]
#[cfg_attr(miri, ignore)] // miri doesn't support processes
+ #[cfg(feature = "std")]
fn reentrant_init() {
let examples_dir = {
let mut exe = std::env::current_exe().unwrap();
@@ -457,6 +465,20 @@ mod sync {
}
}
+ #[cfg(not(feature = "std"))]
+ #[test]
+ #[should_panic(expected = "reentrant init")]
+ fn reentrant_init() {
+ let x: OnceCell<Box<i32>> = OnceCell::new();
+ let dangling_ref: Cell<Option<&i32>> = Cell::new(None);
+ x.get_or_init(|| {
+ let r = x.get_or_init(|| Box::new(92));
+ dangling_ref.set(Some(r));
+ Box::new(62)
+ });
+ eprintln!("use after free: {:?}", dangling_ref.get().unwrap());
+ }
+
#[test]
fn lazy_new() {
let called = AtomicUsize::new(0);
@@ -636,10 +658,9 @@ mod sync {
}
}
+ #[cfg(feature = "std")]
#[test]
fn get_does_not_block() {
- use std::sync::Barrier;
-
let cell = OnceCell::new();
let barrier = Barrier::new(2);
scope(|scope| {
@@ -671,12 +692,11 @@ mod sync {
#[cfg(feature = "race")]
mod race {
+ #[cfg(feature = "std")]
+ use std::sync::Barrier;
use std::{
num::NonZeroUsize,
- sync::{
- atomic::{AtomicUsize, Ordering::SeqCst},
- Barrier,
- },
+ sync::atomic::{AtomicUsize, Ordering::SeqCst},
};
use crossbeam_utils::thread::scope;
@@ -728,6 +748,7 @@ mod race {
assert_eq!(cell.get(), Some(val1));
}
+ #[cfg(feature = "std")]
#[test]
fn once_non_zero_usize_first_wins() {
let val1 = NonZeroUsize::new(92).unwrap();
@@ -807,12 +828,16 @@ mod race {
#[cfg(all(feature = "race", feature = "alloc"))]
mod race_once_box {
+ #[cfg(feature = "std")]
+ use std::sync::Barrier;
use std::sync::{
atomic::{AtomicUsize, Ordering::SeqCst},
- Arc, Barrier,
+ Arc,
};
+ #[cfg(feature = "std")]
use crossbeam_utils::thread::scope;
+
use once_cell::race::OnceBox;
#[derive(Default)]
@@ -842,6 +867,7 @@ mod race_once_box {
}
}
+ #[cfg(feature = "std")]
#[test]
fn once_box_smoke_test() {
let heap = Heap::default();
@@ -896,6 +922,7 @@ mod race_once_box {
assert_eq!(heap.total(), 0);
}
+ #[cfg(feature = "std")]
#[test]
fn once_box_first_wins() {
let cell = OnceBox::new();