summaryrefslogtreecommitdiffstats
path: root/vendor/stacker/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/stacker/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/stacker/tests')
-rw-r--r--vendor/stacker/tests/simple.rs27
-rw-r--r--vendor/stacker/tests/smoke.rs95
2 files changed, 122 insertions, 0 deletions
diff --git a/vendor/stacker/tests/simple.rs b/vendor/stacker/tests/simple.rs
new file mode 100644
index 000000000..d4c7820d6
--- /dev/null
+++ b/vendor/stacker/tests/simple.rs
@@ -0,0 +1,27 @@
+extern crate stacker;
+
+const RED_ZONE: usize = 100 * 1024; // 100k
+const STACK_PER_RECURSION: usize = 1 * 1024 * 1024; // 1MB
+
+pub fn ensure_sufficient_stack<R, F: FnOnce() -> R + std::panic::UnwindSafe>(f: F) -> R {
+ stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)
+}
+
+#[inline(never)]
+fn recurse(n: usize) {
+ let x = [42u8; 50000];
+ if n != 0 {
+ ensure_sufficient_stack(|| recurse(n - 1));
+ }
+ drop(x);
+}
+
+#[test]
+fn foo() {
+ let limit = if cfg!(target_arch = "wasm32") {
+ 2000
+ } else {
+ 10000
+ };
+ recurse(limit);
+}
diff --git a/vendor/stacker/tests/smoke.rs b/vendor/stacker/tests/smoke.rs
new file mode 100644
index 000000000..2ec0de24d
--- /dev/null
+++ b/vendor/stacker/tests/smoke.rs
@@ -0,0 +1,95 @@
+extern crate stacker;
+
+use std::sync::mpsc;
+use std::thread;
+
+#[inline(never)]
+fn __stacker_black_box(_: *const u8) {}
+
+#[test]
+fn deep() {
+ fn foo(n: usize, s: &mut [u8]) {
+ __stacker_black_box(s.as_ptr());
+ if n > 0 {
+ stacker::maybe_grow(64 * 1024, 1024 * 1024, || {
+ let mut s = [0u8; 1024];
+ foo(n - 1, &mut s);
+ __stacker_black_box(s.as_ptr());
+ })
+ } else {
+ println!("bottom");
+ }
+ }
+
+ let limit = if cfg!(target_arch = "wasm32") {
+ 2000
+ } else {
+ 256 * 1024
+ };
+ foo(limit, &mut []);
+}
+
+#[test]
+#[cfg_attr(target_arch = "wasm32", ignore)]
+fn panic() {
+ fn foo(n: usize, s: &mut [u8]) {
+ __stacker_black_box(s.as_ptr());
+ if n > 0 {
+ stacker::maybe_grow(64 * 1024, 1024 * 1024, || {
+ let mut s = [0u8; 1024];
+ foo(n - 1, &mut s);
+ __stacker_black_box(s.as_ptr());
+ })
+ } else {
+ panic!("bottom");
+ }
+ }
+
+ let (tx, rx) = mpsc::channel::<()>();
+ thread::spawn(move || {
+ foo(64 * 1024, &mut []);
+ drop(tx);
+ })
+ .join()
+ .unwrap_err();
+
+ assert!(rx.recv().is_err());
+}
+
+fn recursive<F: FnOnce()>(n: usize, f: F) -> usize {
+ if n > 0 {
+ stacker::grow(64 * 1024, || recursive(n - 1, f) + 1)
+ } else {
+ f();
+ 0
+ }
+}
+
+#[test]
+#[cfg_attr(target_arch = "wasm32", ignore)]
+fn catch_panic() {
+ let panic_result = std::panic::catch_unwind(|| {
+ recursive(100, || panic!());
+ });
+ assert!(panic_result.is_err());
+}
+
+#[test]
+#[cfg_attr(target_arch = "wasm32", ignore)]
+fn catch_panic_inside() {
+ stacker::grow(64 * 1024, || {
+ let panic_result = std::panic::catch_unwind(|| {
+ recursive(100, || panic!());
+ });
+ assert!(panic_result.is_err());
+ });
+}
+
+#[test]
+#[cfg_attr(target_arch = "wasm32", ignore)]
+fn catch_panic_leaf() {
+ stacker::grow(64 * 1024, || {
+ let panic_result = std::panic::catch_unwind(|| panic!());
+ assert!(panic_result.is_err());
+ });
+}