RustedSciThe 0.4.20

Rust framework for symbolic and numerical computing:BVP ( Newton-Raphson frozen/damped/with collocations ), IVP( BDF, Radau, Backward Euler, LSODE, LSODA, RK45, DoPri), nonlinear equations ( Levenberg, Gavin) and more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Small, opt-in reporting support for verbose integration and story tests.
//!
//! Reports are written after the measured operation has finished. The helper
//! is intentionally outside solver code so file-system work cannot enter a
//! solver timer or a production hot path. Each profile-qualified canonical
//! test name maps to a single canonical report file; a later run replaces that
//! profile's file and records a new UTC timestamp. Release writes also keep an
//! immutable copy below the profile's `archive/` directory.

use chrono::{SecondsFormat, Utc};
use std::fmt;
use std::fs;
use std::io;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use tabled::{Table, Tabled};

static REPORT_SEQUENCE: AtomicU64 = AtomicU64::new(0);

thread_local! {
    static ACTIVE_CAPTURE: std::cell::RefCell<Option<String>> = const { std::cell::RefCell::new(None) };
}

/// Writes one dated report for a named test and returns the resulting path.
///
/// RST_TEST_REPORT_DIR can override the repository-local test_reports
/// directory. Reports are partitioned by the Cargo profile (`debug` or
/// `release`) so a smoke run cannot replace a release baseline. Release
/// reports are additionally copied to `profile/archive/` with a UTC timestamp.
/// Set `RST_TEST_REPORT_ARCHIVE=always` to archive debug reports too, or use
/// `never` to disable archival for a local run. The optional
/// RST_TEST_REPORT_PROFILE override is useful for process-isolated harnesses.
pub fn write_test_report(
    suite: &str,
    canonical_test_name: &str,
    body: &str,
) -> io::Result<PathBuf> {
    let root = std::env::var_os("RST_TEST_REPORT_DIR")
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_reports"));
    write_test_report_in(&root, suite, canonical_test_name, body)
}

/// Writes a replaceable progress snapshot without creating an archive entry.
///
/// Long-running benchmark drivers use this for live compact tables. The final
/// report should still be written through [`write_test_report`] so release
/// archival remains atomic and immutable.
pub fn write_test_report_snapshot(
    suite: &str,
    canonical_test_name: &str,
    body: &str,
) -> io::Result<PathBuf> {
    let root = std::env::var_os("RST_TEST_REPORT_DIR")
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_reports"));
    let suite = sanitize_component(suite);
    let test_name = format!("{}__progress", sanitize_component(canonical_test_name));
    let profile = report_profile();
    write_test_report_in_profile(
        &root,
        &suite,
        &test_name,
        profile,
        canonical_test_name,
        body,
        false,
    )
}

/// Variant with an explicit root, primarily useful for utility tests.
pub fn write_test_report_in(
    root: &Path,
    suite: &str,
    canonical_test_name: &str,
    body: &str,
) -> io::Result<PathBuf> {
    let suite = sanitize_component(suite);
    let test_name = sanitize_component(canonical_test_name);
    let profile = report_profile();
    write_test_report_in_profile(
        root,
        &suite,
        &test_name,
        profile,
        canonical_test_name,
        body,
        archive_enabled(profile),
    )
}

fn write_test_report_in_profile(
    root: &Path,
    suite: &str,
    test_name: &str,
    profile: &str,
    canonical_test_name: &str,
    body: &str,
    archive: bool,
) -> io::Result<PathBuf> {
    let directory = root.join(&suite).join(profile);
    fs::create_dir_all(&directory)?;

    let path = directory.join(format!("{test_name}.md"));
    let timestamp = Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true);
    let payload = format!(
        "# Test report: {canonical_test_name}\n\n- suite: {suite}\n- profile: {profile}\n- recorded_at_utc: {timestamp}\n- canonical_test: {canonical_test_name}\n\n{body}\n"
    );

    // Write through a sibling temporary file so a reader never observes a
    // partially written report. Windows cannot rename over an existing
    // destination, therefore remove only this report's old file before the
    // final rename.
    let temporary = directory.join(format!(".{test_name}.tmp-{}", report_suffix()));
    fs::write(&temporary, &payload)?;
    if path.exists() {
        fs::remove_file(&path)?;
    }
    fs::rename(&temporary, &path)?;

    if archive {
        write_immutable_archive(&directory, test_name, &timestamp, payload.as_bytes())?;
    }

    Ok(path)
}

fn write_immutable_archive(
    directory: &Path,
    test_name: &str,
    timestamp: &str,
    payload: &[u8],
) -> io::Result<()> {
    let archive_directory = directory.join("archive");
    fs::create_dir_all(&archive_directory)?;
    let archive_stamp = timestamp.replace(':', "-");
    let base_name = format!("{test_name}__{archive_stamp}");

    for collision in 0..1000u32 {
        let suffix = if collision == 0 {
            String::new()
        } else {
            format!("__{collision}")
        };
        let path = archive_directory.join(format!("{base_name}{suffix}.md"));
        match fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&path)
        {
            Ok(mut file) => {
                file.write_all(payload)?;
                file.flush()?;
                return Ok(());
            }
            Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
            Err(error) => return Err(error),
        }
    }

    Err(io::Error::new(
        io::ErrorKind::AlreadyExists,
        "too many report archive filename collisions",
    ))
}

/// Captures verbose test output and writes it as a dated report on drop.
///
/// The capture is thread-local and deliberately lives in the test utility
/// layer. It does not add any work to solver code or to measured benchmark
/// regions. The output is still forwarded to stdout immediately, so existing
/// `--nocapture` workflows keep their behaviour.
pub struct TestReportCapture {
    root: Option<PathBuf>,
    suite: String,
    canonical_test_name: String,
    previous: Option<String>,
}

impl TestReportCapture {
    /// Starts capturing output for one canonical test name.
    pub fn new(suite: impl Into<String>, canonical_test_name: impl Into<String>) -> Self {
        Self::new_with_root(None, suite, canonical_test_name)
    }

    /// Starts capturing output with an explicit report root.
    ///
    /// This is primarily useful for isolated utility tests; normal suites
    /// should use [`Self::new`] so `RST_TEST_REPORT_DIR` remains supported.
    pub fn new_in(
        root: impl Into<PathBuf>,
        suite: impl Into<String>,
        canonical_test_name: impl Into<String>,
    ) -> Self {
        Self::new_with_root(Some(root.into()), suite, canonical_test_name)
    }

    fn new_with_root(
        root: Option<PathBuf>,
        suite: impl Into<String>,
        canonical_test_name: impl Into<String>,
    ) -> Self {
        let previous = ACTIVE_CAPTURE.with(|active| active.borrow_mut().replace(String::new()));
        Self {
            root,
            suite: suite.into(),
            canonical_test_name: canonical_test_name.into(),
            previous,
        }
    }
}

impl Drop for TestReportCapture {
    fn drop(&mut self) {
        let body = ACTIVE_CAPTURE
            .with(|active| active.borrow_mut().take())
            .unwrap_or_default();
        let status = if std::thread::panicking() {
            "failed"
        } else {
            "passed"
        };
        let body = format!("status: {status}\n\n{body}");
        let result = self
            .root
            .as_deref()
            .map(|root| write_test_report_in(root, &self.suite, &self.canonical_test_name, &body))
            .unwrap_or_else(|| write_test_report(&self.suite, &self.canonical_test_name, &body));
        if let Err(error) = result {
            eprintln!(
                "[test report] unable to write {}::{}: {error}",
                self.suite, self.canonical_test_name
            );
        }
        ACTIVE_CAPTURE.with(|active| {
            *active.borrow_mut() = self.previous.take();
        });
    }
}

/// Forwards one already formatted test line to stdout and the active report.
///
/// Set `RST_TEST_REPORT_STDOUT=off` (or `false`/`0`) for a large release run
/// when the report file, rather than the terminal, should be the diagnostic
/// output. The report capture remains active in that mode.
pub fn capture_test_line(args: fmt::Arguments<'_>) {
    capture_test_block(args.to_string());
}

/// Captures a preformatted multi-line block as one reporting event.
///
/// This is useful for tables: the caller can collect rows during the test and
/// emit one compact artifact after the measured work has completed.
pub fn capture_test_block(block: impl AsRef<str>) {
    let block = block.as_ref();
    if report_stdout_enabled() {
        std::println!("{block}");
    }
    ACTIVE_CAPTURE.with(|active| {
        if let Some(body) = active.borrow_mut().as_mut() {
            body.push_str(block);
            body.push('\n');
        }
    });
}

/// Captures a compact [`tabled`] table in the active report.
///
/// Rows should normally be accumulated in a local `Vec` while the expensive
/// operation runs. Formatting is performed only once, outside that operation,
/// and is therefore not part of a solver or callback timing scope.
pub fn capture_test_table<T: Tabled>(title: impl AsRef<str>, rows: &[T]) {
    let table = if rows.is_empty() {
        "(no rows)".to_owned()
    } else {
        Table::new(rows).to_string()
    };
    capture_test_block(format!("{}\n{}", title.as_ref(), table));
}

fn report_stdout_enabled() -> bool {
    !matches!(
        std::env::var("RST_TEST_REPORT_STDOUT").as_deref(),
        Ok("off") | Ok("false") | Ok("0")
    )
}

fn sanitize_component(value: &str) -> String {
    let sanitized: String = value
        .chars()
        .map(|character| {
            if character.is_ascii_alphanumeric() || matches!(character, '-' | '_') {
                character
            } else {
                '_'
            }
        })
        .collect();
    if sanitized.is_empty() {
        "unnamed".to_string()
    } else {
        sanitized
    }
}

fn report_profile() -> &'static str {
    if let Ok(profile) = std::env::var("RST_TEST_REPORT_PROFILE") {
        if profile.eq_ignore_ascii_case("release") {
            return "release";
        }
        if profile.eq_ignore_ascii_case("debug") {
            return "debug";
        }
    }
    if cfg!(debug_assertions) {
        "debug"
    } else {
        "release"
    }
}

fn archive_enabled(profile: &str) -> bool {
    match std::env::var("RST_TEST_REPORT_ARCHIVE") {
        Ok(value) if value.eq_ignore_ascii_case("always") => true,
        Ok(value) if value.eq_ignore_ascii_case("never") => false,
        Ok(value) if value.eq_ignore_ascii_case("debug") => profile == "debug",
        Ok(value) if value.eq_ignore_ascii_case("release") => profile == "release",
        Ok(value) if value.eq_ignore_ascii_case("true") => true,
        Ok(value) if value.eq_ignore_ascii_case("false") => false,
        _ => profile == "release",
    }
}

fn report_suffix() -> String {
    let sequence = REPORT_SEQUENCE.fetch_add(1, Ordering::Relaxed);
    format!("{}-{sequence}", std::process::id())
}

#[cfg(test)]
mod tests {
    use super::{
        TestReportCapture, capture_test_line, capture_test_table, sanitize_component,
        write_test_report_in, write_test_report_in_profile,
    };
    use std::fs;

    #[test]
    fn canonical_name_is_safe_and_report_is_replaced() {
        let root =
            std::env::temp_dir().join(format!("rustedscithe-test-report-{}", std::process::id()));
        let _ = fs::remove_dir_all(&root);

        let first = write_test_report_in(&root, "BVP_Damp", "module::tests::story", "first")
            .expect("first report should be written");
        let second = write_test_report_in(&root, "BVP_Damp", "module::tests::story", "second")
            .expect("second report should replace the first");
        assert_eq!(first, second);
        assert!(fs::read_to_string(second).unwrap().contains("second"));
        assert_eq!(sanitize_component("a::b/c"), "a__b_c");

        let _ = fs::remove_dir_all(root);
    }

    #[test]
    fn capture_forwards_output_and_writes_report_on_drop() {
        let root = std::env::temp_dir().join(format!(
            "rustedscithe-test-report-capture-{}",
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&root);
        {
            let _capture = TestReportCapture::new_in(&root, "BVP_Damp_AOT", "module::tests::aot");
            capture_test_line(format_args!("AOT row: {}", 7));
        }
        let profile = if cfg!(debug_assertions) {
            "debug"
        } else {
            "release"
        };
        let report = root
            .join("BVP_Damp_AOT")
            .join(profile)
            .join("module__tests__aot.md");
        let contents = fs::read_to_string(report).expect("captured report should exist");
        assert!(contents.contains("status: passed"));
        assert!(contents.contains("AOT row: 7"));
        assert!(contents.contains("- profile: "));
        let _ = fs::remove_dir_all(root);
    }

    #[test]
    fn release_report_keeps_an_immutable_archive_copy() {
        let root = std::env::temp_dir().join(format!(
            "rustedscithe-test-report-archive-{}",
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&root);

        let canonical = write_test_report_in_profile(
            &root,
            "LSODE2_AOT",
            "module__tests__aot",
            "release",
            "module::tests::aot",
            "release row",
            true,
        )
        .expect("release report should be written");
        let archive_directory = canonical.parent().unwrap().join("archive");
        let archived: Vec<_> = fs::read_dir(&archive_directory)
            .expect("archive directory should exist")
            .map(|entry| entry.expect("archive entry should be readable").path())
            .collect();
        assert_eq!(archived.len(), 1);
        assert!(
            fs::read_to_string(&archived[0])
                .unwrap()
                .contains("release row")
        );

        let _ = fs::remove_dir_all(root);
    }

    #[test]
    fn table_capture_writes_one_compact_block() {
        #[derive(tabled::Tabled)]
        struct Row {
            route: &'static str,
            elapsed_ms: &'static str,
        }

        let root = std::env::temp_dir().join(format!(
            "rustedscithe-test-report-table-{}",
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&root);
        {
            let _capture = TestReportCapture::new_in(&root, "Radau_Large", "table_story");
            capture_test_table(
                "summary",
                &[Row {
                    route: "ExprLegacy",
                    elapsed_ms: "1.25",
                }],
            );
        }
        let report = root
            .join("Radau_Large")
            .join("debug")
            .join("table_story.md");
        let contents = fs::read_to_string(report).expect("table report should exist");
        assert!(contents.contains("summary"));
        assert!(contents.contains("ExprLegacy"));
        assert!(contents.contains("elapsed_ms"));
        let _ = fs::remove_dir_all(root);
    }
}