use std::fmt::Write as _;
use super::Exports;
use crate::work::{Work, measure_kind};
use crate::{LoadOptions, MemoryTree, TargetKind, Workspace};
const UNIT: usize = 500;
const CEILING: usize = 64 * UNIT;
const DEMO: &str = "[package]\nname = \"demo\"\nversion = \"0.1.0\"\nedition = \"2021\"\n";
#[test]
fn native_surface_storage_preserves_physical_failure_and_budget_ownership() {
use crate::meter::{Meter, NativeAdmission, push_item_work};
use crate::{Budget, ExportError, LoadErrorKind, RelPath};
use purrdf_lex::{allocation::Memory, walk::WorkList};
let file = RelPath::new("src/lib.rs").expect("path");
let meter = Meter::new(&LoadOptions::default());
let mut admission = NativeAdmission::items(&meter, &file, Some(7), 0);
let mut values = Vec::<u64>::new();
let error = Memory::new(&mut admission)
.reserve(&mut values, usize::MAX)
.expect_err("impossible native layout");
assert_eq!(values, []);
assert_eq!(
meter.usage().item_bytes,
0,
"layout fails before allocation or admission"
);
for carried in [None, Some("demo::Alias".to_owned())] {
let error = admission.graph_error(error.into());
assert_eq!((error.file.as_str(), error.line), ("src/lib.rs", Some(7)));
let mapped = ExportError::storage(error, carried);
assert!(matches!(mapped, ExportError::Storage(_)));
assert!(!mapped.to_string().contains("over budget"));
assert!(std::error::Error::source(&mapped).is_some());
}
let meter = Meter::new(&LoadOptions {
max_item_bytes: 0,
..LoadOptions::default()
});
let mut work = WorkList::<_, 1>::new();
push_item_work(&mut work, 1, &meter, &file, Some(8)).expect("inline neighbor");
let error = push_item_work(&mut work, 2, &meter, &file, Some(8))
.expect_err("actual native spill budget");
assert!(matches!(
error.kind,
LoadErrorKind::Budget {
budget: Budget::ItemBytes,
limit: 0
}
));
assert!(matches!(
ExportError::storage(error, None),
ExportError::Budget(_)
));
let error =
push_item_work(&mut work, 2, &meter, &file, Some(8)).expect_err("same spill refused again");
assert!(matches!(
ExportError::storage(error, Some("demo::Alias".to_owned())),
ExportError::CarryBudget { .. }
));
assert_eq!(
work.pop(),
Some(1),
"refused push preserves existing payload"
);
}
fn tree(lib: &str) -> MemoryTree {
[("Cargo.toml", DEMO), ("src/lib.rs", lib)]
.into_iter()
.collect()
}
fn load(lib: &str) -> Workspace {
Workspace::load(&tree(lib), &["."]).unwrap_or_else(|e| panic!("{e}"))
}
fn list(ws: &Workspace) -> Exports {
let target = ws
.target_ids()
.find(|id| ws.target(*id).is_some_and(|t| t.kind == TargetKind::Lib))
.expect("a library target");
ws.exported_items(target).unwrap_or_else(|e| panic!("{e}"))
}
fn counted(kind: Work, lib: &str, check: impl FnOnce(&Exports)) -> usize {
let ws = load(lib);
let (listed, tally) = measure_kind(kind, CEILING, || list(&ws));
check(&listed);
tally.get(kind)
}
fn assert_linear(kind: Work, build: impl Fn(usize) -> String, check: impl Fn(&Exports, usize)) {
let counts = [1, 2, 3].map(|units| {
let size = units * UNIT;
counted(kind, &build(size), |listed| check(listed, size))
});
let [one, two, three] = counts;
assert!(one > 0, "{kind:?} counted nothing: {counts:?}");
assert!(
three + one <= 2 * two,
"{kind:?} grows faster than its names: {counts:?}"
);
}
fn pub_globs(n: usize) -> String {
let mut lib = String::new();
for i in 0..n {
writeln!(lib, "mod m{i} {{ pub fn f{i}() {{}} }}\npub use m{i}::*;").expect("write");
}
lib
}
fn carrying_globs(n: usize) -> String {
let mut lib = String::new();
for i in 0..n {
writeln!(
lib,
"mod m{i} {{ pub use std::cell::Cell as C{i}; }}\npub use m{i}::*;"
)
.expect("write");
}
for i in 0..n {
writeln!(
lib,
"mod k{i} {{ pub use std::rc::Rc; pub use std::fmt::Debug; }}\npub use k{i}::*;"
)
.expect("write");
}
lib.push_str("pub struct Debug;\n");
lib
}
fn check_carrying(listed: &Exports, size: usize) {
assert_eq!(listed.items.len(), 1);
assert_eq!(listed.reexports.len(), 2 * size);
assert!(
listed
.reexports
.iter()
.all(|reexport| reexport.path != "demo::Debug")
);
}
#[test]
fn a_module_of_pub_globs_is_listed_in_view_reads_linear_in_its_names() {
assert_linear(Work::ViewReads, pub_globs, |listed, size| {
assert_eq!(listed.items.len(), size);
});
}
#[test]
fn a_module_of_pub_globs_that_each_carry_a_re_export_is_listed_in_view_reads_linear_in_its_names() {
assert_linear(Work::ViewReads, carrying_globs, check_carrying);
}
#[test]
fn a_module_of_pub_globs_that_each_carry_a_re_export_is_listed_in_walk_steps_linear_in_its_names() {
assert_linear(Work::WalkSteps, carrying_globs, check_carrying);
}
fn sibling_shadows(n: usize) -> String {
let mut lib = String::new();
for i in 0..n {
writeln!(
lib,
"mod m{i} {{ pub use std::cell::Cell as C{i}; }}\nmod s{i} {{ fn C{i}() {{}} }}\npub use m{i}::*;\npub use s{i}::*;"
)
.expect("write");
}
lib
}
#[test]
fn readers_of_carried_names_beside_sibling_shadows_list_in_linear_work_and_bounded_bytes() {
let check = |listed: &Exports, size: usize| assert_eq!(listed.reexports.len(), size);
assert_linear(Work::ViewReads, sibling_shadows, check);
assert_linear(Work::WalkSteps, sibling_shadows, check);
let lib = sibling_shadows(3000);
let spent = load(&lib).usage().item_bytes;
let options = LoadOptions {
max_item_bytes: spent + (32 << 20),
..LoadOptions::default()
};
let ws = Workspace::load_with(&tree(&lib), &["."], &options).expect("the load fits");
assert_eq!(list(&ws).reexports.len(), 3000);
}