Cotton code
50 lines · 2.4 KB · RustRaw
//! Cotton Story's speed (contract, 17 Nov 2026): `story.lines` and//! `story.press` run on every keystroke and answer a 10-page scene in a few//! milliseconds; `story.script` lays a 120-page story out in under a second.//! Measured in release: `cargo test --release -p cotton-rules --test story_speed -- --ignored --nocapture`.use std::time::Instant;use cotton_rules::story::layout::{script, SceneIn};use cotton_rules::story::lines::lines;use cotton_rules::story::press::{press, Key};/// A scene of about `pages` pages: action, names, asides and speech.fn scene(pages: usize, salt: usize) -> String {    let mut body = String::new();    let mut rows = 0;    let mut at = 0;    while rows < pages * 55 {        body.push_str(&format!("Meera crosses the yard as bus {salt}-{at} pulls out, its lights sweeping the wet concrete.\n\nMEERA\n(to herself)\nOne more shift. Just one more, and then it is somebody else's night.\n\nARJUN\nThe ticket says {at}.\n\n"));        rows += 11;        at += 1;    }    body}fn best_of<T>(runs: usize, mut f: impl FnMut() -> T) -> std::time::Duration {    (0..runs).map(|_| {        let start = Instant::now();        std::hint::black_box(f());        start.elapsed()    }).min().unwrap()}#[test]#[ignore = "a measurement: run in release with --ignored --nocapture"]fn story_answers_fast_enough() {    let names = vec!["MEERA".to_string(), "ARJUN".to_string()];    let ten = scene(10, 0);    let lines_took = best_of(20, || lines(&ten, &names));    let cursor = ten.encode_utf16().count();    let press_took = best_of(20, || press(&ten, cursor, Key::Return, &names, None));    let story: Vec<SceneIn> = (0..60).map(|at| SceneIn { id: format!("s{at}"), heading: format!("INT. DEPOT {at} - NIGHT"), body: scene(2, at) }).collect();    let script_took = best_of(5, || script(&story, &names));    let pages = script(&story, &names).pages.len();    println!("story.lines on a 10-page scene ({} lines): {lines_took:?}", ten.lines().count());    println!("story.press (Return at the end) on it: {press_took:?}");    println!("story.script on {pages} pages ({} scenes): {script_took:?}", story.len());    assert!(pages >= 110, "the story is about 120 pages ({pages})");    assert!(lines_took.as_millis() < 5 && press_took.as_millis() < 5, "keystroke rules in a few milliseconds");    assert!(script_took.as_millis() < 1000, "a 120-page story laid out in under a second");}