Skip to content

Commit

Permalink
!fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Sep 13, 2023
1 parent b81d247 commit cb20192
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 4 deletions.
17 changes: 13 additions & 4 deletions eyeball-im-util/src/vector/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ where
impl<S, L> Stream for DynamicLimit<S, L>
where
S: Stream,
S::Item: VectorDiffContainer + std::fmt::Debug,
VectorDiffContainerStreamElement<S>: Clone + Send + Sync + 'static + std::fmt::Debug,
S::Item: VectorDiffContainer,
VectorDiffContainerStreamElement<S>: Clone + Send + Sync + 'static,
VectorDiffContainerStreamFamily<S>:
VectorDiffContainerFamily<Member<VectorDiffContainerStreamElement<S>> = S::Item>,
L: Stream<Item = usize>,
Expand All @@ -82,8 +82,8 @@ where
impl<S, L> DynamicLimitProj<'_, S, L>
where
S: Stream,
S::Item: VectorDiffContainer + std::fmt::Debug,
VectorDiffContainerStreamElement<S>: Clone + Send + Sync + 'static + std::fmt::Debug,
S::Item: VectorDiffContainer,
VectorDiffContainerStreamElement<S>: Clone + Send + Sync + 'static,
VectorDiffContainerStreamFamily<S>:
VectorDiffContainerFamily<Member<VectorDiffContainerStreamElement<S>> = S::Item>,
L: Stream<Item = usize>,
Expand Down Expand Up @@ -173,6 +173,15 @@ where
}
VectorDiff::PopFront => {
self.push_ready_value(VectorDiff::PopFront);

if length > limit {
// Push back a new item.
self.push_ready_value(VectorDiff::PushBack {
// SAFETY: It's safe to `unwrap` here as we are sure a value exists at index
// `limit - 1`. We are also sure that `limit > 1`.
value: self.buffered_vector.get(limit - 1).unwrap().clone(),
})
}
}
VectorDiff::PopBack => {
if is_full {
Expand Down
175 changes: 175 additions & 0 deletions eyeball-im-util/tests/it/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ fn append() {
// Observe nothing.
assert_pending!(sub);

// Check the content of the vector.
{
let expected = vector![10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
assert_eq!(*ob, expected);

Observable::set(&mut limit, 0);
assert_next_eq!(sub, VectorDiff::Truncate { length: 0 });

Observable::set(&mut limit, 42);
assert_next_eq!(sub, VectorDiff::Append { values: expected });
}

drop(ob);
assert_closed!(sub);
}
Expand All @@ -239,6 +251,169 @@ fn clear() {
assert_next_eq!(sub, VectorDiff::PushBack { value: 10 });
assert_next_eq!(sub, VectorDiff::Clear);

// Check the content of the vector.
{
let expected = vector![];
assert_eq!(*ob, expected);
}

drop(ob);
assert_closed!(sub);
}

#[test]
fn push_front() {
let mut ob = ObservableVector::<usize>::new();
let mut limit = Observable::new(0);
let mut sub =
DynamicLimit::new(ob.clone(), ob.subscribe().into_stream(), Observable::subscribe(&limit));

// Set limit to 2.
Observable::set(&mut limit, 2);

// Add 1 value.
ob.push_front(10);

// Observe 1 value.
assert_next_eq!(sub, VectorDiff::PushFront { value: 10 });

// Add 1 value.
ob.push_front(11);

// Observe 1 value.
assert_next_eq!(sub, VectorDiff::PushFront { value: 11 });

// Add 1 value.
ob.push_front(12);

// Observe 1 value being removed, and 1 new value.
assert_next_eq!(sub, VectorDiff::PopBack);
assert_next_eq!(sub, VectorDiff::PushFront { value: 12 });
assert_pending!(sub);

// Add 1 value.
ob.push_front(13);

// Observe 1 value being removed, and 1 new value.
assert_next_eq!(sub, VectorDiff::PopBack);
assert_next_eq!(sub, VectorDiff::PushFront { value: 13 });
assert_pending!(sub);

// Check the content of the vector.
{
let expected = vector![13, 12, 11, 10];
assert_eq!(*ob, expected);

Observable::set(&mut limit, 0);
assert_next_eq!(sub, VectorDiff::Truncate { length: 0 });

Observable::set(&mut limit, 42);
assert_next_eq!(sub, VectorDiff::Append { values: expected });
}

drop(ob);
assert_closed!(sub);
}

#[test]
fn push_back() {
let mut ob = ObservableVector::<usize>::new();
let mut limit = Observable::new(0);
let mut sub =
DynamicLimit::new(ob.clone(), ob.subscribe().into_stream(), Observable::subscribe(&limit));

// Set limit to 2.
Observable::set(&mut limit, 2);

// Add 1 value.
ob.push_back(10);

// Observe 1 value.
assert_next_eq!(sub, VectorDiff::PushBack { value: 10 });

// Add 1 value.
ob.push_back(11);

// Observe 1 value.
assert_next_eq!(sub, VectorDiff::PushBack { value: 11 });

// Add 1 value.
ob.push_back(12);

// Observe nothing.
assert_pending!(sub);

// Add 1 value.
ob.push_back(13);

// Observe nothing.
assert_pending!(sub);

// Check the content of the vector.
{
let expected = vector![10, 11, 12, 13];
assert_eq!(*ob, expected);

Observable::set(&mut limit, 0);
assert_next_eq!(sub, VectorDiff::Truncate { length: 0 });

Observable::set(&mut limit, 42);
assert_next_eq!(sub, VectorDiff::Append { values: expected });
}

drop(ob);
assert_closed!(sub);
}

#[test]
fn pop_front() {
let mut ob = ObservableVector::<usize>::new();
let mut limit = Observable::new(0);
let mut sub =
DynamicLimit::new(ob.clone(), ob.subscribe().into_stream(), Observable::subscribe(&limit));

// Add 4 values.
ob.append(vector![10, 11, 12, 13]);

// Set limit to 2.
Observable::set(&mut limit, 2);

// Observe 2 values.
assert_next_eq!(sub, VectorDiff::Append { values: vector![10, 11] });

// Remove 1 value.
ob.pop_front();

// Observe 1 value being removed, and 1 new value being inserted at the back.
assert_next_eq!(sub, VectorDiff::PopFront);
assert_next_eq!(sub, VectorDiff::PushBack { value: 12 });

// Remove 1 value.
ob.pop_front();

// Observe 1 value being removed, and 1 new value being inserted at the back.
assert_next_eq!(sub, VectorDiff::PopFront);
assert_next_eq!(sub, VectorDiff::PushBack { value: 13 });

// Remove 1 value.
ob.pop_front();

// Observe only 1 value being removed.
assert_next_eq!(sub, VectorDiff::PopFront);
assert_pending!(sub);

// Check the content of the vector.
{
let expected = vector![13];
assert_eq!(*ob, expected);

Observable::set(&mut limit, 0);
assert_next_eq!(sub, VectorDiff::Truncate { length: 0 });

Observable::set(&mut limit, 42);
assert_next_eq!(sub, VectorDiff::Append { values: expected });
}

drop(ob);
assert_closed!(sub);
}

0 comments on commit cb20192

Please sign in to comment.