Skip to content

Commit

Permalink
Merge pull request #264 from benesch/consolidate-single-element
Browse files Browse the repository at this point in the history
Properly consolidate slices with one element
  • Loading branch information
frankmcsherry authored Mar 28, 2020
2 parents c014eca + b54e84c commit 11fb737
Showing 1 changed file with 59 additions and 5 deletions.
64 changes: 59 additions & 5 deletions src/consolidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ pub fn consolidate_slice<T: Ord, R: Semigroup>(slice: &mut [(T, R)]) -> usize {
}

offset
}
else {
slice.len()
} else if slice.len() == 1 && !slice[0].1.is_zero() {
1
} else {
0
}
}

Expand Down Expand Up @@ -131,8 +132,61 @@ pub fn consolidate_updates_slice<D: Ord, T: Ord, R: Semigroup>(slice: &mut [(D,
}

offset
} else if slice.len() == 1 && !slice[0].2.is_zero() {
1
} else {
0
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_consolidate() {
let test_cases = vec![
(
vec![("a", -1), ("b", -2), ("a", 1)],
vec![("b", -2)],
),
(
vec![("a", -1), ("b", 0), ("a", 1)],
vec![],
),
(
vec![("a", 0)],
vec![],
),
];

for (mut input, output) in test_cases {
consolidate(&mut input);
assert_eq!(input, output);
}
}
else {
slice.len()


#[test]
fn test_consolidate_updates() {
let test_cases = vec![
(
vec![("a", 1, -1), ("b", 1, -2), ("a", 1, 1)],
vec![("b", 1, -2)],
),
(
vec![("a", 1, -1), ("b", 1, 0), ("a", 1, 1)],
vec![],
),
(
vec![("a", 1, 0)],
vec![],
),
];

for (mut input, output) in test_cases {
consolidate_updates(&mut input);
assert_eq!(input, output);
}
}
}

0 comments on commit 11fb737

Please sign in to comment.