b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 1 | use crate::geom::Rect; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 2 | use crate::plan::{Config, Plan, Strategy, Style, plan, plan_canonical}; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 3 | use crate::{plan_blocks, validate}; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 4 | use proptest::prelude::*; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 5 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 6 | const G: f64 = 10.0; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 7 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 8 | fn region() -> Rect { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 9 | Rect::new(G, G, 1180.0, 680.0) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 10 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 11 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 12 | fn split(rect: Rect, ratios: &[f64], horizontal: bool) -> Vec<Rect> { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 13 | let total: f64 = ratios.iter().sum(); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 14 | let len = if horizontal { rect.w } else { rect.h }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 15 | let avail = len - G * (ratios.len() - 1) as f64; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 16 | let mut pos = if horizontal { rect.x } else { rect.y }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 17 | ratios |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 18 | .iter() |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 19 | .map(|r| { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 20 | let size = avail * r / total; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 21 | let out = if horizontal { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 22 | Rect::new(pos, rect.y, size, rect.h) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 23 | } else { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 24 | Rect::new(rect.x, pos, rect.w, size) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 25 | }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 26 | pos += size + G; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 27 | out |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 28 | }) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 29 | .collect() |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 30 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 31 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 32 | fn cols(rect: Rect, n: usize) -> Vec<Rect> { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 33 | split(rect, &vec![1.0; n], true) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 34 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 35 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 36 | fn rows(rect: Rect, n: usize) -> Vec<Rect> { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 37 | split(rect, &vec![1.0; n], false) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 38 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 39 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 40 | fn cfg(style: Style) -> Config { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 41 | Config { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 42 | style, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 43 | ..Config::default() |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 44 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 45 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 46 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 47 | fn assert_clean(p: &Plan, c: &Config) { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 48 | let v = validate(p, c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 49 | assert!(v.is_empty(), "violations {v:?} in {p:?}"); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 50 | assert!(p.phases() <= 4, "{} phases", p.phases()); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 51 | assert!( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 52 | p.groups.iter().all(|g| g.strategy != Strategy::Pinch), |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 53 | "fell back to pinch: {p:?}" |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 54 | ); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 55 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 56 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 57 | fn strategy(p: &Plan) -> Strategy { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 58 | p.groups |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 59 | .iter() |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 60 | .map(|g| g.strategy) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 61 | .find(|s| *s != Strategy::Unchanged) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 62 | .unwrap_or(Strategy::Unchanged) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 63 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 64 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 65 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 66 | fn swap_uses_two_lanes() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 67 | let c = cfg(Style::Orthogonal); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 68 | let ab = cols(region(), 2); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 69 | let ba = vec![ab[1], ab[0]]; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 70 | let p = plan(&ab, &ba, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 71 | assert_clean(&p, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 72 | assert_eq!(strategy(&p), Strategy::Lanes { lanes: 2 }); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 73 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 74 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 75 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 76 | fn full_reversal_needs_one_lane_per_window() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 77 | let c = cfg(Style::Orthogonal); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 78 | let a = cols(region(), 4); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 79 | let b: Vec<Rect> = (0..4).map(|i| a[3 - i]).collect(); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 80 | let p = plan(&a, &b, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 81 | assert_clean(&p, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 82 | assert_eq!(strategy(&p), Strategy::Lanes { lanes: 4 }); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 83 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 84 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 85 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 86 | fn orientation_flip_is_two_phases() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 87 | let c = cfg(Style::Orthogonal); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 88 | let p = plan(&cols(region(), 3), &rows(region(), 3), &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 89 | assert_clean(&p, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 90 | assert_eq!(strategy(&p), Strategy::AxisOrder); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 91 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 92 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 93 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 94 | fn growing_both_axes_depends_on_style() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 95 | let grid = |wide: bool| { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 96 | let r = if wide { [7.0, 3.0] } else { [1.0, 1.0] }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 97 | let rs = split(region(), &r, false); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 98 | let mut out = split(rs[0], &r, true); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 99 | out.extend(split(rs[1], &[1.0, 1.0], true)); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 100 | out |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 101 | }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 102 | let o = cfg(Style::Orthogonal); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 103 | let d = cfg(Style::Direct); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 104 | let po = plan(&grid(false), &grid(true), &o); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 105 | let pd = plan(&grid(false), &grid(true), &d); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 106 | assert_clean(&po, &o); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 107 | assert_clean(&pd, &d); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 108 | assert_eq!((po.phases(), pd.phases()), (2, 1)); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 109 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 110 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 111 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 112 | fn insert_and_split_rewinds() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 113 | let c = cfg(Style::Orthogonal); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 114 | let layout = |on: bool| { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 115 | let n = if on { 4 } else { 3 }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 116 | let ratios: Vec<f64> = (0..n) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 117 | .map(|i| if i == 0 && on { 2.0 } else { 1.0 }) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 118 | .collect(); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 119 | let cs = split(region(), &ratios, true); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 120 | let (a, mut b, c3) = (cs[0], cs[1], cs[n - 1]); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 121 | let d = if on { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 122 | cs[2] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 123 | } else { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 124 | Rect::new((b.right() + c3.x) / 2.0, b.y, 0.0, b.h) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 125 | }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 126 | let e = if on { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 127 | let s = rows(b, 2); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 128 | b = s[0]; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 129 | s[1] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 130 | } else { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 131 | Rect::new(b.x, b.bottom(), b.w, 0.0) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 132 | }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 133 | vec![a, b, c3, d, e] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 134 | }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 135 | let on = plan(&layout(false), &layout(true), &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 136 | let off = plan(&layout(true), &layout(false), &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 137 | assert_clean(&on, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 138 | assert_clean(&off, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 139 | assert!(on.groups.iter().all(|g| !g.opens_hole)); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 140 | let mut rewound = off.keyframes.clone(); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 141 | rewound.reverse(); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 142 | assert_eq!(on.keyframes, rewound); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 143 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 144 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 145 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 146 | fn inserting_a_seed_is_one_phase() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 147 | let c = cfg(Style::Orthogonal); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 148 | let three = cols(region(), 3); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 149 | let four = cols(region(), 4); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 150 | let seed = Rect::new( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 151 | (three[1].right() + three[2].x) / 2.0, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 152 | three[1].y, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 153 | 0.0, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 154 | three[1].h, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 155 | ); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 156 | let from = vec![three[0], three[1], three[2], seed]; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 157 | let to = vec![four[0], four[1], four[3], four[2]]; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 158 | let p = plan(&from, &to, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 159 | assert_clean(&p, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 160 | assert_eq!(strategy(&p), Strategy::Direct); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 161 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 162 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 163 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 164 | fn tag_rows_swap_rigidly_with_two_lanes() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 165 | let c = cfg(Style::Orthogonal); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 166 | let layout = |first_top: bool| { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 167 | let rs = rows(region(), 2); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 168 | let (t1, t2) = if first_top { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 169 | (rs[0], rs[1]) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 170 | } else { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 171 | (rs[1], rs[0]) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 172 | }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 173 | let mut out = cols(t1, 3); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 174 | out.extend(cols(t2, 2)); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 175 | out |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 176 | }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 177 | let blocks = vec![vec![0, 1, 2], vec![3, 4]]; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 178 | let p = plan_blocks(&layout(true), &layout(false), &blocks, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 179 | assert_clean(&p, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 180 | assert_eq!(strategy(&p), Strategy::Lanes { lanes: 2 }); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 181 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 182 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 183 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 184 | fn simultaneous_reversals_on_both_axes_use_the_grid() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 185 | let c = cfg(Style::Orthogonal); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 186 | let halves = cols(region(), 2); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 187 | let (l, r) = (rows(halves[0], 2), rows(halves[1], 2)); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 188 | let from = vec![l[0], r[1], r[0], l[1]]; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 189 | let left3 = rows(halves[0], 3); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 190 | let to = vec![left3[0], left3[1], left3[2], halves[1]]; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 191 | let p = plan(&from, &to, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 192 | assert_clean(&p, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 193 | assert!(matches!(strategy(&p), Strategy::Grid { .. })); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 194 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 195 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 196 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 197 | fn distant_groups_plan_independently() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 198 | let c = cfg(Style::Orthogonal); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 199 | let thirds = cols(region(), 3); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 200 | let l = rows(thirds[0], 2); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 201 | let from = vec![l[0], l[1], thirds[1], thirds[2]]; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 202 | let to = vec![l[1], l[0], thirds[1], thirds[2]]; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 203 | let p = plan(&from, &to, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 204 | assert_clean(&p, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 205 | assert!( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 206 | p.groups |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 207 | .iter() |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 208 | .any(|g| g.members == [3] && g.strategy == Strategy::Unchanged) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 209 | ); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 210 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 211 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 212 | struct Rng(u64); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 213 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 214 | impl Rng { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 215 | fn next(&mut self) -> u64 { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 216 | self.0 ^= self.0 << 13; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 217 | self.0 ^= self.0 >> 7; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 218 | self.0 ^= self.0 << 17; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 219 | self.0 |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 220 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 221 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 222 | fn below(&mut self, n: usize) -> usize { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 223 | (self.next() % n as u64) as usize |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 224 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 225 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 226 | fn unit(&mut self) -> f64 { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 227 | (self.next() >> 11) as f64 / (1u64 << 53) as f64 |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 228 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 229 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 230 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 231 | fn random_layout(rng: &mut Rng, n: usize) -> Vec<Rect> { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 232 | let mut ids: Vec<usize> = (0..n).collect(); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 233 | for i in (1..n).rev() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 234 | ids.swap(i, rng.below(i + 1)); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 235 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 236 | let mut out = vec![Rect::default(); n]; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 237 | let horizontal = rng.below(2) == 0; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 238 | build(rng, &ids, region(), horizontal, &mut out); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 239 | out |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 240 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 241 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 242 | fn build(rng: &mut Rng, ids: &[usize], rect: Rect, horizontal: bool, out: &mut [Rect]) { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 243 | if let [only] = ids { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 244 | out[*only] = rect; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 245 | return; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 246 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 247 | let k = ids.len().min(2 + rng.below(2)); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 248 | let mut cuts = Vec::with_capacity(k); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 249 | let mut rest = ids; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 250 | for i in 0..k { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 251 | let take = if i == k - 1 { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 252 | rest.len() |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 253 | } else { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 254 | 1 + rng.below(rest.len() - (k - 1 - i)) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 255 | }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 256 | let (head, tail) = rest.split_at(take); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 257 | cuts.push(head); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 258 | rest = tail; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 259 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 260 | let ratios: Vec<f64> = cuts.iter().map(|_| 0.5 + rng.unit()).collect(); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 261 | for (part, ids) in split(rect, &ratios, horizontal).into_iter().zip(cuts) { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 262 | build(rng, ids, part, !horizontal, out); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 263 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 264 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 265 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 266 | proptest! { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 267 | #![proptest_config(ProptestConfig::with_cases(512))] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 268 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 269 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 270 | fn random_transitions_plan_cleanly(seed in 1u64.., n in 2usize..9, direct: bool) { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 271 | let mut rng = Rng(seed); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 272 | let c = cfg(if direct { Style::Direct } else { Style::Orthogonal }); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 273 | let (a, b) = (random_layout(&mut rng, n), random_layout(&mut rng, n)); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 274 | let p = plan(&a, &b, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 275 | assert_clean(&p, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 276 | prop_assert_eq!(&p.keyframes[0], &a); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 277 | prop_assert_eq!(p.keyframes.last().unwrap(), &b); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 278 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 279 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 280 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 281 | fn planning_back_rewinds(seed in 1u64.., n in 2usize..9) { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 282 | let mut rng = Rng(seed); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 283 | let c = cfg(Style::Orthogonal); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 284 | let (a, b) = (random_layout(&mut rng, n), random_layout(&mut rng, n)); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 285 | let mut back = plan_canonical(&b, &a, &c).keys; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 286 | back.reverse(); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 287 | prop_assert_eq!(plan_canonical(&a, &b, &c).keys, back); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 288 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 289 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 290 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 291 | fn interrupts_replan_cleanly(seed in 1u64.., n in 2usize..9, at in 0.0f64..1.0, s in 0.0f64..1.0) { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 292 | let mut rng = Rng(seed); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 293 | let c = cfg(Style::Orthogonal); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 294 | let (a, b, d) = (random_layout(&mut rng, n), random_layout(&mut rng, n), random_layout(&mut rng, n)); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 295 | let first = plan(&a, &b, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 296 | if first.phases() == 0 { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 297 | return Ok(()); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 298 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 299 | let phase = ((at * first.phases() as f64) as usize).min(first.phases() - 1); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 300 | let mid = first.sample(phase, s); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 301 | let second = plan(&mid, &d, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 302 | assert_clean(&second, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 303 | prop_assert_eq!(&second.keyframes[0], &mid); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 304 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 305 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 306 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 307 | #[test] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 308 | #[ignore = "statistics, run with --ignored --nocapture"] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 309 | fn strategy_distribution() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 310 | use std::collections::BTreeMap; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 311 | for style in [Style::Orthogonal, Style::Direct] { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 312 | let c = cfg(style); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 313 | let mut rng = Rng(0x9e37_79b9_7f4a_7c15); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 314 | let mut counts: BTreeMap<String, usize> = BTreeMap::new(); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 315 | let mut holes = 0; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 316 | for _ in 0..20_000 { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 317 | let n = 2 + rng.below(7); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 318 | let (a, b) = (random_layout(&mut rng, n), random_layout(&mut rng, n)); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 319 | let p = plan(&a, &b, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 320 | assert_clean(&p, &c); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 321 | holes += p.groups.iter().filter(|g| g.opens_hole).count(); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 322 | let key = match strategy(&p) { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 323 | Strategy::Lanes { .. } => "Lanes".to_string(), |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 324 | Strategy::Grid { .. } => "Grid".to_string(), |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 325 | s => format!("{s:?}"), |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 326 | }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 327 | *counts.entry(key).or_default() += 1; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 328 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 329 | println!("{style:?}: {counts:?}, plans opening a hole: {holes}"); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 330 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 331 | } |