makepad/tools/run/fixture/diff/async_basic.rs
Admin 496eec58d5 makepad run / Relax: differential check covers the O1 tier
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 13:02:12 +02:00

37 lines
2 KiB
Rust

// diff_fast corpus program: `#[test] run` calls work() in rounds (the O3 tier installs between rounds); every
// result line is `D ...`. rustc --test -O and `makepad test` (baseline and O3) must print identical D lines.
macro_rules! d { ($($a:tt)*) => { println!("D {}", format!($($a)*)) } }
fn rounds(f: fn()) {
let ms: u64 = std::env::var("DIFF_SLEEP_MS").ok().and_then(|v| v.parse().ok()).unwrap_or(0);
for r in 0..4 { f(); if r < 3 && ms > 0 { std::thread::sleep(std::time::Duration::from_millis(ms)); } }
}
#[test]
fn run() { rounds(work); }
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll, Wake, Waker};
struct Noop; impl Wake for Noop { fn wake(self: Arc<Self>) {} }
struct YieldOnce(bool);
impl Future for YieldOnce { type Output = (); fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> { if self.0 { Poll::Ready(()) } else { self.0 = true; cx.waker().wake_by_ref(); Poll::Pending } } }
fn block_on<F: Future>(f: F) -> (F::Output, u32) {
let mut f = Box::pin(f); let w = Waker::from(Arc::new(Noop)); let mut cx = Context::from_waker(&w); let mut polls = 0;
loop { polls += 1; if let Poll::Ready(v) = f.as_mut().poll(&mut cx) { return (v, polls); } }
}
async fn leaf(x: u64) -> u64 { YieldOnce(false).await; x * 3 + 1 }
async fn mid(x: u64) -> u64 { let a = leaf(x).await; let b = leaf(a).await; a + b }
async fn top(n: u64) -> (u64, String) {
let mut t = 0; let s = String::from("held-across-await");
t += mid(n).await; t += mid(n + 1).await;
let arr = [t; 4]; YieldOnce(false).await;
(arr.iter().sum(), s)
}
async fn boxed(n: u32) -> u32 { if n == 0 { 0 } else { n + Box::pin(boxed(n - 1)).await } }
fn work() {
d!("{:?}", block_on(top(25)));
let futs: Vec<Pin<Box<dyn Future<Output = u64>>>> = vec![Box::pin(leaf(1)), Box::pin(mid(2)), Box::pin(async { 7 })];
let mut tot = 0; for f in futs { tot += block_on(f).0; } d!("{}", tot);
let c = async { let v = vec![1u32, 2, 3]; let r = async { v.iter().sum::<u32>() }.await; r + v.len() as u32 };
d!("{:?}", block_on(c));
}