Replay compare-dumps: a differing buffer's line names its first word in each play

compare-dumps said which word of a buffer differed first outside every
cover, but not what either play held there, so telling a NaN from a
rounding step or a stale word meant reading both dumps by hand. A
Difference now carries that word in each play's dump (first_values,
little-endian; a short end's bytes with zeros above them, none past a
dump's end), and its line prints them after the word's index:
"the first word W (a 0x........, b 0x........)". The stream doc says so.

The three compare tests take the values in their expected lines and
Differences, and a new unit test compares three buffers whose second
word differs in b, ends 2 bytes into it, or ends before it.
This commit is contained in:
Anees Iqbal committed 2026-11-27 02:09:46 -08:00
1 parent 866a8dd2f2
commit 8ac09ba43e
3 files changed
+76 -8

No files matched your search

@@ -296,6 +296,9 @@ pub struct Difference {
pub covered: u64,
/// The first differing word outside every cover, by its index (its byte offset over 4).
pub first_uncovered: Option<u64>,
/// That word in each play's dump, little-endian, a short end's bytes read with zeros above
/// them: none past that dump's end, or where no word differs outside every cover.
pub first_values: (Option<u32>, Option<u32>),
/// The two dumps' lengths in bytes.
pub lengths: (u64, u64),
}
@@ -347,7 +350,11 @@ impl Comparison {
pub fn lines(&self) -> Vec<String> {
let mut lines = Vec::new();
for d in self.differences.iter().filter(|d| d.uncovered() > 0) {
let first = d.first_uncovered.map_or("-".to_owned(), |w| w.to_string());
let value = |v: Option<u32>| v.map_or("none".to_owned(), |v| format!("{v:#010x}"));
let first = d.first_uncovered.map_or("-".to_owned(), |w| {
let (a, b) = d.first_values;
format!("{w} (a {}, b {})", value(a), value(b))
});
lines.push(format!(
"differs cb #{} obj #{}: {} words outside every cover, the first word {first}; \
{} covered; lengths {} and {}",
@@ -479,6 +486,18 @@ pub(super) fn words(a: &[u8], b: &[u8], covered: &[(u64, u64)]) -> (u64, u64, Op
(differing, under, first)
}
/// Word `index` of `bytes`, little-endian, a short end's bytes read with zeros above them: none
/// past its end.
fn word_at(bytes: &[u8], index: u64) -> Option<u32> {
let at = usize::try_from(index).ok()?.checked_mul(4)?;
let word = bytes
.get(at..(at + 4).min(bytes.len()))
.filter(|w| !w.is_empty())?;
let mut four = [0; 4];
four[..word.len()].copy_from_slice(word);
Some(u32::from_le_bytes(four))
}
/// The dumps of two plays, in `a` and `b`, compared.
pub fn compare_dumps(a: &Path, b: &Path) -> io::Result<Comparison> {
let (mut a, mut b) = (read_dir(a)?, read_dir(b)?);
@@ -524,6 +543,7 @@ pub fn compare_dumps(a: &Path, b: &Path) -> io::Result<Comparison> {
differing,
covered: under,
first_uncovered: first,
first_values: first.map_or((None, None), |w| (word_at(&bx, w), word_at(&by, w))),
lengths: (bx.len() as u64, by.len() as u64),
});
}
@@ -3463,6 +3463,7 @@ fn the_dump_compare_reports_a_differing_word_outside_every_cover() {
differing: 2,
covered: 1,
first_uncovered: Some(0x600),
first_values: (Some(0), Some(1)),
lengths: (0x2000, 0x2000),
};
assert_eq!(comparison.differences, [difference]);
@@ -3472,8 +3473,8 @@ fn the_dump_compare_reports_a_differing_word_outside_every_cover() {
assert_eq!(
lines,
[
"differs cb #20 obj #3: 1 words outside every cover, the first word 1536; 1 covered; \
lengths 8192 and 8192",
"differs cb #20 obj #3: 1 words outside every cover, the first word 1536 (a \
0x00000000, b 0x00000001); 1 covered; lengths 8192 and 8192",
"compared 2 buffers (0 as both plays' CPU copies, 0 as their device copies, read \
back): 1 differ, 1 words outside every cover, 1 covered; 0 dumps in one play only, 0 \
unread, 0 of different copies: DIFFER",
@@ -3496,6 +3497,7 @@ fn the_dump_compare_reports_a_word_whose_cover_the_buffer_s_last_write_voided()
differing: 2,
covered: 0,
first_uncovered: Some(4),
first_values: (Some(0), Some(1)),
lengths: (0x2000, 0x2000),
};
assert_eq!(comparison.differences, [difference]);
@@ -3561,23 +3563,68 @@ fn the_dump_compare_leaves_out_a_covered_texture_and_reports_one_outside_every_c
assert_eq!([cover(5), cover(6), cover(7)], ["0..32", "-", "-"]);
let comparison = compare_dumps(a.path(), b.path()).unwrap();
assert_eq!(comparison.compared, 3);
let differs = |object, covered, first_uncovered| Difference {
let differs = |object, covered, first_uncovered, first_values| Difference {
command_buffer: 20,
object,
differing: 1,
covered,
first_uncovered,
first_values,
lengths: (32, 32),
};
assert_eq!(
comparison.differences,
[differs(5, 1, None), differs(7, 0, Some(0))]
[
differs(5, 1, None, (None, None)),
differs(7, 0, Some(0), (Some(7), Some(70)))
]
);
assert!(!comparison.agrees());
assert_eq!(
comparison.lines()[0],
"differs cb #20 obj #7: 1 words outside every cover, the first word 0; 0 covered; \
lengths 32 and 32"
"differs cb #20 obj #7: 1 words outside every cover, the first word 0 (a 0x00000007, b \
0x00000046); 0 covered; lengths 32 and 32"
);
}
/// The compare prints the first word that differs outside every cover as each play's dump holds
/// it: of three buffers, alike in their first word, the second word of #2 is 2 in `a` and 9 in
/// `b`; #3's `b` ends 2 bytes into it, read with zeros above them; #4's `b` ends before it, none.
#[test]
fn the_dump_compare_prints_the_first_differing_word_of_each_play() {
let (a, b) = (
ScratchDir::new("dumps-a").unwrap(),
ScratchDir::new("dumps-b").unwrap(),
);
let words = |words: &[u32]| -> Vec<u8> { words.iter().flat_map(|w| w.to_le_bytes()).collect() };
let short = [&words(&[1])[..], &[0xFF, 0xEE]].concat();
for (object, b_bytes) in [(2, words(&[1, 9])), (3, short), (4, words(&[1]))] {
let name = format!("cb1-obj{object}.bin");
std::fs::write(a.path().join(&name), words(&[1, 2])).unwrap();
std::fs::write(b.path().join(&name), b_bytes).unwrap();
}
let comparison = compare_dumps(a.path(), b.path()).unwrap();
let values: Vec<_> = (comparison.differences.iter())
.map(|d| (d.object, d.first_uncovered, d.first_values))
.collect();
let first = |b| (Some(1), (Some(2), b));
let firsts = [
(2, first(Some(9))),
(3, first(Some(0xEEFF))),
(4, first(None)),
];
let firsts = firsts.map(|(object, (word, values))| (object, word, values));
assert_eq!(values, firsts);
assert_eq!(
comparison.lines()[..3],
[
"differs cb #1 obj #2: 1 words outside every cover, the first word 1 (a 0x00000002, b \
0x00000009); 0 covered; lengths 8 and 8",
"differs cb #1 obj #3: 1 words outside every cover, the first word 1 (a 0x00000002, b \
0x0000eeff); 0 covered; lengths 8 and 6",
"differs cb #1 obj #4: 1 words outside every cover, the first word 1 (a 0x00000002, b \
none); 0 covered; lengths 8 and 4",
]
);
}
+2 -1
View File
@@ -1203,7 +1203,8 @@ bytes, where the cover rule (above) held any byte of the texture covered then, a
the rule's ranges are the texture's bytes in its heap, which its parts are not laid out as.
`command-replay compare-dumps --a DIR --b DIR` compares two plays' dumps word by word (4 bytes), leaving out a word
wholly inside a range either play held covered, and prints `differs cb #C obj #O: N words outside every cover, the first word
W; M covered; lengths A and B` for each buffer that differs outside every cover, `only in a|b:
W (a X, b Y); M covered; lengths A and B` for each buffer that differs outside every cover, `X` and `Y` that word in each
play's dump as `0x` and 8 hex digits (a short end's bytes with zeros above them, `none` past a dump's end), `only in a|b:
cbC-objO` for a dump one play lacks, `unread in a|b: cbC-objO: WHY`, `copies differ: cbC-objO: a
read its COPY, b its COPY` for a buffer the two plays read different copies of (`one copy` where a
dump holds no `.copy` file), which it does not compare, then `compared N buffers (C as both plays'