Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exclude = [
]

[dependencies]
hashbrown = { version = "0.15", default-features = false, features = ["default-hasher", "inline-more"] }
hashbrown = { version = ">=0.15,<=0.16", default-features = false, features = ["default-hasher", "inline-more"] }
memchr = "2.7.4"

[features]
Expand All @@ -26,6 +26,7 @@ unified_diff = []

[dev-dependencies]
# criterion = "0.4.0"
cov-mark = "2.1.0"
expect-test = "1.4.0"
# git-repository = "0.25.0"
# similar = { version = "2.2.0", features = ["bytes"] }
Expand Down
6 changes: 5 additions & 1 deletion src/myers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Myers {
unsafe { MiddleSnakeSearch::<false>::new(self.kforward, file1, file2) };
let mut backwards_search =
unsafe { MiddleSnakeSearch::<true>::new(self.kbackward, file1, file2) };
let is_odd = (file2.len() - file2.len()) & 1 != 0;
let is_odd = file2.len().wrapping_sub(file1.len()) & 1 != 0;

let mut ec = 0;

Expand All @@ -111,6 +111,8 @@ impl Myers {
backwards_search.contains(k)
&& backwards_search.x_pos_at_diagonal(k) <= token_idx1
}) {
#[cfg(test)]
cov_mark::hit!(ODD_SPLIT);
match res {
SearchResult::Snake => found_snake = true,
SearchResult::Found {
Expand All @@ -135,6 +137,8 @@ impl Myers {
if let Some(res) = backwards_search.run(file1, file2, |k, token_idx1| {
forward_search.contains(k) && token_idx1 <= forward_search.x_pos_at_diagonal(k)
}) {
#[cfg(test)]
cov_mark::hit!(EVEN_SPLIT);
match res {
SearchResult::Snake => found_snake = true,
SearchResult::Found {
Expand Down
4 changes: 4 additions & 0 deletions src/myers/middle_snake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::util::{common_postfix, common_prefix};
const SNAKE_CNT: u32 = 20;
const K_HEUR: u32 = 4;

#[derive(Debug)]
pub struct MiddleSnakeSearch<const BACK: bool> {
kvec: NonNull<i32>,
kmin: i32,
Expand Down Expand Up @@ -98,6 +99,8 @@ impl<const BACK: bool> MiddleSnakeSearch<BACK> {
let mut res = None;
let mut k = self.kmax;
while k >= self.kmin {
#[cfg(test)]
cov_mark::hit!(SPLIT_SEARCH_ITER);
let mut token_idx1 = if BACK {
if self.x_pos_at_diagonal(k - 1) < self.x_pos_at_diagonal(k + 1) {
self.x_pos_at_diagonal(k - 1)
Expand Down Expand Up @@ -249,6 +252,7 @@ impl<const BACK: bool> MiddleSnakeSearch<BACK> {
}
}

#[derive(Debug)]
pub enum SearchResult {
Snake,
Found { token_idx1: i32, token_idx2: i32 },
Expand Down
65 changes: 65 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,71 @@ fn foo() -> Bar{
}
}

#[test]
fn myers_is_odd() {
let before = "a\nb\nx\ny\nx\n";
let after = "b\na\nx\ny\n";

cov_mark::check!(ODD_SPLIT);
// if the check for odd doesn't work then
// we still find the correct result but the number of search
// iterations increases
cov_mark::check_count!(SPLIT_SEARCH_ITER, 9);
let input = InternedInput::new(before, after);
let diff = Diff::compute(Algorithm::Myers, &input);
expect![[r#"
@@ -1,5 +1,4 @@
-a
b
+a
x
y
-x
"#]]
.assert_eq(
&diff
.unified_diff(
&BasicLineDiffPrinter(&input.interner),
UnifiedDiffConfig::default(),
&input,
)
.to_string(),
);
}
#[test]
fn myers_is_even() {
let before = "a\nb\nx\nx\ny\n";
let after = "b\na\nx\ny\nx\n";

cov_mark::check!(EVEN_SPLIT);
// if the check for is_odd incorrectly always true then we take a fastpath
// when we shouldn't which always leads to inifite iterations/recursion
// still we check the number of iterations here in case the search
// is buggy in more subtle ways
cov_mark::check_count!(SPLIT_SEARCH_ITER, 15);
Comment on lines +158 to +162
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am loving these tests! TIL cov_mark - very useful.

let input = InternedInput::new(before, after);
let diff = Diff::compute(Algorithm::Myers, &input);
expect![[r#"
@@ -1,5 +1,5 @@
-a
b
-x
+a
x
y
+x
"#]]
.assert_eq(
&diff
.unified_diff(
&BasicLineDiffPrinter(&input.interner),
UnifiedDiffConfig::default(),
&input,
)
.to_string(),
);
}

#[test]
fn identical_files() {
let file = r#"fn foo() -> Bar{
Expand Down
Loading