ftui-render 0.4.0

Render kernel: cells, buffers, diffs, and ANSI presentation.
Documentation
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
459
460
461
462
463
464
465
466
467
468
//! Property-based invariant tests for the ftui-render diff algorithm.
//!
//! These tests verify structural invariants of `BufferDiff` that must hold
//! for **any** pair of buffers:
//!
//! 1. Identical buffers produce zero changes.
//! 2. Every change position is within bounds.
//! 3. Every changed cell actually differs between old and new.
//! 4. No unchanged cell is reported as changed (no false positives).
//! 5. Diff is deterministic (same inputs → same output).
//! 6. `compute` and `compute_into` produce identical results.
//! 7. Runs cover exactly the same positions as the raw changes.
//! 8. Runs are sorted by row-major order.
//! 9. `compute_dirty` is a superset-or-equal of `compute` (no missed changes).
//! 10. Full diff captures every cell in the buffer.
//! 14. `runs()` and `runs_into()` produce identical output (isomorphism).
//! 15. `runs_into` reuses capacity across calls.

use ftui_render::buffer::Buffer;
use ftui_render::cell::Cell;
use ftui_render::diff::BufferDiff;
use proptest::prelude::*;

// ── Helpers ─────────────────────────────────────────────────────────────

/// Dimensions strategy: small enough for fast tests, large enough for edge cases.
fn dims() -> impl Strategy<Value = (u16, u16)> {
    (1u16..=80, 1u16..=40)
}

/// Apply random scattered changes to a buffer.
fn apply_changes(buf: &mut Buffer, changes: &[(u16, u16, char)]) {
    for &(x, y, ch) in changes {
        if x < buf.width() && y < buf.height() {
            buf.set_raw(x, y, Cell::from_char(ch));
        }
    }
}

/// Strategy for a vec of (x, y, char) change triples within given bounds.
fn change_set(max_w: u16, max_h: u16) -> impl Strategy<Value = Vec<(u16, u16, char)>> {
    proptest::collection::vec(
        (
            0..max_w,
            0..max_h,
            prop_oneof![
                Just('A'),
                Just('X'),
                Just('Z'),
                Just('#'),
                Just(' '),
                (0x21u32..=0x7E).prop_map(|c| char::from_u32(c).unwrap()),
            ],
        ),
        0..200,
    )
}

// ═════════════════════════════════════════════════════════════════════════
// 1. Identical buffers produce zero changes
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn identical_buffers_produce_empty_diff((w, h) in dims()) {
        let buf = Buffer::new(w, h);
        let diff = BufferDiff::compute(&buf, &buf);
        prop_assert!(diff.is_empty(),
            "Diff between identical {}x{} buffers should be empty, got {} changes",
            w, h, diff.len());
    }

    /// After applying the same changes to both buffers, diff should be empty.
    #[test]
    fn same_changes_produce_empty_diff(
        (w, h) in dims(),
        changes in change_set(80, 40),
    ) {
        let mut buf1 = Buffer::new(w, h);
        let mut buf2 = Buffer::new(w, h);
        apply_changes(&mut buf1, &changes);
        apply_changes(&mut buf2, &changes);
        let diff = BufferDiff::compute(&buf1, &buf2);
        prop_assert!(diff.is_empty(),
            "Same changes should produce empty diff, got {} changes", diff.len());
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 2. Every change position is within bounds
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn change_positions_in_bounds(
        (w, h) in dims(),
        changes in change_set(80, 40),
    ) {
        let old = Buffer::new(w, h);
        let mut new = old.clone();
        apply_changes(&mut new, &changes);
        let diff = BufferDiff::compute(&old, &new);

        for &(x, y) in diff.changes() {
            prop_assert!(x < w, "x={} >= width={}", x, w);
            prop_assert!(y < h, "y={} >= height={}", y, h);
        }
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 3. Every reported change is a true positive (cells actually differ)
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn no_false_positive_changes(
        (w, h) in dims(),
        changes in change_set(80, 40),
    ) {
        let old = Buffer::new(w, h);
        let mut new = old.clone();
        apply_changes(&mut new, &changes);
        let diff = BufferDiff::compute(&old, &new);

        for &(x, y) in diff.changes() {
            let old_cell = old.get(x, y).unwrap();
            let new_cell = new.get(x, y).unwrap();
            prop_assert!(!old_cell.bits_eq(new_cell),
                "False positive: cells at ({}, {}) are identical but reported as changed", x, y);
        }
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 4. No true change is missed (completeness / no false negatives)
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn no_false_negative_changes(
        (w, h) in dims(),
        changes in change_set(80, 40),
    ) {
        let old = Buffer::new(w, h);
        let mut new = old.clone();
        apply_changes(&mut new, &changes);
        let diff = BufferDiff::compute(&old, &new);

        // Build a set of reported changes for quick lookup.
        let change_set: std::collections::HashSet<(u16, u16)> =
            diff.changes().iter().copied().collect();

        // Check every cell: if it differs, it must be in the diff.
        for y in 0..h {
            for x in 0..w {
                let old_cell = old.get(x, y).unwrap();
                let new_cell = new.get(x, y).unwrap();
                if !old_cell.bits_eq(new_cell) {
                    prop_assert!(change_set.contains(&(x, y)),
                        "False negative: cell ({}, {}) differs but not in diff", x, y);
                }
            }
        }
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 5. Diff is deterministic
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn diff_is_deterministic(
        (w, h) in dims(),
        changes in change_set(80, 40),
    ) {
        let old = Buffer::new(w, h);
        let mut new = old.clone();
        apply_changes(&mut new, &changes);

        let diff1 = BufferDiff::compute(&old, &new);
        let diff2 = BufferDiff::compute(&old, &new);

        prop_assert_eq!(diff1.changes(), diff2.changes(),
            "Two compute() calls produced different results");
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 6. compute and compute_into produce identical results
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn compute_and_compute_into_equivalent(
        (w, h) in dims(),
        changes in change_set(80, 40),
    ) {
        let old = Buffer::new(w, h);
        let mut new = old.clone();
        apply_changes(&mut new, &changes);

        let diff_fresh = BufferDiff::compute(&old, &new);
        let mut diff_reused = BufferDiff::new();
        diff_reused.compute_into(&old, &new);

        prop_assert_eq!(diff_fresh.changes(), diff_reused.changes(),
            "compute() and compute_into() disagree");
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 7. Runs cover exactly the same positions as raw changes
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn runs_cover_all_changes(
        (w, h) in dims(),
        changes in change_set(80, 40),
    ) {
        let old = Buffer::new(w, h);
        let mut new = old.clone();
        apply_changes(&mut new, &changes);
        let diff = BufferDiff::compute(&old, &new);

        // Expand runs back to individual positions.
        let runs = diff.runs();
        let mut run_positions: Vec<(u16, u16)> = Vec::new();
        for run in &runs {
            for x in run.x0..=run.x1 {
                run_positions.push((x, run.y));
            }
        }
        run_positions.sort();

        let mut raw_positions: Vec<(u16, u16)> = diff.changes().to_vec();
        raw_positions.sort();

        prop_assert_eq!(run_positions, raw_positions,
            "Runs don't cover the same positions as raw changes");
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 8. Changes are sorted in row-major order
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn changes_sorted_row_major(
        (w, h) in dims(),
        changes in change_set(80, 40),
    ) {
        let old = Buffer::new(w, h);
        let mut new = old.clone();
        apply_changes(&mut new, &changes);
        let diff = BufferDiff::compute(&old, &new);

        let positions = diff.changes();
        for window in positions.windows(2) {
            let (x1, y1) = window[0];
            let (x2, y2) = window[1];
            prop_assert!(
                (y1, x1) < (y2, x2),
                "Changes not in row-major order: ({},{}) before ({},{})", x1, y1, x2, y2
            );
        }
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 9. Full diff covers every cell
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn full_diff_covers_all_cells((w, h) in dims()) {
        let diff = BufferDiff::full(w, h);
        let expected = (w as usize) * (h as usize);
        prop_assert_eq!(diff.len(), expected,
            "Full diff should have {}*{}={} changes, got {}", w, h, expected, diff.len());
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 10. Runs are sorted and non-overlapping
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn runs_sorted_and_non_overlapping(
        (w, h) in dims(),
        changes in change_set(80, 40),
    ) {
        let old = Buffer::new(w, h);
        let mut new = old.clone();
        apply_changes(&mut new, &changes);
        let diff = BufferDiff::compute(&old, &new);
        let runs = diff.runs();

        for window in runs.windows(2) {
            let a = &window[0];
            let b = &window[1];
            if a.y == b.y {
                // Same row: runs must not overlap
                prop_assert!(a.x1 < b.x0,
                    "Overlapping runs on row {}: [{}, {}] and [{}, {}]",
                    a.y, a.x0, a.x1, b.x0, b.x1);
            } else {
                // Different rows: must be in ascending order
                prop_assert!(a.y < b.y,
                    "Runs not sorted by row: y={} before y={}", a.y, b.y);
            }
        }
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 11. Dirty diff is a superset of regular diff
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn dirty_diff_superset_of_compute(
        (w, h) in dims(),
        changes in change_set(80, 40),
    ) {
        let old = Buffer::new(w, h);
        let mut new = old.clone();
        apply_changes(&mut new, &changes);

        let exact_diff = BufferDiff::compute(&old, &new);
        let dirty_diff = BufferDiff::compute_dirty(&old, &new);

        // Every change in compute() must appear in compute_dirty().
        let dirty_set: std::collections::HashSet<(u16, u16)> =
            dirty_diff.changes().iter().copied().collect();

        for &(x, y) in exact_diff.changes() {
            prop_assert!(dirty_set.contains(&(x, y)),
                "compute_dirty missed change at ({}, {})", x, y);
        }
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 12. Diff symmetry: |diff(A,B)| == |diff(B,A)|
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn diff_symmetric_count(
        (w, h) in dims(),
        changes in change_set(80, 40),
    ) {
        let old = Buffer::new(w, h);
        let mut new = old.clone();
        apply_changes(&mut new, &changes);

        let forward = BufferDiff::compute(&old, &new);
        let backward = BufferDiff::compute(&new, &old);

        prop_assert_eq!(forward.len(), backward.len(),
            "Forward diff has {} changes but backward has {}", forward.len(), backward.len());

        // Same positions should be detected in both directions.
        prop_assert_eq!(forward.changes(), backward.changes(),
            "Forward and backward diffs report different positions");
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 13. compute_into clears previous state
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn compute_into_clears_previous(
        (w, h) in dims(),
        changes1 in change_set(80, 40),
        changes2 in change_set(80, 40),
    ) {
        let base = Buffer::new(w, h);

        let mut buf1 = base.clone();
        apply_changes(&mut buf1, &changes1);
        let mut buf2 = base.clone();
        apply_changes(&mut buf2, &changes2);

        let mut diff = BufferDiff::new();

        // First compute
        diff.compute_into(&base, &buf1);
        let first_len = diff.len();

        // Second compute should overwrite, not append
        diff.compute_into(&base, &buf2);

        // Verify the diff now matches a fresh compute for buf2
        let fresh = BufferDiff::compute(&base, &buf2);
        prop_assert_eq!(diff.changes(), fresh.changes(),
            "compute_into didn't reset: first had {} changes, reuse has {}, fresh has {}",
            first_len, diff.len(), fresh.len());
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 14. runs() and runs_into() produce identical output (bd-1tssj isomorphism)
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn runs_and_runs_into_isomorphic(
        (w, h) in dims(),
        changes in change_set(80, 40),
    ) {
        let old = Buffer::new(w, h);
        let mut new = old.clone();
        apply_changes(&mut new, &changes);
        let diff = BufferDiff::compute(&old, &new);

        let allocating = diff.runs();
        let mut reuse_buf = Vec::new();
        diff.runs_into(&mut reuse_buf);

        prop_assert_eq!(&allocating, &reuse_buf,
            "runs() and runs_into() differ for {}x{} with {} changes",
            w, h, diff.len());
    }
}

// ═════════════════════════════════════════════════════════════════════════
// 15. runs_into reuses capacity (no extra allocation after warmup)
// ═════════════════════════════════════════════════════════════════════════

proptest! {
    #[test]
    fn runs_into_reuses_capacity(
        (w, h) in dims(),
        changes1 in change_set(80, 40),
        changes2 in change_set(80, 40),
    ) {
        let base = Buffer::new(w, h);
        let mut buf1 = base.clone();
        apply_changes(&mut buf1, &changes1);
        let mut buf2 = base.clone();
        apply_changes(&mut buf2, &changes2);

        let diff1 = BufferDiff::compute(&base, &buf1);
        let diff2 = BufferDiff::compute(&base, &buf2);

        let mut reuse_buf = Vec::new();

        // First call warms up the buffer.
        diff1.runs_into(&mut reuse_buf);
        let cap_after_first = reuse_buf.capacity();

        // Second call should reuse the capacity if the result fits.
        diff2.runs_into(&mut reuse_buf);
        let cap_after_second = reuse_buf.capacity();

        // Capacity should not shrink (Vec::clear preserves capacity).
        prop_assert!(cap_after_second >= cap_after_first.min(reuse_buf.len()),
            "runs_into shrank capacity: {} -> {}", cap_after_first, cap_after_second);
    }
}