Skip to content

Commit

Permalink
Always call finish (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Dec 29, 2023
1 parent f5c1afa commit 1871278
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to similar are documented here.

## 2.4.0

* Fixed a bug where the LCS diff algorithm didn't always call `D::finish`. (#58)

## 2.3.0

* Added support for `Change::value_ref` and `Change::value_mut`.
Expand Down
32 changes: 32 additions & 0 deletions src/algorithms/lcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ where
{
if is_empty_range(&new_range) {
d.delete(old_range.start, old_range.len(), new_range.start)?;
d.finish()?;
return Ok(());
} else if is_empty_range(&old_range) {
d.insert(old_range.start, new_range.start, new_range.len())?;
d.finish()?;
return Ok(());
}

Expand All @@ -67,6 +69,7 @@ where
// If the sequences are not different then we're done
if common_prefix_len == old_range.len() && (old_range.len() == new_range.len()) {
d.equal(0, 0, old_range.len())?;
d.finish()?;
return Ok(());
}

Expand Down Expand Up @@ -234,3 +237,32 @@ fn test_same() {
diff(&mut d, a, 0..a.len(), b, 0..b.len()).unwrap();
insta::assert_debug_snapshot!(d.ops());
}

#[test]
fn test_finish_called() {
struct HasRunFinish(bool);

impl DiffHook for HasRunFinish {
type Error = ();
fn finish(&mut self) -> Result<(), Self::Error> {
self.0 = true;
Ok(())
}
}

let mut d = HasRunFinish(false);
let slice = &[1, 2];
let slice2 = &[1, 2, 3];
diff(&mut d, slice, 0..slice.len(), slice2, 0..slice2.len()).unwrap();
assert!(d.0);

let mut d = HasRunFinish(false);
let slice = &[1, 2];
diff(&mut d, slice, 0..slice.len(), slice, 0..slice.len()).unwrap();
assert!(d.0);

let mut d = HasRunFinish(false);
let slice: &[u8] = &[];
diff(&mut d, slice, 0..slice.len(), slice, 0..slice.len()).unwrap();
assert!(d.0);
}
29 changes: 29 additions & 0 deletions src/algorithms/myers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,32 @@ fn test_deadline_reached() {
.unwrap();
insta::assert_debug_snapshot!(d.into_inner().ops());
}

#[test]
fn test_finish_called() {
struct HasRunFinish(bool);

impl DiffHook for HasRunFinish {
type Error = ();
fn finish(&mut self) -> Result<(), Self::Error> {
self.0 = true;
Ok(())
}
}

let mut d = HasRunFinish(false);
let slice = &[1, 2];
let slice2 = &[1, 2, 3];
diff(&mut d, slice, 0..slice.len(), slice2, 0..slice2.len()).unwrap();
assert!(d.0);

let mut d = HasRunFinish(false);
let slice = &[1, 2];
diff(&mut d, slice, 0..slice.len(), slice, 0..slice.len()).unwrap();
assert!(d.0);

let mut d = HasRunFinish(false);
let slice: &[u8] = &[];
diff(&mut d, slice, 0..slice.len(), slice, 0..slice.len()).unwrap();
assert!(d.0);
}
29 changes: 29 additions & 0 deletions src/algorithms/patience.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,32 @@ fn test_patience_out_of_bounds_bug() {

insta::assert_debug_snapshot!(d.into_inner().ops());
}

#[test]
fn test_finish_called() {
struct HasRunFinish(bool);

impl DiffHook for HasRunFinish {
type Error = ();
fn finish(&mut self) -> Result<(), Self::Error> {
self.0 = true;
Ok(())
}
}

let mut d = HasRunFinish(false);
let slice = &[1, 2];
let slice2 = &[1, 2, 3];
diff(&mut d, slice, 0..slice.len(), slice2, 0..slice2.len()).unwrap();
assert!(d.0);

let mut d = HasRunFinish(false);
let slice = &[1, 2];
diff(&mut d, slice, 0..slice.len(), slice, 0..slice.len()).unwrap();
assert!(d.0);

let mut d = HasRunFinish(false);
let slice: &[u8] = &[];
diff(&mut d, slice, 0..slice.len(), slice, 0..slice.len()).unwrap();
assert!(d.0);
}

0 comments on commit 1871278

Please sign in to comment.