#![allow(clippy::unwrap_used, clippy::indexing_slicing, clippy::panic)]
use core::num::NonZeroU64;
use critpath_core::{
apply, roots, Applied, BuildGraph, EdgeKind, GraphBuilder, Mapped, Millis, SplitDependents,
Transform, TransformError, Unit, UnitId, UnitKind,
};
fn nz(n: u64) -> NonZeroU64 {
NonZeroU64::new(n).unwrap()
}
fn lib(name: &str, total_ms: u64, metadata_ms: u64) -> Unit {
Unit {
package: name.to_owned(),
version: "1.0.0".to_owned(),
target: name.to_owned(),
kind: UnitKind::Lib,
total: Millis::new(total_ms),
metadata_at: Some(Millis::new(metadata_ms)),
requires_full_closure: false,
}
}
fn opaque(name: &str, total_ms: u64, kind: UnitKind) -> Unit {
Unit {
package: name.to_owned(),
version: "1.0.0".to_owned(),
target: name.to_owned(),
kind,
total: Millis::new(total_ms),
metadata_at: None,
requires_full_closure: kind != UnitKind::Lib,
}
}
fn producers(graph: &BuildGraph, id: UnitId) -> Vec<UnitId> {
let mut ids: Vec<UnitId> = graph
.dependencies(id)
.unwrap()
.iter()
.map(|dep| dep.unit)
.collect();
ids.sort();
ids
}
fn targets(graph: &BuildGraph) -> Vec<String> {
graph.units().map(|(_, u)| u.target.clone()).collect()
}
type Shape = (
Vec<(String, UnitKind, u64, Option<u64>, bool)>,
Vec<(usize, usize, EdgeKind)>,
);
fn shape(graph: &BuildGraph) -> Shape {
let units = graph
.units()
.map(|(_, u)| {
(
u.target.clone(),
u.kind,
u.total.get(),
u.metadata_at.map(Millis::get),
u.requires_full_closure,
)
})
.collect();
let mut edges: Vec<(usize, usize, EdgeKind)> = Vec::new();
for (consumer, _) in graph.units() {
for dep in graph.dependencies(consumer).unwrap() {
edges.push((dep.unit.index(), consumer.index(), dep.kind));
}
}
edges.sort();
(units, edges)
}
fn split_halves(applied: &Applied, old: UnitId) -> (UnitId, UnitId) {
match applied.mapped(old) {
Some(Mapped::Split { first, second }) => (first, second),
other => panic!("expected unit {old} to have been split, found {other:?}"),
}
}
fn kept(applied: &Applied, old: UnitId) -> UnitId {
match applied.mapped(old) {
Some(Mapped::Kept(new)) => new,
other => panic!("expected unit {old} to have survived, found {other:?}"),
}
}
fn last_unit_of_a_larger_graph(count: usize) -> UnitId {
let mut builder = GraphBuilder::new();
let mut ids = Vec::new();
for i in 0..count {
ids.push(builder.add_unit(lib(&format!("u{i}"), 10, 5)).unwrap());
}
let _ = builder.build().unwrap();
*ids.last().unwrap()
}
#[test]
fn roots_are_the_units_nothing_depends_on() {
let mut b = GraphBuilder::new();
let script = b
.add_unit(opaque("script", 100, UnitKind::BuildScriptCompile))
.unwrap();
let run = b
.add_unit(opaque("run", 20, UnitKind::BuildScriptRun))
.unwrap();
let core = b.add_unit(lib("core", 400, 200)).unwrap();
let app = b.add_unit(opaque("app", 50, UnitKind::Bin)).unwrap();
let tool = b.add_unit(opaque("tool", 60, UnitKind::Bin)).unwrap();
b.add_edge(script, run, EdgeKind::Full).unwrap();
b.add_edge(run, core, EdgeKind::Full).unwrap();
b.add_edge(core, app, EdgeKind::Full).unwrap();
let graph = b.build().unwrap();
assert_eq!(roots(&graph), vec![app, tool]);
}
#[test]
fn roots_of_an_empty_graph_are_empty() {
let graph = GraphBuilder::new().build().unwrap();
assert_eq!(roots(&graph), Vec::new());
}
#[test]
fn pessimistic_split_halves_the_weight_and_feeds_every_dependent() {
let mut b = GraphBuilder::new();
let producer = b.add_unit(lib("producer", 100, 60)).unwrap();
let target = b.add_unit(lib("target", 1000, 600)).unwrap();
let first_dep = b.add_unit(lib("first_dep", 200, 100)).unwrap();
let second_dep = b.add_unit(lib("second_dep", 300, 150)).unwrap();
b.add_edge(producer, target, EdgeKind::Metadata).unwrap();
b.add_edge(target, first_dep, EdgeKind::Metadata).unwrap();
b.add_edge(target, second_dep, EdgeKind::Full).unwrap();
let graph = b.build().unwrap();
let applied = apply(
&graph,
&Transform::SplitUnit {
unit: target,
numerator: 1,
denominator: nz(2),
dependents: SplitDependents::Pessimistic,
overhead: Millis::new(40),
},
)
.unwrap();
let split = applied.graph();
let (first, second) = split_halves(&applied, target);
let a = split.unit(first).unwrap();
let z = split.unit(second).unwrap();
assert_eq!(a.total, Millis::new(540));
assert_eq!(z.total, Millis::new(540));
assert_eq!(a.metadata_at, Some(Millis::new(300)));
assert_eq!(z.metadata_at, Some(Millis::new(300)));
assert_eq!(a.kind, UnitKind::Lib);
assert_eq!(z.kind, UnitKind::Lib);
assert!(!a.requires_full_closure);
assert!(!z.requires_full_closure);
assert_eq!(a.package, "target");
assert_eq!(z.package, "target");
assert_eq!(split.len(), 5);
assert_eq!(graph.edge_count(), 3);
assert_eq!(split.edge_count(), 6);
let new_first_dep = kept(&applied, first_dep);
let new_second_dep = kept(&applied, second_dep);
let new_producer = kept(&applied, producer);
assert_eq!(producers(split, new_first_dep), vec![first, second]);
assert_eq!(producers(split, new_second_dep), vec![first, second]);
assert_eq!(producers(split, first), vec![new_producer]);
assert_eq!(producers(split, second), vec![new_producer]);
for dep in split.dependencies(new_first_dep).unwrap() {
assert_eq!(dep.kind, EdgeKind::Metadata);
}
for dep in split.dependencies(new_second_dep).unwrap() {
assert_eq!(dep.kind, EdgeKind::Full);
}
for part in [first, second] {
for dep in split.dependencies(part).unwrap() {
assert_eq!(dep.kind, EdgeKind::Metadata);
}
}
assert_eq!(graph.total_work(), Millis::new(1600));
assert_eq!(split.total_work(), Millis::new(1680));
assert_eq!(
targets(split),
vec![
"producer".to_owned(),
"target (part a)".to_owned(),
"target (part b)".to_owned(),
"first_dep".to_owned(),
"second_dep".to_owned(),
]
);
}
#[test]
fn optimistic_split_gives_each_dependent_exactly_one_part() {
let mut b = GraphBuilder::new();
let target = b.add_unit(lib("target", 800, 400)).unwrap();
let d0 = b.add_unit(lib("d0", 10, 5)).unwrap();
let d1 = b.add_unit(lib("d1", 10, 5)).unwrap();
let d2 = b.add_unit(lib("d2", 10, 5)).unwrap();
let d3 = b.add_unit(lib("d3", 10, 5)).unwrap();
for dependent in [d0, d1, d2, d3] {
b.add_edge(target, dependent, EdgeKind::Metadata).unwrap();
}
let graph = b.build().unwrap();
let applied = apply(
&graph,
&Transform::SplitUnit {
unit: target,
numerator: 1,
denominator: nz(2),
dependents: SplitDependents::Optimistic,
overhead: Millis::ZERO,
},
)
.unwrap();
let split = applied.graph();
let (first, second) = split_halves(&applied, target);
for dependent in [d0, d1, d2, d3] {
assert_eq!(producers(split, kept(&applied, dependent)), vec![first]);
}
assert!(split.dependents(second).unwrap().is_empty());
assert_eq!(split.edge_count(), 4);
for dependent in [d0, d1, d2, d3] {
assert_eq!(
split.dependencies(kept(&applied, dependent)).unwrap().len(),
1
);
}
assert_eq!(split.unit(first).unwrap().total, Millis::new(400));
assert_eq!(split.unit(second).unwrap().total, Millis::new(400));
}
#[test]
fn an_optimistic_split_attaches_every_dependent_to_the_part_that_finishes_first() {
let mut b = GraphBuilder::new();
let target = b.add_unit(lib("target", 800, 400)).unwrap();
let d0 = b.add_unit(lib("d0", 10, 5)).unwrap();
let d1 = b.add_unit(lib("d1", 10, 5)).unwrap();
b.add_edge(target, d0, EdgeKind::Metadata).unwrap();
b.add_edge(target, d1, EdgeKind::Full).unwrap();
let graph = b.build().unwrap();
let applied = apply(
&graph,
&Transform::SplitUnit {
unit: target,
numerator: 3,
denominator: nz(4),
dependents: SplitDependents::Optimistic,
overhead: Millis::ZERO,
},
)
.unwrap();
let split = applied.graph();
let (first, second) = split_halves(&applied, target);
assert_eq!(split.unit(first).unwrap().total, Millis::new(600));
assert_eq!(split.unit(second).unwrap().total, Millis::new(200));
assert_eq!(
split.unit(first).unwrap().metadata_at,
Some(Millis::new(300))
);
assert_eq!(
split.unit(second).unwrap().metadata_at,
Some(Millis::new(100))
);
assert_eq!(producers(split, kept(&applied, d0)), vec![second]);
assert_eq!(producers(split, kept(&applied, d1)), vec![second]);
assert!(split.dependents(first).unwrap().is_empty());
assert_eq!(split.edge_count(), 2);
}
#[test]
fn split_by_one_third_rounds_to_nearest_and_costs_only_the_overhead() {
let mut b = GraphBuilder::new();
let target = b.add_unit(lib("target", 1000, 900)).unwrap();
let graph = b.build().unwrap();
let applied = apply(
&graph,
&Transform::SplitUnit {
unit: target,
numerator: 1,
denominator: nz(3),
dependents: SplitDependents::Pessimistic,
overhead: Millis::new(25),
},
)
.unwrap();
let split = applied.graph();
let (first, second) = split_halves(&applied, target);
assert_eq!(split.unit(first).unwrap().total, Millis::new(358));
assert_eq!(split.unit(second).unwrap().total, Millis::new(692));
assert_eq!(
split.unit(first).unwrap().metadata_at,
Some(Millis::new(300))
);
assert_eq!(
split.unit(second).unwrap().metadata_at,
Some(Millis::new(600))
);
assert_eq!(graph.total_work(), Millis::new(1000));
assert_eq!(split.total_work(), Millis::new(1050));
}
#[test]
fn split_clamps_metadata_to_the_part_that_carries_it() {
let mut b = GraphBuilder::new();
let target = b.add_unit(lib("target", 999, 999)).unwrap();
let graph = b.build().unwrap();
let applied = apply(
&graph,
&Transform::SplitUnit {
unit: target,
numerator: 1,
denominator: nz(2),
dependents: SplitDependents::Pessimistic,
overhead: Millis::ZERO,
},
)
.unwrap();
let split = applied.graph();
let (first, second) = split_halves(&applied, target);
assert_eq!(split.unit(first).unwrap().total, Millis::new(500));
assert_eq!(
split.unit(first).unwrap().metadata_at,
Some(Millis::new(500))
);
assert_eq!(split.unit(second).unwrap().total, Millis::new(499));
assert_eq!(
split.unit(second).unwrap().metadata_at,
Some(Millis::new(499))
);
}
#[test]
fn splitting_a_unit_with_no_dependents_is_the_same_under_both_assumptions() {
let mut b = GraphBuilder::new();
let leaf = b.add_unit(lib("leaf", 100, 50)).unwrap();
let root = b.add_unit(opaque("root", 400, UnitKind::Bin)).unwrap();
b.add_edge(leaf, root, EdgeKind::Full).unwrap();
let graph = b.build().unwrap();
assert_eq!(roots(&graph), vec![root]);
let split = |dependents| {
apply(
&graph,
&Transform::SplitUnit {
unit: root,
numerator: 1,
denominator: nz(2),
dependents,
overhead: Millis::new(10),
},
)
.unwrap()
};
let optimistic = split(SplitDependents::Optimistic);
let pessimistic = split(SplitDependents::Pessimistic);
assert_eq!(shape(optimistic.graph()), shape(pessimistic.graph()));
assert_eq!(
optimistic.mapping().collect::<Vec<_>>(),
pessimistic.mapping().collect::<Vec<_>>()
);
let split = optimistic.graph();
let (first, second) = split_halves(&optimistic, root);
assert_eq!(roots(split), vec![first, second]);
assert_eq!(split.unit(first).unwrap().total, Millis::new(210));
assert_eq!(split.unit(second).unwrap().total, Millis::new(210));
assert_eq!(split.edge_count(), 2);
assert!(split.unit(first).unwrap().requires_full_closure);
assert!(split.unit(second).unwrap().requires_full_closure);
assert_eq!(split.unit(first).unwrap().metadata_at, None);
assert_eq!(split.unit(second).unwrap().metadata_at, None);
}
#[test]
fn splitting_a_zero_weight_unit_yields_two_units_of_overhead() {
let mut b = GraphBuilder::new();
let target = b.add_unit(lib("target", 0, 0)).unwrap();
let graph = b.build().unwrap();
let applied = apply(
&graph,
&Transform::SplitUnit {
unit: target,
numerator: 1,
denominator: nz(2),
dependents: SplitDependents::Pessimistic,
overhead: Millis::new(7),
},
)
.unwrap();
let split = applied.graph();
let (first, second) = split_halves(&applied, target);
assert_eq!(split.unit(first).unwrap().total, Millis::new(7));
assert_eq!(split.unit(second).unwrap().total, Millis::new(7));
assert_eq!(split.unit(first).unwrap().metadata_at, Some(Millis::ZERO));
assert_eq!(split.total_work(), Millis::new(14));
}
#[test]
fn splitting_a_build_script_is_refused() {
let mut b = GraphBuilder::new();
let compile = b
.add_unit(opaque("script", 300, UnitKind::BuildScriptCompile))
.unwrap();
let run = b
.add_unit(opaque("run", 200, UnitKind::BuildScriptRun))
.unwrap();
b.add_edge(compile, run, EdgeKind::Full).unwrap();
let graph = b.build().unwrap();
for (unit, kind) in [
(compile, UnitKind::BuildScriptCompile),
(run, UnitKind::BuildScriptRun),
] {
let error = apply(
&graph,
&Transform::SplitUnit {
unit,
numerator: 1,
denominator: nz(2),
dependents: SplitDependents::Pessimistic,
overhead: Millis::ZERO,
},
)
.unwrap_err();
assert_eq!(error, TransformError::UnsplittableKind { unit, kind });
assert!(error.to_string().contains("cannot be split"));
}
}
#[test]
fn splitting_off_more_than_the_whole_unit_is_refused() {
let mut b = GraphBuilder::new();
let target = b.add_unit(lib("target", 500, 250)).unwrap();
let graph = b.build().unwrap();
let error = apply(
&graph,
&Transform::SplitUnit {
unit: target,
numerator: 4,
denominator: nz(3),
dependents: SplitDependents::Pessimistic,
overhead: Millis::ZERO,
},
)
.unwrap_err();
assert_eq!(
error,
TransformError::FractionAboveOne {
unit: target,
numerator: 4,
denominator: nz(3),
}
);
assert!(error.to_string().contains("no larger than the denominator"));
let applied = apply(
&graph,
&Transform::SplitUnit {
unit: target,
numerator: 3,
denominator: nz(3),
dependents: SplitDependents::Pessimistic,
overhead: Millis::ZERO,
},
)
.unwrap();
let (first, second) = split_halves(&applied, target);
assert_eq!(applied.graph().unit(first).unwrap().total, Millis::new(500));
assert_eq!(applied.graph().unit(second).unwrap().total, Millis::ZERO);
}
#[test]
fn splitting_a_unit_from_another_graph_is_refused() {
let mut small = GraphBuilder::new();
let _ = small.add_unit(lib("only", 100, 50)).unwrap();
let small = small.build().unwrap();
let outside = last_unit_of_a_larger_graph(5);
let error = apply(
&small,
&Transform::SplitUnit {
unit: outside,
numerator: 1,
denominator: nz(2),
dependents: SplitDependents::Pessimistic,
overhead: Millis::ZERO,
},
)
.unwrap_err();
assert_eq!(
error,
TransformError::UnknownUnit {
unit: outside,
units: 1,
}
);
}
#[test]
fn removing_an_edge_deletes_that_edge_and_nothing_else() {
let mut b = GraphBuilder::new();
let a = b.add_unit(lib("a", 100, 50)).unwrap();
let mid = b.add_unit(lib("mid", 200, 100)).unwrap();
let end = b.add_unit(opaque("end", 300, UnitKind::Bin)).unwrap();
b.add_edge(a, mid, EdgeKind::Metadata).unwrap();
b.add_edge(mid, end, EdgeKind::Full).unwrap();
let graph = b.build().unwrap();
let applied = apply(&graph, &Transform::RemoveEdge { from: a, to: mid }).unwrap();
let cut = applied.graph();
assert_eq!(cut.len(), 3);
assert_eq!(cut.edge_count(), 1);
assert_eq!(cut.total_work(), Millis::new(600));
assert_eq!(targets(cut), vec!["a", "mid", "end"]);
assert_eq!(
producers(cut, kept(&applied, end)),
vec![kept(&applied, mid)]
);
assert!(cut.dependencies(kept(&applied, mid)).unwrap().is_empty());
assert_eq!(roots(cut), vec![kept(&applied, a), kept(&applied, end)]);
}
#[test]
fn removing_an_absent_edge_is_an_error() {
let mut b = GraphBuilder::new();
let a = b.add_unit(lib("a", 100, 50)).unwrap();
let mid = b.add_unit(lib("mid", 200, 100)).unwrap();
let end = b.add_unit(opaque("end", 300, UnitKind::Bin)).unwrap();
b.add_edge(a, mid, EdgeKind::Metadata).unwrap();
b.add_edge(mid, end, EdgeKind::Full).unwrap();
let graph = b.build().unwrap();
for (from, to) in [(a, end), (mid, a)] {
let error = apply(&graph, &Transform::RemoveEdge { from, to }).unwrap_err();
assert_eq!(error, TransformError::MissingEdge { from, to });
assert!(error.to_string().contains("no edge"));
}
}
fn dragged_in_graph() -> (BuildGraph, [UnitId; 5]) {
let mut b = GraphBuilder::new();
let deep = b.add_unit(lib("deep", 700, 300)).unwrap();
let mid = b.add_unit(lib("mid", 500, 200)).unwrap();
let heavy = b.add_unit(lib("heavy", 900, 400)).unwrap();
let shared = b.add_unit(lib("shared", 150, 80)).unwrap();
let app = b.add_unit(opaque("app", 250, UnitKind::Bin)).unwrap();
b.add_edge(deep, mid, EdgeKind::Metadata).unwrap();
b.add_edge(mid, heavy, EdgeKind::Metadata).unwrap();
b.add_edge(shared, heavy, EdgeKind::Metadata).unwrap();
b.add_edge(heavy, app, EdgeKind::Full).unwrap();
b.add_edge(shared, app, EdgeKind::Full).unwrap();
let graph = b.build().unwrap();
(graph, [deep, mid, heavy, shared, app])
}
#[test]
fn removing_a_unit_also_removes_what_only_it_dragged_in() {
let (graph, [deep, mid, heavy, shared, app]) = dragged_in_graph();
assert_eq!(roots(&graph), vec![app]);
let applied = apply(&graph, &Transform::RemoveUnits { units: vec![heavy] }).unwrap();
let pruned = applied.graph();
assert_eq!(targets(pruned), vec!["shared", "app"]);
assert_eq!(pruned.len(), 2);
assert_eq!(pruned.edge_count(), 1);
assert_eq!(
producers(pruned, kept(&applied, app)),
vec![kept(&applied, shared)]
);
assert_eq!(pruned.total_work(), Millis::new(400));
assert_eq!(applied.mapped(deep), Some(Mapped::Removed));
assert_eq!(applied.mapped(mid), Some(Mapped::Removed));
assert_eq!(applied.mapped(heavy), Some(Mapped::Removed));
}
#[test]
fn the_mapping_relates_every_surviving_unit_to_its_original() {
let (graph, [deep, mid, heavy, shared, app]) = dragged_in_graph();
let applied = apply(&graph, &Transform::RemoveUnits { units: vec![heavy] }).unwrap();
let pruned = applied.graph();
assert_eq!(applied.mapping().len(), 5);
assert_eq!(
applied.mapping().map(|(old, _)| old).collect::<Vec<_>>(),
vec![deep, mid, heavy, shared, app]
);
let mut survivors = 0;
for (old, mapped) in applied.mapping() {
if let Mapped::Kept(new) = mapped {
survivors += 1;
assert_eq!(
graph.unit(old).unwrap().target,
pruned.unit(new).unwrap().target
);
}
}
assert_eq!(survivors, 2);
assert!(kept(&applied, shared).index() < kept(&applied, app).index());
}
#[test]
fn removing_no_units_changes_nothing() {
let (graph, _) = dragged_in_graph();
let applied = apply(&graph, &Transform::RemoveUnits { units: Vec::new() }).unwrap();
assert_eq!(shape(applied.graph()), shape(&graph));
}
#[test]
fn removing_every_unit_yields_an_empty_graph() {
let (graph, units) = dragged_in_graph();
let applied = apply(
&graph,
&Transform::RemoveUnits {
units: units.to_vec(),
},
)
.unwrap();
let empty = applied.graph();
assert_eq!(empty.len(), 0);
assert!(empty.is_empty());
assert_eq!(empty.edge_count(), 0);
assert_eq!(empty.total_work(), Millis::ZERO);
assert_eq!(roots(empty), Vec::new());
for (_, mapped) in applied.mapping() {
assert_eq!(mapped, Mapped::Removed);
}
}
#[test]
fn removing_the_root_removes_everything_it_kept_alive() {
let (graph, [_, _, _, _, app]) = dragged_in_graph();
let applied = apply(&graph, &Transform::RemoveUnits { units: vec![app] }).unwrap();
assert_eq!(applied.graph().len(), 0);
}
#[test]
fn removing_the_same_unit_twice_is_not_an_error() {
let (graph, [_, _, heavy, _, _]) = dragged_in_graph();
let once = apply(&graph, &Transform::RemoveUnits { units: vec![heavy] }).unwrap();
let twice = apply(
&graph,
&Transform::RemoveUnits {
units: vec![heavy, heavy],
},
)
.unwrap();
assert_eq!(shape(once.graph()), shape(twice.graph()));
}
#[test]
fn removing_a_unit_from_another_graph_is_an_error() {
let (graph, [_, _, heavy, _, _]) = dragged_in_graph();
let outside = last_unit_of_a_larger_graph(9);
let error = apply(
&graph,
&Transform::RemoveUnits {
units: vec![heavy, outside],
},
)
.unwrap_err();
assert_eq!(
error,
TransformError::UnknownUnit {
unit: outside,
units: 5,
}
);
assert!(error.to_string().contains("not part of this graph"));
}
#[test]
fn an_optimistic_split_leaves_the_edges_it_does_not_touch_alone() {
let (graph, [deep, mid, heavy, shared, app]) = dragged_in_graph();
let applied = apply(
&graph,
&Transform::SplitUnit {
unit: heavy,
numerator: 1,
denominator: nz(4),
dependents: SplitDependents::Optimistic,
overhead: Millis::new(20),
},
)
.unwrap();
let split = applied.graph();
let (first, second) = split_halves(&applied, heavy);
assert_eq!(split.unit(first).unwrap().total, Millis::new(245));
assert_eq!(split.unit(second).unwrap().total, Millis::new(695));
assert_eq!(
split.unit(first).unwrap().metadata_at,
Some(Millis::new(100))
);
assert_eq!(
split.unit(second).unwrap().metadata_at,
Some(Millis::new(300))
);
assert_eq!(graph.total_work(), Millis::new(2500));
assert_eq!(split.total_work(), Millis::new(2540));
assert_eq!(split.edge_count(), 7);
let new_mid = kept(&applied, mid);
let new_shared = kept(&applied, shared);
assert_eq!(producers(split, new_mid), vec![kept(&applied, deep)]);
assert_eq!(producers(split, first), vec![new_mid, new_shared]);
assert_eq!(producers(split, second), vec![new_mid, new_shared]);
assert_eq!(
producers(split, kept(&applied, app)),
vec![first, new_shared]
);
}
#[test]
fn the_mapping_can_be_read_without_matching_on_it() {
let (graph, [deep, _, heavy, shared, _]) = dragged_in_graph();
let split = apply(
&graph,
&Transform::SplitUnit {
unit: heavy,
numerator: 1,
denominator: nz(2),
dependents: SplitDependents::Pessimistic,
overhead: Millis::ZERO,
},
)
.unwrap();
let (first, second) = split_halves(&split, heavy);
assert_eq!(split.mapped(heavy).unwrap().primary(), Some(first));
assert_eq!(split.mapped(heavy).unwrap().secondary(), Some(second));
assert!(!split.mapped(heavy).unwrap().is_removed());
let pruned = apply(&graph, &Transform::RemoveUnits { units: vec![heavy] }).unwrap();
assert_eq!(
pruned.mapped(shared).unwrap().primary(),
Some(kept(&pruned, shared))
);
assert_eq!(pruned.mapped(shared).unwrap().secondary(), None);
assert!(pruned.mapped(deep).unwrap().is_removed());
assert_eq!(pruned.mapped(deep).unwrap().primary(), None);
assert_eq!(pruned.graph().len(), 2);
assert_eq!(pruned.into_graph().len(), 2);
}
#[test]
fn the_same_transform_applied_twice_gives_identical_graphs() {
let (graph, [_, _, heavy, _, app]) = dragged_in_graph();
let transforms = [
Transform::SplitUnit {
unit: heavy,
numerator: 2,
denominator: nz(7),
dependents: SplitDependents::Optimistic,
overhead: Millis::new(33),
},
Transform::SplitUnit {
unit: heavy,
numerator: 2,
denominator: nz(7),
dependents: SplitDependents::Pessimistic,
overhead: Millis::new(33),
},
Transform::RemoveUnits { units: vec![heavy] },
Transform::RemoveEdge {
from: heavy,
to: app,
},
];
for transform in &transforms {
let first = apply(&graph, transform).unwrap();
let second = apply(&graph, transform).unwrap();
assert_eq!(shape(first.graph()), shape(second.graph()));
assert_eq!(
first.mapping().collect::<Vec<_>>(),
second.mapping().collect::<Vec<_>>()
);
}
}