use rpds::VectorSync;
#[must_use]
pub(crate) fn kept_first<T>(steps: &VectorSync<T>, len: usize) -> VectorSync<T> {
let mut kept = steps.clone();
while kept.len() > len {
kept.drop_last_mut();
}
kept
}
#[cfg(test)]
mod tests {
use rpds::VectorSync;
use super::kept_first;
fn sequence_of(numbers: &[u32]) -> VectorSync<u32> {
let mut sequence: VectorSync<u32> = VectorSync::new_sync();
for number in numbers {
sequence.push_back_mut(*number);
}
sequence
}
fn elements(sequence: &VectorSync<u32>) -> Vec<u32> {
sequence.iter().copied().collect()
}
#[test]
fn kept_first_drops_the_places_behind_the_position_and_keeps_the_rest() {
let sequence = sequence_of(&[1, 2, 3, 4]);
let kept = kept_first(&sequence, 2);
assert_eq!(elements(&kept), vec![1, 2]);
}
#[test]
fn kept_first_asks_for_the_whole_sequence_and_gets_the_whole_sequence() {
let sequence = sequence_of(&[1, 2, 3]);
assert_eq!(
elements(&kept_first(&sequence, 3)),
vec![1, 2, 3],
"a position already at the end leaves every place standing"
);
assert_eq!(
elements(&kept_first(&sequence, 9)),
vec![1, 2, 3],
"and a position past the end is not an error and does not drop anything more"
);
}
#[test]
fn kept_first_on_an_empty_sequence_is_an_empty_sequence() {
let nothing: VectorSync<u32> = VectorSync::new_sync();
assert!(
kept_first(¬hing, 0).is_empty(),
"a sequence holding nothing has nothing to keep"
);
assert!(
kept_first(¬hing, 4).is_empty(),
"and a position past it is not an error"
);
let one = sequence_of(&[7]);
assert!(
kept_first(&one, 0).is_empty(),
"a length of zero keeps nothing rather than everything"
);
}
}