sort: add tests for stable and unstable sort

This commit is contained in:
John Shin 2023-06-27 12:15:58 -07:00
parent ca315499c3
commit d2e7ba2da1
2 changed files with 22 additions and 5 deletions

View file

@ -106,7 +106,7 @@ pub fn version_cmp(mut a: &str, mut b: &str) -> Ordering {
// 1. Compare leading non-numerical part // 1. Compare leading non-numerical part
// 2. Compare leading numerical part // 2. Compare leading numerical part
// 3. Repeat // 3. Repeat
loop { while !a.is_empty() || !b.is_empty() {
let a_numerical_start = a.find(|c: char| c.is_ascii_digit()).unwrap_or(a.len()); let a_numerical_start = a.find(|c: char| c.is_ascii_digit()).unwrap_or(a.len());
let b_numerical_start = b.find(|c: char| c.is_ascii_digit()).unwrap_or(b.len()); let b_numerical_start = b.find(|c: char| c.is_ascii_digit()).unwrap_or(b.len());
@ -139,11 +139,9 @@ pub fn version_cmp(mut a: &str, mut b: &str) -> Ordering {
a = &a[a_numerical_end..]; a = &a[a_numerical_end..];
b = &b[b_numerical_end..]; b = &b[b_numerical_end..];
}
if a.is_empty() && b.is_empty() { Ordering::Equal
return std::cmp::Ordering::Equal;
}
}
} }
#[cfg(test)] #[cfg(test)]

View file

@ -134,6 +134,25 @@ fn test_version_empty_lines() {
test_helper("version-empty-lines", &["-V", "--version-sort"]); test_helper("version-empty-lines", &["-V", "--version-sort"]);
} }
#[test]
fn test_version_sort_unstable() {
new_ucmd!()
.arg("--sort=version")
.pipe_in("0.1\n0.02\n0.2\n0.002\n0.3\n")
.succeeds()
.stdout_is("0.1\n0.002\n0.02\n0.2\n0.3\n");
}
#[test]
fn test_version_sort_stable() {
new_ucmd!()
.arg("--stable")
.arg("--sort=version")
.pipe_in("0.1\n0.02\n0.2\n0.002\n0.3\n")
.succeeds()
.stdout_is("0.1\n0.02\n0.2\n0.002\n0.3\n");
}
#[test] #[test]
fn test_human_numeric_whitespace() { fn test_human_numeric_whitespace() {
test_helper( test_helper(