Skip to content

Commit

Permalink
Delete is_empty, is_full, len methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Stjepan Glavina committed Apr 27, 2019
1 parent cdbc6a1 commit 3d46387
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 221 deletions.
120 changes: 0 additions & 120 deletions src/spsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ pub fn new<T>(cap: usize) -> (Producer<T>, Consumer<T>) {
///
/// assert_eq!(p.push(10), Ok(()));
/// assert_eq!(p.push(20), Err(PushError(20)));
///
/// assert!(!p.is_empty());
/// assert!(p.is_full());
/// ```
pub struct Producer<T> {
/// The inner representation of the queue.
Expand Down Expand Up @@ -246,63 +243,6 @@ impl<T> Producer<T> {
pub fn capacity(&self) -> usize {
self.inner.cap
}

/// Returns `true` if the queue is empty.
///
/// # Examples
///
/// ```
/// use crossbeam_queue::spsc;
///
/// let (p, c) = spsc::new(100);
///
/// assert!(p.is_empty());
/// p.push(1).unwrap();
/// assert!(!p.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns `true` if the queue is full.
///
/// # Examples
///
/// ```
/// use crossbeam_queue::spsc;
///
/// let (p, c) = spsc::new(1);
///
/// assert!(!p.is_full());
/// p.push(1).unwrap();
/// assert!(p.is_full());
/// ```
pub fn is_full(&self) -> bool {
self.len() == self.inner.cap
}

/// Returns the number of elements in the queue.
///
/// # Examples
///
/// ```
/// use crossbeam_queue::spsc;
///
/// let (p, c) = spsc::new(100);
/// assert_eq!(p.len(), 0);
///
/// p.push(10).unwrap();
/// assert_eq!(p.len(), 1);
///
/// p.push(20).unwrap();
/// assert_eq!(p.len(), 2);
/// ```
pub fn len(&self) -> usize {
let head = self.inner.head.load(Ordering::Acquire);
let tail = self.tail.get();
self.head.set(head);
self.inner.distance(head, tail)
}
}

impl<T> fmt::Debug for Producer<T> {
Expand All @@ -323,9 +263,6 @@ impl<T> fmt::Debug for Producer<T> {
///
/// assert_eq!(c.pop(), Ok(10));
/// assert_eq!(c.pop(), Err(PopError));
///
/// assert!(c.is_empty());
/// assert!(!c.is_full());
/// ```
pub struct Consumer<T> {
/// The inner representation of the queue.
Expand Down Expand Up @@ -401,63 +338,6 @@ impl<T> Consumer<T> {
pub fn capacity(&self) -> usize {
self.inner.cap
}

/// Returns `true` if the queue is empty.
///
/// # Examples
///
/// ```
/// use crossbeam_queue::spsc;
///
/// let (p, c) = spsc::new(100);
///
/// assert!(c.is_empty());
/// p.push(1).unwrap();
/// assert!(!c.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns `true` if the queue is full.
///
/// # Examples
///
/// ```
/// use crossbeam_queue::spsc;
///
/// let (p, c) = spsc::new(1);
///
/// assert!(!c.is_full());
/// p.push(1).unwrap();
/// assert!(c.is_full());
/// ```
pub fn is_full(&self) -> bool {
self.len() == self.inner.cap
}

/// Returns the number of elements in the queue.
///
/// # Examples
///
/// ```
/// use crossbeam_queue::spsc;
///
/// let (p, c) = spsc::new(100);
/// assert_eq!(c.len(), 0);
///
/// p.push(10).unwrap();
/// assert_eq!(c.len(), 1);
///
/// p.push(20).unwrap();
/// assert_eq!(c.len(), 2);
/// ```
pub fn len(&self) -> usize {
let head = self.head.get();
let tail = self.inner.tail.load(Ordering::Acquire);
self.tail.set(tail);
self.inner.distance(head, tail)
}
}

impl<T> fmt::Debug for Consumer<T> {
Expand Down
101 changes: 0 additions & 101 deletions tests/spsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,107 +35,6 @@ fn zero_capacity() {
let _ = spsc::new::<i32>(0);
}

#[test]
fn len_empty_full() {
let (p, c) = spsc::new(2);

assert_eq!(p.len(), 0);
assert_eq!(c.len(), 0);
assert_eq!(p.is_empty(), true);
assert_eq!(c.is_empty(), true);
assert_eq!(p.is_full(), false);
assert_eq!(c.is_full(), false);

p.push(()).unwrap();

assert_eq!(p.len(), 1);
assert_eq!(c.len(), 1);
assert_eq!(p.is_empty(), false);
assert_eq!(c.is_empty(), false);
assert_eq!(p.is_full(), false);
assert_eq!(c.is_full(), false);

p.push(()).unwrap();

assert_eq!(p.len(), 2);
assert_eq!(c.len(), 2);
assert_eq!(p.is_empty(), false);
assert_eq!(c.is_empty(), false);
assert_eq!(p.is_full(), true);
assert_eq!(c.is_full(), true);

c.pop().unwrap();

assert_eq!(p.len(), 1);
assert_eq!(c.len(), 1);
assert_eq!(p.is_empty(), false);
assert_eq!(c.is_empty(), false);
assert_eq!(p.is_full(), false);
assert_eq!(c.is_full(), false);
}

#[test]
fn len() {
const COUNT: usize = 25_000;
const CAP: usize = 1000;

let (p, c) = spsc::new(CAP);
assert_eq!(p.len(), 0);
assert_eq!(c.len(), 0);

for _ in 0..CAP / 10 {
for i in 0..50 {
p.push(i).unwrap();
assert_eq!(p.len(), i + 1);
}

for i in 0..50 {
c.pop().unwrap();
assert_eq!(c.len(), 50 - i - 1);
}
}
assert_eq!(p.len(), 0);
assert_eq!(c.len(), 0);

for i in 0..CAP {
p.push(i).unwrap();
assert_eq!(p.len(), i + 1);
}

for _ in 0..CAP {
c.pop().unwrap();
}
assert_eq!(c.len(), 0);

let p = scope(|s| {
s.spawn(move |_| {
for i in 0..COUNT {
loop {
if let Ok(x) = c.pop() {
assert_eq!(x, i);
break;
}
}
let len = c.len();
assert!(len <= CAP);
}
});

s.spawn(move |_| {
for i in 0..COUNT {
while p.push(i).is_err() {}
let len = p.len();
assert!(len <= CAP);
}
p
})
.join()
.unwrap()
})
.unwrap();
assert_eq!(p.len(), 0);
}

#[test]
fn parallel() {
const COUNT: usize = 100_000;
Expand Down

0 comments on commit 3d46387

Please sign in to comment.